工具#
工具是可由代理执行的代码,以执行操作。工具可以是一个简单的函数,如计算器,也可以是对第三方服务的API调用,如股票价格查询或天气预报。在AI代理的上下文中,工具被设计为由代理执行,以响应模型生成的函数调用。
AutoGen 提供了 autogen_core.tools
模块,其中包含一套内置工具和实用程序,用于创建和运行自定义工具。
内置工具#
其中一个内置工具是 PythonCodeExecutionTool
,它允许代理执行 Python 代码片段。
以下是如何创建该工具并使用它。
from autogen_core import CancellationToken
from autogen_ext.code_executors.docker import DockerCommandLineCodeExecutor
from autogen_ext.tools.code_execution import PythonCodeExecutionTool
# Create the tool.
code_executor = DockerCommandLineCodeExecutor()
await code_executor.start()
code_execution_tool = PythonCodeExecutionTool(code_executor)
cancellation_token = CancellationToken()
# Use the tool directly without an agent.
code = "print('Hello, world!')"
result = await code_execution_tool.run_json({"code": code}, cancellation_token)
print(code_execution_tool.return_value_as_string(result))
Hello, world!
DockerCommandLineCodeExecutor
类是一个内置的代码执行器,它在docker容器的命令行环境中运行Python代码片段。
PythonCodeExecutionTool
类封装了代码执行器
并提供了一个简单的接口来执行Python代码片段。
其他内置工具的示例
LocalSearchTool
和GlobalSearchTool
用于使用 GraphRAG。mcp_server_tools
用于将 模型上下文协议 (MCP) 服务器用作工具。HttpTool
用于向REST API发送HTTP请求。LangChainToolAdapter
用于使用 LangChain 工具。
自定义函数工具#
一个工具也可以是一个执行特定操作的简单Python函数。要创建一个自定义函数工具,你只需要创建一个Python函数并使用FunctionTool
类来包装它。
FunctionTool
类使用描述和类型注释来告知LLM何时以及如何使用给定的函数。描述提供了关于函数目的和预期使用场景的上下文,而类型注释则告知LLM预期的参数和返回类型。
例如,一个获取公司股价的简单工具可能如下所示:
import random
from autogen_core import CancellationToken
from autogen_core.tools import FunctionTool
from typing_extensions import Annotated
async def get_stock_price(ticker: str, date: Annotated[str, "Date in YYYY/MM/DD"]) -> float:
# Returns a random stock price for demonstration purposes.
return random.uniform(10, 200)
# Create a function tool.
stock_price_tool = FunctionTool(get_stock_price, description="Get the stock price.")
# Run the tool.
cancellation_token = CancellationToken()
result = await stock_price_tool.run_json({"ticker": "AAPL", "date": "2021/01/01"}, cancellation_token)
# Print the result.
print(stock_price_tool.return_value_as_string(result))
36.63801673457121
使用模型客户端调用工具#
模型客户端在提供工具列表时可以生成工具调用。
这里是一个如何使用FunctionTool
类与OpenAIChatCompletionClient
的示例。其他模型客户端类也可以类似使用。更多详情请参见Model Clients。
import json
from autogen_core.models import AssistantMessage, FunctionExecutionResult, FunctionExecutionResultMessage, UserMessage
from autogen_ext.models.openai import OpenAIChatCompletionClient
# Create the OpenAI chat completion client. Using OPENAI_API_KEY from environment variable.
client = OpenAIChatCompletionClient(model="gpt-4o-mini")
# Create a user message.
user_message = UserMessage(content="What is the stock price of AAPL on 2021/01/01?", source="user")
# Run the chat completion with the stock_price_tool defined above.
cancellation_token = CancellationToken()
create_result = await client.create(
messages=[user_message], tools=[stock_price_tool], cancellation_token=cancellation_token
)
create_result.content
[FunctionCall(id='call_tpJ5J1Xoxi84Sw4v0scH0qBM', arguments='{"ticker":"AAPL","date":"2021/01/01"}', name='get_stock_price')]
结果是一个包含FunctionCall
对象的列表,这些对象可用于运行相应的工具。
arguments = json.loads(create_result.content[0].arguments) # type: ignore
tool_result = await stock_price_tool.run_json(arguments, cancellation_token)
tool_result_str = stock_price_tool.return_value_as_string(tool_result)
tool_result_str
'32.381250753393104'
现在你可以进行另一个模型客户端调用,让模型生成对工具执行结果的反思。
工具调用的结果被包装在一个FunctionExecutionResult
对象中,该对象包含工具执行的结果和被调用工具的ID。模型客户端可以使用此信息生成对工具执行结果的反思。
# Create a function execution result
exec_result = FunctionExecutionResult(
call_id=create_result.content[0].id, # type: ignore
content=tool_result_str,
is_error=False,
name=stock_price_tool.name,
)
# Make another chat completion with the history and function execution result message.
messages = [
user_message,
AssistantMessage(content=create_result.content, source="assistant"), # assistant message with tool call
FunctionExecutionResultMessage(content=[exec_result]), # function execution result message
]
create_result = await client.create(messages=messages, cancellation_token=cancellation_token) # type: ignore
print(create_result.content)
The stock price of AAPL (Apple Inc.) on January 1, 2021, was approximately $32.38.
配备工具的代理#
将模型客户端和工具结合在一起,您可以创建一个配备工具的代理,该代理可以使用工具执行操作,并反思这些操作的结果。
import asyncio
import json
from dataclasses import dataclass
from typing import List
from autogen_core import (
AgentId,
FunctionCall,
MessageContext,
RoutedAgent,
SingleThreadedAgentRuntime,
message_handler,
)
from autogen_core.models import (
ChatCompletionClient,
LLMMessage,
SystemMessage,
UserMessage,
)
from autogen_core.tools import FunctionTool, Tool
from autogen_ext.models.openai import OpenAIChatCompletionClient
@dataclass
class Message:
content: str
class ToolUseAgent(RoutedAgent):
def __init__(self, model_client: ChatCompletionClient, tool_schema: List[Tool]) -> None:
super().__init__("An agent with tools")
self._system_messages: List[LLMMessage] = [SystemMessage(content="You are a helpful AI assistant.")]
self._model_client = model_client
self._tools = tool_schema
@message_handler
async def handle_user_message(self, message: Message, ctx: MessageContext) -> Message:
# Create a session of messages.
session: List[LLMMessage] = self._system_messages + [UserMessage(content=message.content, source="user")]
# Run the chat completion with the tools.
create_result = await self._model_client.create(
messages=session,
tools=self._tools,
cancellation_token=ctx.cancellation_token,
)
# If there are no tool calls, return the result.
if isinstance(create_result.content, str):
return Message(content=create_result.content)
assert isinstance(create_result.content, list) and all(
isinstance(call, FunctionCall) for call in create_result.content
)
# Add the first model create result to the session.
session.append(AssistantMessage(content=create_result.content, source="assistant"))
# Execute the tool calls.
results = await asyncio.gather(
*[self._execute_tool_call(call, ctx.cancellation_token) for call in create_result.content]
)
# Add the function execution results to the session.
session.append(FunctionExecutionResultMessage(content=results))
# Run the chat completion again to reflect on the history and function execution results.
create_result = await self._model_client.create(
messages=session,
cancellation_token=ctx.cancellation_token,
)
assert isinstance(create_result.content, str)
# Return the result as a message.
return Message(content=create_result.content)
async def _execute_tool_call(
self, call: FunctionCall, cancellation_token: CancellationToken
) -> FunctionExecutionResult:
# Find the tool by name.
tool = next((tool for tool in self._tools if tool.name == call.name), None)
assert tool is not None
# Run the tool and capture the result.
try:
arguments = json.loads(call.arguments)
result = await tool.run_json(arguments, cancellation_token)
return FunctionExecutionResult(
call_id=call.id, content=tool.return_value_as_string(result), is_error=False, name=tool.name
)
except Exception as e:
return FunctionExecutionResult(call_id=call.id, content=str(e), is_error=True, name=tool.name)
当处理用户消息时,ToolUseAgent
类首先使用模型客户端生成一系列对工具的函数调用,然后运行这些工具并生成关于工具执行结果的反思。该反思随后作为代理的响应返回给用户。
要运行代理,让我们创建一个运行时并将代理注册到运行时。
# Create a runtime.
runtime = SingleThreadedAgentRuntime()
# Create the tools.
tools: List[Tool] = [FunctionTool(get_stock_price, description="Get the stock price.")]
# Register the agents.
await ToolUseAgent.register(
runtime,
"tool_use_agent",
lambda: ToolUseAgent(
OpenAIChatCompletionClient(model="gpt-4o-mini"),
tools,
),
)
AgentType(type='tool_use_agent')
本示例使用OpenAIChatCompletionClient
,
对于Azure OpenAI和其他客户端,请参阅Model Clients。
让我们用一个关于股票价格的问题来测试代理。
# Start processing messages.
runtime.start()
# Send a direct message to the tool agent.
tool_use_agent = AgentId("tool_use_agent", "default")
response = await runtime.send_message(Message("What is the stock price of NVDA on 2024/06/01?"), tool_use_agent)
print(response.content)
# Stop processing messages.
await runtime.stop()
The stock price of NVIDIA (NVDA) on June 1, 2024, was approximately $140.05.