olbrain-python-sdk 0.3.0__tar.gz → 0.3.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/MANIFEST.in +1 -1
- olbrain_python_sdk-0.3.2/PKG-INFO +531 -0
- olbrain_python_sdk-0.3.2/README.md +483 -0
- olbrain_python_sdk-0.3.2/examples/basic_usage.py +163 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/examples/session_management.py +0 -16
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/examples/streaming_responses.py +0 -14
- olbrain_python_sdk-0.3.2/olbrain/AGENTS.md +167 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/olbrain/__init__.py +12 -9
- olbrain_python_sdk-0.3.2/olbrain/client.py +746 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/olbrain/session.py +6 -12
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/olbrain/streaming.py +12 -17
- olbrain_python_sdk-0.3.2/olbrain_python_sdk.egg-info/PKG-INFO +531 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/olbrain_python_sdk.egg-info/SOURCES.txt +1 -1
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/olbrain_python_sdk.egg-info/top_level.txt +0 -1
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/pyproject.toml +6 -3
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/setup.py +8 -7
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/tests/test_client.py +40 -60
- olbrain_python_sdk-0.3.2/tests/test_session.py +89 -0
- olbrain_python_sdk-0.3.0/PKG-INFO +0 -299
- olbrain_python_sdk-0.3.0/README.md +0 -251
- olbrain_python_sdk-0.3.0/examples/basic_usage.py +0 -149
- olbrain_python_sdk-0.3.0/olbrain/client.py +0 -622
- olbrain_python_sdk-0.3.0/olbrain_python_sdk.egg-info/PKG-INFO +0 -299
- olbrain_python_sdk-0.3.0/test_agent.py +0 -143
- olbrain_python_sdk-0.3.0/tests/test_session.py +0 -112
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/.gitignore +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/LICENSE +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/examples/advanced_features.py +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/examples/error_handling.py +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/olbrain/exceptions.py +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/olbrain/utils.py +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/olbrain_python_sdk.egg-info/dependency_links.txt +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/olbrain_python_sdk.egg-info/not-zip-safe +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/olbrain_python_sdk.egg-info/requires.txt +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/requirements.txt +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/setup.cfg +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/tests/__init__.py +0 -0
- {olbrain_python_sdk-0.3.0 → olbrain_python_sdk-0.3.2}/tests/test_exceptions.py +0 -0
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: olbrain-python-sdk
|
|
3
|
+
Version: 0.3.2
|
|
4
|
+
Summary: Official Python SDK for Olbrain AI agents
|
|
5
|
+
Home-page: https://github.com/Olbrain/olbrain-sdk
|
|
6
|
+
Author: Olbrain Team
|
|
7
|
+
Author-email: Olbrain Team <support@olbrain.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Homepage, https://github.com/Olbrain/olbrain-sdk/tree/main/packages/python
|
|
10
|
+
Project-URL: Bug Reports, https://github.com/Olbrain/olbrain-sdk/issues
|
|
11
|
+
Project-URL: Source, https://github.com/Olbrain/olbrain-sdk
|
|
12
|
+
Project-URL: Documentation, https://docs.olbrain.com/python-sdk
|
|
13
|
+
Keywords: olbrain,ai,agents,chatbot,nlp,artificial intelligence,sdk,api,conversation,streaming
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
26
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
27
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
28
|
+
Requires-Python: >=3.7
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
License-File: LICENSE
|
|
31
|
+
Requires-Dist: requests>=2.25.0
|
|
32
|
+
Requires-Dist: urllib3>=1.26.0
|
|
33
|
+
Provides-Extra: dev
|
|
34
|
+
Requires-Dist: pytest>=6.0.0; extra == "dev"
|
|
35
|
+
Requires-Dist: pytest-asyncio>=0.18.0; extra == "dev"
|
|
36
|
+
Requires-Dist: pytest-cov>=2.12.0; extra == "dev"
|
|
37
|
+
Requires-Dist: black>=22.0.0; extra == "dev"
|
|
38
|
+
Requires-Dist: flake8>=4.0.0; extra == "dev"
|
|
39
|
+
Requires-Dist: mypy>=0.910; extra == "dev"
|
|
40
|
+
Requires-Dist: requests-mock>=1.9.0; extra == "dev"
|
|
41
|
+
Provides-Extra: async
|
|
42
|
+
Requires-Dist: aiohttp>=3.8.0; extra == "async"
|
|
43
|
+
Requires-Dist: aiofiles>=0.7.0; extra == "async"
|
|
44
|
+
Dynamic: author
|
|
45
|
+
Dynamic: home-page
|
|
46
|
+
Dynamic: license-file
|
|
47
|
+
Dynamic: requires-python
|
|
48
|
+
|
|
49
|
+
# Olbrain Python SDK
|
|
50
|
+
|
|
51
|
+
[](https://pypi.org/project/olbrain-python-sdk/)
|
|
52
|
+
[](https://pypi.org/project/olbrain-python-sdk/)
|
|
53
|
+
[](https://opensource.org/licenses/MIT)
|
|
54
|
+
|
|
55
|
+
Official Python SDK for integrating Olbrain AI agents into your applications.
|
|
56
|
+
|
|
57
|
+
> **Coding assistants:** the complete API reference ships in the package as [`olbrain/AGENTS.md`](olbrain/AGENTS.md) (`site-packages/olbrain/AGENTS.md`).
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install olbrain-python-sdk
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Development Installation
|
|
66
|
+
|
|
67
|
+
For local development and testing:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
# Clone the repository
|
|
71
|
+
git clone https://github.com/Olbrain/olbrain-sdk.git
|
|
72
|
+
cd olbrain-sdk/packages/python
|
|
73
|
+
|
|
74
|
+
# Create virtual environment
|
|
75
|
+
python3 -m venv venv
|
|
76
|
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
|
77
|
+
|
|
78
|
+
# Install in development mode
|
|
79
|
+
pip install -e .
|
|
80
|
+
|
|
81
|
+
# Run tests
|
|
82
|
+
pytest tests/
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Quick Start
|
|
86
|
+
|
|
87
|
+
Get your credentials from the Olbrain dashboard:
|
|
88
|
+
- **Agent ID**: Your unique agent identifier
|
|
89
|
+
- **API Key**: Starts with `sk_live_` (production) or `sk_` (test)
|
|
90
|
+
|
|
91
|
+
### Basic Example
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
from olbrain import AgentClient
|
|
95
|
+
|
|
96
|
+
# Initialize client
|
|
97
|
+
client = AgentClient(
|
|
98
|
+
agent_id="your-agent-id",
|
|
99
|
+
api_key="sk_live_your_api_key"
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
# Create a session and send a message
|
|
103
|
+
session_id = client.create_session(title="My Chat")
|
|
104
|
+
response = client.send_and_wait(session_id, "Hello!")
|
|
105
|
+
|
|
106
|
+
print(f"Response: {response.text}")
|
|
107
|
+
print(f"Tokens used: {response.token_usage.total_tokens}")
|
|
108
|
+
|
|
109
|
+
client.close()
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Using Context Manager
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from olbrain import AgentClient
|
|
116
|
+
|
|
117
|
+
# Automatically closes client when done
|
|
118
|
+
with AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key") as client:
|
|
119
|
+
session_id = client.create_session(title="My Chat")
|
|
120
|
+
response = client.send_and_wait(session_id, "What is machine learning?")
|
|
121
|
+
print(response.text)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Features
|
|
125
|
+
|
|
126
|
+
- **Simple API** - Just `agent_id` and `api_key` to get started
|
|
127
|
+
- **Session Management** - Create, update, archive sessions with metadata
|
|
128
|
+
- **Sync & Streaming** - Both request-response and real-time streaming
|
|
129
|
+
- **Token Tracking** - Monitor usage and costs per request
|
|
130
|
+
- **Model Override** - Switch models per-message
|
|
131
|
+
- **Error Handling** - Comprehensive exception hierarchy
|
|
132
|
+
|
|
133
|
+
## Usage
|
|
134
|
+
|
|
135
|
+
### 1. Synchronous Messaging (Request-Response)
|
|
136
|
+
|
|
137
|
+
Send a message and wait for the complete response:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
from olbrain import AgentClient
|
|
141
|
+
|
|
142
|
+
with AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key") as client:
|
|
143
|
+
# Create a new chat session
|
|
144
|
+
session_id = client.create_session(title="My Chat")
|
|
145
|
+
|
|
146
|
+
# Send message and wait for response
|
|
147
|
+
response = client.send_and_wait(session_id, "What is Python?")
|
|
148
|
+
|
|
149
|
+
# Access response data
|
|
150
|
+
print(f"Agent: {response.text}")
|
|
151
|
+
print(f"Tokens used: {response.token_usage.total_tokens}")
|
|
152
|
+
print(f"Model: {response.model_used}")
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### 2. Real-Time Streaming (Callback Pattern)
|
|
156
|
+
|
|
157
|
+
Receive messages in real-time as they arrive:
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
from olbrain import AgentClient
|
|
161
|
+
|
|
162
|
+
client = AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key")
|
|
163
|
+
|
|
164
|
+
# Define message handler
|
|
165
|
+
def on_message(msg):
|
|
166
|
+
role = msg.get('role', 'unknown') # 'user' or 'assistant'
|
|
167
|
+
content = msg.get('content', '')
|
|
168
|
+
tokens = msg.get('token_usage', {}).get('total', 0)
|
|
169
|
+
|
|
170
|
+
print(f"[{role.upper()}]: {content}")
|
|
171
|
+
if tokens > 0:
|
|
172
|
+
print(f" Tokens: {tokens}")
|
|
173
|
+
|
|
174
|
+
# Create session with streaming callback
|
|
175
|
+
session_id = client.create_session(
|
|
176
|
+
title="Streaming Chat",
|
|
177
|
+
on_message=on_message
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
# Send message - responses arrive via callback
|
|
181
|
+
client.send(session_id, "Tell me a story")
|
|
182
|
+
|
|
183
|
+
# Keep client running to receive messages
|
|
184
|
+
client.run() # Blocks until Ctrl+C
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### 3. Session Management
|
|
188
|
+
|
|
189
|
+
Create, retrieve, and manage chat sessions:
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
from olbrain import AgentClient
|
|
193
|
+
|
|
194
|
+
with AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key") as client:
|
|
195
|
+
# Create session with metadata
|
|
196
|
+
session = client.create_session(
|
|
197
|
+
title="Support Chat",
|
|
198
|
+
user_id="user-123",
|
|
199
|
+
metadata={"source": "web", "priority": "high"},
|
|
200
|
+
mode="production"
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
# Get session info
|
|
204
|
+
session_info = client.get_session(session)
|
|
205
|
+
print(f"Title: {session_info.title}")
|
|
206
|
+
print(f"Message count: {session_info.message_count}")
|
|
207
|
+
|
|
208
|
+
# Get message history
|
|
209
|
+
messages = client.get_messages(session, limit=20)
|
|
210
|
+
for msg in messages.get('messages', []):
|
|
211
|
+
print(f"{msg['role']}: {msg['content']}")
|
|
212
|
+
|
|
213
|
+
# Update session
|
|
214
|
+
updated = client.update_session(session, title="Updated Chat Title")
|
|
215
|
+
|
|
216
|
+
# Get session statistics
|
|
217
|
+
stats = client.get_session_stats(session)
|
|
218
|
+
print(f"Stats: {stats}")
|
|
219
|
+
|
|
220
|
+
# Archive/delete session
|
|
221
|
+
client.delete_session(session)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### 4. Model Override
|
|
225
|
+
|
|
226
|
+
Send messages using a specific model:
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
from olbrain import AgentClient
|
|
230
|
+
|
|
231
|
+
with AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key") as client:
|
|
232
|
+
session_id = client.create_session()
|
|
233
|
+
|
|
234
|
+
# Use specific model for this request
|
|
235
|
+
response = client.send_and_wait(
|
|
236
|
+
session_id,
|
|
237
|
+
"Solve this complex problem...",
|
|
238
|
+
model="gpt-4" # Or any other available model
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
print(f"Model used: {response.model_used}")
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### 5. Multiple Messages in Same Session
|
|
245
|
+
|
|
246
|
+
Maintain conversation context:
|
|
247
|
+
|
|
248
|
+
```python
|
|
249
|
+
from olbrain import AgentClient
|
|
250
|
+
|
|
251
|
+
with AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key") as client:
|
|
252
|
+
session_id = client.create_session(title="Q&A Session")
|
|
253
|
+
|
|
254
|
+
# First message
|
|
255
|
+
response1 = client.send_and_wait(session_id, "What is machine learning?")
|
|
256
|
+
print(f"Response 1: {response1.text}\n")
|
|
257
|
+
|
|
258
|
+
# Follow-up - agent remembers context
|
|
259
|
+
response2 = client.send_and_wait(session_id, "Can you give me a practical example?")
|
|
260
|
+
print(f"Response 2: {response2.text}\n")
|
|
261
|
+
|
|
262
|
+
# Another follow-up
|
|
263
|
+
response3 = client.send_and_wait(session_id, "What are the main algorithms?")
|
|
264
|
+
print(f"Response 3: {response3.text}")
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### 6. Error Handling
|
|
268
|
+
|
|
269
|
+
Handle different error scenarios gracefully:
|
|
270
|
+
|
|
271
|
+
```python
|
|
272
|
+
from olbrain import AgentClient
|
|
273
|
+
from olbrain.exceptions import (
|
|
274
|
+
AuthenticationError,
|
|
275
|
+
SessionNotFoundError,
|
|
276
|
+
RateLimitError,
|
|
277
|
+
NetworkError,
|
|
278
|
+
OlbrainError
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
try:
|
|
282
|
+
client = AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key")
|
|
283
|
+
session_id = client.create_session()
|
|
284
|
+
response = client.send_and_wait(session_id, "Hello")
|
|
285
|
+
|
|
286
|
+
except AuthenticationError:
|
|
287
|
+
print("❌ Authentication failed - check your API key")
|
|
288
|
+
|
|
289
|
+
except SessionNotFoundError:
|
|
290
|
+
print("❌ Session not found - create a new session")
|
|
291
|
+
|
|
292
|
+
except RateLimitError as e:
|
|
293
|
+
print(f"⏱️ Rate limited - retry after {e.retry_after} seconds")
|
|
294
|
+
|
|
295
|
+
except NetworkError as e:
|
|
296
|
+
print(f"🌐 Network error - check your connection: {e}")
|
|
297
|
+
|
|
298
|
+
except OlbrainError as e:
|
|
299
|
+
print(f"❌ SDK error: {e}")
|
|
300
|
+
|
|
301
|
+
finally:
|
|
302
|
+
if 'client' in locals():
|
|
303
|
+
client.close()
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## Configuration
|
|
307
|
+
|
|
308
|
+
### API Key
|
|
309
|
+
|
|
310
|
+
The SDK validates API keys. Valid prefixes are:
|
|
311
|
+
- `sk_live_` - Production keys
|
|
312
|
+
- `org_live_` - Organization keys
|
|
313
|
+
- `sk_` - Test keys
|
|
314
|
+
- `org_` - Test organization keys
|
|
315
|
+
|
|
316
|
+
```python
|
|
317
|
+
# Valid API key
|
|
318
|
+
client = AgentClient(
|
|
319
|
+
agent_id="your-agent-id",
|
|
320
|
+
api_key="sk_live_your_api_key_here"
|
|
321
|
+
)
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
### Environment Variables
|
|
325
|
+
|
|
326
|
+
```bash
|
|
327
|
+
export OLBRAIN_API_KEY="sk_live_your_api_key"
|
|
328
|
+
export OLBRAIN_AGENT_ID="your-agent-id"
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### Logging
|
|
332
|
+
|
|
333
|
+
Enable debug logging to troubleshoot issues:
|
|
334
|
+
|
|
335
|
+
```python
|
|
336
|
+
import logging
|
|
337
|
+
|
|
338
|
+
# Set SDK logging level
|
|
339
|
+
logging.basicConfig(
|
|
340
|
+
level=logging.DEBUG,
|
|
341
|
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
# Create client after setting up logging
|
|
345
|
+
client = AgentClient(agent_id="...", api_key="...")
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
## Troubleshooting
|
|
349
|
+
|
|
350
|
+
### Common Issues
|
|
351
|
+
|
|
352
|
+
**"Invalid API key" error**
|
|
353
|
+
- Ensure your API key starts with `sk_live_`, `org_live_`, `sk_`, or `org_`
|
|
354
|
+
- Check that you copied the key correctly (no extra spaces)
|
|
355
|
+
|
|
356
|
+
**"Stream connection failed" error**
|
|
357
|
+
- Check that your agent ID is correct
|
|
358
|
+
- Verify you have a stable internet connection
|
|
359
|
+
- Try restarting the client
|
|
360
|
+
|
|
361
|
+
**"Session not found" error**
|
|
362
|
+
- Verify the session ID is correct
|
|
363
|
+
- Check that the session hasn't expired
|
|
364
|
+
- Create a new session if needed
|
|
365
|
+
|
|
366
|
+
**Slow responses**
|
|
367
|
+
- Check network latency
|
|
368
|
+
- Increase the timeout parameter in methods
|
|
369
|
+
- Verify the agent is responsive via the dashboard
|
|
370
|
+
|
|
371
|
+
### Local Development Testing
|
|
372
|
+
|
|
373
|
+
Test the SDK locally with your credentials:
|
|
374
|
+
|
|
375
|
+
```bash
|
|
376
|
+
# Install in development mode
|
|
377
|
+
pip install -e .
|
|
378
|
+
|
|
379
|
+
# Run the test script
|
|
380
|
+
python test_agent.py
|
|
381
|
+
|
|
382
|
+
# Or run unit tests
|
|
383
|
+
pytest tests/ -v
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
The test will:
|
|
387
|
+
1. Initialize the SDK client
|
|
388
|
+
2. Create a chat session
|
|
389
|
+
3. Test real-time message streaming
|
|
390
|
+
4. Accept interactive input (type 'exit' to quit)
|
|
391
|
+
|
|
392
|
+
## API Reference
|
|
393
|
+
|
|
394
|
+
### AgentClient Methods
|
|
395
|
+
|
|
396
|
+
#### Session Management
|
|
397
|
+
|
|
398
|
+
| Method | Parameters | Returns | Description |
|
|
399
|
+
|--------|-----------|---------|-------------|
|
|
400
|
+
| `create_session()` | `title`, `user_id`, `metadata`, `on_message`, `mode`, `description` | `str` (session_id) | Create a new chat session |
|
|
401
|
+
| `get_session(session_id)` | `session_id` | `SessionInfo` | Get session details |
|
|
402
|
+
| `update_session(session_id, ...)` | `session_id`, `title`, `metadata`, `status` | `SessionInfo` | Update session properties |
|
|
403
|
+
| `delete_session(session_id)` | `session_id` | `Dict` | Archive/delete a session |
|
|
404
|
+
| `get_messages(session_id, ...)` | `session_id`, `limit`, `offset` | `Dict` | Get message history |
|
|
405
|
+
| `get_session_stats(session_id)` | `session_id` | `Dict` | Get session statistics |
|
|
406
|
+
|
|
407
|
+
#### Message Operations
|
|
408
|
+
|
|
409
|
+
| Method | Parameters | Returns | Description |
|
|
410
|
+
|--------|-----------|---------|-------------|
|
|
411
|
+
| `send(session_id, message, ...)` | `session_id`, `message`, `user_id`, `metadata`, `model` | `Dict` | Send message (async with callback) |
|
|
412
|
+
| `send_and_wait(session_id, message, ...)` | `session_id`, `message`, `user_id`, `metadata`, `model`, `timeout` | `ChatResponse` | Send and wait for response |
|
|
413
|
+
| `listen(session_id, on_message)` | `session_id`, `on_message` | - | Start listening to session |
|
|
414
|
+
| `run()` | - | - | Block and process streaming messages |
|
|
415
|
+
| `close()` | - | - | Close client and cleanup resources |
|
|
416
|
+
|
|
417
|
+
### Response Objects
|
|
418
|
+
|
|
419
|
+
**ChatResponse**
|
|
420
|
+
```python
|
|
421
|
+
response = client.send_and_wait(session_id, "Hello")
|
|
422
|
+
|
|
423
|
+
# Access response data
|
|
424
|
+
response.text # str - The agent's response
|
|
425
|
+
response.success # bool - Whether request succeeded
|
|
426
|
+
response.token_usage # TokenUsage - Token statistics
|
|
427
|
+
response.model_used # str - Model that generated response
|
|
428
|
+
response.error # str - Error message (if any)
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
**SessionInfo**
|
|
432
|
+
```python
|
|
433
|
+
session = client.get_session(session_id)
|
|
434
|
+
|
|
435
|
+
session.session_id # str - Session identifier
|
|
436
|
+
session.title # str - Session title
|
|
437
|
+
session.message_count # int - Number of messages
|
|
438
|
+
session.metadata # dict - Custom metadata
|
|
439
|
+
session.status # str - 'active' or 'archived'
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
**TokenUsage**
|
|
443
|
+
```python
|
|
444
|
+
tokens = response.token_usage
|
|
445
|
+
|
|
446
|
+
tokens.prompt_tokens # int - Input tokens
|
|
447
|
+
tokens.completion_tokens # int - Output tokens
|
|
448
|
+
tokens.total_tokens # int - Total tokens used
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
### Exceptions
|
|
452
|
+
|
|
453
|
+
| Exception | Description |
|
|
454
|
+
|-----------|-------------|
|
|
455
|
+
| `OlbrainError` | Base exception |
|
|
456
|
+
| `AuthenticationError` | Invalid API key |
|
|
457
|
+
| `SessionNotFoundError` | Session not found |
|
|
458
|
+
| `RateLimitError` | Rate limit exceeded |
|
|
459
|
+
| `NetworkError` | Connection issues |
|
|
460
|
+
| `ValidationError` | Invalid input |
|
|
461
|
+
| `StreamingError` | Streaming error |
|
|
462
|
+
|
|
463
|
+
## Examples
|
|
464
|
+
|
|
465
|
+
Complete examples are available in the [examples/](examples/) directory:
|
|
466
|
+
|
|
467
|
+
- **`basic_usage.py`** - Core SDK features and basic messaging
|
|
468
|
+
- **`session_management.py`** - Full session lifecycle (create, retrieve, update, delete)
|
|
469
|
+
- **`streaming_responses.py`** - Real-time message streaming with callbacks
|
|
470
|
+
- **`error_handling.py`** - Error handling patterns and best practices
|
|
471
|
+
|
|
472
|
+
Run any example:
|
|
473
|
+
```bash
|
|
474
|
+
python examples/basic_usage.py
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
## Best Practices
|
|
478
|
+
|
|
479
|
+
1. **Use Context Manager** - Always use `with` statements for automatic cleanup:
|
|
480
|
+
```python
|
|
481
|
+
with AgentClient(agent_id="...", api_key="...") as client:
|
|
482
|
+
# Your code here
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
2. **Handle Errors** - Always wrap API calls in try-except blocks
|
|
486
|
+
```python
|
|
487
|
+
try:
|
|
488
|
+
response = client.send_and_wait(session_id, message)
|
|
489
|
+
except OlbrainError as e:
|
|
490
|
+
# Handle error appropriately
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
3. **Reuse Sessions** - Keep session IDs to maintain conversation context
|
|
494
|
+
```python
|
|
495
|
+
session_id = client.create_session(title="Customer Chat")
|
|
496
|
+
# Reuse session_id for multiple messages
|
|
497
|
+
response1 = client.send_and_wait(session_id, "First question")
|
|
498
|
+
response2 = client.send_and_wait(session_id, "Follow-up question")
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
4. **Monitor Token Usage** - Track tokens for cost management
|
|
502
|
+
```python
|
|
503
|
+
response = client.send_and_wait(session_id, message)
|
|
504
|
+
print(f"Tokens: {response.token_usage.total_tokens}")
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
5. **Clean Up** - Always close client when done
|
|
508
|
+
```python
|
|
509
|
+
client.close()
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
## Support
|
|
513
|
+
|
|
514
|
+
- **Issues**: [GitHub Issues](https://github.com/Olbrain/olbrain-sdk/issues)
|
|
515
|
+
- **Documentation**: See [examples/](examples/) directory
|
|
516
|
+
- **API Status**: Check [status page](https://olbrain.com/status)
|
|
517
|
+
|
|
518
|
+
## License
|
|
519
|
+
|
|
520
|
+
MIT License - see [LICENSE](LICENSE)
|
|
521
|
+
|
|
522
|
+
## Version
|
|
523
|
+
|
|
524
|
+
Current version: **0.2.0**
|
|
525
|
+
|
|
526
|
+
## Links
|
|
527
|
+
|
|
528
|
+
- [PyPI Package](https://pypi.org/project/olbrain-python-sdk/)
|
|
529
|
+
- [GitHub Repository](https://github.com/Olbrain/olbrain-sdk)
|
|
530
|
+
- [Report Issues](https://github.com/Olbrain/olbrain-sdk/issues)
|
|
531
|
+
- [Olbrain Website](https://olbrain.com)
|