name="Search",        func=search.run,        description="useful for when you need to answer questions about current events. You should ask targeted questions"    ),    Tool(        name="Calculator",        func=llm_math_chain.run,        description="useful for when you need to answer questions about math"    ),    Tool(        name="FooBar-DB",        func=db_chain.run,        description="useful for when you need to answer questions about FooBar. Input should be in the form of a question containing full context"    )]Using LCEL‚ÄãWe will first use LangChain Expression Language to create this agentfrom langchain.prompts import ChatPromptTemplate, MessagesPlaceholderprompt = ChatPromptTemplate.from_messages([    ("system", "You are a helpful assistant"),    ("user", "{input}"),    MessagesPlaceholder(variable_name="agent_scratchpad"),])from langchain.tools.render import format_tool_to_openai_functionllm_with_tools = llm.bind(    functions=[format_tool_to_openai_function(t) for t in tools])from langchain.agents.format_scratchpad import format_to_openai_functionsfrom langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParseragent = {    "input": lambda x: x["input"],    "agent_scratchpad": lambda x: format_to_openai_functions(x['intermediate_steps'])} | prompt | llm_with_tools | OpenAIFunctionsAgentOutputParser()from langchain.agents import AgentExecutoragent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)agent_executor.invoke({"input": "Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?"})            > Entering new AgentExecutor chain...        Invoking: `Search` with `Leo DiCaprio's girlfriend`            ['Blake Lively and DiCaprio are believed to have enjoyed a whirlwind five-month romance in 2011. The pair were seen on a yacht together in Cannes, ...']    Invoking: `Calculator` with `0.43`                    > Entering new LLMMathChain chain...    0.43```text    0.43    ```
Certain OpenAI models (like gpt-3.5-turbo-0613 and gpt-4-0613) have been fine-tuned to detect when a function should be called and respond with the inputs that should be passed to the function. In an API call, you can describe functions and have the model intelligently choose to output a JSON object containing arguments to call those functions. The goal of the OpenAI Function APIs is to more reliably return valid and useful function calls than a generic text completion or chat API.
Certain OpenAI models (like gpt-3.5-turbo-0613 and gpt-4-0613) have been fine-tuned to detect when a function should be called and respond with the inputs that should be passed to the function. In an API call, you can describe functions and have the model intelligently choose to output a JSON object containing arguments to call those functions. The goal of the OpenAI Function APIs is to more reliably return valid and useful function calls than a generic text completion or chat API. ->: name="Search",        func=search.run,        description="useful for when you need to answer questions about current events. You should ask targeted questions"    ),    Tool(        name="Calculator",        func=llm_math_chain.run,        description="useful for when you need to answer questions about math"    ),    Tool(        name="FooBar-DB",        func=db_chain.run,        description="useful for when you need to answer questions about FooBar. Input should be in the form of a question containing full context"    )]Using LCEL‚ÄãWe will first use LangChain Expression Language to create this agentfrom langchain.prompts import ChatPromptTemplate, MessagesPlaceholderprompt = ChatPromptTemplate.from_messages([    ("system", "You are a helpful assistant"),    ("user", "{input}"),    MessagesPlaceholder(variable_name="agent_scratchpad"),])from langchain.tools.render import format_tool_to_openai_functionllm_with_tools = llm.bind(    functions=[format_tool_to_openai_function(t) for t in tools])from langchain.agents.format_scratchpad import format_to_openai_functionsfrom langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParseragent = {    "input": lambda x: x["input"],    "agent_scratchpad": lambda x: format_to_openai_functions(x['intermediate_steps'])} | prompt | llm_with_tools | OpenAIFunctionsAgentOutputParser()from langchain.agents import AgentExecutoragent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)agent_executor.invoke({"input": "Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?"})            > Entering new AgentExecutor chain...        Invoking: `Search` with `Leo DiCaprio's girlfriend`            ['Blake Lively and DiCaprio are believed to have enjoyed a whirlwind five-month romance in 2011. The pair were seen on a yacht together in Cannes, ...']    Invoking: `Calculator` with `0.43`                    > Entering new LLMMathChain chain...    0.43```text    0.43    ```