forked from TabbyML/tabby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python): add tabby-python-client (TabbyML#268)
* feat(python): add tabby-python-client * switch to setup.py
- Loading branch information
Showing
23 changed files
with
1,171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
__pycache__/ | ||
build/ | ||
dist/ | ||
*.egg-info/ | ||
.pytest_cache/ | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# Environments | ||
.env | ||
.venv | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# JetBrains | ||
.idea/ | ||
|
||
/coverage.xml | ||
/.coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# tabby-python-client | ||
A client library for accessing Tabby Server | ||
|
||
## Usage | ||
First, create a client: | ||
|
||
```python | ||
from tabby_client import Client | ||
|
||
client = Client(base_url="https://api.example.com") | ||
``` | ||
|
||
If the endpoints you're going to hit require authentication, use `AuthenticatedClient` instead: | ||
|
||
```python | ||
from tabby_client import AuthenticatedClient | ||
|
||
client = AuthenticatedClient(base_url="https://api.example.com", token="SuperSecretToken") | ||
``` | ||
|
||
Now call your endpoint and use your models: | ||
|
||
```python | ||
from tabby_client.models import MyDataModel | ||
from tabby_client.api.my_tag import get_my_data_model | ||
from tabby_client.types import Response | ||
|
||
my_data: MyDataModel = get_my_data_model.sync(client=client) | ||
# or if you need more info (e.g. status_code) | ||
response: Response[MyDataModel] = get_my_data_model.sync_detailed(client=client) | ||
``` | ||
|
||
Or do the same thing with an async version: | ||
|
||
```python | ||
from tabby_client.models import MyDataModel | ||
from tabby_client.api.my_tag import get_my_data_model | ||
from tabby_client.types import Response | ||
|
||
my_data: MyDataModel = await get_my_data_model.asyncio(client=client) | ||
response: Response[MyDataModel] = await get_my_data_model.asyncio_detailed(client=client) | ||
``` | ||
|
||
By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle. | ||
|
||
```python | ||
client = AuthenticatedClient( | ||
base_url="https://internal_api.example.com", | ||
token="SuperSecretToken", | ||
verify_ssl="/path/to/certificate_bundle.pem", | ||
) | ||
``` | ||
|
||
You can also disable certificate validation altogether, but beware that **this is a security risk**. | ||
|
||
```python | ||
client = AuthenticatedClient( | ||
base_url="https://internal_api.example.com", | ||
token="SuperSecretToken", | ||
verify_ssl=False | ||
) | ||
``` | ||
|
||
There are more settings on the generated `Client` class which let you control more runtime behavior, check out the docstring on that class for more info. | ||
|
||
Things to know: | ||
1. Every path/method combo becomes a Python module with four functions: | ||
1. `sync`: Blocking request that returns parsed data (if successful) or `None` | ||
1. `sync_detailed`: Blocking request that always returns a `Request`, optionally with `parsed` set if the request was successful. | ||
1. `asyncio`: Like `sync` but async instead of blocking | ||
1. `asyncio_detailed`: Like `sync_detailed` but async instead of blocking | ||
|
||
1. All path/query params, and bodies become method arguments. | ||
1. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above) | ||
1. Any endpoint which did not have a tag will be in `tabby_client.api.default` | ||
|
||
## Building / publishing this Client | ||
This project uses [Poetry](https://python-poetry.org/) to manage dependencies and packaging. Here are the basics: | ||
1. Update the metadata in pyproject.toml (e.g. authors, version) | ||
1. If you're using a private repository, configure it with Poetry | ||
1. `poetry config repositories.<your-repository-name> <url-to-your-repository>` | ||
1. `poetry config http-basic.<your-repository-name> <username> <password>` | ||
1. Publish the client with `poetry publish --build -r <your-repository-name>` or, if for public PyPI, just `poetry publish --build` | ||
|
||
If you want to install this client into another project without publishing it (e.g. for development) then: | ||
1. If that project **is using Poetry**, you can simply do `poetry add <path-to-this-client>` from that project | ||
1. If that project is not using Poetry: | ||
1. Build a wheel with `poetry build -f wheel` | ||
1. Install that wheel from the other project `pip install <path-to-wheel>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[tool.black] | ||
line-length = 120 | ||
target_version = ['py38', 'py39', 'py310', 'py311'] | ||
exclude = ''' | ||
( | ||
/( | ||
| \.git | ||
| \.venv | ||
| \.mypy_cache | ||
)/ | ||
) | ||
''' | ||
|
||
[tool.isort] | ||
line_length = 120 | ||
profile = "black" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pathlib | ||
|
||
from setuptools import find_packages, setup | ||
|
||
here = pathlib.Path(__file__).parent.resolve() | ||
long_description = (here / "README.md").read_text(encoding="utf-8") | ||
|
||
setup( | ||
name="tabby-python-client", | ||
version="0.1.0", | ||
description="A client library for accessing Tabby Server", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
packages=find_packages(), | ||
python_requires=">=3.8, <4", | ||
install_requires=["httpx >= 0.15.0, < 0.25.0", "attrs >= 21.3.0", "python-dateutil >= 2.8.0, < 3"], | ||
package_data={"tabby_client": ["py.typed"]}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
""" A client library for accessing Tabby Server """ | ||
from .client import AuthenticatedClient, Client | ||
|
||
__all__ = ( | ||
"AuthenticatedClient", | ||
"Client", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
""" Contains methods for accessing the API """ |
Empty file.
166 changes: 166 additions & 0 deletions
166
clients/tabby-python-client/tabby_client/api/v1/completion.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
from http import HTTPStatus | ||
from typing import Any, Dict, Optional, Union, cast | ||
|
||
import httpx | ||
|
||
from ... import errors | ||
from ...client import Client | ||
from ...models.completion_request import CompletionRequest | ||
from ...models.completion_response import CompletionResponse | ||
from ...types import Response | ||
|
||
|
||
def _get_kwargs( | ||
*, | ||
client: Client, | ||
json_body: CompletionRequest, | ||
) -> Dict[str, Any]: | ||
url = "{}/v1/completions".format(client.base_url) | ||
|
||
headers: Dict[str, str] = client.get_headers() | ||
cookies: Dict[str, Any] = client.get_cookies() | ||
|
||
json_json_body = json_body.to_dict() | ||
|
||
return { | ||
"method": "post", | ||
"url": url, | ||
"headers": headers, | ||
"cookies": cookies, | ||
"timeout": client.get_timeout(), | ||
"follow_redirects": client.follow_redirects, | ||
"json": json_json_body, | ||
} | ||
|
||
|
||
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, CompletionResponse]]: | ||
if response.status_code == HTTPStatus.OK: | ||
response_200 = CompletionResponse.from_dict(response.json()) | ||
|
||
return response_200 | ||
if response.status_code == HTTPStatus.BAD_REQUEST: | ||
response_400 = cast(Any, None) | ||
return response_400 | ||
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, CompletionResponse]]: | ||
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, | ||
json_body: CompletionRequest, | ||
) -> Response[Union[Any, CompletionResponse]]: | ||
r""" | ||
Args: | ||
json_body (CompletionRequest): Example: {'language': 'python', 'segments': {'prefix': | ||
'def fib(n):\n ', 'suffix': '\n return fib(n - 1) + fib(n - 2)'}}. | ||
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, CompletionResponse]] | ||
""" | ||
|
||
kwargs = _get_kwargs( | ||
client=client, | ||
json_body=json_body, | ||
) | ||
|
||
response = httpx.request( | ||
verify=client.verify_ssl, | ||
**kwargs, | ||
) | ||
|
||
return _build_response(client=client, response=response) | ||
|
||
|
||
def sync( | ||
*, | ||
client: Client, | ||
json_body: CompletionRequest, | ||
) -> Optional[Union[Any, CompletionResponse]]: | ||
r""" | ||
Args: | ||
json_body (CompletionRequest): Example: {'language': 'python', 'segments': {'prefix': | ||
'def fib(n):\n ', 'suffix': '\n return fib(n - 1) + fib(n - 2)'}}. | ||
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, CompletionResponse] | ||
""" | ||
|
||
return sync_detailed( | ||
client=client, | ||
json_body=json_body, | ||
).parsed | ||
|
||
|
||
async def asyncio_detailed( | ||
*, | ||
client: Client, | ||
json_body: CompletionRequest, | ||
) -> Response[Union[Any, CompletionResponse]]: | ||
r""" | ||
Args: | ||
json_body (CompletionRequest): Example: {'language': 'python', 'segments': {'prefix': | ||
'def fib(n):\n ', 'suffix': '\n return fib(n - 1) + fib(n - 2)'}}. | ||
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, CompletionResponse]] | ||
""" | ||
|
||
kwargs = _get_kwargs( | ||
client=client, | ||
json_body=json_body, | ||
) | ||
|
||
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, | ||
json_body: CompletionRequest, | ||
) -> Optional[Union[Any, CompletionResponse]]: | ||
r""" | ||
Args: | ||
json_body (CompletionRequest): Example: {'language': 'python', 'segments': {'prefix': | ||
'def fib(n):\n ', 'suffix': '\n return fib(n - 1) + fib(n - 2)'}}. | ||
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, CompletionResponse] | ||
""" | ||
|
||
return ( | ||
await asyncio_detailed( | ||
client=client, | ||
json_body=json_body, | ||
) | ||
).parsed |
Oops, something went wrong.