nfinitmonkeys-cortex-sdk 1.0.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.
- nfinitmonkeys_cortex_sdk-1.0.0/.gitignore +16 -0
- nfinitmonkeys_cortex_sdk-1.0.0/LICENSE +21 -0
- nfinitmonkeys_cortex_sdk-1.0.0/PKG-INFO +274 -0
- nfinitmonkeys_cortex_sdk-1.0.0/README.md +246 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/__init__.py +155 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/_http.py +294 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/client.py +199 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/constants.py +10 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/errors.py +137 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/py.typed +0 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/__init__.py +1 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/chat.py +180 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/completions.py +105 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/conversations.py +124 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/embeddings.py +62 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/iris.py +86 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/keys.py +62 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/models.py +50 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/pdf.py +50 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/performance.py +30 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/plugins.py +50 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/teams.py +122 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/usage.py +38 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/resources/web_search.py +54 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/streaming.py +109 -0
- nfinitmonkeys_cortex_sdk-1.0.0/cortex_sdk/types.py +619 -0
- nfinitmonkeys_cortex_sdk-1.0.0/pyproject.toml +50 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/__init__.py +0 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/conftest.py +123 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/test_chat.py +134 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/test_client.py +93 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/test_conversations.py +118 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/test_errors.py +227 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/test_iris.py +117 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/test_keys.py +88 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/test_streaming.py +111 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/test_teams.py +126 -0
- nfinitmonkeys_cortex_sdk-1.0.0/tests/test_usage.py +119 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 InfiniteMonkeys
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nfinitmonkeys-cortex-sdk
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Official Python SDK for the Cortex LLM Gateway API by InfiniteMonkeys
|
|
5
|
+
Author: InfiniteMonkeys
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: ai,cortex,gateway,llm,sdk
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Requires-Dist: httpx>=0.24.0
|
|
21
|
+
Requires-Dist: pydantic>=2.0
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: coverage>=7.0; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
|
|
25
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
26
|
+
Requires-Dist: respx>=0.20; extra == 'dev'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# Cortex Python SDK
|
|
30
|
+
|
|
31
|
+
Python client for the Cortex LLM Gateway and Admin API.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install cortex-sdk
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Quick Start
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from cortex_sdk import CortexClient
|
|
43
|
+
|
|
44
|
+
client = CortexClient(api_key="sk-cortex-...")
|
|
45
|
+
|
|
46
|
+
# Chat completions
|
|
47
|
+
response = client.chat.completions.create(
|
|
48
|
+
model="default",
|
|
49
|
+
messages=[{"role": "user", "content": "Hello!"}],
|
|
50
|
+
)
|
|
51
|
+
print(response.choices[0].message.content)
|
|
52
|
+
|
|
53
|
+
client.close()
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Async Usage
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import asyncio
|
|
60
|
+
from cortex_sdk import AsyncCortexClient
|
|
61
|
+
|
|
62
|
+
async def main():
|
|
63
|
+
async with AsyncCortexClient(api_key="sk-cortex-...") as client:
|
|
64
|
+
response = await client.chat.completions.create(
|
|
65
|
+
model="default",
|
|
66
|
+
messages=[{"role": "user", "content": "Hello!"}],
|
|
67
|
+
)
|
|
68
|
+
print(response.choices[0].message.content)
|
|
69
|
+
|
|
70
|
+
asyncio.run(main())
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Streaming
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
# Sync streaming
|
|
77
|
+
stream = client.chat.completions.create(
|
|
78
|
+
model="default",
|
|
79
|
+
messages=[{"role": "user", "content": "Tell me a story"}],
|
|
80
|
+
stream=True,
|
|
81
|
+
)
|
|
82
|
+
for chunk in stream:
|
|
83
|
+
if chunk.choices[0].delta.content:
|
|
84
|
+
print(chunk.choices[0].delta.content, end="")
|
|
85
|
+
|
|
86
|
+
# Async streaming
|
|
87
|
+
stream = await async_client.chat.completions.create(
|
|
88
|
+
model="default",
|
|
89
|
+
messages=[{"role": "user", "content": "Tell me a story"}],
|
|
90
|
+
stream=True,
|
|
91
|
+
)
|
|
92
|
+
async for chunk in stream:
|
|
93
|
+
if chunk.choices[0].delta.content:
|
|
94
|
+
print(chunk.choices[0].delta.content, end="")
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Configuration
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
client = CortexClient(
|
|
101
|
+
api_key="sk-cortex-...",
|
|
102
|
+
llm_base_url="https://cortexapi.nfinitmonkeys.com/v1", # LLM gateway
|
|
103
|
+
admin_base_url="https://admin.nfinitmonkeys.com", # Admin API
|
|
104
|
+
timeout=30.0, # Request timeout (seconds)
|
|
105
|
+
streaming_timeout=300.0, # Streaming timeout (seconds)
|
|
106
|
+
max_retries=3, # Retry count for transient errors
|
|
107
|
+
extra_headers={"X-Custom": "value"},
|
|
108
|
+
)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## LLM Gateway
|
|
112
|
+
|
|
113
|
+
### Chat Completions
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
response = client.chat.completions.create(
|
|
117
|
+
model="gpt-4",
|
|
118
|
+
messages=[
|
|
119
|
+
{"role": "system", "content": "You are a helpful assistant."},
|
|
120
|
+
{"role": "user", "content": "What is Python?"},
|
|
121
|
+
],
|
|
122
|
+
temperature=0.7,
|
|
123
|
+
max_tokens=500,
|
|
124
|
+
)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Text Completions
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
response = client.completions.create(
|
|
131
|
+
model="gpt-3.5-turbo-instruct",
|
|
132
|
+
prompt="Once upon a time",
|
|
133
|
+
max_tokens=100,
|
|
134
|
+
)
|
|
135
|
+
print(response.choices[0].text)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Embeddings
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
response = client.embeddings.create(
|
|
142
|
+
model="text-embedding-ada-002",
|
|
143
|
+
input="Hello world",
|
|
144
|
+
)
|
|
145
|
+
print(response.data[0].embedding)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Models
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
models = client.models.list()
|
|
152
|
+
for model in models.data:
|
|
153
|
+
print(model.id)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Admin API
|
|
157
|
+
|
|
158
|
+
### API Keys
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
# List keys
|
|
162
|
+
keys = client.keys.list()
|
|
163
|
+
|
|
164
|
+
# Create key
|
|
165
|
+
key = client.keys.create(name="Production", scopes=["chat", "embeddings"])
|
|
166
|
+
|
|
167
|
+
# Delete key
|
|
168
|
+
client.keys.delete("key-id")
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Teams
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
# List / create / get / delete teams
|
|
175
|
+
teams = client.teams.list()
|
|
176
|
+
team = client.teams.create(name="Engineering")
|
|
177
|
+
team = client.teams.get("team-id")
|
|
178
|
+
client.teams.delete("team-id")
|
|
179
|
+
|
|
180
|
+
# Members
|
|
181
|
+
member = client.teams.add_member("team-id", email="alice@example.com", role="admin")
|
|
182
|
+
client.teams.update_member("team-id", "member-id", role="viewer")
|
|
183
|
+
client.teams.remove_member("team-id", "member-id")
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Usage & Performance
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
stats = client.usage.get(params={"start_date": "2024-01-01"})
|
|
190
|
+
limits = client.usage.limits()
|
|
191
|
+
metrics = client.performance.get()
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Conversations
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
convos = client.conversations.list()
|
|
198
|
+
convo = client.conversations.create(title="My Chat", model="gpt-4")
|
|
199
|
+
convo = client.conversations.get("conv-id")
|
|
200
|
+
convo = client.conversations.update("conv-id", title="New Title")
|
|
201
|
+
client.conversations.delete("conv-id")
|
|
202
|
+
|
|
203
|
+
# Stream messages (SSE)
|
|
204
|
+
for msg in client.conversations.messages("conv-id"):
|
|
205
|
+
print(msg.content)
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Iris (Document Extraction)
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
job = client.iris.extract(
|
|
212
|
+
document_url="https://example.com/invoice.pdf",
|
|
213
|
+
schema_id="invoice-schema",
|
|
214
|
+
)
|
|
215
|
+
jobs = client.iris.list_jobs(limit=10)
|
|
216
|
+
schemas = client.iris.list_schemas()
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Other Resources
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
# Plugins & Optimizations
|
|
223
|
+
plugins = client.plugins.list()
|
|
224
|
+
opts = client.optimizations.get()
|
|
225
|
+
|
|
226
|
+
# Admin models config
|
|
227
|
+
models = client.admin_models.list()
|
|
228
|
+
|
|
229
|
+
# PDF generation
|
|
230
|
+
pdf = client.pdf.generate(content="# Report", template="default")
|
|
231
|
+
|
|
232
|
+
# Web search
|
|
233
|
+
results = client.web_search.search(query="Python programming", num_results=5)
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Error Handling
|
|
237
|
+
|
|
238
|
+
```python
|
|
239
|
+
from cortex_sdk import (
|
|
240
|
+
CortexError,
|
|
241
|
+
AuthenticationError,
|
|
242
|
+
RateLimitError,
|
|
243
|
+
NotFoundError,
|
|
244
|
+
InternalServerError,
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
try:
|
|
248
|
+
response = client.chat.completions.create(...)
|
|
249
|
+
except AuthenticationError:
|
|
250
|
+
print("Invalid API key")
|
|
251
|
+
except RateLimitError as e:
|
|
252
|
+
print(f"Rate limited. Retry after {e.retry_after}s")
|
|
253
|
+
except NotFoundError:
|
|
254
|
+
print("Resource not found")
|
|
255
|
+
except InternalServerError:
|
|
256
|
+
print("Server error — will be retried automatically")
|
|
257
|
+
except CortexError as e:
|
|
258
|
+
print(f"SDK error: {e.message}")
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## Retry Behavior
|
|
262
|
+
|
|
263
|
+
The SDK automatically retries on HTTP 429, 500, 502, 503, and 504 with exponential backoff and jitter. The `Retry-After` header is respected when present. Configure with `max_retries` (default: 3).
|
|
264
|
+
|
|
265
|
+
## Development
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
pip install -e ".[dev]"
|
|
269
|
+
pytest
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## License
|
|
273
|
+
|
|
274
|
+
MIT
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# Cortex Python SDK
|
|
2
|
+
|
|
3
|
+
Python client for the Cortex LLM Gateway and Admin API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install cortex-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from cortex_sdk import CortexClient
|
|
15
|
+
|
|
16
|
+
client = CortexClient(api_key="sk-cortex-...")
|
|
17
|
+
|
|
18
|
+
# Chat completions
|
|
19
|
+
response = client.chat.completions.create(
|
|
20
|
+
model="default",
|
|
21
|
+
messages=[{"role": "user", "content": "Hello!"}],
|
|
22
|
+
)
|
|
23
|
+
print(response.choices[0].message.content)
|
|
24
|
+
|
|
25
|
+
client.close()
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Async Usage
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
import asyncio
|
|
32
|
+
from cortex_sdk import AsyncCortexClient
|
|
33
|
+
|
|
34
|
+
async def main():
|
|
35
|
+
async with AsyncCortexClient(api_key="sk-cortex-...") as client:
|
|
36
|
+
response = await client.chat.completions.create(
|
|
37
|
+
model="default",
|
|
38
|
+
messages=[{"role": "user", "content": "Hello!"}],
|
|
39
|
+
)
|
|
40
|
+
print(response.choices[0].message.content)
|
|
41
|
+
|
|
42
|
+
asyncio.run(main())
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Streaming
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
# Sync streaming
|
|
49
|
+
stream = client.chat.completions.create(
|
|
50
|
+
model="default",
|
|
51
|
+
messages=[{"role": "user", "content": "Tell me a story"}],
|
|
52
|
+
stream=True,
|
|
53
|
+
)
|
|
54
|
+
for chunk in stream:
|
|
55
|
+
if chunk.choices[0].delta.content:
|
|
56
|
+
print(chunk.choices[0].delta.content, end="")
|
|
57
|
+
|
|
58
|
+
# Async streaming
|
|
59
|
+
stream = await async_client.chat.completions.create(
|
|
60
|
+
model="default",
|
|
61
|
+
messages=[{"role": "user", "content": "Tell me a story"}],
|
|
62
|
+
stream=True,
|
|
63
|
+
)
|
|
64
|
+
async for chunk in stream:
|
|
65
|
+
if chunk.choices[0].delta.content:
|
|
66
|
+
print(chunk.choices[0].delta.content, end="")
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Configuration
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
client = CortexClient(
|
|
73
|
+
api_key="sk-cortex-...",
|
|
74
|
+
llm_base_url="https://cortexapi.nfinitmonkeys.com/v1", # LLM gateway
|
|
75
|
+
admin_base_url="https://admin.nfinitmonkeys.com", # Admin API
|
|
76
|
+
timeout=30.0, # Request timeout (seconds)
|
|
77
|
+
streaming_timeout=300.0, # Streaming timeout (seconds)
|
|
78
|
+
max_retries=3, # Retry count for transient errors
|
|
79
|
+
extra_headers={"X-Custom": "value"},
|
|
80
|
+
)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## LLM Gateway
|
|
84
|
+
|
|
85
|
+
### Chat Completions
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
response = client.chat.completions.create(
|
|
89
|
+
model="gpt-4",
|
|
90
|
+
messages=[
|
|
91
|
+
{"role": "system", "content": "You are a helpful assistant."},
|
|
92
|
+
{"role": "user", "content": "What is Python?"},
|
|
93
|
+
],
|
|
94
|
+
temperature=0.7,
|
|
95
|
+
max_tokens=500,
|
|
96
|
+
)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Text Completions
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
response = client.completions.create(
|
|
103
|
+
model="gpt-3.5-turbo-instruct",
|
|
104
|
+
prompt="Once upon a time",
|
|
105
|
+
max_tokens=100,
|
|
106
|
+
)
|
|
107
|
+
print(response.choices[0].text)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Embeddings
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
response = client.embeddings.create(
|
|
114
|
+
model="text-embedding-ada-002",
|
|
115
|
+
input="Hello world",
|
|
116
|
+
)
|
|
117
|
+
print(response.data[0].embedding)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Models
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
models = client.models.list()
|
|
124
|
+
for model in models.data:
|
|
125
|
+
print(model.id)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Admin API
|
|
129
|
+
|
|
130
|
+
### API Keys
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
# List keys
|
|
134
|
+
keys = client.keys.list()
|
|
135
|
+
|
|
136
|
+
# Create key
|
|
137
|
+
key = client.keys.create(name="Production", scopes=["chat", "embeddings"])
|
|
138
|
+
|
|
139
|
+
# Delete key
|
|
140
|
+
client.keys.delete("key-id")
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Teams
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
# List / create / get / delete teams
|
|
147
|
+
teams = client.teams.list()
|
|
148
|
+
team = client.teams.create(name="Engineering")
|
|
149
|
+
team = client.teams.get("team-id")
|
|
150
|
+
client.teams.delete("team-id")
|
|
151
|
+
|
|
152
|
+
# Members
|
|
153
|
+
member = client.teams.add_member("team-id", email="alice@example.com", role="admin")
|
|
154
|
+
client.teams.update_member("team-id", "member-id", role="viewer")
|
|
155
|
+
client.teams.remove_member("team-id", "member-id")
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Usage & Performance
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
stats = client.usage.get(params={"start_date": "2024-01-01"})
|
|
162
|
+
limits = client.usage.limits()
|
|
163
|
+
metrics = client.performance.get()
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Conversations
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
convos = client.conversations.list()
|
|
170
|
+
convo = client.conversations.create(title="My Chat", model="gpt-4")
|
|
171
|
+
convo = client.conversations.get("conv-id")
|
|
172
|
+
convo = client.conversations.update("conv-id", title="New Title")
|
|
173
|
+
client.conversations.delete("conv-id")
|
|
174
|
+
|
|
175
|
+
# Stream messages (SSE)
|
|
176
|
+
for msg in client.conversations.messages("conv-id"):
|
|
177
|
+
print(msg.content)
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Iris (Document Extraction)
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
job = client.iris.extract(
|
|
184
|
+
document_url="https://example.com/invoice.pdf",
|
|
185
|
+
schema_id="invoice-schema",
|
|
186
|
+
)
|
|
187
|
+
jobs = client.iris.list_jobs(limit=10)
|
|
188
|
+
schemas = client.iris.list_schemas()
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Other Resources
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
# Plugins & Optimizations
|
|
195
|
+
plugins = client.plugins.list()
|
|
196
|
+
opts = client.optimizations.get()
|
|
197
|
+
|
|
198
|
+
# Admin models config
|
|
199
|
+
models = client.admin_models.list()
|
|
200
|
+
|
|
201
|
+
# PDF generation
|
|
202
|
+
pdf = client.pdf.generate(content="# Report", template="default")
|
|
203
|
+
|
|
204
|
+
# Web search
|
|
205
|
+
results = client.web_search.search(query="Python programming", num_results=5)
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Error Handling
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
from cortex_sdk import (
|
|
212
|
+
CortexError,
|
|
213
|
+
AuthenticationError,
|
|
214
|
+
RateLimitError,
|
|
215
|
+
NotFoundError,
|
|
216
|
+
InternalServerError,
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
try:
|
|
220
|
+
response = client.chat.completions.create(...)
|
|
221
|
+
except AuthenticationError:
|
|
222
|
+
print("Invalid API key")
|
|
223
|
+
except RateLimitError as e:
|
|
224
|
+
print(f"Rate limited. Retry after {e.retry_after}s")
|
|
225
|
+
except NotFoundError:
|
|
226
|
+
print("Resource not found")
|
|
227
|
+
except InternalServerError:
|
|
228
|
+
print("Server error — will be retried automatically")
|
|
229
|
+
except CortexError as e:
|
|
230
|
+
print(f"SDK error: {e.message}")
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Retry Behavior
|
|
234
|
+
|
|
235
|
+
The SDK automatically retries on HTTP 429, 500, 502, 503, and 504 with exponential backoff and jitter. The `Retry-After` header is respected when present. Configure with `max_retries` (default: 3).
|
|
236
|
+
|
|
237
|
+
## Development
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
pip install -e ".[dev]"
|
|
241
|
+
pytest
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## License
|
|
245
|
+
|
|
246
|
+
MIT
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Cortex SDK — Python client for the Cortex LLM Gateway API."""
|
|
2
|
+
|
|
3
|
+
from cortex_sdk.client import AsyncCortexClient, CortexClient
|
|
4
|
+
from cortex_sdk.errors import (
|
|
5
|
+
APIError,
|
|
6
|
+
AuthenticationError,
|
|
7
|
+
ConnectionError,
|
|
8
|
+
CortexError,
|
|
9
|
+
InternalServerError,
|
|
10
|
+
NotFoundError,
|
|
11
|
+
PermissionDeniedError,
|
|
12
|
+
RateLimitError,
|
|
13
|
+
StreamError,
|
|
14
|
+
TimeoutError,
|
|
15
|
+
ValidationError,
|
|
16
|
+
)
|
|
17
|
+
from cortex_sdk.streaming import AsyncStream, Stream
|
|
18
|
+
from cortex_sdk.types import (
|
|
19
|
+
APIKey,
|
|
20
|
+
APIKeyCreate,
|
|
21
|
+
APIKeyList,
|
|
22
|
+
ChatCompletion,
|
|
23
|
+
ChatCompletionChunk,
|
|
24
|
+
ChatCompletionRequest,
|
|
25
|
+
ChatMessage,
|
|
26
|
+
Choice,
|
|
27
|
+
ChoiceDelta,
|
|
28
|
+
ChoiceMessage,
|
|
29
|
+
Completion,
|
|
30
|
+
CompletionChoice,
|
|
31
|
+
CompletionChunk,
|
|
32
|
+
CompletionRequest,
|
|
33
|
+
Conversation,
|
|
34
|
+
ConversationCreate,
|
|
35
|
+
ConversationList,
|
|
36
|
+
ConversationMessage,
|
|
37
|
+
ConversationUpdate,
|
|
38
|
+
DeleteResponse,
|
|
39
|
+
EmbeddingData,
|
|
40
|
+
EmbeddingRequest,
|
|
41
|
+
EmbeddingResponse,
|
|
42
|
+
FunctionDefinition,
|
|
43
|
+
IrisExtractionRequest,
|
|
44
|
+
IrisJob,
|
|
45
|
+
IrisJobList,
|
|
46
|
+
IrisSchema,
|
|
47
|
+
IrisSchemaList,
|
|
48
|
+
Model,
|
|
49
|
+
ModelConfig,
|
|
50
|
+
ModelConfigList,
|
|
51
|
+
ModelList,
|
|
52
|
+
ModelPermission,
|
|
53
|
+
OptimizationSettings,
|
|
54
|
+
PDFGenerateRequest,
|
|
55
|
+
PDFGenerateResponse,
|
|
56
|
+
PerformanceMetrics,
|
|
57
|
+
Plugin,
|
|
58
|
+
PluginList,
|
|
59
|
+
ResponseFormat,
|
|
60
|
+
StreamChoice,
|
|
61
|
+
Team,
|
|
62
|
+
TeamCreate,
|
|
63
|
+
TeamList,
|
|
64
|
+
TeamMember,
|
|
65
|
+
TeamMemberAdd,
|
|
66
|
+
TeamMemberUpdate,
|
|
67
|
+
ToolDefinition,
|
|
68
|
+
Usage,
|
|
69
|
+
UsageLimits,
|
|
70
|
+
UsageRecord,
|
|
71
|
+
UsageStats,
|
|
72
|
+
WebSearchRequest,
|
|
73
|
+
WebSearchResponse,
|
|
74
|
+
WebSearchResult,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
__all__ = [
|
|
78
|
+
# Clients
|
|
79
|
+
"CortexClient",
|
|
80
|
+
"AsyncCortexClient",
|
|
81
|
+
# Streams
|
|
82
|
+
"Stream",
|
|
83
|
+
"AsyncStream",
|
|
84
|
+
# Errors
|
|
85
|
+
"CortexError",
|
|
86
|
+
"APIError",
|
|
87
|
+
"AuthenticationError",
|
|
88
|
+
"PermissionDeniedError",
|
|
89
|
+
"NotFoundError",
|
|
90
|
+
"RateLimitError",
|
|
91
|
+
"InternalServerError",
|
|
92
|
+
"ConnectionError",
|
|
93
|
+
"TimeoutError",
|
|
94
|
+
"StreamError",
|
|
95
|
+
"ValidationError",
|
|
96
|
+
# Types
|
|
97
|
+
"ChatMessage",
|
|
98
|
+
"FunctionDefinition",
|
|
99
|
+
"ToolDefinition",
|
|
100
|
+
"ResponseFormat",
|
|
101
|
+
"ChatCompletionRequest",
|
|
102
|
+
"ChoiceDelta",
|
|
103
|
+
"ChoiceMessage",
|
|
104
|
+
"Choice",
|
|
105
|
+
"StreamChoice",
|
|
106
|
+
"Usage",
|
|
107
|
+
"ChatCompletion",
|
|
108
|
+
"ChatCompletionChunk",
|
|
109
|
+
"CompletionRequest",
|
|
110
|
+
"CompletionChoice",
|
|
111
|
+
"Completion",
|
|
112
|
+
"CompletionChunk",
|
|
113
|
+
"EmbeddingRequest",
|
|
114
|
+
"EmbeddingData",
|
|
115
|
+
"EmbeddingResponse",
|
|
116
|
+
"Model",
|
|
117
|
+
"ModelPermission",
|
|
118
|
+
"ModelList",
|
|
119
|
+
"APIKey",
|
|
120
|
+
"APIKeyCreate",
|
|
121
|
+
"APIKeyList",
|
|
122
|
+
"TeamMember",
|
|
123
|
+
"Team",
|
|
124
|
+
"TeamCreate",
|
|
125
|
+
"TeamMemberAdd",
|
|
126
|
+
"TeamMemberUpdate",
|
|
127
|
+
"TeamList",
|
|
128
|
+
"UsageRecord",
|
|
129
|
+
"UsageStats",
|
|
130
|
+
"UsageLimits",
|
|
131
|
+
"PerformanceMetrics",
|
|
132
|
+
"ConversationMessage",
|
|
133
|
+
"Conversation",
|
|
134
|
+
"ConversationCreate",
|
|
135
|
+
"ConversationUpdate",
|
|
136
|
+
"ConversationList",
|
|
137
|
+
"IrisExtractionRequest",
|
|
138
|
+
"IrisJob",
|
|
139
|
+
"IrisJobList",
|
|
140
|
+
"IrisSchema",
|
|
141
|
+
"IrisSchemaList",
|
|
142
|
+
"Plugin",
|
|
143
|
+
"PluginList",
|
|
144
|
+
"OptimizationSettings",
|
|
145
|
+
"ModelConfig",
|
|
146
|
+
"ModelConfigList",
|
|
147
|
+
"PDFGenerateRequest",
|
|
148
|
+
"PDFGenerateResponse",
|
|
149
|
+
"WebSearchRequest",
|
|
150
|
+
"WebSearchResult",
|
|
151
|
+
"WebSearchResponse",
|
|
152
|
+
"DeleteResponse",
|
|
153
|
+
]
|
|
154
|
+
|
|
155
|
+
__version__ = "0.1.0"
|