sonnely-sdk 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sonnely_sdk-0.1.2/PKG-INFO +31 -0
- sonnely_sdk-0.1.2/README.md +22 -0
- sonnely_sdk-0.1.2/pyproject.toml +14 -0
- sonnely_sdk-0.1.2/setup.cfg +4 -0
- sonnely_sdk-0.1.2/src/sonnely/__init__.py +94 -0
- sonnely_sdk-0.1.2/src/sonnely_sdk.egg-info/PKG-INFO +31 -0
- sonnely_sdk-0.1.2/src/sonnely_sdk.egg-info/SOURCES.txt +7 -0
- sonnely_sdk-0.1.2/src/sonnely_sdk.egg-info/dependency_links.txt +1 -0
- sonnely_sdk-0.1.2/src/sonnely_sdk.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sonnely-sdk
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Server-side Python client for the Sonnely inference API
|
|
5
|
+
Project-URL: Documentation, https://docs.sonnely.com/docs/sdks
|
|
6
|
+
Project-URL: Homepage, https://sonnely.com
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# Sonnely SDK for Python
|
|
11
|
+
|
|
12
|
+
Run text inference on Sonnely from Python 3.10 or newer. Create a funded [Sonnely API key](https://sonnely.com/console/keys) and store it server-side as `SONNELY_API_KEY`.
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
import os
|
|
16
|
+
from sonnely import Sonnely
|
|
17
|
+
|
|
18
|
+
sonnely = Sonnely(os.environ["SONNELY_API_KEY"])
|
|
19
|
+
request = {
|
|
20
|
+
"model": "z-ai/glm-5.3-flash",
|
|
21
|
+
"messages": [{"role": "user", "content": "Describe winter in two words."}],
|
|
22
|
+
"max_tokens": 128,
|
|
23
|
+
"max_cost_microusd": 500,
|
|
24
|
+
}
|
|
25
|
+
result = sonnely.complete(request, receipt=True)
|
|
26
|
+
print(result["choices"][0]["message"]["content"], result["usage"])
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Call `models()` for live routes and rates. Use `stream(request, receipt=True)` for an iterator that yields text deltas and final usage. The iterator requires the gateway's settlement marker to complete normally. Each completion or stream is billed separately.
|
|
30
|
+
|
|
31
|
+
[SDK guide](https://docs.sonnely.com/docs/sdks) · [API reference](https://docs.sonnely.com/docs/api) · [Usage](https://sonnely.com/console/usage)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Sonnely SDK for Python
|
|
2
|
+
|
|
3
|
+
Run text inference on Sonnely from Python 3.10 or newer. Create a funded [Sonnely API key](https://sonnely.com/console/keys) and store it server-side as `SONNELY_API_KEY`.
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
import os
|
|
7
|
+
from sonnely import Sonnely
|
|
8
|
+
|
|
9
|
+
sonnely = Sonnely(os.environ["SONNELY_API_KEY"])
|
|
10
|
+
request = {
|
|
11
|
+
"model": "z-ai/glm-5.3-flash",
|
|
12
|
+
"messages": [{"role": "user", "content": "Describe winter in two words."}],
|
|
13
|
+
"max_tokens": 128,
|
|
14
|
+
"max_cost_microusd": 500,
|
|
15
|
+
}
|
|
16
|
+
result = sonnely.complete(request, receipt=True)
|
|
17
|
+
print(result["choices"][0]["message"]["content"], result["usage"])
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Call `models()` for live routes and rates. Use `stream(request, receipt=True)` for an iterator that yields text deltas and final usage. The iterator requires the gateway's settlement marker to complete normally. Each completion or stream is billed separately.
|
|
21
|
+
|
|
22
|
+
[SDK guide](https://docs.sonnely.com/docs/sdks) · [API reference](https://docs.sonnely.com/docs/api) · [Usage](https://sonnely.com/console/usage)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sonnely-sdk"
|
|
7
|
+
version = "0.1.2"
|
|
8
|
+
description = "Server-side Python client for the Sonnely inference API"
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
|
|
12
|
+
[project.urls]
|
|
13
|
+
Documentation = "https://docs.sonnely.com/docs/sdks"
|
|
14
|
+
Homepage = "https://sonnely.com"
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Server-side client for the Sonnely inference API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from typing import Any, Iterator
|
|
7
|
+
from urllib.error import HTTPError
|
|
8
|
+
from urllib.request import HTTPRedirectHandler, Request, build_opener
|
|
9
|
+
|
|
10
|
+
__all__ = ["Sonnely", "SonnelyError"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class SonnelyError(Exception):
|
|
14
|
+
"""An HTTP or stream error from the Sonnely gateway."""
|
|
15
|
+
|
|
16
|
+
def __init__(self, message: str, status: int):
|
|
17
|
+
super().__init__(message)
|
|
18
|
+
self.status = status
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class _NoRedirect(HTTPRedirectHandler):
|
|
22
|
+
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
23
|
+
return None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Sonnely:
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
api_key: str | None = None,
|
|
30
|
+
*,
|
|
31
|
+
base_url: str = "https://sonnely.com/api/sonnely/v1",
|
|
32
|
+
timeout: float = 70,
|
|
33
|
+
):
|
|
34
|
+
self._api_key = api_key
|
|
35
|
+
self._base_url = base_url.rstrip("/")
|
|
36
|
+
self._timeout = timeout
|
|
37
|
+
self._opener = build_opener(_NoRedirect())
|
|
38
|
+
|
|
39
|
+
def _send(self, path: str, body: dict[str, Any] | None = None, *, receipt: bool = False):
|
|
40
|
+
headers = {"User-Agent": "SonnelySDK-Python"}
|
|
41
|
+
if body is not None:
|
|
42
|
+
if not self._api_key:
|
|
43
|
+
raise SonnelyError("A Sonnely API key is required for completions.", 401)
|
|
44
|
+
headers["Authorization"] = f"Bearer {self._api_key}"
|
|
45
|
+
headers["Content-Type"] = "application/json"
|
|
46
|
+
if receipt:
|
|
47
|
+
headers["X-Sonnely-Receipt"] = "v1"
|
|
48
|
+
request = Request(
|
|
49
|
+
self._base_url + path,
|
|
50
|
+
data=json.dumps(body, ensure_ascii=False, separators=(",", ":")).encode() if body is not None else None,
|
|
51
|
+
headers=headers,
|
|
52
|
+
)
|
|
53
|
+
try:
|
|
54
|
+
return self._opener.open(request, timeout=self._timeout)
|
|
55
|
+
except HTTPError as error:
|
|
56
|
+
with error:
|
|
57
|
+
try:
|
|
58
|
+
message = json.load(error)["error"]["message"]
|
|
59
|
+
except (ValueError, KeyError, TypeError):
|
|
60
|
+
message = f"Sonnely returned HTTP {error.code}."
|
|
61
|
+
raise SonnelyError(message, error.code) from None
|
|
62
|
+
|
|
63
|
+
def models(self) -> dict[str, Any]:
|
|
64
|
+
"""List public models and provider routes."""
|
|
65
|
+
with self._send("/models") as response:
|
|
66
|
+
return json.load(response)
|
|
67
|
+
|
|
68
|
+
def complete(self, request: dict[str, Any], *, receipt: bool = False) -> dict[str, Any]:
|
|
69
|
+
"""Request a single completion."""
|
|
70
|
+
with self._send("/chat/completions", {**request, "stream": False}, receipt=receipt) as response:
|
|
71
|
+
return json.load(response)
|
|
72
|
+
|
|
73
|
+
def stream(
|
|
74
|
+
self, request: dict[str, Any], *, receipt: bool = False, include_usage: bool = True
|
|
75
|
+
) -> Iterator[dict[str, Any]]:
|
|
76
|
+
"""Yield completion chunks and require Sonnely's settlement marker."""
|
|
77
|
+
body = {**request, "stream": True, "stream_options": {"include_usage": include_usage}}
|
|
78
|
+
with self._send("/chat/completions", body, receipt=receipt) as response:
|
|
79
|
+
fields = []
|
|
80
|
+
for raw_line in response:
|
|
81
|
+
line = raw_line.decode("utf-8").rstrip("\r\n")
|
|
82
|
+
if line.startswith("data:"):
|
|
83
|
+
fields.append(line[5:].removeprefix(" "))
|
|
84
|
+
elif not line and fields:
|
|
85
|
+
data = "\n".join(fields)
|
|
86
|
+
fields.clear()
|
|
87
|
+
if data == "[DONE]":
|
|
88
|
+
return
|
|
89
|
+
event = json.loads(data)
|
|
90
|
+
if "error" in event:
|
|
91
|
+
error = event["error"]
|
|
92
|
+
raise SonnelyError(error.get("message", "Sonnely stream failed."), error.get("code", 502))
|
|
93
|
+
yield event
|
|
94
|
+
raise SonnelyError("The stream ended before Sonnely confirmed settlement.", 502)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sonnely-sdk
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Server-side Python client for the Sonnely inference API
|
|
5
|
+
Project-URL: Documentation, https://docs.sonnely.com/docs/sdks
|
|
6
|
+
Project-URL: Homepage, https://sonnely.com
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# Sonnely SDK for Python
|
|
11
|
+
|
|
12
|
+
Run text inference on Sonnely from Python 3.10 or newer. Create a funded [Sonnely API key](https://sonnely.com/console/keys) and store it server-side as `SONNELY_API_KEY`.
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
import os
|
|
16
|
+
from sonnely import Sonnely
|
|
17
|
+
|
|
18
|
+
sonnely = Sonnely(os.environ["SONNELY_API_KEY"])
|
|
19
|
+
request = {
|
|
20
|
+
"model": "z-ai/glm-5.3-flash",
|
|
21
|
+
"messages": [{"role": "user", "content": "Describe winter in two words."}],
|
|
22
|
+
"max_tokens": 128,
|
|
23
|
+
"max_cost_microusd": 500,
|
|
24
|
+
}
|
|
25
|
+
result = sonnely.complete(request, receipt=True)
|
|
26
|
+
print(result["choices"][0]["message"]["content"], result["usage"])
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Call `models()` for live routes and rates. Use `stream(request, receipt=True)` for an iterator that yields text deltas and final usage. The iterator requires the gateway's settlement marker to complete normally. Each completion or stream is billed separately.
|
|
30
|
+
|
|
31
|
+
[SDK guide](https://docs.sonnely.com/docs/sdks) · [API reference](https://docs.sonnely.com/docs/api) · [Usage](https://sonnely.com/console/usage)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sonnely
|