|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, List, Union, Dict, Optional |
| 4 | +from typing_extensions import Literal |
| 5 | + |
| 6 | +from ...core import NOT_GIVEN, Body, Headers, NotGiven, BaseAPI, maybe_transform, StreamResponse, deepcopy_minimal |
| 7 | + |
| 8 | +import httpx |
| 9 | + |
| 10 | +from ...core import ( |
| 11 | + make_request_options, |
| 12 | +) |
| 13 | +import logging |
| 14 | + |
| 15 | +from ...types.tools import tools_web_search_params, WebSearch, WebSearchChunk |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from ..._client import ZhipuAI |
| 21 | + |
| 22 | +__all__ = ["Tools"] |
| 23 | + |
| 24 | + |
| 25 | +class Tools(BaseAPI): |
| 26 | + |
| 27 | + def __init__(self, client: "ZhipuAI") -> None: |
| 28 | + super().__init__(client) |
| 29 | + |
| 30 | + def web_search( |
| 31 | + self, |
| 32 | + *, |
| 33 | + model: str, |
| 34 | + request_id: Optional[str] | NotGiven = NOT_GIVEN, |
| 35 | + stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN, |
| 36 | + messages: Union[str, List[str], List[int], object, None], |
| 37 | + scope: Optional[str] | NotGiven = NOT_GIVEN, |
| 38 | + location: Optional[str] | NotGiven = NOT_GIVEN, |
| 39 | + recent_days: Optional[int] | NotGiven = NOT_GIVEN, |
| 40 | + extra_headers: Headers | None = None, |
| 41 | + extra_body: Body | None = None, |
| 42 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 43 | + ) -> WebSearch | StreamResponse[WebSearchChunk]: |
| 44 | + |
| 45 | + body = deepcopy_minimal( |
| 46 | + { |
| 47 | + "model": model, |
| 48 | + "request_id": request_id, |
| 49 | + "messages": messages, |
| 50 | + "stream": stream, |
| 51 | + "scope": scope, |
| 52 | + "location": location, |
| 53 | + "recent_days": recent_days, |
| 54 | + }) |
| 55 | + return self._post( |
| 56 | + "/tools", |
| 57 | + body= maybe_transform(body, tools_web_search_params.WebSearchParams), |
| 58 | + options=make_request_options( |
| 59 | + extra_headers=extra_headers, extra_body=extra_body, timeout=timeout |
| 60 | + ), |
| 61 | + cast_type=WebSearch, |
| 62 | + stream=stream or False, |
| 63 | + stream_cls=StreamResponse[WebSearchChunk], |
| 64 | + ) |
0 commit comments