Skip to content

Tool

autogen.tools.Tool #

Tool(*, name=None, description=None, func_or_tool)

A class representing a Tool that can be used by an agent for various tasks.

This class encapsulates a tool with a name, description, and an executable function. The tool can be registered with a ConversableAgent for use either with an LLM or for direct execution.

ATTRIBUTE DESCRIPTION
name

The name of the tool.

TYPE: str

description

The description of the tool.

TYPE: str

func_or_tool

The function or Tool instance to create a Tool from.

TYPE: Union[Tool, Callable[..., Any]]

Create a new Tool object.

PARAMETER DESCRIPTION
name

The name of the tool.

TYPE: str DEFAULT: None

description

The description of the tool.

TYPE: str DEFAULT: None

func_or_tool

The function or Tool instance to create a Tool from.

TYPE: Union[Tool, Callable[..., Any]]

Source code in autogen/tools/tool.py
def __init__(
    self,
    *,
    name: Optional[str] = None,
    description: Optional[str] = None,
    func_or_tool: Union["Tool", Callable[..., Any]],
) -> None:
    """Create a new Tool object.

    Args:
        name (str): The name of the tool.
        description (str): The description of the tool.
        func_or_tool (Union[Tool, Callable[..., Any]]): The function or Tool instance to create a Tool from.
    """
    if isinstance(func_or_tool, Tool):
        self._name: str = name or func_or_tool.name
        self._description: str = description or func_or_tool.description
        self._func: Callable[..., Any] = func_or_tool.func
        self._chat_context_param_names: list[str] = func_or_tool._chat_context_param_names
    elif inspect.isfunction(func_or_tool) or inspect.ismethod(func_or_tool):
        self._chat_context_param_names = get_context_params(func_or_tool, subclass=ChatContext)
        self._func = inject_params(func_or_tool)
        self._name = name or func_or_tool.__name__
        self._description = description or func_or_tool.__doc__ or ""
    else:
        raise ValueError(
            f"Parameter 'func_or_tool' must be a function, method or a Tool instance, it is '{type(func_or_tool)}' instead."
        )

name property #

name

description property #

description

func property #

func

tool_schema property #

tool_schema

Get the schema for the tool.

This is the preferred way of handling function calls with OpeaAI and compatible frameworks.

function_schema property #

function_schema

Get the schema for the function.

This is the old way of handling function calls with OpenAI and compatible frameworks. It is provided for backward compatibility.

realtime_tool_schema property #

realtime_tool_schema

Get the schema for the tool.

This is the preferred way of handling function calls with OpeaAI and compatible frameworks.

register_for_llm #

register_for_llm(agent)

Registers the tool for use with a ConversableAgent's language model (LLM).

This method registers the tool so that it can be invoked by the agent during interactions with the language model.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_for_llm(self, agent: "ConversableAgent") -> None:
    """Registers the tool for use with a ConversableAgent's language model (LLM).

    This method registers the tool so that it can be invoked by the agent during
    interactions with the language model.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    agent.register_for_llm()(self)

register_for_execution #

register_for_execution(agent)

Registers the tool for direct execution by a ConversableAgent.

This method registers the tool so that it can be executed by the agent, typically outside of the context of an LLM interaction.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_for_execution(self, agent: "ConversableAgent") -> None:
    """Registers the tool for direct execution by a ConversableAgent.

    This method registers the tool so that it can be executed by the agent,
    typically outside of the context of an LLM interaction.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    agent.register_for_execution()(self)

register_tool #

register_tool(agent)

Register a tool to be both proposed and executed by an agent.

Equivalent to calling both register_for_llm and register_for_execution with the same agent.

Note: This will not make the agent recommend and execute the call in the one step. If the agent recommends the tool, it will need to be the next agent to speak in order to execute the tool.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_tool(self, agent: "ConversableAgent") -> None:
    """Register a tool to be both proposed and executed by an agent.

    Equivalent to calling both `register_for_llm` and `register_for_execution` with the same agent.

    Note: This will not make the agent recommend and execute the call in the one step. If the agent
    recommends the tool, it will need to be the next agent to speak in order to execute the tool.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    self.register_for_llm(agent)
    self.register_for_execution(agent)