pi169 0.1__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.
- pi169-0.1/PKG-INFO +763 -0
- pi169-0.1/README.md +724 -0
- pi169-0.1/pyproject.toml +3 -0
- pi169-0.1/setup.cfg +4 -0
- pi169-0.1/setup.py +56 -0
- pi169-0.1/src/Pi169.egg-info/PKG-INFO +763 -0
- pi169-0.1/src/Pi169.egg-info/SOURCES.txt +21 -0
- pi169-0.1/src/Pi169.egg-info/dependency_links.txt +1 -0
- pi169-0.1/src/Pi169.egg-info/entry_points.txt +2 -0
- pi169-0.1/src/Pi169.egg-info/requires.txt +12 -0
- pi169-0.1/src/Pi169.egg-info/top_level.txt +1 -0
- pi169-0.1/src/pi169/__init__.py +63 -0
- pi169-0.1/src/pi169/alpie_types.py +139 -0
- pi169-0.1/src/pi169/async_client.py +377 -0
- pi169-0.1/src/pi169/cli.py +36 -0
- pi169-0.1/src/pi169/client.py +366 -0
- pi169-0.1/src/pi169/exceptions.py +79 -0
pi169-0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,763 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pi169
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: Production-ready Python SDK for Alpie-Core, a 4-bit quantized 32B reasoning model achieving state-of-the-art efficiency and performance
|
|
5
|
+
Home-page: https://github.com/169Pi/Pi169-SDK
|
|
6
|
+
Author: 169PI
|
|
7
|
+
Author-email: support@169pi.com
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
Requires-Dist: certifi>=2025.11.12
|
|
18
|
+
Requires-Dist: httpx>=0.28.1
|
|
19
|
+
Requires-Dist: httpcore>=1.0.9
|
|
20
|
+
Requires-Dist: h11>=0.16.0
|
|
21
|
+
Requires-Dist: idna>=3.11
|
|
22
|
+
Requires-Dist: sniffio>=1.3.1
|
|
23
|
+
Requires-Dist: packaging>=25.0
|
|
24
|
+
Requires-Dist: python-dotenv
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=9.0.1; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-mock>=3.15.1; extra == "dev"
|
|
28
|
+
Dynamic: author
|
|
29
|
+
Dynamic: author-email
|
|
30
|
+
Dynamic: classifier
|
|
31
|
+
Dynamic: description
|
|
32
|
+
Dynamic: description-content-type
|
|
33
|
+
Dynamic: home-page
|
|
34
|
+
Dynamic: license
|
|
35
|
+
Dynamic: provides-extra
|
|
36
|
+
Dynamic: requires-dist
|
|
37
|
+
Dynamic: requires-python
|
|
38
|
+
Dynamic: summary
|
|
39
|
+
|
|
40
|
+
# Pi169 Python SDK
|
|
41
|
+
|
|
42
|
+
Pi169 is the official Python SDK and CLI for Alpie-Core, a fine-tuned 32B reasoning model running in native 4-bit precision. It provides a production-ready interface for synchronous, asynchronous, and streaming inference, designed for real-world workloads.
|
|
43
|
+
|
|
44
|
+
## Quick Start
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# Install the SDK
|
|
48
|
+
pip install pi169
|
|
49
|
+
|
|
50
|
+
# Set your API key
|
|
51
|
+
export ALPIE_API_KEY="your_key_here"
|
|
52
|
+
|
|
53
|
+
# Start using the CLI
|
|
54
|
+
pi169 "Explain 4-bit quantization in simple terms"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
That's it! You're ready to interact with Alpie-Core from your terminal.
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install pi169
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Python 3.10+ required
|
|
66
|
+
|
|
67
|
+
## Authentication
|
|
68
|
+
|
|
69
|
+
Pi169 uses Bearer Token authentication.
|
|
70
|
+
|
|
71
|
+
**Synchronous Client:**
|
|
72
|
+
```python
|
|
73
|
+
from pi169 import Pi169Client
|
|
74
|
+
|
|
75
|
+
client = Pi169Client(api_key="YOUR_API_KEY")
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Asynchronous Client:**
|
|
79
|
+
```python
|
|
80
|
+
from pi169.async_client import AsyncPi169Client
|
|
81
|
+
|
|
82
|
+
client = AsyncPi169Client(api_key="YOUR_API_KEY")
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Every request automatically sends ([Create API Key](https://playground.169pi.ai/dashboard/api-keys)):
|
|
86
|
+
```
|
|
87
|
+
Authorization: Bearer <API_KEY>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Base URL & Client Configuration
|
|
91
|
+
|
|
92
|
+
**Synchronous Client:**
|
|
93
|
+
```python
|
|
94
|
+
from pi169 import Pi169Client
|
|
95
|
+
|
|
96
|
+
client = Pi169Client(
|
|
97
|
+
api_key="YOUR_API_KEY",
|
|
98
|
+
base_url="https://api.169pi.com/v1",
|
|
99
|
+
timeout=60.0,
|
|
100
|
+
max_retries=2,
|
|
101
|
+
)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**Asynchronous Client:**
|
|
105
|
+
```python
|
|
106
|
+
from pi169.async_client import AsyncPi169Client
|
|
107
|
+
|
|
108
|
+
client = AsyncPi169Client(
|
|
109
|
+
api_key="YOUR_API_KEY",
|
|
110
|
+
base_url="https://api.169pi.com/v1",
|
|
111
|
+
timeout=60.0,
|
|
112
|
+
max_retries=2,
|
|
113
|
+
)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
- `base_url` → API root
|
|
117
|
+
- `timeout` → max wait time
|
|
118
|
+
- `max_retries` → safe retry logic for network issues
|
|
119
|
+
|
|
120
|
+
## Features
|
|
121
|
+
|
|
122
|
+
- **CLI Integration** for quick command-line interactions
|
|
123
|
+
- Streaming & Non-Streaming Chat Completions
|
|
124
|
+
- **Async/Await Support** for high-performance concurrent requests
|
|
125
|
+
- Clean, type-safe Python Interface (dataclasses, type hints)
|
|
126
|
+
- Robust Error Handling with typed exceptions
|
|
127
|
+
- Production-Ready Networking (retries, timeouts, httpx)
|
|
128
|
+
- Fully Tested with pytest
|
|
129
|
+
- Optimized for Reasoning Models
|
|
130
|
+
|
|
131
|
+
## CLI Integration
|
|
132
|
+
|
|
133
|
+
The Pi169 SDK includes a powerful command-line interface for quick interactions with the Alpie model directly from your terminal.
|
|
134
|
+
|
|
135
|
+
### Setting Up Your API Key
|
|
136
|
+
|
|
137
|
+
Before using the CLI, set your API key as an environment variable:
|
|
138
|
+
|
|
139
|
+
**macOS/Linux:**
|
|
140
|
+
```bash
|
|
141
|
+
export ALPIE_API_KEY="your_api_key_here"
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
**Windows Command Prompt:**
|
|
145
|
+
```cmd
|
|
146
|
+
set ALPIE_API_KEY=your_api_key_here
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**Windows PowerShell:**
|
|
150
|
+
```powershell
|
|
151
|
+
$env:ALPIE_API_KEY = "your_api_key_here"
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### CLI Usage
|
|
155
|
+
|
|
156
|
+
**Non-Streaming Mode (default):**
|
|
157
|
+
```bash
|
|
158
|
+
pi169 "What is the capital of France?"
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**Streaming Mode:**
|
|
162
|
+
```bash
|
|
163
|
+
pi169 "What is the capital of France?" --stream
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
The CLI automatically uses the `ALPIE_API_KEY` environment variable for authentication, making it easy to interact with the API without writing any code.
|
|
167
|
+
|
|
168
|
+
### CLI Options
|
|
169
|
+
|
|
170
|
+
- `--stream` → Enable streaming mode for real-time token-by-token responses
|
|
171
|
+
|
|
172
|
+
## Quickstart Examples
|
|
173
|
+
|
|
174
|
+
### Synchronous Usage
|
|
175
|
+
|
|
176
|
+
#### Non-Streaming Chat Completion
|
|
177
|
+
```python
|
|
178
|
+
import os
|
|
179
|
+
from dotenv import load_dotenv
|
|
180
|
+
from pi169 import Pi169Client
|
|
181
|
+
|
|
182
|
+
load_dotenv()
|
|
183
|
+
|
|
184
|
+
api_key = os.getenv("ALPIE_API_KEY")
|
|
185
|
+
if not api_key:
|
|
186
|
+
raise ValueError("API key missing")
|
|
187
|
+
|
|
188
|
+
client = Pi169Client(api_key=api_key)
|
|
189
|
+
|
|
190
|
+
response = client.chat.completions.create(
|
|
191
|
+
model="alpie-32b",
|
|
192
|
+
messages=[
|
|
193
|
+
{"role": "user", "content": "What is the capital of France?"}
|
|
194
|
+
],
|
|
195
|
+
stream=False,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
# Print the assistant reply
|
|
199
|
+
if response.choices:
|
|
200
|
+
message = response.choices[0].message
|
|
201
|
+
if message and message.content:
|
|
202
|
+
print(message.content)
|
|
203
|
+
else:
|
|
204
|
+
print("No choices returned")
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
#### Streaming Chat Completion
|
|
208
|
+
```python
|
|
209
|
+
import os
|
|
210
|
+
from dotenv import load_dotenv
|
|
211
|
+
from pi169 import Pi169Client
|
|
212
|
+
|
|
213
|
+
load_dotenv()
|
|
214
|
+
|
|
215
|
+
api_key = os.getenv("ALPIE_API_KEY")
|
|
216
|
+
if not api_key:
|
|
217
|
+
raise ValueError("API key missing")
|
|
218
|
+
|
|
219
|
+
client = Pi169Client(api_key=api_key)
|
|
220
|
+
|
|
221
|
+
stream = client.chat.completions.create(
|
|
222
|
+
model="alpie-32b",
|
|
223
|
+
messages=[{"role": "user", "content": "what is the capital of france?"}],
|
|
224
|
+
stream=True,
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
for chunk in stream:
|
|
228
|
+
if not chunk.choices:
|
|
229
|
+
continue
|
|
230
|
+
|
|
231
|
+
choice = chunk.choices[0]
|
|
232
|
+
delta = choice.get("delta", {})
|
|
233
|
+
|
|
234
|
+
content = delta.get("content")
|
|
235
|
+
if content:
|
|
236
|
+
print(content, end="", flush=True)
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Streaming responses yield partial tokens in real time — ideal for chatbots, UIs, and live applications.
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
### Asynchronous Usage
|
|
244
|
+
|
|
245
|
+
#### Non-Streaming Chat Completion (Async)
|
|
246
|
+
```python
|
|
247
|
+
import asyncio
|
|
248
|
+
import os
|
|
249
|
+
from dotenv import load_dotenv
|
|
250
|
+
from pi169.async_client import AsyncPi169Client
|
|
251
|
+
|
|
252
|
+
load_dotenv()
|
|
253
|
+
|
|
254
|
+
async def main():
|
|
255
|
+
api_key = os.getenv("ALPIE_API_KEY")
|
|
256
|
+
if not api_key:
|
|
257
|
+
raise ValueError("API key missing")
|
|
258
|
+
|
|
259
|
+
client = AsyncPi169Client(api_key=api_key)
|
|
260
|
+
|
|
261
|
+
response = await client.chat.completions.create(
|
|
262
|
+
model="alpie-32b",
|
|
263
|
+
messages=[
|
|
264
|
+
{"role": "user", "content": "What is the capital of France?"}
|
|
265
|
+
],
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
print(response.choices[0].message.content)
|
|
269
|
+
|
|
270
|
+
if __name__ == "__main__":
|
|
271
|
+
asyncio.run(main())
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
#### Streaming Chat Completion (Async)
|
|
275
|
+
```python
|
|
276
|
+
import asyncio
|
|
277
|
+
import os
|
|
278
|
+
from dotenv import load_dotenv
|
|
279
|
+
from pi169.async_client import AsyncPi169Client
|
|
280
|
+
|
|
281
|
+
load_dotenv()
|
|
282
|
+
|
|
283
|
+
async def main():
|
|
284
|
+
api_key = os.getenv("ALPIE_API_KEY")
|
|
285
|
+
if not api_key:
|
|
286
|
+
raise ValueError("API key missing")
|
|
287
|
+
|
|
288
|
+
client = AsyncPi169Client(api_key=api_key)
|
|
289
|
+
|
|
290
|
+
stream = await client.chat.completions.create(
|
|
291
|
+
model="alpie-32b",
|
|
292
|
+
messages=[
|
|
293
|
+
{"role": "user", "content": "What is the capital of France?"}
|
|
294
|
+
],
|
|
295
|
+
stream=True,
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
async for chunk in stream:
|
|
299
|
+
if not chunk.choices:
|
|
300
|
+
continue
|
|
301
|
+
choice = chunk.choices[0]
|
|
302
|
+
delta = choice.get("delta", {})
|
|
303
|
+
|
|
304
|
+
content = delta.get("content")
|
|
305
|
+
if content:
|
|
306
|
+
print(content, end="", flush=True)
|
|
307
|
+
|
|
308
|
+
if __name__ == "__main__":
|
|
309
|
+
asyncio.run(main())
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
#### Concurrent Requests (Async)
|
|
313
|
+
Process multiple requests concurrently for better performance:
|
|
314
|
+
|
|
315
|
+
```python
|
|
316
|
+
import asyncio
|
|
317
|
+
import os
|
|
318
|
+
from dotenv import load_dotenv
|
|
319
|
+
from pi169.async_client import AsyncPi169Client
|
|
320
|
+
|
|
321
|
+
load_dotenv()
|
|
322
|
+
|
|
323
|
+
async def main():
|
|
324
|
+
api_key = os.getenv("ALPIE_API_KEY")
|
|
325
|
+
if not api_key:
|
|
326
|
+
raise ValueError("API key missing")
|
|
327
|
+
|
|
328
|
+
client = AsyncPi169Client(api_key=api_key)
|
|
329
|
+
|
|
330
|
+
# Create multiple tasks
|
|
331
|
+
tasks = [
|
|
332
|
+
client.chat.completions.create(
|
|
333
|
+
model="alpie-32b",
|
|
334
|
+
messages=[{"role": "user", "content": f"What is {n} + {n}?"}]
|
|
335
|
+
)
|
|
336
|
+
for n in range(1, 6)
|
|
337
|
+
]
|
|
338
|
+
|
|
339
|
+
# Execute concurrently
|
|
340
|
+
responses = await asyncio.gather(*tasks)
|
|
341
|
+
|
|
342
|
+
for i, response in enumerate(responses, 1):
|
|
343
|
+
print(f"Response {i}: {response.choices[0].message.content}")
|
|
344
|
+
|
|
345
|
+
if __name__ == "__main__":
|
|
346
|
+
asyncio.run(main())
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
---
|
|
350
|
+
|
|
351
|
+
## Available Models
|
|
352
|
+
|
|
353
|
+
| Model | Parameters | Description |
|
|
354
|
+
|-------|------------|-------------|
|
|
355
|
+
| alpie-core | 32B | Advanced reasoning model |
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
## Rate Limits and Quotas
|
|
359
|
+
|
|
360
|
+
Pi169 enforces rate limits to ensure fair and stable usage of the API.
|
|
361
|
+
|
|
362
|
+
### General Rate Limits
|
|
363
|
+
|
|
364
|
+
- **Requests per minute (RPM):** 60
|
|
365
|
+
- **Requests per hour (RPH):** 1000
|
|
366
|
+
|
|
367
|
+
If you exceed the limit, the API will return:
|
|
368
|
+
```json
|
|
369
|
+
{
|
|
370
|
+
"status": 429,
|
|
371
|
+
"error": "rate_limit_exceeded"
|
|
372
|
+
}
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### Retry and Backoff Recommendations
|
|
376
|
+
|
|
377
|
+
- Wait for the `Retry-After` header
|
|
378
|
+
- Avoid retry storms by ensuring you do not send parallel retries
|
|
379
|
+
|
|
380
|
+
### Best Practices
|
|
381
|
+
|
|
382
|
+
- Batch multiple operations into fewer requests
|
|
383
|
+
- Use streaming for long outputs to avoid token bursts
|
|
384
|
+
- Maintain conversation history efficiently
|
|
385
|
+
- Cache responses when appropriate
|
|
386
|
+
- Spread requests evenly instead of sending them in spikes
|
|
387
|
+
- **Use async client for concurrent requests** to maximize throughput while respecting rate limits
|
|
388
|
+
|
|
389
|
+
## Error Handling
|
|
390
|
+
|
|
391
|
+
The SDK includes a full typed exception hierarchy for safe and predictable error handling.
|
|
392
|
+
|
|
393
|
+
### Base Exception
|
|
394
|
+
```python
|
|
395
|
+
class Pi169Error(Exception):
|
|
396
|
+
...
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
### Exception Hierarchy
|
|
400
|
+
```
|
|
401
|
+
Pi169Error
|
|
402
|
+
├── APIError
|
|
403
|
+
├── ContentPolicyViolationError
|
|
404
|
+
├── ContextWindowExceededError
|
|
405
|
+
├── UnsupportedParamsError
|
|
406
|
+
├── AuthError
|
|
407
|
+
├── RateLimitError
|
|
408
|
+
├── ServerError
|
|
409
|
+
├── EngineOverloadedError
|
|
410
|
+
├── TimeoutError
|
|
411
|
+
├── ModelNotFoundError
|
|
412
|
+
├── LimitExceededError
|
|
413
|
+
└── KeyNotActive
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
### Example Error Response (from backend)
|
|
417
|
+
```json
|
|
418
|
+
{
|
|
419
|
+
"error": {
|
|
420
|
+
"message": "Key not active",
|
|
421
|
+
"type": "key_not_active",
|
|
422
|
+
"code": 402
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
SDK automatically maps this to:
|
|
428
|
+
```python
|
|
429
|
+
KeyNotActive("Key not active", status_code=402, response_data={...})
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
### Catching Errors (Sync)
|
|
433
|
+
|
|
434
|
+
**Catch all Pi169-related errors:**
|
|
435
|
+
```python
|
|
436
|
+
from pi169 import Pi169Error
|
|
437
|
+
|
|
438
|
+
try:
|
|
439
|
+
client.chat.completions.create(...)
|
|
440
|
+
except Pi169Error as e:
|
|
441
|
+
print("Error:", e.message)
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
**Catch specific errors:**
|
|
445
|
+
```python
|
|
446
|
+
from pi169 import (
|
|
447
|
+
Pi169Client,
|
|
448
|
+
AuthError,
|
|
449
|
+
RateLimitError,
|
|
450
|
+
TimeoutError,
|
|
451
|
+
KeyNotActive,
|
|
452
|
+
ModelNotFoundError,
|
|
453
|
+
Pi169Error,
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
client = Pi169Client(api_key="YOUR_API_KEY")
|
|
457
|
+
|
|
458
|
+
try:
|
|
459
|
+
response = client.chat.completions.create(
|
|
460
|
+
model="alpie-32b",
|
|
461
|
+
messages=[{"role": "user", "content": "Hello!"}],
|
|
462
|
+
max_tokens=100
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
except AuthError:
|
|
466
|
+
print("Invalid API key.")
|
|
467
|
+
|
|
468
|
+
except KeyNotActive:
|
|
469
|
+
print("Your API key is not active.")
|
|
470
|
+
|
|
471
|
+
except RateLimitError:
|
|
472
|
+
print("Rate limit exceeded. Please try again later.")
|
|
473
|
+
|
|
474
|
+
except TimeoutError:
|
|
475
|
+
print("The request timed out.")
|
|
476
|
+
|
|
477
|
+
except ModelNotFoundError:
|
|
478
|
+
print("The requested model does not exist.")
|
|
479
|
+
|
|
480
|
+
except Pi169Error as e:
|
|
481
|
+
print("A Pi169 SDK error occurred:", e)
|
|
482
|
+
|
|
483
|
+
else:
|
|
484
|
+
print("Response:", response.choices[0].message.content)
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
### Catching Errors (Async)
|
|
488
|
+
|
|
489
|
+
Error handling works identically in async contexts:
|
|
490
|
+
|
|
491
|
+
```python
|
|
492
|
+
import asyncio
|
|
493
|
+
from pi169.async_client import AsyncPi169Client
|
|
494
|
+
from pi169 import (
|
|
495
|
+
AuthError,
|
|
496
|
+
RateLimitError,
|
|
497
|
+
TimeoutError,
|
|
498
|
+
KeyNotActive,
|
|
499
|
+
ModelNotFoundError,
|
|
500
|
+
Pi169Error,
|
|
501
|
+
)
|
|
502
|
+
|
|
503
|
+
async def main():
|
|
504
|
+
client = AsyncPi169Client(api_key="YOUR_API_KEY")
|
|
505
|
+
|
|
506
|
+
try:
|
|
507
|
+
response = await client.chat.completions.create(
|
|
508
|
+
model="alpie-32b",
|
|
509
|
+
messages=[{"role": "user", "content": "Hello!"}],
|
|
510
|
+
max_tokens=100
|
|
511
|
+
)
|
|
512
|
+
|
|
513
|
+
except AuthError:
|
|
514
|
+
print("Invalid API key.")
|
|
515
|
+
|
|
516
|
+
except KeyNotActive:
|
|
517
|
+
print("Your API key is not active.")
|
|
518
|
+
|
|
519
|
+
except RateLimitError:
|
|
520
|
+
print("Rate limit exceeded. Please try again later.")
|
|
521
|
+
|
|
522
|
+
except TimeoutError:
|
|
523
|
+
print("The request timed out.")
|
|
524
|
+
|
|
525
|
+
except ModelNotFoundError:
|
|
526
|
+
print("The requested model does not exist.")
|
|
527
|
+
|
|
528
|
+
except Pi169Error as e:
|
|
529
|
+
print("A Pi169 SDK error occurred:", e)
|
|
530
|
+
|
|
531
|
+
else:
|
|
532
|
+
print("Response:", response.choices[0].message.content)
|
|
533
|
+
|
|
534
|
+
if __name__ == "__main__":
|
|
535
|
+
asyncio.run(main())
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
### Error-to-Exception Mapping
|
|
539
|
+
|
|
540
|
+
| API Error | SDK Exception |
|
|
541
|
+
|-----------|---------------|
|
|
542
|
+
| 401 auth failure | AuthError |
|
|
543
|
+
| 402 key not active | KeyNotActive |
|
|
544
|
+
| 400 invalid params | UnsupportedParamsError |
|
|
545
|
+
| 413 context window exceeded | ContextWindowExceededError |
|
|
546
|
+
| 404 model not found | ModelNotFoundError |
|
|
547
|
+
| 429 rate limit exceeded | RateLimitError |
|
|
548
|
+
| 500 internal server error | ServerError |
|
|
549
|
+
| 503 engine overloaded | EngineOverloadedError |
|
|
550
|
+
|
|
551
|
+
## Best Practices
|
|
552
|
+
|
|
553
|
+
### When to Use CLI vs Python SDK
|
|
554
|
+
|
|
555
|
+
**Use CLI when:**
|
|
556
|
+
- Quick testing or prototyping
|
|
557
|
+
- One-off queries or experiments
|
|
558
|
+
- Shell scripting and automation
|
|
559
|
+
- Learning the API without writing code
|
|
560
|
+
- Debugging API responses
|
|
561
|
+
|
|
562
|
+
**Use Python SDK when:**
|
|
563
|
+
- Building applications or services
|
|
564
|
+
- Need complex conversation flows
|
|
565
|
+
- Require error handling and retries
|
|
566
|
+
- Processing multiple requests
|
|
567
|
+
- Integration with existing Python code
|
|
568
|
+
|
|
569
|
+
### When to Use Async vs Sync
|
|
570
|
+
|
|
571
|
+
**Use Async when:**
|
|
572
|
+
- Making multiple concurrent API requests
|
|
573
|
+
- Building high-performance web servers (FastAPI, aiohttp)
|
|
574
|
+
- Integrating with other async libraries
|
|
575
|
+
- Handling many simultaneous streaming connections
|
|
576
|
+
- You need maximum throughput within rate limits
|
|
577
|
+
|
|
578
|
+
**Use Sync when:**
|
|
579
|
+
- Writing simple scripts
|
|
580
|
+
- Making sequential requests
|
|
581
|
+
- Working in environments without async support
|
|
582
|
+
- Prototyping or learning
|
|
583
|
+
|
|
584
|
+
## Recommended Project Structure
|
|
585
|
+
```
|
|
586
|
+
pi169/
|
|
587
|
+
├── __init__.py
|
|
588
|
+
├── client.py
|
|
589
|
+
├── async_client.py
|
|
590
|
+
├── cli.py
|
|
591
|
+
├── chat/
|
|
592
|
+
│ ├── completions.py
|
|
593
|
+
│ └── async_completions.py
|
|
594
|
+
├── types.py
|
|
595
|
+
├── errors.py
|
|
596
|
+
└── utils/
|
|
597
|
+
└── http.py
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
## Testing
|
|
601
|
+
|
|
602
|
+
The Pi169 SDK includes a complete pytest-based test suite.
|
|
603
|
+
|
|
604
|
+
**Run all tests:**
|
|
605
|
+
```bash
|
|
606
|
+
pytest
|
|
607
|
+
```
|
|
608
|
+
|
|
609
|
+
### Test Directory Structure
|
|
610
|
+
```
|
|
611
|
+
project-root/
|
|
612
|
+
│
|
|
613
|
+
├── src/
|
|
614
|
+
│ └── pi169/
|
|
615
|
+
│ ├── __init__.py
|
|
616
|
+
│ ├── client.py
|
|
617
|
+
│ ├── async_client.py
|
|
618
|
+
│ ├── cli.py
|
|
619
|
+
│ ├── errors.py
|
|
620
|
+
│ ├── types.py
|
|
621
|
+
│ ├── chat/
|
|
622
|
+
│ │ ├── completions.py
|
|
623
|
+
│ │ ├── async_completions.py
|
|
624
|
+
│ │ └── schemas.py
|
|
625
|
+
│ └── utils/
|
|
626
|
+
│ └── http.py
|
|
627
|
+
│
|
|
628
|
+
└── tests/
|
|
629
|
+
├── test_streaming.py
|
|
630
|
+
├── test_async_streaming.py
|
|
631
|
+
├── test_error_mapping.py
|
|
632
|
+
├── test_async_error_mapping.py
|
|
633
|
+
├── test_timeout.py
|
|
634
|
+
├── test_async_timeout.py
|
|
635
|
+
├── test_retry_logic.py
|
|
636
|
+
├── test_async_retry_logic.py
|
|
637
|
+
├── test_chat_completions.py
|
|
638
|
+
├── test_async_chat_completions.py
|
|
639
|
+
└── test_cli.py
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
### What Each Test File Covers
|
|
643
|
+
|
|
644
|
+
| Test File | Purpose |
|
|
645
|
+
|-----------|---------|
|
|
646
|
+
| test_streaming.py | Verifies streaming responses & chunk iteration |
|
|
647
|
+
| test_async_streaming.py | Verifies async streaming responses |
|
|
648
|
+
| test_error_mapping.py | Ensures correct mapping to SDK exceptions |
|
|
649
|
+
| test_async_error_mapping.py | Tests async error handling |
|
|
650
|
+
| test_timeout.py | Tests request timeout behavior |
|
|
651
|
+
| test_async_timeout.py | Tests async timeout behavior |
|
|
652
|
+
| test_retry_logic.py | Validates retry handling on failures |
|
|
653
|
+
| test_async_retry_logic.py | Tests async retry logic |
|
|
654
|
+
| test_chat_completions.py | Tests non-streaming chat completion flow |
|
|
655
|
+
| test_async_chat_completions.py | Tests async non-streaming completions |
|
|
656
|
+
| test_cli.py | Tests CLI functionality and argument parsing |
|
|
657
|
+
|
|
658
|
+
### Install Test Dependencies
|
|
659
|
+
```bash
|
|
660
|
+
pip install pytest pytest-httpx pytest-asyncio respx pytest-mock
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
This allows mocking API responses so tests run offline.
|
|
664
|
+
|
|
665
|
+
### Running the Tests
|
|
666
|
+
|
|
667
|
+
**Run all tests:**
|
|
668
|
+
```bash
|
|
669
|
+
pytest
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
**Verbose mode:**
|
|
673
|
+
```bash
|
|
674
|
+
pytest -v
|
|
675
|
+
```
|
|
676
|
+
|
|
677
|
+
**Run a single test file:**
|
|
678
|
+
```bash
|
|
679
|
+
pytest tests/test_streaming.py
|
|
680
|
+
```
|
|
681
|
+
|
|
682
|
+
**Run async tests:**
|
|
683
|
+
```bash
|
|
684
|
+
pytest tests/test_async_streaming.py
|
|
685
|
+
```
|
|
686
|
+
|
|
687
|
+
**Run CLI tests:**
|
|
688
|
+
```bash
|
|
689
|
+
pytest tests/test_cli.py
|
|
690
|
+
```
|
|
691
|
+
|
|
692
|
+
**Run a single test:**
|
|
693
|
+
```bash
|
|
694
|
+
pytest tests/test_streaming.py::test_basic_stream
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
## Environment Variables
|
|
698
|
+
|
|
699
|
+
The SDK works seamlessly with environment variables for secure API key management:
|
|
700
|
+
|
|
701
|
+
```bash
|
|
702
|
+
# .env file
|
|
703
|
+
ALPIE_API_KEY=your_api_key_here
|
|
704
|
+
```
|
|
705
|
+
|
|
706
|
+
```python
|
|
707
|
+
import os
|
|
708
|
+
from dotenv import load_dotenv
|
|
709
|
+
from pi169 import Pi169Client
|
|
710
|
+
|
|
711
|
+
load_dotenv()
|
|
712
|
+
|
|
713
|
+
api_key = os.getenv("ALPIE_API_KEY")
|
|
714
|
+
client = Pi169Client(api_key=api_key)
|
|
715
|
+
```
|
|
716
|
+
|
|
717
|
+
## Support
|
|
718
|
+
|
|
719
|
+
For questions, feature requests, or bug reports:
|
|
720
|
+
|
|
721
|
+
- **GitHub Issues:** [https://github.com/169Pi/Pi169-SDK](https://github.com/169Pi/Pi169-SDK)
|
|
722
|
+
- **Support Email:** support@169pi.com
|
|
723
|
+
|
|
724
|
+
When contacting support, include:
|
|
725
|
+
|
|
726
|
+
- SDK version
|
|
727
|
+
- Python version
|
|
728
|
+
- API endpoint used
|
|
729
|
+
- Error message or traceback
|
|
730
|
+
- Minimal reproducible example if possible
|
|
731
|
+
- Whether you're using sync, async, or CLI
|
|
732
|
+
|
|
733
|
+
## Changelog
|
|
734
|
+
|
|
735
|
+
### Version 0.1
|
|
736
|
+
|
|
737
|
+
**Initial public release of Pi169 Python SDK with sync, async, and streaming support for Alpie-Core.**
|
|
738
|
+
|
|
739
|
+
**New Features:**
|
|
740
|
+
- CLI integration for command-line interactions
|
|
741
|
+
- Async/await support with `AsyncPi169Client` client
|
|
742
|
+
- Async streaming support
|
|
743
|
+
- Async error handling
|
|
744
|
+
- Concurrent request examples
|
|
745
|
+
- Context manager support for both sync and async clients
|
|
746
|
+
- Chat completions (sync and streaming)
|
|
747
|
+
- Typed exceptions and error mapping
|
|
748
|
+
- Retry logic, timeout configuration, and base client setup
|
|
749
|
+
- Available models listing
|
|
750
|
+
- Full pytest-based test suite
|
|
751
|
+
|
|
752
|
+
**Documentation:**
|
|
753
|
+
- CLI usage guide and examples
|
|
754
|
+
- Comprehensive async usage examples
|
|
755
|
+
- Best practices for CLI vs Python SDK and sync vs async
|
|
756
|
+
- Test suite documentation
|
|
757
|
+
- Environment variable support examples
|
|
758
|
+
|
|
759
|
+
## License
|
|
760
|
+
|
|
761
|
+
Apache 2.0
|
|
762
|
+
|
|
763
|
+
© 169PI
|