[docs]classAPIResponderOutputParser(BaseOutputParser):"""Parse the response and error tags."""def_load_json_block(self,serialized_block:str)->str:try:response_content=json.loads(serialized_block,strict=False)returnresponse_content.get("response","ERROR parsing response.")exceptjson.JSONDecodeError:return"ERROR parsing response."except:raise
[docs]defparse(self,llm_output:str)->str:"""Parse the response and error tags."""json_match=re.search(r"```json(.*?)```",llm_output,re.DOTALL)ifjson_match:returnself._load_json_block(json_match.group(1).strip())else:raiseValueError(f"No response found in output: {llm_output}.")
[docs]classAPIResponderChain(LLMChain):"""Get the response parser."""@classmethoddefis_lc_serializable(cls)->bool:returnFalse
[docs]@classmethoddeffrom_llm(cls,llm:BaseLanguageModel,verbose:bool=True,**kwargs:Any)->LLMChain:"""Get the response parser."""output_parser=APIResponderOutputParser()prompt=PromptTemplate(template=RESPONSE_TEMPLATE,output_parser=output_parser,input_variables=["response","instructions"],)returncls(prompt=prompt,llm=llm,verbose=verbose,**kwargs)