03. Agent

Tool Calling Agent

With tool calls, the model has more than one tool (tool) end Detecting when it should be called and typing it to the tool Can be delivered to.

You can intelligently choose to describe the tool in the API call and let the model output structured objects like JSON with arguments to call these tools.

The goal of the tool API is more reliably valid and useful than can be done using plain text completion or chat API Tool call (tool call) Is to return.

Combining these structured outputs with the fact that you can bind multiple tools to the tool call chat model and select the tool the model will call, you can create an agent that repeatedly calls the tool and receives results until the query is resolved.

This is more of an OpenAI tool agent designed for OpenAI's specific tool calling style. Generalized version is.

In addition to OpenAI using LangChain's ToolCall interface, this agent Anthropic , Google Gemini , Mistral Supports a wider range of supplier implementations such as.

Reference link

Copy

# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv

# Load API key information
load_dotenv()

Copy

 True 

Copy

Copy

Copy

Copy

Copy

Agent prompt creation

  • chat_history : Variables that store previous conversations (if you don't support multi-turns, you can omit them)

  • agent_scratchpad : Variables that the agent temporarily saves

  • input : User input

Copy

Agent creation

Copy

AgentExecutor

AgentExecutor is a class that runs agents that use tools.

Main properties

  • agent : Agent that creates plans and determines behavior at each stage of the execution loop

  • tools : List of valid tools available to agents

  • return_intermediate_steps : Whether to return the agent's mid-stage path along with the final output

  • max_iterations : Maximum number of steps before exiting the run loop

  • max_execution_time : Maximum time that can be spent on the running loop

  • early_stopping_method : Agent AgentFinish Early termination method to use when not returning. ("force" or "generate")

  • "force" Returns a string that was stopped by reaching the time or iterative limit.

  • "generate" Is the agent's LLM chain last called to generate a final answer based on the previous steps.

  • handle_parsing_errors : How to handle errors in the agent's output parser. (True, False, or error handling function)

  • trim_intermediate_steps : How to trim the middle step. (-1 trim not, or trim function)

Main method

  1. invoke : Agent execution

  2. stream : Stream the steps needed to reach the final output

Main features

  1. Tool verification : Make sure it is a tool compatible with the agent

  2. Execution control : Set maximum number of iterations and run time limits

  3. Error handling : Provides various processing options for output parsing errors

  4. Intermediate management : Intermediate trimming and return options

  5. Asynchronous support : Asynchronous execution and streaming support

Optimization tips

  • max_iterations Wow max_execution_time Manage run time by setting up properly

  • trim_intermediate_steps Optimize memory usage by utilizing

  • For complex tasks stream Step-by-step results monitoring using methods

Copy

Copy

  • tool_callback : Function that handles tool call output

  • observation_callback : Function that handles the observation (Observation) output

  • result_callback : Function that handles the final answer output

Define the following three functions, which customizes the intermediate step output.

Output intermediate step output as custom function

Copy

Copy

Copy

output

Contents

Action

actions : AgentAction or its subclass messages : Chat message corresponding to action call

Observation

steps : Records of what the agent has done so far, including current actions and their observations messages : Chat message including function call result (i.e. observation)

Final Answer

output : AgentFinish messages : Chat message including final output

The contents of these outputs are summarized as follows.

Then, when the final goal is achieved, the agent will output the final answer.

... (Continue until goal achievement) ...

  1. Action output

  2. Observation output

  3. Action output

  4. Observation output

It will look like this:

stream() The output of (Action, Observation) alternates between pairs, and finally ends with an answer if the agent has achieved the goal.

AgentExecutor stream() We will use the method to stream the intermediate stages of the agent.

Check step-by-step results with Stream output

Copy

Check Agent's response process by streaming method.

Copy

This callback function can be useful when outputting intermediate steps from Streamlit and serving it to the user.

Below is a callback function used to neatly output Agent's mid-step process.

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Reference - RunnableWithMessageHistory

RunnableWithMessageHistory Please refer to the link below for more information on this.

To remember the previous conversation RunnableWithMessageHistory Using AgentExecutor Wraps.

Agent to remember the previous conversation

Copy

Copy

If you check the output content below, you can see that the output value of the intermediate content has changed to the output value of the callback function I changed.

Copy

Here is how to modify and use callback.

Copy

Last updated