chuk-ai-session-manager 0.1.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.
- chuk_ai_session_manager-0.1.1/PKG-INFO +501 -0
- chuk_ai_session_manager-0.1.1/README.md +479 -0
- chuk_ai_session_manager-0.1.1/pyproject.toml +62 -0
- chuk_ai_session_manager-0.1.1/setup.cfg +4 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/__init__.py +57 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/exceptions.py +129 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/infinite_conversation.py +316 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/models/__init__.py +44 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/models/event_source.py +8 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/models/event_type.py +9 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/models/session.py +316 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/models/session_event.py +166 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/models/session_metadata.py +37 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/models/session_run.py +115 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/models/token_usage.py +316 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/sample_tools.py +194 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/session_aware_tool_processor.py +178 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/session_prompt_builder.py +474 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/storage/__init__.py +44 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/storage/base.py +50 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/storage/providers/__init__.py +0 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/storage/providers/file.py +348 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/storage/providers/memory.py +96 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager/storage/providers/redis.py +295 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager.egg-info/PKG-INFO +501 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager.egg-info/SOURCES.txt +31 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager.egg-info/dependency_links.txt +1 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager.egg-info/requires.txt +19 -0
- chuk_ai_session_manager-0.1.1/src/chuk_ai_session_manager.egg-info/top_level.txt +1 -0
- chuk_ai_session_manager-0.1.1/tests/test_infinite_conversation.py +144 -0
- chuk_ai_session_manager-0.1.1/tests/test_infinite_conversation_advanced.py +170 -0
- chuk_ai_session_manager-0.1.1/tests/test_session_aware_tool_processor.py +111 -0
- chuk_ai_session_manager-0.1.1/tests/test_session_prompt_builder.py +57 -0
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: chuk-ai-session-manager
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Session manager for AI applications
|
|
5
|
+
Requires-Python: >=3.11
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: aiofiles>=24.1.0
|
|
8
|
+
Requires-Dist: chuk-tool-processor>=0.4.1
|
|
9
|
+
Requires-Dist: pydantic>=2.11.3
|
|
10
|
+
Provides-Extra: tiktoken
|
|
11
|
+
Requires-Dist: tiktoken>=0.9.0; extra == "tiktoken"
|
|
12
|
+
Provides-Extra: redis
|
|
13
|
+
Requires-Dist: redis>=4.0.0; extra == "redis"
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
16
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
17
|
+
Requires-Dist: redis>=4.0.0; extra == "dev"
|
|
18
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
19
|
+
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
20
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
21
|
+
Provides-Extra: full
|
|
22
|
+
|
|
23
|
+
# chuk session manager
|
|
24
|
+
|
|
25
|
+
[](https://www.python.org/downloads/)
|
|
26
|
+
[](https://opensource.org/licenses/MIT)
|
|
27
|
+
|
|
28
|
+
A production-ready, async-first session management system for AI applications, with robust support for conversations, tool calls, hierarchical relationships, and comprehensive observability.
|
|
29
|
+
|
|
30
|
+
## 🚀 Quick Install
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# Install with uv (recommended)
|
|
34
|
+
uv pip install chuk-ai-session-manager
|
|
35
|
+
|
|
36
|
+
# With Redis support
|
|
37
|
+
uv pip install chuk-ai-session-manager[redis]
|
|
38
|
+
|
|
39
|
+
# Full install with all dependencies
|
|
40
|
+
uv pip install chuk-ai-session-manager[full]
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## ✨ Key Features
|
|
44
|
+
|
|
45
|
+
- 🔄 **Fully Async**: Built from the ground up for non-blocking I/O and high concurrency
|
|
46
|
+
- 🗃️ **Multiple Storage Backends**: Choose from in-memory, file-based, or Redis storage
|
|
47
|
+
- 🌳 **Hierarchical Sessions**: Create parent-child relationships for complex workflows
|
|
48
|
+
- 📝 **Event Tracking**: Record all conversation interactions with complete audit trails
|
|
49
|
+
- 💰 **Token & Cost Tracking**: Real-time token counting and cost estimation across providers
|
|
50
|
+
- 🛠️ **Tool Integration**: Session-aware tool execution with caching and retry logic
|
|
51
|
+
- ♾️ **Infinite Conversations**: Automatic segmentation for conversations exceeding token limits
|
|
52
|
+
- 🔄 **Retry Patterns**: Built-in LLM cooperation and tool execution reliability
|
|
53
|
+
- 🤖 **OpenAI Integration**: Production-ready patterns with auto-discovery
|
|
54
|
+
- 📊 **Complete Observability**: Performance monitoring, error tracking, and analytics
|
|
55
|
+
|
|
56
|
+
## 🎯 Production Highlights
|
|
57
|
+
|
|
58
|
+
This isn't just a demo framework - it's designed for production AI applications with features like:
|
|
59
|
+
|
|
60
|
+
- **Real OpenAI Integration**: Tested with live GPT-4o-mini API calls
|
|
61
|
+
- **Concurrent Tool Execution**: Multiple tools executed in parallel (200ms for 3 tools)
|
|
62
|
+
- **Precise Cost Tracking**: Token usage down to fractions of a penny ($0.000845 for complex workflows)
|
|
63
|
+
- **Error Recovery**: Multi-layer retry patterns with complete failure tracking
|
|
64
|
+
- **Auto-Discovery**: Registry-based tool detection with zero manual configuration
|
|
65
|
+
- **Complete Audit Trails**: Every operation logged with parent-child relationships
|
|
66
|
+
|
|
67
|
+
## 🏃♂️ Quick Start
|
|
68
|
+
|
|
69
|
+
### Basic Session with Events
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
import asyncio
|
|
73
|
+
from chuk_ai_session_manager.models.session import Session
|
|
74
|
+
from chuk_ai_session_manager.models.session_event import SessionEvent
|
|
75
|
+
from chuk_ai_session_manager.models.event_source import EventSource
|
|
76
|
+
from chuk_ai_session_manager.storage import SessionStoreProvider, InMemorySessionStore
|
|
77
|
+
|
|
78
|
+
async def main():
|
|
79
|
+
# Set up storage
|
|
80
|
+
store = InMemorySessionStore()
|
|
81
|
+
SessionStoreProvider.set_store(store)
|
|
82
|
+
|
|
83
|
+
# Create a session
|
|
84
|
+
session = await Session.create()
|
|
85
|
+
|
|
86
|
+
# Add events with automatic token tracking
|
|
87
|
+
await session.add_event_and_save(await SessionEvent.create_with_tokens(
|
|
88
|
+
message="How do I calculate the area of a circle?",
|
|
89
|
+
prompt="How do I calculate the area of a circle?",
|
|
90
|
+
model="gpt-4o-mini",
|
|
91
|
+
source=EventSource.USER
|
|
92
|
+
))
|
|
93
|
+
|
|
94
|
+
await session.add_event_and_save(await SessionEvent.create_with_tokens(
|
|
95
|
+
message="The area of a circle is calculated using the formula: A = πr²",
|
|
96
|
+
prompt="How do I calculate the area of a circle?",
|
|
97
|
+
completion="The area of a circle is calculated using the formula: A = πr²",
|
|
98
|
+
model="gpt-4o-mini",
|
|
99
|
+
source=EventSource.LLM
|
|
100
|
+
))
|
|
101
|
+
|
|
102
|
+
# Print session info with cost tracking
|
|
103
|
+
print(f"Session ID: {session.id}")
|
|
104
|
+
print(f"Event count: {len(session.events)}")
|
|
105
|
+
print(f"Total tokens: {session.total_tokens}")
|
|
106
|
+
print(f"Estimated cost: ${session.total_cost:.6f}")
|
|
107
|
+
|
|
108
|
+
if __name__ == "__main__":
|
|
109
|
+
asyncio.run(main())
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### OpenAI Integration with Auto-Discovery
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
import asyncio
|
|
116
|
+
import json
|
|
117
|
+
from openai import AsyncOpenAI
|
|
118
|
+
from chuk_tool_processor.registry import initialize
|
|
119
|
+
from chuk_ai_session_manager.models.session import Session
|
|
120
|
+
from chuk_ai_session_manager.storage import SessionStoreProvider, InMemorySessionStore
|
|
121
|
+
|
|
122
|
+
# Import tools - auto-registers via decorators
|
|
123
|
+
from your_tools import sample_tools
|
|
124
|
+
|
|
125
|
+
async def openai_integration_demo():
|
|
126
|
+
# Setup
|
|
127
|
+
store = InMemorySessionStore()
|
|
128
|
+
SessionStoreProvider.set_store(store)
|
|
129
|
+
session = await Session.create()
|
|
130
|
+
|
|
131
|
+
# Auto-discover tools from registry
|
|
132
|
+
registry = await initialize()
|
|
133
|
+
tools_list = await registry.list_tools()
|
|
134
|
+
print(f"🔧 Auto-discovered {len(tools_list)} tools")
|
|
135
|
+
|
|
136
|
+
# Generate OpenAI function schemas automatically
|
|
137
|
+
openai_tools = await generate_openai_functions_from_registry(registry)
|
|
138
|
+
|
|
139
|
+
# Call OpenAI with auto-discovered tools
|
|
140
|
+
client = AsyncOpenAI()
|
|
141
|
+
response = await client.chat.completions.create(
|
|
142
|
+
model="gpt-4o-mini",
|
|
143
|
+
messages=[{"role": "user", "content": "What's the weather in Tokyo and calculate 15.5 × 23.2?"}],
|
|
144
|
+
tools=openai_tools,
|
|
145
|
+
tool_choice="auto"
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
# Execute tools and track in session
|
|
149
|
+
processor = await CleanSessionAwareToolProcessor.create(session_id=session.id)
|
|
150
|
+
tool_results = await processor.process_llm_message(response.choices[0].message.model_dump())
|
|
151
|
+
|
|
152
|
+
# Results automatically logged with complete observability
|
|
153
|
+
print(f"Executed {len(tool_results)} tools successfully!")
|
|
154
|
+
print(f"Total cost: ${session.total_cost:.6f}")
|
|
155
|
+
|
|
156
|
+
asyncio.run(openai_integration_demo())
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## 📚 Storage Options
|
|
160
|
+
|
|
161
|
+
### In-Memory (Default)
|
|
162
|
+
```python
|
|
163
|
+
from chuk_ai_session_manager.storage import InMemorySessionStore, SessionStoreProvider
|
|
164
|
+
|
|
165
|
+
# Great for testing or single-process applications
|
|
166
|
+
store = InMemorySessionStore()
|
|
167
|
+
SessionStoreProvider.set_store(store)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### File Storage
|
|
171
|
+
```python
|
|
172
|
+
from chuk_ai_session_manager.storage.providers.file import create_file_session_store
|
|
173
|
+
|
|
174
|
+
# Persistent JSON file storage with async I/O
|
|
175
|
+
store = await create_file_session_store(directory="./sessions")
|
|
176
|
+
SessionStoreProvider.set_store(store)
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Redis Storage
|
|
180
|
+
```python
|
|
181
|
+
from chuk_ai_session_manager.storage.providers.redis import create_redis_session_store
|
|
182
|
+
|
|
183
|
+
# Distributed storage for production with TTL
|
|
184
|
+
store = await create_redis_session_store(
|
|
185
|
+
host="localhost",
|
|
186
|
+
port=6379,
|
|
187
|
+
expiration_seconds=86400 # 24-hour TTL
|
|
188
|
+
)
|
|
189
|
+
SessionStoreProvider.set_store(store)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## 🌳 Hierarchical Sessions
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
# Create parent-child relationships for complex workflows
|
|
196
|
+
parent = await Session.create()
|
|
197
|
+
child1 = await Session.create(parent_id=parent.id)
|
|
198
|
+
child2 = await Session.create(parent_id=parent.id)
|
|
199
|
+
|
|
200
|
+
# Navigate the hierarchy efficiently
|
|
201
|
+
ancestors = await child1.ancestors()
|
|
202
|
+
descendants = await parent.descendants()
|
|
203
|
+
|
|
204
|
+
# Build prompts with inherited context
|
|
205
|
+
from chuk_ai_session_manager.session_prompt_builder import build_prompt_from_session, PromptStrategy
|
|
206
|
+
|
|
207
|
+
prompt = await build_prompt_from_session(
|
|
208
|
+
child1,
|
|
209
|
+
strategy=PromptStrategy.HIERARCHICAL,
|
|
210
|
+
include_parent_context=True
|
|
211
|
+
)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## 💰 Token & Cost Tracking
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
# Automatic token counting with cost estimation
|
|
218
|
+
event = await SessionEvent.create_with_tokens(
|
|
219
|
+
message="Explain quantum computing in simple terms",
|
|
220
|
+
prompt="Explain quantum computing in simple terms",
|
|
221
|
+
completion="Quantum computing uses qubits that can be both 0 and 1...",
|
|
222
|
+
model="gpt-4",
|
|
223
|
+
source=EventSource.LLM
|
|
224
|
+
)
|
|
225
|
+
await session.add_event_and_save(event)
|
|
226
|
+
|
|
227
|
+
# Real-time usage analytics
|
|
228
|
+
print(f"Total tokens: {session.total_tokens}")
|
|
229
|
+
print(f"Estimated cost: ${session.total_cost:.6f}")
|
|
230
|
+
|
|
231
|
+
# Per-model breakdown
|
|
232
|
+
for model, usage in session.token_summary.usage_by_model.items():
|
|
233
|
+
print(f"{model}: {usage.total_tokens} tokens (${usage.estimated_cost_usd:.6f})")
|
|
234
|
+
|
|
235
|
+
# Usage by source (user, llm, system)
|
|
236
|
+
usage_by_source = await session.get_token_usage_by_source()
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## 🛠️ Tool Processing with Registry
|
|
240
|
+
|
|
241
|
+
```python
|
|
242
|
+
from chuk_tool_processor.registry import register_tool
|
|
243
|
+
|
|
244
|
+
# Clean tool registration with decorators
|
|
245
|
+
@register_tool(name="weather", namespace="default", description="Get weather info")
|
|
246
|
+
class WeatherTool:
|
|
247
|
+
async def execute(self, location: str) -> Dict[str, Any]:
|
|
248
|
+
# Your tool implementation
|
|
249
|
+
return {"location": location, "temperature": 22.5, "condition": "Sunny"}
|
|
250
|
+
|
|
251
|
+
# Session-aware tool execution with retry and caching
|
|
252
|
+
processor = await SessionAwareToolProcessor.create(session_id=session.id)
|
|
253
|
+
|
|
254
|
+
# Process LLM response with tool calls
|
|
255
|
+
llm_response = {
|
|
256
|
+
"role": "assistant",
|
|
257
|
+
"content": None,
|
|
258
|
+
"tool_calls": [
|
|
259
|
+
{
|
|
260
|
+
"function": {
|
|
261
|
+
"name": "weather",
|
|
262
|
+
"arguments": '{"location": "London"}'
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
]
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
# Execute tools with automatic session tracking
|
|
269
|
+
results = await processor.process_llm_message(llm_response, llm_callback)
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## ♾️ Infinite Conversations
|
|
273
|
+
|
|
274
|
+
```python
|
|
275
|
+
from chuk_ai_session_manager.infinite_conversation import InfiniteConversationManager, SummarizationStrategy
|
|
276
|
+
|
|
277
|
+
# Handle conversations that exceed token limits
|
|
278
|
+
manager = InfiniteConversationManager(
|
|
279
|
+
token_threshold=3000,
|
|
280
|
+
summarization_strategy=SummarizationStrategy.KEY_POINTS
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
# Automatic segmentation with context preservation
|
|
284
|
+
new_session_id = await manager.process_message(
|
|
285
|
+
session_id,
|
|
286
|
+
message,
|
|
287
|
+
source,
|
|
288
|
+
llm_callback
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
# Retrieve complete history across all segments
|
|
292
|
+
history = await manager.get_full_conversation_history(new_session_id)
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## 🔄 LLM Retry Patterns
|
|
296
|
+
|
|
297
|
+
```python
|
|
298
|
+
class LLMRetryManager:
|
|
299
|
+
"""Production-ready LLM retry logic with session tracking."""
|
|
300
|
+
|
|
301
|
+
async def get_valid_tool_calls(self, llm, messages, processor, max_attempts=5):
|
|
302
|
+
for attempt in range(1, max_attempts + 1):
|
|
303
|
+
# Call LLM
|
|
304
|
+
response = await llm.chat_completion(messages)
|
|
305
|
+
|
|
306
|
+
# Log attempt in session
|
|
307
|
+
await session.add_event_and_save(SessionEvent(
|
|
308
|
+
message={"attempt": attempt, "response": response},
|
|
309
|
+
type=EventType.MESSAGE,
|
|
310
|
+
source=EventSource.LLM
|
|
311
|
+
))
|
|
312
|
+
|
|
313
|
+
# Try to execute tools
|
|
314
|
+
try:
|
|
315
|
+
tool_results = await processor.process_llm_message(response)
|
|
316
|
+
|
|
317
|
+
# Check for failures
|
|
318
|
+
failed_tools = [r for r in tool_results if r.error]
|
|
319
|
+
if not failed_tools:
|
|
320
|
+
return response, tool_results # Success!
|
|
321
|
+
|
|
322
|
+
except Exception as e:
|
|
323
|
+
continue # Retry on failure
|
|
324
|
+
|
|
325
|
+
raise RuntimeError(f"Failed after {max_attempts} attempts")
|
|
326
|
+
|
|
327
|
+
# Complete audit trail of all retry attempts
|
|
328
|
+
# Separation of concerns: LLM cooperation vs tool reliability
|
|
329
|
+
# Automatic recovery with detailed error tracking
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
## 📊 Production Observability
|
|
333
|
+
|
|
334
|
+
### Complete Event Hierarchy
|
|
335
|
+
```python
|
|
336
|
+
# Every operation creates a traceable event tree
|
|
337
|
+
• user_message [abc123...]
|
|
338
|
+
• llm_message [def456...]
|
|
339
|
+
• tool_call [ghi789...] - weather ✅ Success
|
|
340
|
+
• tool_call [jkl012...] - calculator ✅ Success
|
|
341
|
+
• tool_call [mno345...] - search ✅ Success
|
|
342
|
+
|
|
343
|
+
# Parent-child relationships maintained automatically
|
|
344
|
+
# Performance monitoring with execution spans
|
|
345
|
+
# Error tracking with detailed stack traces
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
### Real-Time Analytics
|
|
349
|
+
```python
|
|
350
|
+
# Token usage across all operations
|
|
351
|
+
session.total_tokens # 441 tokens
|
|
352
|
+
session.total_cost # $0.000845
|
|
353
|
+
|
|
354
|
+
# Per-model breakdown
|
|
355
|
+
session.token_summary.usage_by_model
|
|
356
|
+
# gpt-4o-mini: 230 tokens ($0.000432)
|
|
357
|
+
# tool-execution: 211 tokens ($0.000413)
|
|
358
|
+
|
|
359
|
+
# Performance metrics
|
|
360
|
+
execution_time # 202ms for 3 concurrent tools
|
|
361
|
+
success_rate # 100% with retry patterns
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
## 🎯 Real Production Results
|
|
365
|
+
|
|
366
|
+
Based on actual demo runs with live OpenAI API:
|
|
367
|
+
|
|
368
|
+
```
|
|
369
|
+
🚀 Clean OpenAI Demo with Registry Auto-Discovery
|
|
370
|
+
|
|
371
|
+
🔧 Auto-discovered 3 tools from registry:
|
|
372
|
+
• default.calculator: Perform basic arithmetic operations
|
|
373
|
+
• default.weather: Get current weather information
|
|
374
|
+
• default.search: Search for information on the internet
|
|
375
|
+
|
|
376
|
+
🤖 Calling OpenAI with 3 auto-discovered tools...
|
|
377
|
+
|
|
378
|
+
📞 LLM wants to call 3 tools:
|
|
379
|
+
• weather({"location": "Tokyo"})
|
|
380
|
+
• calculator({"operation": "multiply", "a": 15.5, "b": 23.2})
|
|
381
|
+
• search({"query": "renewable energy"})
|
|
382
|
+
|
|
383
|
+
✅ Tool Results:
|
|
384
|
+
🌤️ Tokyo: 21.0°C, Sunny (Humidity: 42%, Wind: 4.1 km/h)
|
|
385
|
+
🧮 15.5 multiply 23.2 = 359.6
|
|
386
|
+
🔍 'renewable energy': Found 2 results
|
|
387
|
+
|
|
388
|
+
💰 Token Usage:
|
|
389
|
+
Total tokens: 441 | Estimated cost: $0.000845
|
|
390
|
+
📊 gpt-4o-mini: 230 tokens ($0.000432)
|
|
391
|
+
📊 tool-execution: 211 tokens ($0.000413)
|
|
392
|
+
|
|
393
|
+
🎉 All tools executed successfully in 202ms!
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
## 📖 Examples
|
|
397
|
+
|
|
398
|
+
### Production OpenAI Integration
|
|
399
|
+
```bash
|
|
400
|
+
# Complete OpenAI integration with auto-discovery
|
|
401
|
+
uv run examples/clean_openai_demo.py
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### LLM Retry Patterns
|
|
405
|
+
```bash
|
|
406
|
+
# Demonstrates retry logic for uncooperative LLMs
|
|
407
|
+
uv run examples/llm_retry_demo.py
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
### Token Cost Tracking
|
|
411
|
+
```bash
|
|
412
|
+
# Real-time token usage and cost monitoring
|
|
413
|
+
uv run examples/session_token_usage_example.py
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
### Infinite Conversations
|
|
417
|
+
```bash
|
|
418
|
+
# Automatic conversation segmentation
|
|
419
|
+
uv run examples/example_infinite_conversation.py
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
### FastAPI Integration
|
|
423
|
+
```bash
|
|
424
|
+
# Complete REST API with session management
|
|
425
|
+
uv run examples/fastapi_session_example.py
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
### Basic Session Management
|
|
429
|
+
```bash
|
|
430
|
+
# Fundamental session and event operations
|
|
431
|
+
uv run examples/session_example.py
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
## 🏗️ Architecture
|
|
435
|
+
|
|
436
|
+
The CHUK AI Session Manager provides a comprehensive foundation for production AI applications:
|
|
437
|
+
|
|
438
|
+
- **Session Layer**: Hierarchical conversation management with async operations
|
|
439
|
+
- **Event Layer**: Complete audit trails with parent-child relationships
|
|
440
|
+
- **Storage Layer**: Pluggable backends (memory, file, Redis) with async I/O
|
|
441
|
+
- **Tool Layer**: Registry-based auto-discovery with session-aware execution
|
|
442
|
+
- **Cost Layer**: Real-time token tracking and cost estimation
|
|
443
|
+
- **Retry Layer**: Multi-level error recovery patterns
|
|
444
|
+
- **Observability Layer**: Performance monitoring and analytics
|
|
445
|
+
|
|
446
|
+
## 🔧 Advanced Configuration
|
|
447
|
+
|
|
448
|
+
### Custom Tool Processor
|
|
449
|
+
```python
|
|
450
|
+
class CustomSessionAwareToolProcessor:
|
|
451
|
+
"""Production tool processor with registry integration."""
|
|
452
|
+
|
|
453
|
+
@classmethod
|
|
454
|
+
async def create(cls, session_id: str):
|
|
455
|
+
registry = await get_default_registry()
|
|
456
|
+
strategy = InProcessStrategy(registry)
|
|
457
|
+
executor = ToolExecutor(registry=registry, strategy=strategy)
|
|
458
|
+
return cls(session_id, registry, executor)
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
### Session Runs for Workflows
|
|
462
|
+
```python
|
|
463
|
+
# Track multi-step processes
|
|
464
|
+
run = await SessionRun.create(metadata={"task": "data_analysis"})
|
|
465
|
+
await run.mark_running()
|
|
466
|
+
|
|
467
|
+
# Associate events with runs
|
|
468
|
+
await session.add_event_and_save(SessionEvent(
|
|
469
|
+
message="Processing dataset...",
|
|
470
|
+
source=EventSource.SYSTEM,
|
|
471
|
+
task_id=run.id
|
|
472
|
+
))
|
|
473
|
+
|
|
474
|
+
await run.mark_completed()
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
### Prompt Building Strategies
|
|
478
|
+
```python
|
|
479
|
+
# Multiple strategies for different use cases
|
|
480
|
+
strategies = [
|
|
481
|
+
PromptStrategy.MINIMAL, # Basic task + latest results
|
|
482
|
+
PromptStrategy.TASK_FOCUSED, # Emphasizes original task
|
|
483
|
+
PromptStrategy.TOOL_FOCUSED, # Detailed tool information
|
|
484
|
+
PromptStrategy.CONVERSATION, # Recent message history
|
|
485
|
+
PromptStrategy.HIERARCHICAL, # Includes parent context
|
|
486
|
+
]
|
|
487
|
+
|
|
488
|
+
prompt = await build_prompt_from_session(session, strategy=PromptStrategy.CONVERSATION)
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
## 🤝 Contributing
|
|
492
|
+
|
|
493
|
+
We welcome contributions! This project is designed for production use and follows best practices for async Python development.
|
|
494
|
+
|
|
495
|
+
## 📝 License
|
|
496
|
+
|
|
497
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
|
498
|
+
|
|
499
|
+
---
|
|
500
|
+
|
|
501
|
+
**Ready for Production** • **Async Native** • **Complete Observability** • **Cost Optimized**
|