olbrain-python-sdk 0.2.0__tar.gz → 0.2.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.2.2/PKG-INFO +251 -0
- olbrain_python_sdk-0.2.2/README.md +203 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/examples/advanced_features.py +20 -20
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/examples/basic_usage.py +3 -3
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/examples/error_handling.py +21 -21
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/examples/session_management.py +3 -3
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/examples/streaming_responses.py +13 -13
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain/__init__.py +2 -2
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain/client.py +9 -6
- olbrain_python_sdk-0.2.2/olbrain_python_sdk.egg-info/PKG-INFO +251 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/setup.py +1 -1
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/test_agent.py +2 -2
- olbrain_python_sdk-0.2.2/tests/test_client.py +131 -0
- olbrain_python_sdk-0.2.0/PKG-INFO +0 -357
- olbrain_python_sdk-0.2.0/README.md +0 -309
- olbrain_python_sdk-0.2.0/olbrain_python_sdk.egg-info/PKG-INFO +0 -357
- olbrain_python_sdk-0.2.0/tests/test_client.py +0 -91
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/.gitignore +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/LICENSE +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/MANIFEST.in +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain/exceptions.py +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain/session.py +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain/streaming.py +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain/utils.py +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain_python_sdk.egg-info/SOURCES.txt +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain_python_sdk.egg-info/dependency_links.txt +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain_python_sdk.egg-info/not-zip-safe +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain_python_sdk.egg-info/requires.txt +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/olbrain_python_sdk.egg-info/top_level.txt +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/pyproject.toml +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/requirements.txt +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/setup.cfg +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/tests/__init__.py +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/tests/test_exceptions.py +0 -0
- {olbrain_python_sdk-0.2.0 → olbrain_python_sdk-0.2.2}/tests/test_session.py +0 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: olbrain-python-sdk
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Summary: Official Python SDK for Olbrain AI agents
|
|
5
|
+
Home-page: https://github.com/Olbrain/olbrain-python-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-python-sdk
|
|
10
|
+
Project-URL: Bug Reports, https://github.com/Olbrain/olbrain-python-sdk/issues
|
|
11
|
+
Project-URL: Source, https://github.com/Olbrain/olbrain-python-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
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install olbrain-python-sdk
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Quick Start
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from olbrain import AgentClient
|
|
67
|
+
|
|
68
|
+
# Initialize client
|
|
69
|
+
client = AgentClient(
|
|
70
|
+
agent_id="your-agent-id",
|
|
71
|
+
api_key="sk_live_your_api_key"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# Create a session and send a message
|
|
75
|
+
session_id = client.create_session(title="My Chat")
|
|
76
|
+
response = client.send_and_wait(session_id, "Hello!")
|
|
77
|
+
|
|
78
|
+
print(response.text)
|
|
79
|
+
client.close()
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Features
|
|
83
|
+
|
|
84
|
+
- **Simple API** - Just `agent_id` and `api_key` to get started
|
|
85
|
+
- **Session Management** - Create, update, archive sessions with metadata
|
|
86
|
+
- **Sync & Streaming** - Both request-response and real-time streaming
|
|
87
|
+
- **Token Tracking** - Monitor usage and costs per request
|
|
88
|
+
- **Model Override** - Switch models per-message
|
|
89
|
+
- **Error Handling** - Comprehensive exception hierarchy
|
|
90
|
+
|
|
91
|
+
## Usage
|
|
92
|
+
|
|
93
|
+
### Synchronous Messaging
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from olbrain import AgentClient
|
|
97
|
+
|
|
98
|
+
with AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key") as client:
|
|
99
|
+
session_id = client.create_session()
|
|
100
|
+
response = client.send_and_wait(session_id, "What is Python?")
|
|
101
|
+
|
|
102
|
+
print(response.text)
|
|
103
|
+
print(f"Tokens: {response.token_usage.total_tokens}")
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Real-Time Streaming
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from olbrain import AgentClient
|
|
110
|
+
|
|
111
|
+
client = AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key")
|
|
112
|
+
|
|
113
|
+
def on_message(msg):
|
|
114
|
+
print(f"[{msg['role']}]: {msg['content']}")
|
|
115
|
+
|
|
116
|
+
session_id = client.create_session(on_message=on_message)
|
|
117
|
+
client.send(session_id, "Tell me a story")
|
|
118
|
+
client.run() # Blocks and processes messages
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Session Management
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
# Create session with metadata
|
|
125
|
+
session_id = client.create_session(
|
|
126
|
+
title="Support Chat",
|
|
127
|
+
user_id="user-123",
|
|
128
|
+
metadata={"source": "web"},
|
|
129
|
+
mode="production"
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
# Get session info
|
|
133
|
+
info = client.get_session(session_id)
|
|
134
|
+
print(f"Messages: {info.message_count}")
|
|
135
|
+
|
|
136
|
+
# Get message history
|
|
137
|
+
messages = client.get_messages(session_id, limit=20)
|
|
138
|
+
|
|
139
|
+
# Archive session
|
|
140
|
+
client.delete_session(session_id)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Model Override
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
response = client.send_and_wait(
|
|
147
|
+
session_id,
|
|
148
|
+
"Complex question here",
|
|
149
|
+
model="gpt-4" # Override default model
|
|
150
|
+
)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Error Handling
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from olbrain import AgentClient
|
|
157
|
+
from olbrain.exceptions import (
|
|
158
|
+
AuthenticationError,
|
|
159
|
+
SessionNotFoundError,
|
|
160
|
+
RateLimitError,
|
|
161
|
+
OlbrainError
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
try:
|
|
165
|
+
client = AgentClient(agent_id="...", api_key="...")
|
|
166
|
+
response = client.send_and_wait(session_id, "Hello")
|
|
167
|
+
except AuthenticationError:
|
|
168
|
+
print("Invalid API key")
|
|
169
|
+
except RateLimitError as e:
|
|
170
|
+
print(f"Rate limited. Retry after {e.retry_after}s")
|
|
171
|
+
except OlbrainError as e:
|
|
172
|
+
print(f"Error: {e}")
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Configuration
|
|
176
|
+
|
|
177
|
+
### Environment Variables
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
export OLBRAIN_API_KEY="sk_live_your_api_key"
|
|
181
|
+
export OLBRAIN_AGENT_ID="your-agent-id"
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Logging
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
import logging
|
|
188
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## API Reference
|
|
192
|
+
|
|
193
|
+
### AgentClient
|
|
194
|
+
|
|
195
|
+
| Method | Description |
|
|
196
|
+
|--------|-------------|
|
|
197
|
+
| `create_session()` | Create a new chat session |
|
|
198
|
+
| `send(session_id, message)` | Send message (async, use callback) |
|
|
199
|
+
| `send_and_wait(session_id, message)` | Send message and wait for response |
|
|
200
|
+
| `get_session(session_id)` | Get session details |
|
|
201
|
+
| `update_session(session_id, ...)` | Update session title/metadata |
|
|
202
|
+
| `delete_session(session_id)` | Archive a session |
|
|
203
|
+
| `get_messages(session_id)` | Get message history |
|
|
204
|
+
| `get_session_stats(session_id)` | Get token usage stats |
|
|
205
|
+
| `close()` | Clean up resources |
|
|
206
|
+
|
|
207
|
+
### Response Objects
|
|
208
|
+
|
|
209
|
+
**ChatResponse**
|
|
210
|
+
- `text` - Response text
|
|
211
|
+
- `success` - Success status
|
|
212
|
+
- `token_usage` - TokenUsage object
|
|
213
|
+
- `model_used` - Model that generated response
|
|
214
|
+
|
|
215
|
+
**TokenUsage**
|
|
216
|
+
- `prompt_tokens` - Input tokens
|
|
217
|
+
- `completion_tokens` - Output tokens
|
|
218
|
+
- `total_tokens` - Total tokens
|
|
219
|
+
- `cost` - Cost in USD
|
|
220
|
+
|
|
221
|
+
### Exceptions
|
|
222
|
+
|
|
223
|
+
| Exception | Description |
|
|
224
|
+
|-----------|-------------|
|
|
225
|
+
| `OlbrainError` | Base exception |
|
|
226
|
+
| `AuthenticationError` | Invalid API key |
|
|
227
|
+
| `SessionNotFoundError` | Session not found |
|
|
228
|
+
| `RateLimitError` | Rate limit exceeded |
|
|
229
|
+
| `NetworkError` | Connection issues |
|
|
230
|
+
| `ValidationError` | Invalid input |
|
|
231
|
+
| `StreamingError` | Streaming error |
|
|
232
|
+
|
|
233
|
+
## Examples
|
|
234
|
+
|
|
235
|
+
See the [examples/](examples/) directory:
|
|
236
|
+
|
|
237
|
+
- `basic_usage.py` - Core SDK features
|
|
238
|
+
- `session_management.py` - Session CRUD operations
|
|
239
|
+
- `streaming_responses.py` - Real-time streaming
|
|
240
|
+
- `error_handling.py` - Error handling patterns
|
|
241
|
+
- `advanced_features.py` - Advanced usage
|
|
242
|
+
|
|
243
|
+
## License
|
|
244
|
+
|
|
245
|
+
MIT License - see [LICENSE](LICENSE)
|
|
246
|
+
|
|
247
|
+
## Links
|
|
248
|
+
|
|
249
|
+
- [PyPI](https://pypi.org/project/olbrain-python-sdk/)
|
|
250
|
+
- [GitHub](https://github.com/Olbrain/olbrain-python-sdk)
|
|
251
|
+
- [Issues](https://github.com/Olbrain/olbrain-python-sdk/issues)
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# Olbrain Python SDK
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/olbrain-python-sdk/)
|
|
4
|
+
[](https://pypi.org/project/olbrain-python-sdk/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
Official Python SDK for integrating Olbrain AI agents into your applications.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install olbrain-python-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from olbrain import AgentClient
|
|
19
|
+
|
|
20
|
+
# Initialize client
|
|
21
|
+
client = AgentClient(
|
|
22
|
+
agent_id="your-agent-id",
|
|
23
|
+
api_key="sk_live_your_api_key"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
# Create a session and send a message
|
|
27
|
+
session_id = client.create_session(title="My Chat")
|
|
28
|
+
response = client.send_and_wait(session_id, "Hello!")
|
|
29
|
+
|
|
30
|
+
print(response.text)
|
|
31
|
+
client.close()
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Features
|
|
35
|
+
|
|
36
|
+
- **Simple API** - Just `agent_id` and `api_key` to get started
|
|
37
|
+
- **Session Management** - Create, update, archive sessions with metadata
|
|
38
|
+
- **Sync & Streaming** - Both request-response and real-time streaming
|
|
39
|
+
- **Token Tracking** - Monitor usage and costs per request
|
|
40
|
+
- **Model Override** - Switch models per-message
|
|
41
|
+
- **Error Handling** - Comprehensive exception hierarchy
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
### Synchronous Messaging
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from olbrain import AgentClient
|
|
49
|
+
|
|
50
|
+
with AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key") as client:
|
|
51
|
+
session_id = client.create_session()
|
|
52
|
+
response = client.send_and_wait(session_id, "What is Python?")
|
|
53
|
+
|
|
54
|
+
print(response.text)
|
|
55
|
+
print(f"Tokens: {response.token_usage.total_tokens}")
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Real-Time Streaming
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from olbrain import AgentClient
|
|
62
|
+
|
|
63
|
+
client = AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key")
|
|
64
|
+
|
|
65
|
+
def on_message(msg):
|
|
66
|
+
print(f"[{msg['role']}]: {msg['content']}")
|
|
67
|
+
|
|
68
|
+
session_id = client.create_session(on_message=on_message)
|
|
69
|
+
client.send(session_id, "Tell me a story")
|
|
70
|
+
client.run() # Blocks and processes messages
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Session Management
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
# Create session with metadata
|
|
77
|
+
session_id = client.create_session(
|
|
78
|
+
title="Support Chat",
|
|
79
|
+
user_id="user-123",
|
|
80
|
+
metadata={"source": "web"},
|
|
81
|
+
mode="production"
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
# Get session info
|
|
85
|
+
info = client.get_session(session_id)
|
|
86
|
+
print(f"Messages: {info.message_count}")
|
|
87
|
+
|
|
88
|
+
# Get message history
|
|
89
|
+
messages = client.get_messages(session_id, limit=20)
|
|
90
|
+
|
|
91
|
+
# Archive session
|
|
92
|
+
client.delete_session(session_id)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Model Override
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
response = client.send_and_wait(
|
|
99
|
+
session_id,
|
|
100
|
+
"Complex question here",
|
|
101
|
+
model="gpt-4" # Override default model
|
|
102
|
+
)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Error Handling
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from olbrain import AgentClient
|
|
109
|
+
from olbrain.exceptions import (
|
|
110
|
+
AuthenticationError,
|
|
111
|
+
SessionNotFoundError,
|
|
112
|
+
RateLimitError,
|
|
113
|
+
OlbrainError
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
client = AgentClient(agent_id="...", api_key="...")
|
|
118
|
+
response = client.send_and_wait(session_id, "Hello")
|
|
119
|
+
except AuthenticationError:
|
|
120
|
+
print("Invalid API key")
|
|
121
|
+
except RateLimitError as e:
|
|
122
|
+
print(f"Rate limited. Retry after {e.retry_after}s")
|
|
123
|
+
except OlbrainError as e:
|
|
124
|
+
print(f"Error: {e}")
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Configuration
|
|
128
|
+
|
|
129
|
+
### Environment Variables
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
export OLBRAIN_API_KEY="sk_live_your_api_key"
|
|
133
|
+
export OLBRAIN_AGENT_ID="your-agent-id"
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Logging
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
import logging
|
|
140
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## API Reference
|
|
144
|
+
|
|
145
|
+
### AgentClient
|
|
146
|
+
|
|
147
|
+
| Method | Description |
|
|
148
|
+
|--------|-------------|
|
|
149
|
+
| `create_session()` | Create a new chat session |
|
|
150
|
+
| `send(session_id, message)` | Send message (async, use callback) |
|
|
151
|
+
| `send_and_wait(session_id, message)` | Send message and wait for response |
|
|
152
|
+
| `get_session(session_id)` | Get session details |
|
|
153
|
+
| `update_session(session_id, ...)` | Update session title/metadata |
|
|
154
|
+
| `delete_session(session_id)` | Archive a session |
|
|
155
|
+
| `get_messages(session_id)` | Get message history |
|
|
156
|
+
| `get_session_stats(session_id)` | Get token usage stats |
|
|
157
|
+
| `close()` | Clean up resources |
|
|
158
|
+
|
|
159
|
+
### Response Objects
|
|
160
|
+
|
|
161
|
+
**ChatResponse**
|
|
162
|
+
- `text` - Response text
|
|
163
|
+
- `success` - Success status
|
|
164
|
+
- `token_usage` - TokenUsage object
|
|
165
|
+
- `model_used` - Model that generated response
|
|
166
|
+
|
|
167
|
+
**TokenUsage**
|
|
168
|
+
- `prompt_tokens` - Input tokens
|
|
169
|
+
- `completion_tokens` - Output tokens
|
|
170
|
+
- `total_tokens` - Total tokens
|
|
171
|
+
- `cost` - Cost in USD
|
|
172
|
+
|
|
173
|
+
### Exceptions
|
|
174
|
+
|
|
175
|
+
| Exception | Description |
|
|
176
|
+
|-----------|-------------|
|
|
177
|
+
| `OlbrainError` | Base exception |
|
|
178
|
+
| `AuthenticationError` | Invalid API key |
|
|
179
|
+
| `SessionNotFoundError` | Session not found |
|
|
180
|
+
| `RateLimitError` | Rate limit exceeded |
|
|
181
|
+
| `NetworkError` | Connection issues |
|
|
182
|
+
| `ValidationError` | Invalid input |
|
|
183
|
+
| `StreamingError` | Streaming error |
|
|
184
|
+
|
|
185
|
+
## Examples
|
|
186
|
+
|
|
187
|
+
See the [examples/](examples/) directory:
|
|
188
|
+
|
|
189
|
+
- `basic_usage.py` - Core SDK features
|
|
190
|
+
- `session_management.py` - Session CRUD operations
|
|
191
|
+
- `streaming_responses.py` - Real-time streaming
|
|
192
|
+
- `error_handling.py` - Error handling patterns
|
|
193
|
+
- `advanced_features.py` - Advanced usage
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
MIT License - see [LICENSE](LICENSE)
|
|
198
|
+
|
|
199
|
+
## Links
|
|
200
|
+
|
|
201
|
+
- [PyPI](https://pypi.org/project/olbrain-python-sdk/)
|
|
202
|
+
- [GitHub](https://github.com/Olbrain/olbrain-python-sdk)
|
|
203
|
+
- [Issues](https://github.com/Olbrain/olbrain-python-sdk/issues)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"""
|
|
2
|
-
Advanced features example for the
|
|
2
|
+
Advanced features example for the Olbrain Python SDK.
|
|
3
3
|
|
|
4
4
|
This example demonstrates:
|
|
5
5
|
- Context manager usage
|
|
@@ -14,8 +14,8 @@ import time
|
|
|
14
14
|
import logging
|
|
15
15
|
from typing import Dict, Any
|
|
16
16
|
from contextlib import contextmanager
|
|
17
|
-
from
|
|
18
|
-
from
|
|
17
|
+
from olbrain import AgentClient
|
|
18
|
+
from olbrain.exceptions import OlbrainError
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
# Configure logging for performance monitoring
|
|
@@ -32,7 +32,7 @@ def timer(operation_name: str):
|
|
|
32
32
|
logger.info(f"{operation_name} took {end_time - start_time:.2f} seconds")
|
|
33
33
|
|
|
34
34
|
|
|
35
|
-
class
|
|
35
|
+
class AdvancedOlbrainClient:
|
|
36
36
|
"""Extended client with additional features."""
|
|
37
37
|
|
|
38
38
|
def __init__(self, agent_id: str, api_key: str, **kwargs):
|
|
@@ -100,15 +100,15 @@ def demonstrate_context_manager_usage():
|
|
|
100
100
|
print("🎯 Context Manager Usage")
|
|
101
101
|
print("=" * 60)
|
|
102
102
|
|
|
103
|
-
api_key = os.getenv("
|
|
104
|
-
agent_id = os.getenv("
|
|
103
|
+
api_key = os.getenv("OLBRAIN_API_KEY", "demo-key")
|
|
104
|
+
agent_id = os.getenv("OLBRAIN_AGENT_ID", "demo-agent")
|
|
105
105
|
|
|
106
106
|
if api_key == "demo-key":
|
|
107
107
|
print("⚠️ Skipping context manager demo - no valid API key")
|
|
108
108
|
return
|
|
109
109
|
|
|
110
110
|
# Using the client as a context manager ensures proper cleanup
|
|
111
|
-
with
|
|
111
|
+
with AgentClient(api_key=api_key) as client:
|
|
112
112
|
print("✅ Client initialized in context manager")
|
|
113
113
|
|
|
114
114
|
try:
|
|
@@ -135,7 +135,7 @@ def demonstrate_custom_configuration():
|
|
|
135
135
|
print("⚙️ Custom Configuration")
|
|
136
136
|
print("=" * 60)
|
|
137
137
|
|
|
138
|
-
api_key = os.getenv("
|
|
138
|
+
api_key = os.getenv("OLBRAIN_API_KEY", "demo-key")
|
|
139
139
|
|
|
140
140
|
# Configuration for high-performance usage
|
|
141
141
|
high_perf_config = {
|
|
@@ -161,11 +161,11 @@ def demonstrate_custom_configuration():
|
|
|
161
161
|
# Demonstrate different configurations
|
|
162
162
|
try:
|
|
163
163
|
# Test with quick config
|
|
164
|
-
with
|
|
164
|
+
with AgentClient(api_key=api_key, **quick_config) as quick_client:
|
|
165
165
|
print("\n✅ Quick client initialized")
|
|
166
166
|
|
|
167
167
|
# Test with high-performance config
|
|
168
|
-
with
|
|
168
|
+
with AgentClient(api_key=api_key, **high_perf_config) as hp_client:
|
|
169
169
|
print("✅ High-performance client initialized")
|
|
170
170
|
|
|
171
171
|
except Exception as e:
|
|
@@ -180,15 +180,15 @@ def demonstrate_metadata_handling():
|
|
|
180
180
|
print("📊 Metadata Handling")
|
|
181
181
|
print("=" * 60)
|
|
182
182
|
|
|
183
|
-
api_key = os.getenv("
|
|
184
|
-
agent_id = os.getenv("
|
|
183
|
+
api_key = os.getenv("OLBRAIN_API_KEY", "demo-key")
|
|
184
|
+
agent_id = os.getenv("OLBRAIN_AGENT_ID", "demo-agent")
|
|
185
185
|
|
|
186
186
|
if api_key == "demo-key":
|
|
187
187
|
print("⚠️ Skipping metadata demo - no valid API key")
|
|
188
188
|
return
|
|
189
189
|
|
|
190
190
|
try:
|
|
191
|
-
with
|
|
191
|
+
with AgentClient(api_key=api_key) as client:
|
|
192
192
|
agent = client.get_agent(agent_id)
|
|
193
193
|
|
|
194
194
|
# Create session with rich metadata
|
|
@@ -279,8 +279,8 @@ def demonstrate_performance_monitoring():
|
|
|
279
279
|
print("📈 Performance Monitoring")
|
|
280
280
|
print("=" * 60)
|
|
281
281
|
|
|
282
|
-
api_key = os.getenv("
|
|
283
|
-
agent_id = os.getenv("
|
|
282
|
+
api_key = os.getenv("OLBRAIN_API_KEY", "demo-key")
|
|
283
|
+
agent_id = os.getenv("OLBRAIN_AGENT_ID", "demo-agent")
|
|
284
284
|
|
|
285
285
|
if api_key == "demo-key":
|
|
286
286
|
print("⚠️ Skipping performance monitoring demo - no valid API key")
|
|
@@ -288,7 +288,7 @@ def demonstrate_performance_monitoring():
|
|
|
288
288
|
|
|
289
289
|
try:
|
|
290
290
|
# Use our extended client with metrics
|
|
291
|
-
extended_client =
|
|
291
|
+
extended_client = AdvancedAgentClient(api_key=api_key)
|
|
292
292
|
agent = extended_client.get_agent_with_metrics(agent_id)
|
|
293
293
|
|
|
294
294
|
print("🚀 Running performance test with metrics collection...")
|
|
@@ -351,15 +351,15 @@ def demonstrate_advanced_session_management():
|
|
|
351
351
|
print("💼 Advanced Session Management")
|
|
352
352
|
print("=" * 60)
|
|
353
353
|
|
|
354
|
-
api_key = os.getenv("
|
|
355
|
-
agent_id = os.getenv("
|
|
354
|
+
api_key = os.getenv("OLBRAIN_API_KEY", "demo-key")
|
|
355
|
+
agent_id = os.getenv("OLBRAIN_AGENT_ID", "demo-agent")
|
|
356
356
|
|
|
357
357
|
if api_key == "demo-key":
|
|
358
358
|
print("⚠️ Skipping advanced session demo - no valid API key")
|
|
359
359
|
return
|
|
360
360
|
|
|
361
361
|
try:
|
|
362
|
-
with
|
|
362
|
+
with AgentClient(api_key=api_key) as client:
|
|
363
363
|
agent = client.get_agent(agent_id)
|
|
364
364
|
|
|
365
365
|
# Create multiple sessions with different purposes
|
|
@@ -453,7 +453,7 @@ def demonstrate_advanced_session_management():
|
|
|
453
453
|
|
|
454
454
|
def main():
|
|
455
455
|
"""Run all advanced feature demonstrations."""
|
|
456
|
-
print("🚀
|
|
456
|
+
print("🚀 Olbrain SDK Advanced Features Examples")
|
|
457
457
|
print("This demo showcases advanced usage patterns and features.\n")
|
|
458
458
|
|
|
459
459
|
demonstrate_context_manager_usage()
|
|
@@ -23,16 +23,16 @@ def main():
|
|
|
23
23
|
"""Basic usage example."""
|
|
24
24
|
|
|
25
25
|
# Get credentials from environment variables
|
|
26
|
-
api_key = os.getenv("OLBRAIN_API_KEY", "
|
|
26
|
+
api_key = os.getenv("OLBRAIN_API_KEY", "sk_live_your-api-key-here")
|
|
27
27
|
agent_id = os.getenv("OLBRAIN_AGENT_ID", "your-agent-id-here")
|
|
28
28
|
# Optional: custom agent URL (auto-constructed from agent_id if not provided)
|
|
29
29
|
agent_url = os.getenv("OLBRAIN_AGENT_URL")
|
|
30
30
|
|
|
31
|
-
if api_key == "
|
|
31
|
+
if api_key == "sk_live_your-api-key-here" or agent_id == "your-agent-id-here":
|
|
32
32
|
print("Please set OLBRAIN_API_KEY and OLBRAIN_AGENT_ID environment variables")
|
|
33
33
|
print()
|
|
34
34
|
print("Example:")
|
|
35
|
-
print(" export OLBRAIN_API_KEY=
|
|
35
|
+
print(" export OLBRAIN_API_KEY=sk_live_your_key_here")
|
|
36
36
|
print(" export OLBRAIN_AGENT_ID=your-agent-id")
|
|
37
37
|
return
|
|
38
38
|
|