Basics
Agents significantly enhance their capabilities by leveraging tools, which provide access to external data, APIs, and additional functionality.
In AG2, tool usage happens in two stages: - An agent suggests which tool to use (via its LLM). - Another agent executes the selected tool.
Typically, you'll create two agents. One to determine the appropriate tool and another to carry out the execution.
Note
In a conversation, the executor agent must always follow the agent that suggests a tool.
import Example from "/snippets/python-examples/toolregister.mdx";
-
We define a tool, a function that will be attached to our agents. The
Annotated
parameter is included in the LLM call to ensure it understands the purpose ofdate_string
. -
The
date_agent
decides whether to use the tool based on its LLM reasoning. -
The
executor_agent
executes the tool and returns the output as its response. -
We register the tool with the agents and provide a description to help the LLM determine when to use it.
-
Since this is a two-way conversation, the
executor_agent
follows thedate_agent
. If thedate_agent
suggests using the tool, theexecutor_agent
executes it accordingly.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 register_for_execution
and register_for_llm
to register a tool. So, instead of using register_function
, you can register them with the function definition.