From 82a94f28d3113efa01439993d677b11825f94240 Mon Sep 17 00:00:00 2001 From: Meng Zhang Date: Sat, 21 Oct 2023 15:49:18 -0700 Subject: [PATCH] chore: add update-python-client to Makefile --- Makefile | 12 ++ clients/tabby-python-client/setup.py | 2 +- .../tabby_client/api/v1beta/__init__.py | 0 .../tabby_client/api/v1beta/search.py | 194 ++++++++++++++++++ .../tabby_client/models/__init__.py | 4 + .../tabby_client/models/completion_request.py | 19 ++ .../models/completion_response.py | 24 ++- .../tabby_client/models/debug_data.py | 84 ++++++++ .../tabby_client/models/debug_options.py | 57 +++++ experimental/openapi/gen-python.sh | 9 - 10 files changed, 394 insertions(+), 11 deletions(-) create mode 100644 clients/tabby-python-client/tabby_client/api/v1beta/__init__.py create mode 100644 clients/tabby-python-client/tabby_client/api/v1beta/search.py create mode 100644 clients/tabby-python-client/tabby_client/models/debug_data.py create mode 100644 clients/tabby-python-client/tabby_client/models/debug_options.py delete mode 100755 experimental/openapi/gen-python.sh diff --git a/Makefile b/Makefile index 26e45bfe7..f8d1ac4ce 100644 --- a/Makefile +++ b/Makefile @@ -30,3 +30,15 @@ update-openapi-doc: ["components", "schemas", "DebugOptions"] \ ])' | jq '.servers[0] |= { url: "https://playground.app.tabbyml.com", description: "Playground server" }' \ > website/static/openapi.json + +update-python-client: + rm -rf clients/tabby-python-client + curl http://localhost:8080/api-docs/openapi.json | jq ' \ + delpaths([ \ + ["paths", "/v1beta/chat/completions"] \ + ])' > /tmp/openapi.json + + cd clients && openapi-python-client generate \ + --path /tmp/openapi.json \ + --config ../experimental/openapi/python.yaml \ + --meta setup diff --git a/clients/tabby-python-client/setup.py b/clients/tabby-python-client/setup.py index 96347632f..b520cacc2 100644 --- a/clients/tabby-python-client/setup.py +++ b/clients/tabby-python-client/setup.py @@ -7,7 +7,7 @@ long_description = (here / "README.md").read_text(encoding="utf-8") setup( name="tabby-python-client", - version="0.3.0", + version="0.3.1", description="A client library for accessing Tabby Server", long_description=long_description, long_description_content_type="text/markdown", diff --git a/clients/tabby-python-client/tabby_client/api/v1beta/__init__.py b/clients/tabby-python-client/tabby_client/api/v1beta/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/clients/tabby-python-client/tabby_client/api/v1beta/search.py b/clients/tabby-python-client/tabby_client/api/v1beta/search.py new file mode 100644 index 000000000..0a9e30095 --- /dev/null +++ b/clients/tabby-python-client/tabby_client/api/v1beta/search.py @@ -0,0 +1,194 @@ +from http import HTTPStatus +from typing import Any, Dict, Optional, Union, cast + +import httpx + +from ... import errors +from ...client import Client +from ...models.search_response import SearchResponse +from ...types import UNSET, Response, Unset + + +def _get_kwargs( + *, + client: Client, + q: str = "get", + limit: Union[Unset, None, int] = 20, + offset: Union[Unset, None, int] = 0, +) -> Dict[str, Any]: + url = "{}/v1beta/search".format(client.base_url) + + headers: Dict[str, str] = client.get_headers() + cookies: Dict[str, Any] = client.get_cookies() + + params: Dict[str, Any] = {} + params["q"] = q + + params["limit"] = limit + + params["offset"] = offset + + params = {k: v for k, v in params.items() if v is not UNSET and v is not None} + + return { + "method": "get", + "url": url, + "headers": headers, + "cookies": cookies, + "timeout": client.get_timeout(), + "follow_redirects": client.follow_redirects, + "params": params, + } + + +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, SearchResponse]]: + if response.status_code == HTTPStatus.OK: + response_200 = SearchResponse.from_dict(response.json()) + + return response_200 + if response.status_code == HTTPStatus.NOT_IMPLEMENTED: + response_501 = cast(Any, None) + return response_501 + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(response.status_code, response.content) + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, SearchResponse]]: + return Response( + status_code=HTTPStatus(response.status_code), + content=response.content, + headers=response.headers, + parsed=_parse_response(client=client, response=response), + ) + + +def sync_detailed( + *, + client: Client, + q: str = "get", + limit: Union[Unset, None, int] = 20, + offset: Union[Unset, None, int] = 0, +) -> Response[Union[Any, SearchResponse]]: + """ + Args: + q (str): Default: 'get'. + limit (Union[Unset, None, int]): Default: 20. + offset (Union[Unset, None, int]): + + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Response[Union[Any, SearchResponse]] + """ + + kwargs = _get_kwargs( + client=client, + q=q, + limit=limit, + offset=offset, + ) + + response = httpx.request( + verify=client.verify_ssl, + **kwargs, + ) + + return _build_response(client=client, response=response) + + +def sync( + *, + client: Client, + q: str = "get", + limit: Union[Unset, None, int] = 20, + offset: Union[Unset, None, int] = 0, +) -> Optional[Union[Any, SearchResponse]]: + """ + Args: + q (str): Default: 'get'. + limit (Union[Unset, None, int]): Default: 20. + offset (Union[Unset, None, int]): + + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Union[Any, SearchResponse] + """ + + return sync_detailed( + client=client, + q=q, + limit=limit, + offset=offset, + ).parsed + + +async def asyncio_detailed( + *, + client: Client, + q: str = "get", + limit: Union[Unset, None, int] = 20, + offset: Union[Unset, None, int] = 0, +) -> Response[Union[Any, SearchResponse]]: + """ + Args: + q (str): Default: 'get'. + limit (Union[Unset, None, int]): Default: 20. + offset (Union[Unset, None, int]): + + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Response[Union[Any, SearchResponse]] + """ + + kwargs = _get_kwargs( + client=client, + q=q, + limit=limit, + offset=offset, + ) + + async with httpx.AsyncClient(verify=client.verify_ssl) as _client: + response = await _client.request(**kwargs) + + return _build_response(client=client, response=response) + + +async def asyncio( + *, + client: Client, + q: str = "get", + limit: Union[Unset, None, int] = 20, + offset: Union[Unset, None, int] = 0, +) -> Optional[Union[Any, SearchResponse]]: + """ + Args: + q (str): Default: 'get'. + limit (Union[Unset, None, int]): Default: 20. + offset (Union[Unset, None, int]): + + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Union[Any, SearchResponse] + """ + + return ( + await asyncio_detailed( + client=client, + q=q, + limit=limit, + offset=offset, + ) + ).parsed diff --git a/clients/tabby-python-client/tabby_client/models/__init__.py b/clients/tabby-python-client/tabby_client/models/__init__.py index 9b707a15f..8a24c738a 100644 --- a/clients/tabby-python-client/tabby_client/models/__init__.py +++ b/clients/tabby-python-client/tabby_client/models/__init__.py @@ -5,6 +5,8 @@ from .chat_completion_request import ChatCompletionRequest from .choice import Choice from .completion_request import CompletionRequest from .completion_response import CompletionResponse +from .debug_data import DebugData +from .debug_options import DebugOptions from .health_state import HealthState from .hit import Hit from .hit_document import HitDocument @@ -21,6 +23,8 @@ __all__ = ( "Choice", "CompletionRequest", "CompletionResponse", + "DebugData", + "DebugOptions", "HealthState", "Hit", "HitDocument", diff --git a/clients/tabby-python-client/tabby_client/models/completion_request.py b/clients/tabby-python-client/tabby_client/models/completion_request.py index 8bbcc9297..98aed46de 100644 --- a/clients/tabby-python-client/tabby_client/models/completion_request.py +++ b/clients/tabby-python-client/tabby_client/models/completion_request.py @@ -5,6 +5,7 @@ import attr from ..types import UNSET, Unset if TYPE_CHECKING: + from ..models.debug_options import DebugOptions from ..models.segments import Segments @@ -26,12 +27,14 @@ class CompletionRequest: user (Union[Unset, None, str]): A unique identifier representing your end-user, which can help Tabby to monitor & generating reports. + debug_options (Union[Unset, None, DebugOptions]): """ prompt: Union[Unset, None, str] = UNSET language: Union[Unset, None, str] = UNSET segments: Union[Unset, None, "Segments"] = UNSET user: Union[Unset, None, str] = UNSET + debug_options: Union[Unset, None, "DebugOptions"] = UNSET additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict) def to_dict(self) -> Dict[str, Any]: @@ -42,6 +45,9 @@ class CompletionRequest: segments = self.segments.to_dict() if self.segments else None user = self.user + debug_options: Union[Unset, None, Dict[str, Any]] = UNSET + if not isinstance(self.debug_options, Unset): + debug_options = self.debug_options.to_dict() if self.debug_options else None field_dict: Dict[str, Any] = {} field_dict.update(self.additional_properties) @@ -54,11 +60,14 @@ class CompletionRequest: field_dict["segments"] = segments if user is not UNSET: field_dict["user"] = user + if debug_options is not UNSET: + field_dict["debug_options"] = debug_options return field_dict @classmethod def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T: + from ..models.debug_options import DebugOptions from ..models.segments import Segments d = src_dict.copy() @@ -77,11 +86,21 @@ class CompletionRequest: user = d.pop("user", UNSET) + _debug_options = d.pop("debug_options", UNSET) + debug_options: Union[Unset, None, DebugOptions] + if _debug_options is None: + debug_options = None + elif isinstance(_debug_options, Unset): + debug_options = UNSET + else: + debug_options = DebugOptions.from_dict(_debug_options) + completion_request = cls( prompt=prompt, language=language, segments=segments, user=user, + debug_options=debug_options, ) completion_request.additional_properties = d diff --git a/clients/tabby-python-client/tabby_client/models/completion_response.py b/clients/tabby-python-client/tabby_client/models/completion_response.py index bc1a4c84a..e03b09513 100644 --- a/clients/tabby-python-client/tabby_client/models/completion_response.py +++ b/clients/tabby-python-client/tabby_client/models/completion_response.py @@ -1,9 +1,12 @@ -from typing import TYPE_CHECKING, Any, Dict, List, Type, TypeVar +from typing import TYPE_CHECKING, Any, Dict, List, Type, TypeVar, Union import attr +from ..types import UNSET, Unset + if TYPE_CHECKING: from ..models.choice import Choice + from ..models.debug_data import DebugData T = TypeVar("T", bound="CompletionResponse") @@ -18,10 +21,12 @@ class CompletionResponse: Attributes: id (str): choices (List['Choice']): + debug_data (Union[Unset, None, DebugData]): """ id: str choices: List["Choice"] + debug_data: Union[Unset, None, "DebugData"] = UNSET additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict) def to_dict(self) -> Dict[str, Any]: @@ -32,6 +37,10 @@ class CompletionResponse: choices.append(choices_item) + debug_data: Union[Unset, None, Dict[str, Any]] = UNSET + if not isinstance(self.debug_data, Unset): + debug_data = self.debug_data.to_dict() if self.debug_data else None + field_dict: Dict[str, Any] = {} field_dict.update(self.additional_properties) field_dict.update( @@ -40,12 +49,15 @@ class CompletionResponse: "choices": choices, } ) + if debug_data is not UNSET: + field_dict["debug_data"] = debug_data return field_dict @classmethod def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T: from ..models.choice import Choice + from ..models.debug_data import DebugData d = src_dict.copy() id = d.pop("id") @@ -57,9 +69,19 @@ class CompletionResponse: choices.append(choices_item) + _debug_data = d.pop("debug_data", UNSET) + debug_data: Union[Unset, None, DebugData] + if _debug_data is None: + debug_data = None + elif isinstance(_debug_data, Unset): + debug_data = UNSET + else: + debug_data = DebugData.from_dict(_debug_data) + completion_response = cls( id=id, choices=choices, + debug_data=debug_data, ) completion_response.additional_properties = d diff --git a/clients/tabby-python-client/tabby_client/models/debug_data.py b/clients/tabby-python-client/tabby_client/models/debug_data.py new file mode 100644 index 000000000..9e2a57a06 --- /dev/null +++ b/clients/tabby-python-client/tabby_client/models/debug_data.py @@ -0,0 +1,84 @@ +from typing import TYPE_CHECKING, Any, Dict, List, Type, TypeVar, Union + +import attr + +from ..types import UNSET, Unset + +if TYPE_CHECKING: + from ..models.snippet import Snippet + + +T = TypeVar("T", bound="DebugData") + + +@attr.s(auto_attribs=True) +class DebugData: + """ + Attributes: + prompt (str): + snippets (Union[Unset, List['Snippet']]): + """ + + prompt: str + snippets: Union[Unset, List["Snippet"]] = UNSET + additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict) + + def to_dict(self) -> Dict[str, Any]: + prompt = self.prompt + snippets: Union[Unset, List[Dict[str, Any]]] = UNSET + if not isinstance(self.snippets, Unset): + snippets = [] + for snippets_item_data in self.snippets: + snippets_item = snippets_item_data.to_dict() + + snippets.append(snippets_item) + + field_dict: Dict[str, Any] = {} + field_dict.update(self.additional_properties) + field_dict.update( + { + "prompt": prompt, + } + ) + if snippets is not UNSET: + field_dict["snippets"] = snippets + + return field_dict + + @classmethod + def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T: + from ..models.snippet import Snippet + + d = src_dict.copy() + prompt = d.pop("prompt") + + snippets = [] + _snippets = d.pop("snippets", UNSET) + for snippets_item_data in _snippets or []: + snippets_item = Snippet.from_dict(snippets_item_data) + + snippets.append(snippets_item) + + debug_data = cls( + prompt=prompt, + snippets=snippets, + ) + + debug_data.additional_properties = d + return debug_data + + @property + def additional_keys(self) -> List[str]: + return list(self.additional_properties.keys()) + + def __getitem__(self, key: str) -> Any: + return self.additional_properties[key] + + def __setitem__(self, key: str, value: Any) -> None: + self.additional_properties[key] = value + + def __delitem__(self, key: str) -> None: + del self.additional_properties[key] + + def __contains__(self, key: str) -> bool: + return key in self.additional_properties diff --git a/clients/tabby-python-client/tabby_client/models/debug_options.py b/clients/tabby-python-client/tabby_client/models/debug_options.py new file mode 100644 index 000000000..e3aff2da2 --- /dev/null +++ b/clients/tabby-python-client/tabby_client/models/debug_options.py @@ -0,0 +1,57 @@ +from typing import Any, Dict, List, Type, TypeVar + +import attr + +T = TypeVar("T", bound="DebugOptions") + + +@attr.s(auto_attribs=True) +class DebugOptions: + """ + Attributes: + enabled (bool): When true, returns debug_data in completion response. + """ + + enabled: bool + additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict) + + def to_dict(self) -> Dict[str, Any]: + enabled = self.enabled + + field_dict: Dict[str, Any] = {} + field_dict.update(self.additional_properties) + field_dict.update( + { + "enabled": enabled, + } + ) + + return field_dict + + @classmethod + def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T: + d = src_dict.copy() + enabled = d.pop("enabled") + + debug_options = cls( + enabled=enabled, + ) + + debug_options.additional_properties = d + return debug_options + + @property + def additional_keys(self) -> List[str]: + return list(self.additional_properties.keys()) + + def __getitem__(self, key: str) -> Any: + return self.additional_properties[key] + + def __setitem__(self, key: str, value: Any) -> None: + self.additional_properties[key] = value + + def __delitem__(self, key: str) -> None: + del self.additional_properties[key] + + def __contains__(self, key: str) -> bool: + return key in self.additional_properties diff --git a/experimental/openapi/gen-python.sh b/experimental/openapi/gen-python.sh deleted file mode 100755 index 0bd7d4cf6..000000000 --- a/experimental/openapi/gen-python.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -ex - -cd clients - -openapi-python-client generate \ - --path ../website/static/openapi.json \ - --config ../experimental/openapi/python.yaml \ - --meta setup