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]#
Bases:
BaseTool
[BaseModel
,Any
],Component
[HttpToolConfig
]A wrapper for using an HTTP server as a tool.
- Parameters:
name (str) – The name of the tool.
description (str, optional) – A description of the tool.
scheme (str) – The scheme to use for the request. Must be either “http” or “https”.
host (str) – The host to send the request to.
port (int) – The port to send the request to.
path (str, optional) – The path to send the request to. Defaults to “/”. Can include path parameters like “/{param1}/{param2}” which will be templated from input args.
method (str, optional) – The HTTP method to use, will default to POST if not provided. Must be one of “GET”, “POST”, “PUT”, “DELETE”, “PATCH”.
headers (dict[str, Any], optional) – A dictionary of headers to send with the request.
json_schema (dict[str, Any]) – A JSON Schema object defining the expected parameters for the tool. Path parameters must also be included in the schema and must be strings.
return_type (Literal["text", "json"], optional) – The type of response to return from the tool. Defaults to “text”.
Note
This tool requires the
http-tool
extra for theautogen-ext
package.To install:
pip install -U "autogen-agentchat" "autogen-ext[http-tool]"
Example
Simple use case:
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) Self [source]#
Create a new instance of the component from a configuration object.
- Parameters:
config (T) – The configuration object.
- Returns:
Self – The new instance of the component.
- _to_config() HttpToolConfig [source]#
Dump the configuration that would be requite to create a new instance of a component matching the configuration of this instance.
- Returns:
T – The configuration of the component.
- component_config_schema#
alias of
HttpToolConfig
- component_provider_override: ClassVar[str | None] = 'autogen_ext.tools.http.HttpTool'#
Override the provider string for the component. This should be used to prevent internal module names being a part of the module name.
- component_type: ClassVar[ComponentType] = 'tool'#
The logical type of the component.
- async run(args: BaseModel, cancellation_token: CancellationToken) Any [source]#
Execute the HTTP tool with the given arguments.
- Parameters:
args – The validated input arguments
cancellation_token – Token for cancelling the operation
- Returns:
The response body from the HTTP call in JSON format
- Raises:
Exception – If tool execution fails