-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
8d29269
commit 1fb6248
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
autogen/agentchat/contrib/capabilities/tools_capability.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/agentchat/contrib/capabilities/test_tools_capability.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |