autogen_ext.tools.http#

class HttpTool(name: str, host: str, port: int, json_schema: dict[str, Any], headers: dict[str, Any] | None = None, description: str = 'HTTP tool', path: str = '/', scheme: Literal['http', 'https'] = 'http', method: Literal['GET', 'POST', 'PUT', 'DELETE', 'PATCH'] = 'POST', return_type: Literal['text', 'json'] = 'text')[source]#

基类: BaseTool[BaseModel, Any], Component[HttpToolConfig]

用作工具的 HTTP 服务器的包装器。

参数:
  • name (str) – 工具的名称。

  • description (str, 可选) – 工具的描述。

  • scheme (str) – 用于请求的方案。必须是“http”或“https”。

  • host (str) – 发送请求的主机。

  • port (int) – 发送请求的端口。

  • path (str, 可选) – 发送请求的路径。默认为“/”。可以包含路径参数,例如“/{param1}/{param2}”,这些参数将从输入参数进行模板化。

  • method (str, 可选) – 要使用的 HTTP 方法,如果未提供,则默认为 POST。必须是“GET”、“POST”、“PUT”、“DELETE”、“PATCH”之一。

  • headers (dict[str, Any], 可选) – 要随请求一起发送的标头字典。

  • json_schema (dict[str, Any]) – 一个 JSON Schema 对象,用于定义工具的预期参数。路径参数也必须包含在 schema 中,并且必须是字符串。

  • return_type (Literal["text", "json"], 可选) – 从工具返回的响应类型。默认为“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)


asyncio.run(main())
classmethod _from_config(config: HttpToolConfig) Self[source]#

从配置对象创建一个组件的新实例。

参数:

config (T) – 配置对象。

返回:

Self – 组件的新实例。

_to_config() HttpToolConfig[source]#

转储创建与此实例配置匹配的组件新实例所需的配置。

返回:

T – 组件的配置。

component_config_schema#

HttpToolConfig 的别名

component_provider_override: ClassVar[str | None] = 'autogen_ext.tools.http.HttpTool'#

覆盖组件的提供者字符串。 这应该用于防止内部模块名称成为模块名称的一部分。

component_type: ClassVar[ComponentType] = 'tool'#

组件的逻辑类型。

async run(args: BaseModel, cancellation_token: CancellationToken) Any[source]#

使用给定参数执行 HTTP 工具。

参数:
  • args – 验证后的输入参数

  • cancellation_token – 用于取消操作的令牌

返回:

来自 HTTP 调用的 JSON 格式的响应正文

引发:

Exception – 如果工具执行失败