chuk-ai-session-manager 0.2__py3-none-any.whl → 0.3__py3-none-any.whl

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