Skip to content

Commit

Permalink
chore: add update-python-client to Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
wsxiaoys committed Oct 21, 2023
1 parent 8a5be05 commit 82a94f2
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 11 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion clients/tabby-python-client/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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",
Expand Down
Empty file.
194 changes: 194 additions & 0 deletions clients/tabby-python-client/tabby_client/api/v1beta/search.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions clients/tabby-python-client/tabby_client/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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
Expand All @@ -21,6 +23,8 @@
"Choice",
"CompletionRequest",
"CompletionResponse",
"DebugData",
"DebugOptions",
"HealthState",
"Hit",
"HitDocument",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ..types import UNSET, Unset

if TYPE_CHECKING:
from ..models.debug_options import DebugOptions
from ..models.segments import Segments


Expand All @@ -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]:
Expand All @@ -42,6 +45,9 @@ def to_dict(self) -> Dict[str, Any]:
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)
Expand All @@ -54,11 +60,14 @@ def to_dict(self) -> Dict[str, Any]:
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()
Expand All @@ -77,11 +86,21 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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]:
Expand All @@ -32,6 +37,10 @@ def to_dict(self) -> Dict[str, Any]:

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(
Expand All @@ -40,12 +49,15 @@ def to_dict(self) -> Dict[str, Any]:
"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")
Expand All @@ -57,9 +69,19 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:

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
Expand Down
Loading

0 comments on commit 82a94f2

Please sign in to comment.