Tools
Agents gain significant utility through tools as they provide access to external data, APIs, and functionality.
In AG2, using tools is done in two parts, an agent suggests which tools to use (via their LLM) and another executes the tool.
Note
In a conversation the executor agent must follow the agent that suggests a tool.
In the swarm example above, we attached tools to our agents and, as part of the swarm, AG2 created a tool executor agent to run recommended tools. Typically, you'll create two agents, one to decide which tool to use and another to execute it.
import Example from "/snippets/python-examples/toolregister.mdx";
-
Here's the tool, a function, that we'll attach to our agents, the
Annotated
parameter will be included in the call to the LLM so it understands what thedate_string
needs. -
The date_agent will determine whether to use the tool, using its LLM.
-
The executor_agent will run the tool and return the output as its reply.
-
Registering the tool with the agents and giving it a description to help the LLM determine.
-
We have a two-way chat, so after the date_agent the executor_agent will run and, if it sees that the date_agent suggested the use of the tool, it will execute it.
executor_agent (to date_agent): I was born on the 25th of March 1995, what day was it? -------------------------------------------------------------------------------- >>>>>>>> USING AUTO REPLY... date_agent (to executor_agent): ***** Suggested tool call (call_iOOZMTCoIVVwMkkSVu04Krj8): get_weekday ***** Arguments: {"date_string":"1995-03-25"} **************************************************************************** -------------------------------------------------------------------------------- >>>>>>>> EXECUTING FUNCTION get_weekday... Call ID: call_iOOZMTCoIVVwMkkSVu04Krj8 Input arguments: {'date_string': '1995-03-25'} executor_agent (to date_agent): ***** Response from calling tool (call_iOOZMTCoIVVwMkkSVu04Krj8) ***** Saturday ********************************************************************** -------------------------------------------------------------------------------- >>>>>>>> USING AUTO REPLY... date_agent (to executor_agent): It was a Saturday. --------------------------------------------------------------------------------
Alternatively, you can use decorators to register a tool. So, instead of using register_function
, you can register them with the function definition.