We will import two last utility functions: a component for formatting intermediate steps to messages, and a component for converting the output message into an agent action/agent finish.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()Now that we have our agent, let's play around with it!
Let's pass in a simple question and empty intermediate steps and see what it returns:agent.invoke({    "input": "how many letters in the word educa?",    "intermediate_steps": []})We can see that it responds with an AgentAction to take (it's actually an AgentActionMessageLog - a subclass of AgentAction which also tracks the full message log).So this is just the first step - now we need to write a runtime for this.
The simplest one is just one that continuously loops, calling the agent, then taking the action, and repeating until an AgentFinish is returned.
Let's code that up below:from langchain.schema.agent import AgentFinishintermediate_steps = []while True:    output = agent.invoke({        "input": "how many letters in the word educa?",        "intermediate_steps": intermediate_steps    })    if isinstance(output, AgentFinish):        final_result = output.return_values["output"]        break    else:        print(output.tool, output.tool_input)        tool = {            "get_word_length": get_word_length        }[output.tool]        observation = tool.run(output.tool_input)        intermediate_steps.append((output, observation))print(final_result)We can see this prints out the following:get_word_length {'word': 'educa'}There are 5 letters in the word "educa".Woo! It's working.To simplify this a bit, we can import and use the AgentExecutor class.
The core idea of agents is to use an LLM to choose a sequence of actions to take.
The core idea of agents is to use an LLM to choose a sequence of actions to take. ->: We will import two last utility functions: a component for formatting intermediate steps to messages, and a component for converting the output message into an agent action/agent finish.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()Now that we have our agent, let's play around with it!
Let's pass in a simple question and empty intermediate steps and see what it returns:agent.invoke({    "input": "how many letters in the word educa?",    "intermediate_steps": []})We can see that it responds with an AgentAction to take (it's actually an AgentActionMessageLog - a subclass of AgentAction which also tracks the full message log).So this is just the first step - now we need to write a runtime for this.
The simplest one is just one that continuously loops, calling the agent, then taking the action, and repeating until an AgentFinish is returned.
Let's code that up below:from langchain.schema.agent import AgentFinishintermediate_steps = []while True:    output = agent.invoke({        "input": "how many letters in the word educa?",        "intermediate_steps": intermediate_steps    })    if isinstance(output, AgentFinish):        final_result = output.return_values["output"]        break    else:        print(output.tool, output.tool_input)        tool = {            "get_word_length": get_word_length        }[output.tool]        observation = tool.run(output.tool_input)        intermediate_steps.append((output, observation))print(final_result)We can see this prints out the following:get_word_length {'word': 'educa'}There are 5 letters in the word "educa".Woo! It's working.To simplify this a bit, we can import and use the AgentExecutor class.