Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pydantic import BaseModel, Field
from typing_extensions import Self

DEFAULT_TIMEOUT_CONFIG = 5.0


class HttpToolConfig(BaseModel):
name: str
Expand Down Expand Up @@ -53,6 +55,10 @@ class HttpToolConfig(BaseModel):
"""
The type of response to return from the tool.
"""
timeout: float = DEFAULT_TIMEOUT_CONFIG
"""
The timeout for the tool request in seconds.
"""


class HttpTool(BaseTool[BaseModel, Any], Component[HttpToolConfig]):
Expand All @@ -73,6 +79,8 @@ class HttpTool(BaseTool[BaseModel, Any], Component[HttpToolConfig]):
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".
timeout (float, optional): The timeout for HTTP requests in seconds.
Defaults to 5.0.

.. note::
This tool requires the :code:`http-tool` extra for the :code:`autogen-ext` package.
Expand Down Expand Up @@ -148,6 +156,7 @@ def __init__(
scheme: Literal["http", "https"] = "http",
method: Literal["GET", "POST", "PUT", "DELETE", "PATCH"] = "POST",
return_type: Literal["text", "json"] = "text",
timeout: float = DEFAULT_TIMEOUT_CONFIG,
) -> None:
self.server_params = HttpToolConfig(
name=name,
Expand All @@ -160,6 +169,7 @@ def __init__(
headers=headers,
json_schema=json_schema,
return_type=return_type,
timeout=timeout,
)

# Use regex to find all path parameters, we will need those later to template the path
Expand Down Expand Up @@ -211,7 +221,8 @@ async def run(self, args: BaseModel, cancellation_token: CancellationToken) -> A
port=self.server_params.port,
path=path,
)
async with httpx.AsyncClient() as client:
timeout_config = httpx.Timeout(timeout=self.server_params.timeout)
async with httpx.AsyncClient(timeout=timeout_config) as client:
match self.server_params.method:
case "GET":
response = await client.get(url, headers=self.server_params.headers, params=model_dump)
Expand Down
Loading