Skip to content

Commit

Permalink
Change to use Tool interface
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentGenie committed Jan 17, 2025
1 parent b62ba45 commit 9d1b2c6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
26 changes: 6 additions & 20 deletions autogen/agentchat/contrib/capabilities/tools_capability.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
# Copyright (c) 2023 - 2025, Owners of https://github.com/ag2ai
#
# SPDX-License-Identifier: Apache-2.0
from dataclasses import dataclass
from typing import Any, Callable, Optional

from autogen.agentchat import ConversableAgent, register_function


@dataclass
class ToolSpecs:
tool_func: Callable[..., Any]
tool_description: str
tool_name: Optional[str] = None
from autogen.agentchat import ConversableAgent
from autogen.tools import Tool


class ToolsCapability:
"""Adding a list of tools as composable capabilities to a single agent.
Note: both caller and executor of the tools are the same agent.
"""

def __init__(self, tool_list: list[ToolSpecs]):
self.tools = [specs for specs in tool_list]
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 specs in self.tools:
register_function(
f=specs.tool_func,
caller=agent,
executor=agent,
description=specs.tool_description,
name=specs.tool_name,
)
for tool in self.tools:
tool.register_tool(agent=agent)
10 changes: 10 additions & 0 deletions autogen/tools/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ 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 execyted by an agent.
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
17 changes: 9 additions & 8 deletions test/agentchat/contrib/capabilities/test_tools_capability.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import pytest

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


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

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


Expand All @@ -31,9 +32,9 @@ def test_agent():


class TestToolsCapability:
def test_add_capability(self, add_tool_spec, test_agent) -> None:
def test_add_capability(self, add_tools, test_agent) -> None:
# Arrange
tools_capability = ToolsCapability(tool_list=[add_tool_spec])
tools_capability = ToolsCapability(tool_list=[add_tools])
assert "tools" not in test_agent.llm_config
# Act
tools_capability.add_to_agent(agent=test_agent)
Expand Down

0 comments on commit 9d1b2c6

Please sign in to comment.