|
| 1 | +# TODO: Remove when Python 3.9 support is dropped |
| 2 | +from __future__ import annotations |
| 3 | + |
1 | 4 | import asyncio |
2 | 5 | import base64 |
3 | 6 | import json |
4 | 7 | from collections.abc import Sequence |
5 | 8 | from enum import Enum |
6 | 9 | from functools import partial |
7 | | -from typing import Annotated, Any, TypeAlias, cast |
| 10 | +from typing import Annotated, Any, cast |
8 | 11 |
|
9 | 12 | import requests |
10 | 13 | from langchain_core.tools import BaseTool |
11 | 14 | from pydantic import BaseModel, BeforeValidator, Field, PrivateAttr |
12 | 15 | from requests.exceptions import RequestException |
13 | 16 |
|
| 17 | +# TODO: Remove when Python 3.9 support is dropped |
| 18 | +from typing_extensions import TypeAlias |
| 19 | + |
14 | 20 | # Type aliases for common types |
15 | 21 | JsonDict: TypeAlias = dict[str, Any] |
16 | 22 | Headers: TypeAlias = dict[str, str] |
@@ -140,21 +146,20 @@ def _prepare_request_params(self, kwargs: JsonDict) -> tuple[str, JsonDict, Json |
140 | 146 | for key, value in kwargs.items(): |
141 | 147 | param_location = self._execute_config.parameter_locations.get(key) |
142 | 148 |
|
143 | | - match param_location: |
144 | | - case ParameterLocation.PATH: |
| 149 | + if param_location == ParameterLocation.PATH: |
| 150 | + url = url.replace(f"{{{key}}}", str(value)) |
| 151 | + elif param_location == ParameterLocation.QUERY: |
| 152 | + query_params[key] = value |
| 153 | + elif param_location in (ParameterLocation.BODY, ParameterLocation.FILE): |
| 154 | + body_params[key] = value |
| 155 | + else: |
| 156 | + # Default behavior |
| 157 | + if f"{{{key}}}" in url: |
145 | 158 | url = url.replace(f"{{{key}}}", str(value)) |
146 | | - case ParameterLocation.QUERY: |
| 159 | + elif self._execute_config.method in {"GET", "DELETE"}: |
147 | 160 | query_params[key] = value |
148 | | - case ParameterLocation.BODY | ParameterLocation.FILE: |
| 161 | + else: |
149 | 162 | body_params[key] = value |
150 | | - case _: |
151 | | - # Default behavior |
152 | | - if f"{{{key}}}" in url: |
153 | | - url = url.replace(f"{{{key}}}", str(value)) |
154 | | - elif self._execute_config.method in {"GET", "DELETE"}: |
155 | | - query_params[key] = value |
156 | | - else: |
157 | | - body_params[key] = value |
158 | 163 |
|
159 | 164 | return url, body_params, query_params |
160 | 165 |
|
@@ -355,13 +360,12 @@ def to_langchain(self) -> BaseTool: |
355 | 360 | python_type: type = str # Default to str |
356 | 361 | if isinstance(details, dict): |
357 | 362 | type_str = details.get("type", "string") |
358 | | - match type_str: |
359 | | - case "number": |
360 | | - python_type = float |
361 | | - case "integer": |
362 | | - python_type = int |
363 | | - case "boolean": |
364 | | - python_type = bool |
| 363 | + if type_str == "number": |
| 364 | + python_type = float |
| 365 | + elif type_str == "integer": |
| 366 | + python_type = int |
| 367 | + elif type_str == "boolean": |
| 368 | + python_type = bool |
365 | 369 |
|
366 | 370 | field = Field(description=details.get("description", "")) |
367 | 371 | else: |
@@ -480,7 +484,7 @@ def to_langchain(self) -> Sequence[BaseTool]: |
480 | 484 | """ |
481 | 485 | return [tool.to_langchain() for tool in self.tools] |
482 | 486 |
|
483 | | - def meta_tools(self) -> "Tools": |
| 487 | + def meta_tools(self) -> Tools: |
484 | 488 | """Return meta tools for tool discovery and execution |
485 | 489 |
|
486 | 490 | Meta tools enable dynamic tool discovery and execution based on natural language queries. |
|
0 commit comments