uplinkswitch-sdk 0.1.0__py3-none-any.whl
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.
- uplinkswitch_sdk/__init__.py +126 -0
- uplinkswitch_sdk/models.py +1162 -0
- uplinkswitch_sdk/py.typed +0 -0
- uplinkswitch_sdk-0.1.0.dist-info/METADATA +29 -0
- uplinkswitch_sdk-0.1.0.dist-info/RECORD +7 -0
- uplinkswitch_sdk-0.1.0.dist-info/WHEEL +4 -0
- uplinkswitch_sdk-0.1.0.dist-info/licenses/LICENSE +9 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""UplinkSwitch typed Python SDK (standard library only)."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import random
|
|
7
|
+
import time
|
|
8
|
+
import urllib.error
|
|
9
|
+
import urllib.request
|
|
10
|
+
from typing import Any, Dict, Iterator, List, Optional, Union
|
|
11
|
+
|
|
12
|
+
from .models import MODELS, ModelId, ModelInfo
|
|
13
|
+
|
|
14
|
+
__all__ = ["UplinkSwitch", "APIError", "MODELS", "ModelId", "ModelInfo", "estimate_cost"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class APIError(Exception):
|
|
18
|
+
def __init__(self, status: int, body: str) -> None:
|
|
19
|
+
super().__init__("API error %d: %s" % (status, body[:300]))
|
|
20
|
+
self.status = status
|
|
21
|
+
self.body = body
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _Completions:
|
|
25
|
+
def __init__(self, client: "UplinkSwitch") -> None:
|
|
26
|
+
self._c = client
|
|
27
|
+
|
|
28
|
+
def create(self, *, model: ModelId, messages: List[Dict[str, Any]], stream: bool = False, **kwargs: Any) -> Union[Dict[str, Any], Iterator[Dict[str, Any]]]:
|
|
29
|
+
c = self._c
|
|
30
|
+
last: Optional[BaseException] = None
|
|
31
|
+
for m in [model, *c.fallback_models]:
|
|
32
|
+
try:
|
|
33
|
+
resp = c._send("/chat/completions", "POST", dict(kwargs, model=m, messages=messages, stream=stream))
|
|
34
|
+
if stream:
|
|
35
|
+
return _parse_sse(resp)
|
|
36
|
+
with resp:
|
|
37
|
+
data = json.loads(resp.read().decode("utf-8"))
|
|
38
|
+
charge = resp.headers.get("x-uplink-charge")
|
|
39
|
+
try:
|
|
40
|
+
charge_usd: Optional[float] = float(charge) if charge is not None else None
|
|
41
|
+
except ValueError:
|
|
42
|
+
charge_usd = None
|
|
43
|
+
data["_uplink"] = {"charge_usd": charge_usd, "request_id": resp.headers.get("x-request-id")}
|
|
44
|
+
return data
|
|
45
|
+
except APIError as e:
|
|
46
|
+
last = e
|
|
47
|
+
if e.status in (404, 503):
|
|
48
|
+
continue
|
|
49
|
+
raise
|
|
50
|
+
assert last is not None
|
|
51
|
+
raise last
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class _Chat:
|
|
55
|
+
def __init__(self, client: "UplinkSwitch") -> None:
|
|
56
|
+
self.completions = _Completions(client)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class _Models:
|
|
60
|
+
def __init__(self, client: "UplinkSwitch") -> None:
|
|
61
|
+
self._c = client
|
|
62
|
+
|
|
63
|
+
def list(self) -> Dict[str, Any]:
|
|
64
|
+
with self._c._send("/models", "GET") as r:
|
|
65
|
+
return json.loads(r.read().decode("utf-8"))
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class UplinkSwitch:
|
|
69
|
+
def __init__(self, api_key: Optional[str] = None, base_url: Optional[str] = None, max_retries: int = 2, fallback_models: Optional[List[ModelId]] = None) -> None:
|
|
70
|
+
self.api_key = api_key if api_key is not None else os.environ.get("UPLINKSWITCH_API_KEY")
|
|
71
|
+
self.base_url = (base_url or "https://api.uplinkswitch.com/v1").rstrip("/")
|
|
72
|
+
self.max_retries = max_retries
|
|
73
|
+
self.fallback_models: List[str] = list(fallback_models or [])
|
|
74
|
+
self.chat = _Chat(self)
|
|
75
|
+
|
|
76
|
+
def models(self) -> _Models:
|
|
77
|
+
return _Models(self)
|
|
78
|
+
|
|
79
|
+
def _send(self, path: str, method: str, body: Optional[Dict[str, Any]] = None) -> Any:
|
|
80
|
+
headers = {"content-type": "application/json"}
|
|
81
|
+
if self.api_key:
|
|
82
|
+
headers["authorization"] = "Bearer " + self.api_key
|
|
83
|
+
data = json.dumps(body).encode("utf-8") if body is not None else None
|
|
84
|
+
last: Optional[BaseException] = None
|
|
85
|
+
for attempt in range(self.max_retries + 1):
|
|
86
|
+
if attempt:
|
|
87
|
+
time.sleep(0.25 * 2 ** (attempt - 1) + random.random() * 0.1)
|
|
88
|
+
req = urllib.request.Request(self.base_url + path, data=data, headers=headers, method=method)
|
|
89
|
+
try:
|
|
90
|
+
return urllib.request.urlopen(req, timeout=120)
|
|
91
|
+
except urllib.error.HTTPError as e:
|
|
92
|
+
err = APIError(e.code, e.read().decode("utf-8", "replace"))
|
|
93
|
+
if e.code == 429 or e.code >= 500:
|
|
94
|
+
last = err
|
|
95
|
+
continue
|
|
96
|
+
raise err
|
|
97
|
+
except (urllib.error.URLError, OSError) as e:
|
|
98
|
+
last = e
|
|
99
|
+
assert last is not None
|
|
100
|
+
raise last
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _parse_sse(resp: Any) -> Iterator[Dict[str, Any]]:
|
|
104
|
+
with resp:
|
|
105
|
+
for raw in resp:
|
|
106
|
+
line = raw.decode("utf-8").strip()
|
|
107
|
+
if line.startswith("data:"):
|
|
108
|
+
d = line[5:].strip()
|
|
109
|
+
if d and d != "[DONE]":
|
|
110
|
+
yield json.loads(d)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def estimate_cost(response: Dict[str, Any]) -> Optional[float]:
|
|
114
|
+
"""Estimated USD cost from catalog prices; None when the model is unknown."""
|
|
115
|
+
m = MODELS.get(response.get("model", ""))
|
|
116
|
+
u = response.get("usage")
|
|
117
|
+
if m is None or not u or m.input is None or m.output is None:
|
|
118
|
+
return None
|
|
119
|
+
prompt = u.get("prompt_tokens", 0)
|
|
120
|
+
details = u.get("prompt_tokens_details") or {}
|
|
121
|
+
if "cached_tokens" in details:
|
|
122
|
+
cached = details["cached_tokens"]
|
|
123
|
+
pc = (prompt - cached) * m.input + cached * (m.cache_read if m.cache_read is not None else m.input)
|
|
124
|
+
else:
|
|
125
|
+
pc = prompt * m.input
|
|
126
|
+
return (pc + u.get("completion_tokens", 0) * m.output) / 1e6
|