Skip to content

Commit

Permalink
Tools capability (#526)
Browse files Browse the repository at this point in the history
* Add tools capability

* Add unit test

* Change to use Tool interface

* Update

* Added to docstring and test

Signed-off-by: Mark Sze <[email protected]>

---------

Signed-off-by: Mark Sze <[email protected]>
Co-authored-by: Mark Sze <[email protected]>
  • Loading branch information
AgentGenie and marklysze authored Jan 20, 2025
1 parent 8d29269 commit 1fb6248
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
24 changes: 24 additions & 0 deletions autogen/agentchat/contrib/capabilities/tools_capability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2023 - 2025, Owners of https://github.com/ag2ai
#
# SPDX-License-Identifier: Apache-2.0

from autogen.agentchat import ConversableAgent
from autogen.tools import Tool


class ToolsCapability:
"""Adding a list of tools as composable capabilities to a single agent.
This class can be inherited from to allow code to run at the point of creating or adding the capability.
Note: both caller and executor of the tools are the same agent.
"""

def __init__(self, tool_list: list[Tool]):
self.tools = [tool for tool in tool_list]

def add_to_agent(self, agent: ConversableAgent):
"""
Add tools to the given agent.
"""
for tool in self.tools:
tool.register_tool(agent=agent)
15 changes: 15 additions & 0 deletions autogen/tools/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ def register_for_execution(self, agent: "ConversableAgent") -> None:
"""
agent.register_for_execution()(self)

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)

def __call__(self, *args: Any, **kwargs: Any) -> Any:
"""Execute the tool by calling its underlying function with the provided arguments.
Expand Down
43 changes: 43 additions & 0 deletions test/agentchat/contrib/capabilities/test_tools_capability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
#
# SPDX-License-Identifier: Apache-2.0

import pytest

from autogen import AssistantAgent
from autogen.agentchat.contrib.capabilities.tools_capability import ToolsCapability
from autogen.tools import Tool


@pytest.fixture
def add_tools():
def add(x: int, y: int) -> int:
return x + y

return Tool(
name="add_function",
description="Provide add function to two argument and return sum.",
func_or_tool=add,
)


@pytest.fixture
def test_agent():
return AssistantAgent(
name="test_agent",
llm_config={
"config_list": [{"model": "gpt-4O", "api_key": "sk-proj-ABC"}],
},
)


class TestToolsCapability:
def test_add_capability(self, add_tools, test_agent) -> None:
# Arrange
tools_capability = ToolsCapability(tool_list=[add_tools])
assert "tools" not in test_agent.llm_config
# Act
tools_capability.add_to_agent(agent=test_agent)
# Assert that the tool was added for LLM and Execution
assert len(test_agent.llm_config["tools"]) == 1 # LLM
assert len(test_agent.function_map) == 1 # Execution

0 comments on commit 1fb6248

Please sign in to comment.