Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools capability #526

Merged
merged 6 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions autogen/agentchat/contrib/capabilities/tools_capability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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


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 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,
)
41 changes: 41 additions & 0 deletions test/agentchat/contrib/capabilities/test_tools_capability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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 ToolSpecs, ToolsCapability


@pytest.fixture
def add_tool_spec():
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",
)


@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_tool_spec, test_agent) -> None:
# Arrange
tools_capability = ToolsCapability(tool_list=[add_tool_spec])
assert "tools" not in test_agent.llm_config
# Act
tools_capability.add_to_agent(agent=test_agent)
# Assert
assert len(test_agent.llm_config["tools"]) == 1
Loading