teepin 0.2.0__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.
- teepin-0.2.0/LICENSE +69 -0
- teepin-0.2.0/PKG-INFO +478 -0
- teepin-0.2.0/README.md +441 -0
- teepin-0.2.0/pyproject.toml +75 -0
- teepin-0.2.0/setup.cfg +4 -0
- teepin-0.2.0/setup.py +4 -0
- teepin-0.2.0/src/teepin/__init__.py +187 -0
- teepin-0.2.0/src/teepin/_core.py +344 -0
- teepin-0.2.0/src/teepin/_inference.py +284 -0
- teepin-0.2.0/src/teepin/_version.py +4 -0
- teepin-0.2.0/src/teepin/async_client.py +261 -0
- teepin-0.2.0/src/teepin/async_inference.py +275 -0
- teepin-0.2.0/src/teepin/async_storage.py +1102 -0
- teepin-0.2.0/src/teepin/client.py +265 -0
- teepin-0.2.0/src/teepin/exceptions.py +215 -0
- teepin-0.2.0/src/teepin/inference.py +289 -0
- teepin-0.2.0/src/teepin/models.py +427 -0
- teepin-0.2.0/src/teepin/py.typed +0 -0
- teepin-0.2.0/src/teepin/storage.py +1242 -0
- teepin-0.2.0/src/teepin.egg-info/PKG-INFO +478 -0
- teepin-0.2.0/src/teepin.egg-info/SOURCES.txt +29 -0
- teepin-0.2.0/src/teepin.egg-info/dependency_links.txt +1 -0
- teepin-0.2.0/src/teepin.egg-info/requires.txt +9 -0
- teepin-0.2.0/src/teepin.egg-info/top_level.txt +1 -0
- teepin-0.2.0/tests/test_config.py +55 -0
- teepin-0.2.0/tests/test_core.py +94 -0
- teepin-0.2.0/tests/test_inference_async.py +520 -0
- teepin-0.2.0/tests/test_inference_core.py +208 -0
- teepin-0.2.0/tests/test_inference_sync.py +489 -0
- teepin-0.2.0/tests/test_storage_async.py +301 -0
- teepin-0.2.0/tests/test_storage_sync.py +519 -0
teepin-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
10
|
+
|
|
11
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
12
|
+
|
|
13
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
14
|
+
|
|
15
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
16
|
+
|
|
17
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
18
|
+
|
|
19
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
20
|
+
|
|
21
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
22
|
+
|
|
23
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
24
|
+
|
|
25
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
26
|
+
|
|
27
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
28
|
+
|
|
29
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
30
|
+
|
|
31
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
32
|
+
|
|
33
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
34
|
+
|
|
35
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
36
|
+
|
|
37
|
+
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
38
|
+
|
|
39
|
+
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
40
|
+
|
|
41
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
|
42
|
+
|
|
43
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
44
|
+
|
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
46
|
+
|
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
48
|
+
|
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
50
|
+
|
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
52
|
+
|
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
54
|
+
|
|
55
|
+
END OF TERMS AND CONDITIONS
|
|
56
|
+
|
|
57
|
+
Copyright 2026 TEEPIN Project
|
|
58
|
+
|
|
59
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
60
|
+
you may not use this file except in compliance with the License.
|
|
61
|
+
You may obtain a copy of the License at
|
|
62
|
+
|
|
63
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
64
|
+
|
|
65
|
+
Unless required by applicable law or agreed to in writing, software
|
|
66
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
67
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
68
|
+
See the License for the specific language governing permissions and
|
|
69
|
+
limitations under the License.
|
teepin-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: teepin
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Python SDK for Teepin Web Services (TWS): Teepin Storage and compute
|
|
5
|
+
Author-email: TEEPIN Team <support@teepin.com>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://teepin.com
|
|
8
|
+
Project-URL: Documentation, https://github.com/FlashbackAi/teepin-python#readme
|
|
9
|
+
Project-URL: Repository, https://github.com/FlashbackAi/teepin-python
|
|
10
|
+
Project-URL: Issues, https://github.com/FlashbackAi/teepin-python/issues
|
|
11
|
+
Keywords: teepin,tws,cloud,object storage,s3,gpu,compute
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Framework :: AsyncIO
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Topic :: System :: Archiving
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: requests>=2.31.0
|
|
29
|
+
Requires-Dist: aiohttp>=3.9.0
|
|
30
|
+
Requires-Dist: pydantic>=2.0.0
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest>=7.4.0; extra == "dev"
|
|
33
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
34
|
+
Requires-Dist: mypy>=1.5.0; extra == "dev"
|
|
35
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# Teepin Python SDK
|
|
39
|
+
|
|
40
|
+
The Python SDK of Teepin Web Services (TWS): Teepin Storage, Teepin Inference,
|
|
41
|
+
and compute.
|
|
42
|
+
A synchronous client on `requests` and an asynchronous one on `aiohttp`, for
|
|
43
|
+
Python 3.9 and later.
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install teepin
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Or from source:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
git clone https://github.com/FlashbackAi/teepin-python
|
|
53
|
+
cd teepin-python
|
|
54
|
+
pip install -e .
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Quick start
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from teepin import Client
|
|
61
|
+
|
|
62
|
+
client = Client(api_key="tpk_...") # or set TEEPIN_API_KEY
|
|
63
|
+
|
|
64
|
+
client.storage.create_bucket("my-bucket")
|
|
65
|
+
obj = client.storage.upload_file("my-bucket", "videos/launch.mp4", "launch.mp4")
|
|
66
|
+
print(obj.key, obj.size, obj.etag)
|
|
67
|
+
|
|
68
|
+
client.storage.download_file("my-bucket", "videos/launch.mp4", "copy.mp4")
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`upload_file` sends a file of any size (up to 5 TiB): up to 16 MiB in one
|
|
72
|
+
request, otherwise in parts sent 8 at a time. Each part is retried on its own,
|
|
73
|
+
and the stored object is checked against the bytes sent, so a corrupted
|
|
74
|
+
transfer is never reported as success. `download_file` fetches large objects as
|
|
75
|
+
parallel ranges, all pinned to the same version of the object, and writes a
|
|
76
|
+
temporary file that is renamed into place only when the download is complete
|
|
77
|
+
and verified.
|
|
78
|
+
|
|
79
|
+
The asynchronous client has the same methods:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
import asyncio
|
|
83
|
+
from teepin import AsyncClient
|
|
84
|
+
|
|
85
|
+
async def main():
|
|
86
|
+
async with AsyncClient() as client:
|
|
87
|
+
objs = await asyncio.gather(
|
|
88
|
+
*(client.storage.upload_file("my-bucket", f"logs/{n}", n) for n in ["a.log", "b.log"])
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
asyncio.run(main())
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Configuration
|
|
95
|
+
|
|
96
|
+
| Argument | Environment | Default |
|
|
97
|
+
| --- | --- | --- |
|
|
98
|
+
| `api_key` | `TEEPIN_API_KEY` | none |
|
|
99
|
+
| `base_url` | `TEEPIN_BASE_URL` | `https://api.teepin.com` |
|
|
100
|
+
| `timeout` | | 60 s for each API call that is not a transfer |
|
|
101
|
+
| `transfer_timeout` | | 300 s without a byte before a transfer is cut off |
|
|
102
|
+
| `max_retries` | | 4 |
|
|
103
|
+
| `inference_max_retries` | | 2, for Teepin Inference (`models`, `chat`) |
|
|
104
|
+
| `create(..., timeout=)` | | 600 s for a chat answer (a stream: its headers) |
|
|
105
|
+
| `project_id` | | when `api_key` is a signed-in session's token rather than a project key |
|
|
106
|
+
|
|
107
|
+
`Client(base_url, api_key, timeout)` keeps the argument order of version 0.1.
|
|
108
|
+
|
|
109
|
+
### Retries
|
|
110
|
+
|
|
111
|
+
A request that fails for a passing reason is sent again after a wait with
|
|
112
|
+
full jitter, or the server's `Retry-After`. It is always retried on `429`,
|
|
113
|
+
`502` and `503`. It is retried on `500`, `504` or a broken connection only
|
|
114
|
+
when sending it twice is harmless (reads, deletes, uploads that replace).
|
|
115
|
+
Requests that cannot be repeated (start an upload, rename, complete) are
|
|
116
|
+
retried only when they never reached the server. Errors record the number of
|
|
117
|
+
tries in `err.attempts`.
|
|
118
|
+
|
|
119
|
+
Teepin Inference has its own rules. The server bills a chat completion once it
|
|
120
|
+
has the model's answer, even if the client has gone, so a chat request is sent
|
|
121
|
+
again only when it certainly was not billed:
|
|
122
|
+
|
|
123
|
+
- retried: a `429`; Teepin's own `500 internal_error`, `502 upstream_error`,
|
|
124
|
+
`503 model_unavailable` or `503 billing_unavailable` (told by the OpenAI-style
|
|
125
|
+
error body); a connection that was never opened (refused, or DNS failed);
|
|
126
|
+
- never retried: a timeout, or a connection reset or cut after the request was
|
|
127
|
+
sent; a `504`; a `5xx` whose body is not Teepin's (a load balancer's HTML
|
|
128
|
+
page); `400`, `401`, `402`, `403`, `404`, `413`; a stream once it has started.
|
|
129
|
+
|
|
130
|
+
Reads (`models.list`, `models.attestation`) are retried on any `429`, `5xx` or
|
|
131
|
+
connection error. Retries go up to `inference_max_retries` times (2; 0 turns
|
|
132
|
+
them off), waiting from 0.5 s up to 8 s with full jitter, or the server's
|
|
133
|
+
`Retry-After` up to 30 s.
|
|
134
|
+
|
|
135
|
+
A chat request waits up to 600 s for its answer, whatever the client's
|
|
136
|
+
`timeout`; `create(..., timeout=120)` changes it for one call. For a stream it
|
|
137
|
+
is the wait for the headers; after them a stream is cut off only by
|
|
138
|
+
`transfer_timeout` seconds without a byte.
|
|
139
|
+
|
|
140
|
+
When an answer is lost after the server acted, the SDK recovers rather than
|
|
141
|
+
failing:
|
|
142
|
+
|
|
143
|
+
- A bucket that turns out to exist on a retry is fetched.
|
|
144
|
+
- A delete or cancel whose object is gone on a retry succeeds.
|
|
145
|
+
- A rename whose source is gone on a retry returns the destination.
|
|
146
|
+
- A completed upload whose answer was lost is confirmed by comparing the
|
|
147
|
+
object's ETag with the one computed locally.
|
|
148
|
+
|
|
149
|
+
## Storage
|
|
150
|
+
|
|
151
|
+
Every route of the storage API is one method; the list is under
|
|
152
|
+
[Reference](#reference).
|
|
153
|
+
|
|
154
|
+
### Upload
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
s = client.storage
|
|
158
|
+
|
|
159
|
+
# bytes, or a binary file from where it stands
|
|
160
|
+
s.upload("b", "data.bin", b"...", content_type="application/octet-stream", metadata={"owner": "ana"})
|
|
161
|
+
|
|
162
|
+
# progress, part size, parallelism
|
|
163
|
+
s.upload_file("b", "big.iso", "big.iso", part_size=64 << 20, concurrency=16,
|
|
164
|
+
progress=lambda done, total: print(f"{done}/{total}"))
|
|
165
|
+
|
|
166
|
+
# resume: continue the newest unfinished upload of the same key
|
|
167
|
+
from teepin import UploadError
|
|
168
|
+
try:
|
|
169
|
+
s.upload_file("b", "big.iso", "big.iso", resume=True)
|
|
170
|
+
except UploadError as e:
|
|
171
|
+
print("left open to resume:", e.upload_id, e.__cause__)
|
|
172
|
+
|
|
173
|
+
# a stream of unknown size (a pipe, a socket; or an async iterable with AsyncClient)
|
|
174
|
+
import sys
|
|
175
|
+
s.upload_stream("b", "backup.tar", sys.stdin.buffer)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
With `resume=True`, parts the server already holds are skipped when their size
|
|
179
|
+
and MD5 match the file. If the upload fails, it is left open for 7 days to be
|
|
180
|
+
resumed instead of being cancelled.
|
|
181
|
+
|
|
182
|
+
### Download
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
s.download_file("b", "big.iso", "/tmp/big.iso") # parallel ranges, temp file + rename
|
|
186
|
+
data = s.download_bytes("b", "small.json") # into memory
|
|
187
|
+
with open("out.log", "wb") as f:
|
|
188
|
+
s.download_stream("b", "app.log", f) # in order, to any writable
|
|
189
|
+
|
|
190
|
+
with s.get_object("b", "app.log", range=(0, 1023)) as r: # one request, streamed
|
|
191
|
+
head = r.read()
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
A range that breaks off is continued from where it stopped. If the object is
|
|
195
|
+
replaced mid-download, `ObjectChangedError` is raised. An object stored in
|
|
196
|
+
one request is checked against its SHA-256 (`ChecksumMismatchError`).
|
|
197
|
+
|
|
198
|
+
### Listing
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
page = s.list_objects("b", prefix="photos/", delimiter="/", limit=100)
|
|
202
|
+
page.objects, page.prefixes, page.next_cursor # pass next_cursor as cursor= for the next page
|
|
203
|
+
|
|
204
|
+
for obj in s.iter_objects("b", "photos/"): # every object, pages fetched as needed
|
|
205
|
+
print(obj.key, obj.size)
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### ETags
|
|
209
|
+
|
|
210
|
+
The ETag of an object is its SHA-256; for an upload in parts it is the SHA-256
|
|
211
|
+
of the parts' SHA-256s with `-<parts>`. ETags are unquoted. To tell whether
|
|
212
|
+
a local file matches an object:
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
from teepin import compute_etag, part_size_for
|
|
216
|
+
|
|
217
|
+
obj = s.stat_object("b", "big.iso")
|
|
218
|
+
if obj.part_count:
|
|
219
|
+
# uploaded by upload() with the default part size
|
|
220
|
+
local = compute_etag("big.iso", part_size_for(obj.size), obj.part_count)
|
|
221
|
+
else:
|
|
222
|
+
local = compute_etag("big.iso")
|
|
223
|
+
same = local == obj.etag
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
For an object in parts, pass the part size it was uploaded with.
|
|
227
|
+
`part_size_for(size, want)` gives the size `upload` picks.
|
|
228
|
+
|
|
229
|
+
### Signed links
|
|
230
|
+
|
|
231
|
+
Every method sends your API key, so call them from a backend you control.
|
|
232
|
+
To let a browser download or upload one object, sign a link and hand it only
|
|
233
|
+
that:
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
link = s.presign_download("b", "report.pdf", ttl_seconds=900, attachment=True) # up to 24 h
|
|
237
|
+
up = s.presign_upload("b", "avatars/ana.png", ttl_seconds=600, max_bytes=5 << 20, content_type="image/png")
|
|
238
|
+
# the browser sends: PUT up.url with up.headers, once
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
`get_signed`, `head_signed` and `put_signed` use such links the way a browser
|
|
242
|
+
would, without the API key (handy for tests).
|
|
243
|
+
|
|
244
|
+
### Errors
|
|
245
|
+
|
|
246
|
+
Every error is a `teepin.TeepinError`. API answers raise the subclass for
|
|
247
|
+
their status and code. Each carries `status_code`, `code` (for example
|
|
248
|
+
`object_not_found`), `message` and `request_id` (quote it to support).
|
|
249
|
+
|
|
250
|
+
| Error | When |
|
|
251
|
+
| --- | --- |
|
|
252
|
+
| `NotFoundError` | 404: bucket, object, upload |
|
|
253
|
+
| `ConflictError` | 409: `bucket_exists`, `bucket_not_empty`, `object_exists` |
|
|
254
|
+
| `PreconditionFailedError` | 412: `if_match` / `if_none_match` |
|
|
255
|
+
| `TooLargeError` | 413, or `object_too_large` |
|
|
256
|
+
| `NoCreditError` | 402 `insufficient_credit` |
|
|
257
|
+
| `OnHoldError` | 402 `storage_on_hold`; `delete_after` says when held data goes |
|
|
258
|
+
| `UnauthorizedError`, `ForbiddenError` | 401, 403 (e.g. a key without `storage:write`) |
|
|
259
|
+
| `APIConnectionError` | no answer: the network, a timeout |
|
|
260
|
+
| `ObjectChangedError` | the object was replaced during a download |
|
|
261
|
+
| `ChecksumMismatchError` | the bytes do not match the object's checksum |
|
|
262
|
+
| `UploadError` | an upload in parts failed; `upload_id`, cause in `__cause__` |
|
|
263
|
+
|
|
264
|
+
### S3 tools
|
|
265
|
+
|
|
266
|
+
Teepin Storage also has an S3-compatible endpoint, so code and tools written
|
|
267
|
+
for S3 work with an S3 access key (created in the console; an API key does
|
|
268
|
+
not work there):
|
|
269
|
+
|
|
270
|
+
```python
|
|
271
|
+
import boto3
|
|
272
|
+
from botocore.config import Config
|
|
273
|
+
|
|
274
|
+
s3 = boto3.client("s3", endpoint_url="https://s3.teepin.com", region_name="teepin",
|
|
275
|
+
aws_access_key_id="...", aws_secret_access_key="...",
|
|
276
|
+
config=Config(s3={"addressing_style": "path"}))
|
|
277
|
+
s3.upload_file("big.bin", "my-bucket", "big.bin")
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Tested with boto3 1.36; see `examples/s3_boto3.py`.
|
|
281
|
+
|
|
282
|
+
## Inference
|
|
283
|
+
|
|
284
|
+
One OpenAI-compatible API for every model; the model is chosen per request.
|
|
285
|
+
The API key needs the `inference:invoke` permission (and may be limited to
|
|
286
|
+
some models).
|
|
287
|
+
|
|
288
|
+
```python
|
|
289
|
+
from teepin import Client
|
|
290
|
+
|
|
291
|
+
client = Client(api_key="tpk_...")
|
|
292
|
+
|
|
293
|
+
for m in client.models.list(): # the models this key may call
|
|
294
|
+
print(m.id, m.display_name, m.pricing.input_per_million_tokens, m.confidential)
|
|
295
|
+
if m.attestation: # a confidential model not fully verified right now
|
|
296
|
+
print(" ", m.attestation.level, m.attestation.warning)
|
|
297
|
+
|
|
298
|
+
reply = client.chat.completions.create(
|
|
299
|
+
model="teepin/llama-3.3-70b",
|
|
300
|
+
messages=[{"role": "user", "content": "What is a GPU?"}],
|
|
301
|
+
max_tokens=200,
|
|
302
|
+
)
|
|
303
|
+
print(reply.choices[0].message.content)
|
|
304
|
+
print(reply.usage.total_tokens, reply.request_id, reply.attestation, reply.rate_limit)
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
Only the arguments you give are sent: `max_tokens` or `max_completion_tokens`,
|
|
308
|
+
`temperature`, `top_p`, `top_k`, `min_p`, `stop`, `seed`, `n`,
|
|
309
|
+
`frequency_penalty`, `presence_penalty`, `repetition_penalty`, `logit_bias`,
|
|
310
|
+
`logprobs`, `top_logprobs`, `tools`, `tool_choice`, `response_format`,
|
|
311
|
+
`stream_options`, `user` and `chat_template_kwargs` (for example
|
|
312
|
+
`{"enable_thinking": False}`). `extra_body={...}` sends fields this SDK does
|
|
313
|
+
not know yet. Messages are plain dicts, and content may be OpenAI content parts
|
|
314
|
+
(`{"type": "image_url", "image_url": {"url": ...}}`).
|
|
315
|
+
|
|
316
|
+
Every result keeps the JSON it was read from in `.raw`, so a field the SDK does
|
|
317
|
+
not know yet is still there (`reply.raw["new_field"]`).
|
|
318
|
+
|
|
319
|
+
### Streaming
|
|
320
|
+
|
|
321
|
+
```python
|
|
322
|
+
with client.chat.completions.create(
|
|
323
|
+
model="teepin/llama-3.3-70b",
|
|
324
|
+
messages=[{"role": "user", "content": "Write a haiku."}],
|
|
325
|
+
stream=True,
|
|
326
|
+
stream_options={"include_usage": True}, # usage on the last chunk
|
|
327
|
+
) as stream:
|
|
328
|
+
print(stream.request_id, stream.attestation) # known as soon as the headers arrive
|
|
329
|
+
for chunk in stream:
|
|
330
|
+
if chunk.choices:
|
|
331
|
+
print(chunk.choices[0].delta.content or "", end="")
|
|
332
|
+
print(stream.usage)
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
The stream ends at `[DONE]`. If the model fails mid-stream, or the stream ends
|
|
336
|
+
without `[DONE]`, iterating raises `StreamError` (`code` is the server's, for
|
|
337
|
+
example `stream_error`, or `stream_ended_early`). Leaving the `with` early
|
|
338
|
+
closes the connection.
|
|
339
|
+
|
|
340
|
+
### Attestation
|
|
341
|
+
|
|
342
|
+
A confidential model is served in a hardware-attested enclave. Each answer says
|
|
343
|
+
how far the enclave was verified in `attestation` (the `X-Teepin-Attestation`
|
|
344
|
+
header): `verified`, `verified-older-release` or `not-verified`, which is a
|
|
345
|
+
warning, never an error. The proof itself, to check yourself:
|
|
346
|
+
|
|
347
|
+
```python
|
|
348
|
+
proof = client.models.attestation("teepin/confidential-llama") # a dict, as the provider gives it
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
It raises `NotFoundError` with `code` `attestation_not_supported` for a model
|
|
352
|
+
that is not confidential, and `model_not_found` for one that does not exist or
|
|
353
|
+
the key may not call.
|
|
354
|
+
|
|
355
|
+
### Async
|
|
356
|
+
|
|
357
|
+
```python
|
|
358
|
+
import asyncio
|
|
359
|
+
from teepin import AsyncClient
|
|
360
|
+
|
|
361
|
+
async def main():
|
|
362
|
+
async with AsyncClient() as client:
|
|
363
|
+
models = await client.models.list()
|
|
364
|
+
reply = await client.chat.completions.create(
|
|
365
|
+
model=models[0].id, messages=[{"role": "user", "content": "Hello"}]
|
|
366
|
+
)
|
|
367
|
+
print(reply.choices[0].message.content)
|
|
368
|
+
|
|
369
|
+
stream = await client.chat.completions.create(
|
|
370
|
+
model=models[0].id, messages=[{"role": "user", "content": "Hello"}], stream=True
|
|
371
|
+
)
|
|
372
|
+
async with stream:
|
|
373
|
+
async for chunk in stream:
|
|
374
|
+
print(chunk.choices[0].delta.content or "", end="")
|
|
375
|
+
|
|
376
|
+
asyncio.run(main())
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
### Inference errors
|
|
380
|
+
|
|
381
|
+
Errors carry `status`, `code`, `type`, `message`, `request_id` and
|
|
382
|
+
`retry_after` (seconds, when the server sent `Retry-After`).
|
|
383
|
+
|
|
384
|
+
| Error | When |
|
|
385
|
+
| --- | --- |
|
|
386
|
+
| `BadRequestError` | 400 `invalid_request` |
|
|
387
|
+
| `UnauthorizedError` | 401 `unauthorized` |
|
|
388
|
+
| `NoCreditError` (also `InsufficientCreditError`) | 402 `insufficient_credit`: add credit in the console |
|
|
389
|
+
| `ForbiddenError` (also `PermissionDeniedError`) | 403 `missing_scope` (no `inference:invoke`), `forbidden` |
|
|
390
|
+
| `NotFoundError` | 404 `model_not_found`, `attestation_not_supported` |
|
|
391
|
+
| `TooLargeError` | 413 `body_too_large` |
|
|
392
|
+
| `RateLimitError` | 429 `rate_limit_exceeded`, `too_many_concurrent_requests`, `rate_limited` |
|
|
393
|
+
| `APIError` | 500 `internal_error`, 502 `upstream_error`, 503 `model_unavailable`, `billing_unavailable` |
|
|
394
|
+
| `APIConnectionError` | no answer: the network, a timeout (a chat request is not retried after a timeout) |
|
|
395
|
+
| `StreamError` | a stream that failed after it started |
|
|
396
|
+
|
|
397
|
+
## Compute
|
|
398
|
+
|
|
399
|
+
```python
|
|
400
|
+
from teepin import CreateInstanceRequest, PortMapping
|
|
401
|
+
|
|
402
|
+
instance = client.instances.create(CreateInstanceRequest(
|
|
403
|
+
name="pytorch-training", image="pytorch/pytorch:latest", gpu_vram="25GB",
|
|
404
|
+
ports=[PortMapping(container=8080, public=80)],
|
|
405
|
+
))
|
|
406
|
+
client.instances.list()
|
|
407
|
+
client.instances.get(instance.id)
|
|
408
|
+
client.instances.metrics(instance.id)
|
|
409
|
+
client.instances.list_types()
|
|
410
|
+
client.instances.delete(instance.id)
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
## Reference
|
|
414
|
+
|
|
415
|
+
`client.storage` (on `AsyncClient`, the same methods, awaited):
|
|
416
|
+
|
|
417
|
+
| Method | Route |
|
|
418
|
+
| --- | --- |
|
|
419
|
+
| `get_hold()` | `GET /v1/storage/hold` |
|
|
420
|
+
| `create_bucket(name)` | `POST /v1/storage/buckets` |
|
|
421
|
+
| `list_buckets()` | `GET /v1/storage/buckets` |
|
|
422
|
+
| `get_bucket(name)` | `GET /v1/storage/buckets/{bucket}` |
|
|
423
|
+
| `delete_bucket(name)` | `DELETE /v1/storage/buckets/{bucket}` |
|
|
424
|
+
| `list_objects(bucket, prefix, delimiter, cursor, limit)`, `iter_objects(bucket, prefix)` | `GET .../objects` |
|
|
425
|
+
| `delete_objects(bucket, keys)` | `POST .../objects/delete` |
|
|
426
|
+
| `put_object(bucket, key, body, content_type, metadata, if_match, if_none_match)` | `PUT .../object` |
|
|
427
|
+
| `stat_object(bucket, key)` | `GET .../object` |
|
|
428
|
+
| `delete_object(bucket, key)` | `DELETE .../object` |
|
|
429
|
+
| `get_object(bucket, key, range, if_none_match, if_match)` | `GET .../object/content` |
|
|
430
|
+
| `head_object(bucket, key)` | `HEAD .../object/content` |
|
|
431
|
+
| `rename_object(bucket, from_key, to_key, overwrite)` | `POST .../object/rename` |
|
|
432
|
+
| `copy_object(bucket, key, source_bucket, source_key, if_match, if_none_match)` | `POST .../object/copy` |
|
|
433
|
+
| `presign_download(bucket, key, ttl_seconds, attachment)` | `POST .../object/download-url` |
|
|
434
|
+
| `presign_upload(bucket, key, ttl_seconds, max_bytes, content_type)` | `POST .../object/upload-url` |
|
|
435
|
+
| `create_upload(bucket, key, content_type, metadata)` | `POST .../uploads` |
|
|
436
|
+
| `list_uploads(bucket, prefix)` | `GET .../uploads` |
|
|
437
|
+
| `abort_upload(bucket, upload_id)` | `DELETE .../uploads/{id}` |
|
|
438
|
+
| `list_parts(bucket, upload_id)` | `GET .../uploads/{id}/parts` |
|
|
439
|
+
| `upload_part(bucket, upload_id, number, data)` | `PUT .../uploads/{id}/parts/{n}` |
|
|
440
|
+
| `complete_upload(bucket, upload_id, parts)` | `POST .../uploads/{id}/complete` |
|
|
441
|
+
| `get_signed(url, range)`, `head_signed(url)` | `GET`, `HEAD /v1/storage/d/{token}` |
|
|
442
|
+
| `put_signed(upload, body)` | `PUT /v1/storage/u/{token}` |
|
|
443
|
+
|
|
444
|
+
`client.models` and `client.chat.completions` (on `AsyncClient`, awaited):
|
|
445
|
+
|
|
446
|
+
| Method | Route |
|
|
447
|
+
| --- | --- |
|
|
448
|
+
| `models.list()` | `GET /v1/models` |
|
|
449
|
+
| `models.attestation(model_id)` | `GET /v1/models/attestation?model=<id>` |
|
|
450
|
+
| `chat.completions.create(model, messages, stream, ...)` | `POST /v1/chat/completions` |
|
|
451
|
+
|
|
452
|
+
High level: `upload`, `upload_file`, `upload_stream`, `download`,
|
|
453
|
+
`download_file`, `download_bytes`, `download_stream`. Helpers:
|
|
454
|
+
`teepin.compute_etag`, `teepin.part_size_for`.
|
|
455
|
+
|
|
456
|
+
## Development
|
|
457
|
+
|
|
458
|
+
```bash
|
|
459
|
+
pip install -e ".[dev]"
|
|
460
|
+
pytest # against an in-process fake of the API, with injected faults
|
|
461
|
+
black src tests
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
`tests/e2e_drill.py` runs the SDK against the real storage API; teepin-core's
|
|
465
|
+
`TestStorageAPI_PythonSDK` drill starts it (see `pkg/api/storage_python_sdk_drill_test.go`).
|
|
466
|
+
|
|
467
|
+
## Examples
|
|
468
|
+
|
|
469
|
+
- `examples/storage_sync.py`: buckets, a large upload with resume, a parallel download, listing, a signed link
|
|
470
|
+
- `examples/storage_async.py`: several uploads at once, an async stream, reading back
|
|
471
|
+
- `examples/s3_boto3.py`: boto3 against `s3.teepin.com`
|
|
472
|
+
- `examples/inference_sync.py`: models, a chat answer, a stream, an attestation proof
|
|
473
|
+
- `examples/inference_async.py`: several chat requests at once, an async stream
|
|
474
|
+
- `examples/basic_sync.py`, `examples/basic_async.py`: compute instances
|
|
475
|
+
|
|
476
|
+
## License
|
|
477
|
+
|
|
478
|
+
Apache License 2.0
|