A programming framework for agentic AI
Allow callable to be used as `registered_tools` in `ToolUseAssistantAgent`. (#3891)
Eric Zhuc4492ca04301
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_tool_use_assistant_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_tool_use_assistant_agent.pyindex bb6ace764..d9cc57b04 100644--- a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_tool_use_assistant_agent.py+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_tool_use_assistant_agent.py@@ -1,4 +1,4 @@-from typing import List, Sequence+from typing import Any, Awaitable, Callable, List, Sequencefrom autogen_core.base import CancellationTokenfrom autogen_core.components import FunctionCall@@ -10,7 +10,7 @@ from autogen_core.components.models import (SystemMessage,UserMessage,)-from autogen_core.components.tools import Tool+from autogen_core.components.tools import FunctionTool, Toolfrom ..base import BaseToolUseChatAgentfrom ..messages import (@@ -27,21 +27,40 @@ class ToolUseAssistantAgent(BaseToolUseChatAgent):"""An agent that provides assistance with tool use.It responds with a StopMessage when 'terminate' is detected in the response.++ Args:+ name (str): The name of the agent.+ model_client (ChatCompletionClient): The model client to use for inference.+ registered_tools (List[Tool | Callable[..., Any] | Callable[..., Awaitable[Any]]): The tools to register with the agent.+ description (str, optional): The description of the agent.+ system_message (str, optional): The system message for the model."""def __init__(self,name: str,model_client: ChatCompletionClient,- registered_tools: List[Tool],+ registered_tools: List[Tool | Callable[..., Any] | Callable[..., Awaitable[Any]]],*,description: str = "An agent that provides assistance with ability to use tools.",system_message: str = "You are a helpful AI assistant. Solve tasks using your tools. Reply with 'TERMINATE' when the task has been completed.",):- super().__init__(name=name, description=description, registered_tools=registered_tools)+ tools: List[Tool] = []+ for tool in registered_tools:+ if isinstance(tool, Tool):+ tools.append(tool)+ elif callable(tool):+ if hasattr(tool, "__doc__") and tool.__doc__ is not None:+ description = tool.__doc__+ else:+ description = ""+ tools.append(FunctionTool(tool, description=description))+ else:+ raise ValueError(f"Unsupported tool type: {type(tool)}")+ super().__init__(name=name, description=description, registered_tools=tools)self._model_client = model_clientself._system_messages = [SystemMessage(content=system_message)]- self._tool_schema = [tool.schema for tool in registered_tools]+ self._tool_schema = [tool.schema for tool in tools]self._model_context: List[LLMMessage] = []async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token: CancellationToken) -> ChatMessage:diff --git a/python/packages/autogen-agentchat/tests/test_tool_use_assistant_agent.py b/python/packages/autogen-agentchat/tests/test_tool_use_assistant_agent.pynew file mode 100644index 000000000..624315227--- /dev/null+++ b/python/packages/autogen-agentchat/tests/test_tool_use_assistant_agent.py@@ -0,0 +1,120 @@+import asyncio+import json+from typing import Any, AsyncGenerator, List++import pytest+from autogen_agentchat.agents import ToolUseAssistantAgent+from autogen_agentchat.messages import (+ TextMessage,+ ToolCallMessage,+ ToolCallResultMessage,+)+from autogen_core.base import CancellationToken+from autogen_core.components.models import FunctionExecutionResult, OpenAIChatCompletionClient+from autogen_core.components.tools import FunctionTool+from openai.resources.chat.completions import AsyncCompletions+from openai.types.chat.chat_completion import ChatCompletion, Choice+from openai.types.chat.chat_completion_chunk import ChatCompletionChunk+from openai.types.chat.chat_completion_message import ChatCompletionMessage+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall, Function+from openai.types.completion_usage import CompletionUsage+++class _MockChatCompletion:+ def __init__(self, chat_completions: List[ChatCompletion]) -> None:+ self._saved_chat_completions = chat_completions+ self._curr_index = 0++ async def mock_create(+ self, *args: Any, **kwargs: Any+ ) -> ChatCompletion | AsyncGenerator[ChatCompletionChunk, None]:+ await asyncio.sleep(0.1)+ completion = self._saved_chat_completions[self._curr_index]+ self._curr_index += 1+ return completion+++def _pass_function(input: str) -> str:+ return "pass"+++async def _fail_function(input: str) -> str:+ return "fail"+++async def _echo_function(input: str) -> str:+ return input+++async def test_round_robin_group_chat_with_tools(monkeypatch: pytest.MonkeyPatch) -> None:+ model = "gpt-4o-2024-05-13"+ chat_completions = [+ ChatCompletion(+ id="id1",+ choices=[+ Choice(+ finish_reason="tool_calls",+ index=0,+ message=ChatCompletionMessage(+ content=None,+ tool_calls=[+ ChatCompletionMessageToolCall(+ id="1",+ type="function",+ function=Function(+ name="pass",+ arguments=json.dumps({"input": "pass"}),+ ),+ )+ ],+ role="assistant",+ ),+ )+ ],+ created=0,+ model=model,+ object="chat.completion",+ usage=CompletionUsage(prompt_tokens=0, completion_tokens=0, total_tokens=0),+ ),+ ChatCompletion(+ id="id2",+ choices=[+ Choice(finish_reason="stop", index=0, message=ChatCompletionMessage(content="Hello", role="assistant"))+ ],+ created=0,+ model=model,+ object="chat.completion",+ usage=CompletionUsage(prompt_tokens=0, completion_tokens=0, total_tokens=0),+ ),+ ChatCompletion(+ id="id2",+ choices=[+ Choice(+ finish_reason="stop", index=0, message=ChatCompletionMessage(content="TERMINATE", role="assistant")+ )+ ],+ created=0,+ model=model,+ object="chat.completion",+ usage=CompletionUsage(prompt_tokens=0, completion_tokens=0, total_tokens=0),+ ),+ ]+ mock = _MockChatCompletion(chat_completions)+ monkeypatch.setattr(AsyncCompletions, "create", mock.mock_create)+ tool_use_agent = ToolUseAssistantAgent(+ "tool_use_agent",+ model_client=OpenAIChatCompletionClient(model=model, api_key=""),+ registered_tools=[_pass_function, _fail_function, FunctionTool(_echo_function, description="Echo")],+ )+ response = await tool_use_agent.on_messages(+ messages=[TextMessage(content="Test", source="user")], cancellation_token=CancellationToken()+ )+ assert isinstance(response, ToolCallMessage)+ tool_call_results = [FunctionExecutionResult(content="", call_id=call.id) for call in response.content]++ response = await tool_use_agent.on_messages(+ messages=[ToolCallResultMessage(content=tool_call_results, source="test")],+ cancellation_token=CancellationToken(),+ )+ assert isinstance(response, TextMessage)