them as AIMessages and FunctionMessages.output parser: we will use our custom parser above to parse the response of the LLMAgentExecutor: we will use the standard AgentExecutor to run the loop of agent-tool-agent-tool...from langchain.prompts import ChatPromptTemplate, MessagesPlaceholderfrom langchain.chat_models import ChatOpenAIfrom langchain.tools.render import format_tool_to_openai_functionfrom langchain.agents.format_scratchpad import format_to_openai_functionsfrom langchain.agents import AgentExecutorprompt = ChatPromptTemplate.from_messages([    ("system", "You are a helpful assistant"),    ("user", "{input}"),    MessagesPlaceholder(variable_name="agent_scratchpad"),])llm = ChatOpenAI(temperature=0)llm_with_tools = llm.bind(    functions=[        # The retriever tool        format_tool_to_openai_function(retriever_tool),         # Response schema        convert_pydantic_to_openai_function(Response)    ])agent = {    "input": lambda x: x["input"],    # Format agent scratchpad from intermediate steps    "agent_scratchpad": lambda x: format_to_openai_functions(x['intermediate_steps'])} | prompt | llm_with_tools | parseagent_executor = AgentExecutor(tools=[retriever_tool], agent=agent, verbose=True)Run the agent‚ÄãWe can now run the agent! Notice how it responds with a dictionary with two keys: answer and sourcesagent_executor.invoke({"input": "what did the president say about kentaji brown jackson"}, return_only_outputs=True)            > Entering new AgentExecutor chain...    [Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you‚Äôre at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I‚Äôd like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer‚Äîan Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.
This notebook covers how to have an agent return a structured output.
This notebook covers how to have an agent return a structured output. ->: them as AIMessages and FunctionMessages.output parser: we will use our custom parser above to parse the response of the LLMAgentExecutor: we will use the standard AgentExecutor to run the loop of agent-tool-agent-tool...from langchain.prompts import ChatPromptTemplate, MessagesPlaceholderfrom langchain.chat_models import ChatOpenAIfrom langchain.tools.render import format_tool_to_openai_functionfrom langchain.agents.format_scratchpad import format_to_openai_functionsfrom langchain.agents import AgentExecutorprompt = ChatPromptTemplate.from_messages([    ("system", "You are a helpful assistant"),    ("user", "{input}"),    MessagesPlaceholder(variable_name="agent_scratchpad"),])llm = ChatOpenAI(temperature=0)llm_with_tools = llm.bind(    functions=[        # The retriever tool        format_tool_to_openai_function(retriever_tool),         # Response schema        convert_pydantic_to_openai_function(Response)    ])agent = {    "input": lambda x: x["input"],    # Format agent scratchpad from intermediate steps    "agent_scratchpad": lambda x: format_to_openai_functions(x['intermediate_steps'])} | prompt | llm_with_tools | parseagent_executor = AgentExecutor(tools=[retriever_tool], agent=agent, verbose=True)Run the agent‚ÄãWe can now run the agent! Notice how it responds with a dictionary with two keys: answer and sourcesagent_executor.invoke({"input": "what did the president say about kentaji brown jackson"}, return_only_outputs=True)            > Entering new AgentExecutor chain...    [Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you‚Äôre at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I‚Äôd like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer‚Äîan Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.