tools.http
- class agentopera.tools.http.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
]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 theagentopera
package.To install:
pip install -U "agentopera" "agentopera[http-tool]"
Example
Simple use case:
import asyncio from agentopera.chatflow.agents import AssistantAgent from agentopera.chatflow.messages import TextMessage from agentopera.core import CancellationToken from agentopera.models.openai import OpenAIChatCompletionClient from agentopera.agents.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())
- component_type = 'tool'
- component_provider_override = 'agentopera.agents.tools.http.HttpTool'
- component_config_schema
alias of
HttpToolConfig
- 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