[docs]classJSONAgentOutputParser(AgentOutputParser):"""Parses tool invocations and final answers in JSON format. Expects output to be in one of two formats. If the output signals that an action should be taken, should be in the below format. This will result in an AgentAction being returned. ``` { "action": "search", "action_input": "2+2" } ``` If the output signals that a final answer should be given, should be in the below format. This will result in an AgentFinish being returned. ``` { "action": "Final Answer", "action_input": "4" } ``` """
[docs]defparse(self,text:str)->Union[AgentAction,AgentFinish]:try:response=parse_json_markdown(text)ifisinstance(response,list):# gpt turbo frequently ignores the directive to emit a single actionlogger.warning("Got multiple action responses: %s",response)response=response[0]ifresponse["action"]=="Final Answer":returnAgentFinish({"output":response["action_input"]},text)else:action_input=response.get("action_input",{})ifaction_inputisNone:action_input={}returnAgentAction(response["action"],action_input,text)exceptExceptionase:raiseOutputParserException(f"Could not parse LLM output: {text}")frome