autogen_ext.tools.http#
- class HttpTool(name: str, host: str, port: int, json_schema: 字典[str, 任何], headers: dict[str, 任何] | 无 = None, description: str = 'HTTP tool', path: str = '/', scheme: 字面量['http', 'https'] = 'http', method: 字面量['GET', 'POST', 'PUT', 'DELETE', 'PATCH'] = 'POST', return_type: 字面量['text', 'json'] = 'text')[源代码]#
基类:
BaseTool
[BaseModel
,Any
],Component
[HttpToolConfig
]使用HTTP服务器作为工具的包装器。
- Parameters:
name (str) – 工具的名称。
description (str, optional) – 工具的说明。
scheme (str) – 用于请求的方案。必须是“http”或“https”。
host (str) – 发送请求的主机。
port (int) – 发送请求的端口。
path (str, optional) – 发送请求的路径。默认为“/”。可以包含路径参数,如“/{param1}/{param2}”,这些参数将从输入参数中模板化。
method (str, optional) – 使用的HTTP方法,如果未提供,则默认为POST。 必须是“GET”、“POST”、“PUT”、“DELETE”、“PATCH”之一。
json_schema (dict[str, Any]) – 一个JSON Schema对象,定义了工具的预期参数。 路径参数也必须包含在模式中,并且必须是字符串。
return_type (Literal["text", "json"], optional) – 从工具返回的响应类型。默认为“text”。
注意
该工具需要
autogen-ext
包中的http-tool
额外组件。安装步骤:
pip install -U "autogen-agentchat" "autogen-ext[http-tool]"
示例
简单用例:
import asyncio from autogen_agentchat.agents import AssistantAgent from autogen_agentchat.messages import TextMessage from autogen_core import CancellationToken from autogen_ext.models.openai import OpenAIChatCompletionClient from autogen_ext.tools.http import HttpTool # Define a JSON schema for a base64 decode tool base64_schema = { "type": "object", "properties": { "value": {"type": "string", "description": "The base64 value to decode"}, }, "required": ["value"], } # Create an HTTP tool for the httpbin API base64_tool = HttpTool( name="base64_decode", description="base64 decode a value", scheme="https", host="httpbin.org", port=443, path="/base64/{value}", method="GET", json_schema=base64_schema, ) async def main(): # Create an assistant with the base64 tool model = OpenAIChatCompletionClient(model="gpt-4") assistant = AssistantAgent("base64_assistant", model_client=model, tools=[base64_tool]) # The assistant can now use the base64 tool to decode the string response = await assistant.on_messages( [TextMessage(content="Can you base64 decode the value 'YWJjZGU=', please?", source="user")], CancellationToken(), ) print(response.chat_message.content) asyncio.run(main())
- classmethod _from_config(config: HttpToolConfig) 自我 [源代码]#
从配置对象创建组件的新实例。
- Parameters:
config (T) – 配置对象。
- Returns:
Self – 组件的新实例。
- component_config_schema#
HttpToolConfig
的别名
- component_provider_override: ClassVar[str | 无] = 'autogen_ext.tools.http.HttpTool'#
覆盖组件的提供商字符串。这应用于防止内部模块名称成为模块名称的一部分。
- component_type: ClassVar[ComponentType] = 'tool'#
组件的逻辑类型。
- async run(args: BaseModel, cancellation_token: CancellationToken) 任何 [源代码]#
使用给定的参数执行HTTP工具。
- Parameters:
args – 经过验证的输入参数
cancellation_token – 用于取消操作的令牌
- Returns:
HTTP调用返回的响应体为JSON格式
- Raises:
异常 – 如果工具执行失败