[docs]classAPIRequesterOutputParser(BaseOutputParser):"""Parse the request and error tags."""def_load_json_block(self,serialized_block:str)->str:try:returnjson.dumps(json.loads(serialized_block,strict=False))exceptjson.JSONDecodeError:return"ERROR serializing request."
[docs]defparse(self,llm_output:str)->str:"""Parse the request and error tags."""json_match=re.search(r"```json(.*?)```",llm_output,re.DOTALL)ifjson_match:returnself._load_json_block(json_match.group(1).strip())message_match=re.search(r"```text(.*?)```",llm_output,re.DOTALL)ifmessage_match:returnf"MESSAGE: {message_match.group(1).strip()}"return"ERROR making request"
[docs]classAPIRequesterChain(LLMChain):"""Get the request parser."""@classmethoddefis_lc_serializable(cls)->bool:returnFalse
[docs]@classmethoddeffrom_llm_and_typescript(cls,llm:BaseLanguageModel,typescript_definition:str,verbose:bool=True,**kwargs:Any,)->LLMChain:"""Get the request parser."""output_parser=APIRequesterOutputParser()prompt=PromptTemplate(template=REQUEST_TEMPLATE,output_parser=output_parser,partial_variables={"schema":typescript_definition},input_variables=["instructions"],)returncls(prompt=prompt,llm=llm,verbose=verbose,**kwargs)