When any other function is called by OpenAI, we treat that as a tool invocation.Therefore, our parsing logic has the following blocks:If no function is called, assume that we should use the response to respond to the user, and therefore return AgentFinishIf the Response function is called, respond to the user with the inputs to that function (our structured output), and therefore return AgentFinishIf any other function is called, treat that as a tool invocation, and therefore return AgentActionMessageLogNote that we are using AgentActionMessageLog rather than AgentAction because it lets us attach a log of messages that we can use in the future to pass back into the agent prompt.from langchain.schema.agent import AgentActionMessageLog, AgentFinishimport jsondef parse(output):    # If no function was invoked, return to user    if "function_call" not in output.additional_kwargs:        return AgentFinish(return_values={"output": output.content}, log=output.content)        # Parse out the function call    function_call = output.additional_kwargs["function_call"]    name = function_call['name']    inputs = json.loads(function_call['arguments'])        # If the Response function was invoked, return to the user with the function inputs    if name == "Response":        return AgentFinish(return_values=inputs, log=str(function_call))    # Otherwise, return an agent action    else:        return AgentActionMessageLog(tool=name, tool_input=inputs, log="", message_log=[output])Create the Agent‚ÄãWe can now put this all together! The components of this agent are:prompt: a simple prompt with placeholders for the user's question and then the agent_scratchpad (any intermediate steps)tools: we can attach the tools and Response format to the LLM as functionsformat scratchpad: in order to format the agent_scratchpad from intermediate steps, we will use the standard format_to_openai_functions. This takes intermediate steps and formats them as AIMessages and FunctionMessages.output
This notebook covers how to have an agent return a structured output.
This notebook covers how to have an agent return a structured output. ->: When any other function is called by OpenAI, we treat that as a tool invocation.Therefore, our parsing logic has the following blocks:If no function is called, assume that we should use the response to respond to the user, and therefore return AgentFinishIf the Response function is called, respond to the user with the inputs to that function (our structured output), and therefore return AgentFinishIf any other function is called, treat that as a tool invocation, and therefore return AgentActionMessageLogNote that we are using AgentActionMessageLog rather than AgentAction because it lets us attach a log of messages that we can use in the future to pass back into the agent prompt.from langchain.schema.agent import AgentActionMessageLog, AgentFinishimport jsondef parse(output):    # If no function was invoked, return to user    if "function_call" not in output.additional_kwargs:        return AgentFinish(return_values={"output": output.content}, log=output.content)        # Parse out the function call    function_call = output.additional_kwargs["function_call"]    name = function_call['name']    inputs = json.loads(function_call['arguments'])        # If the Response function was invoked, return to the user with the function inputs    if name == "Response":        return AgentFinish(return_values=inputs, log=str(function_call))    # Otherwise, return an agent action    else:        return AgentActionMessageLog(tool=name, tool_input=inputs, log="", message_log=[output])Create the Agent‚ÄãWe can now put this all together! The components of this agent are:prompt: a simple prompt with placeholders for the user's question and then the agent_scratchpad (any intermediate steps)tools: we can attach the tools and Response format to the LLM as functionsformat scratchpad: in order to format the agent_scratchpad from intermediate steps, we will use the standard format_to_openai_functions. This takes intermediate steps and formats them as AIMessages and FunctionMessages.output