chuk-ai-session-manager 0.4.1__tar.gz → 0.7__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.7/PKG-INFO +262 -0
- chuk_ai_session_manager-0.7/README.md +236 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/pyproject.toml +22 -7
- chuk_ai_session_manager-0.7/src/chuk_ai_session_manager/__init__.py +275 -0
- chuk_ai_session_manager-0.7/src/chuk_ai_session_manager.egg-info/PKG-INFO +262 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager.egg-info/requires.txt +7 -4
- chuk_ai_session_manager-0.4.1/PKG-INFO +0 -355
- chuk_ai_session_manager-0.4.1/README.md +0 -332
- chuk_ai_session_manager-0.4.1/src/chuk_ai_session_manager/__init__.py +0 -329
- chuk_ai_session_manager-0.4.1/src/chuk_ai_session_manager.egg-info/PKG-INFO +0 -355
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/setup.cfg +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/api/__init__.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/api/simple_api.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/exceptions.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/infinite_conversation.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/models/__init__.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/models/event_source.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/models/event_type.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/models/session.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/models/session_event.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/models/session_metadata.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/models/session_run.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/models/token_usage.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/sample_tools.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/session_aware_tool_processor.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/session_manager.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/session_prompt_builder.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager/session_storage.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager.egg-info/SOURCES.txt +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager.egg-info/dependency_links.txt +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/src/chuk_ai_session_manager.egg-info/top_level.txt +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_basic_functionality.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_exceptions.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_infinite_conversation.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_models.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_prompt_builder.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_session_manager.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_session_manager_advanced.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_simple.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_simple_api.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_storage.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_system_prompt.py +0 -0
- {chuk_ai_session_manager-0.4.1 → chuk_ai_session_manager-0.7}/tests/test_tools.py +0 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: chuk-ai-session-manager
|
|
3
|
+
Version: 0.7
|
|
4
|
+
Summary: Session manager for AI applications
|
|
5
|
+
Requires-Python: >=3.11
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: chuk-sessions>=0.4.1
|
|
8
|
+
Requires-Dist: chuk-tool-processor>=0.4.1
|
|
9
|
+
Requires-Dist: pydantic>=2.11.3
|
|
10
|
+
Provides-Extra: redis
|
|
11
|
+
Requires-Dist: chuk-sessions[redis]>=0.4.1; extra == "redis"
|
|
12
|
+
Requires-Dist: redis>=4.0.0; extra == "redis"
|
|
13
|
+
Provides-Extra: tiktoken
|
|
14
|
+
Requires-Dist: tiktoken>=0.9.0; extra == "tiktoken"
|
|
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: pytest-asyncio>=0.21.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: all
|
|
23
|
+
Requires-Dist: chuk-sessions[redis]>=0.4; extra == "all"
|
|
24
|
+
Requires-Dist: redis>=4.0.0; extra == "all"
|
|
25
|
+
Requires-Dist: tiktoken>=0.9.0; extra == "all"
|
|
26
|
+
|
|
27
|
+
# CHUK AI Session Manager
|
|
28
|
+
|
|
29
|
+
**A powerful session management system for AI applications**
|
|
30
|
+
|
|
31
|
+
[](https://www.python.org/downloads/)
|
|
32
|
+
[](https://opensource.org/licenses/MIT)
|
|
33
|
+
|
|
34
|
+
Automatic conversation tracking, token usage monitoring, tool call logging, infinite context support with automatic summarization, and hierarchical session relationships. Perfect for AI applications that need reliable session management.
|
|
35
|
+
|
|
36
|
+
## 🚀 Quick Start
|
|
37
|
+
|
|
38
|
+
### Installation Options
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# Basic installation (memory storage only)
|
|
42
|
+
pip install chuk-ai-session-manager
|
|
43
|
+
|
|
44
|
+
# With Redis support for production
|
|
45
|
+
pip install chuk-ai-session-manager[redis]
|
|
46
|
+
|
|
47
|
+
# With enhanced token counting
|
|
48
|
+
pip install chuk-ai-session-manager[tiktoken]
|
|
49
|
+
|
|
50
|
+
# Full installation with all optional features
|
|
51
|
+
pip install chuk-ai-session-manager[all]
|
|
52
|
+
|
|
53
|
+
# Development installation
|
|
54
|
+
pip install chuk-ai-session-manager[dev]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Quick Example
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from chuk_ai_session_manager import track_conversation
|
|
61
|
+
|
|
62
|
+
# Track any conversation automatically
|
|
63
|
+
session_id = await track_conversation(
|
|
64
|
+
user_message="What's the weather like?",
|
|
65
|
+
ai_response="I don't have access to real-time weather data.",
|
|
66
|
+
model="gpt-3.5-turbo",
|
|
67
|
+
provider="openai"
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
print(f"Conversation tracked in session: {session_id}")
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
That's it! Zero configuration required.
|
|
74
|
+
|
|
75
|
+
## ⚡ Major Features
|
|
76
|
+
|
|
77
|
+
### 🎯 **Zero-Configuration Tracking**
|
|
78
|
+
```python
|
|
79
|
+
from chuk_ai_session_manager import SessionManager
|
|
80
|
+
|
|
81
|
+
# Just start using it
|
|
82
|
+
sm = SessionManager()
|
|
83
|
+
await sm.user_says("Hello!")
|
|
84
|
+
await sm.ai_responds("Hi there!", model="gpt-4")
|
|
85
|
+
|
|
86
|
+
# Get stats instantly
|
|
87
|
+
stats = await sm.get_stats()
|
|
88
|
+
print(f"Tokens: {stats['total_tokens']}, Cost: ${stats['estimated_cost']:.4f}")
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### 🔄 **Infinite Context**
|
|
92
|
+
```python
|
|
93
|
+
# Automatically handles conversations longer than token limits
|
|
94
|
+
sm = SessionManager(infinite_context=True, token_threshold=4000)
|
|
95
|
+
await sm.user_says("Tell me about the history of computing...")
|
|
96
|
+
await sm.ai_responds("Computing history begins with...", model="gpt-4")
|
|
97
|
+
# Session will auto-segment when limits are reached
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### ⚙️ **Storage Backends**
|
|
101
|
+
|
|
102
|
+
| Installation | Storage | Use Case | Performance |
|
|
103
|
+
|-------------|---------|----------|-------------|
|
|
104
|
+
| `pip install chuk-ai-session-manager` | Memory | Development, testing | 1.8M ops/sec |
|
|
105
|
+
| `pip install chuk-ai-session-manager[redis]` | Redis | Production, persistence | 20K ops/sec |
|
|
106
|
+
|
|
107
|
+
### 🛠️ **Tool Integration**
|
|
108
|
+
```python
|
|
109
|
+
# Automatic tool call tracking
|
|
110
|
+
await sm.tool_used(
|
|
111
|
+
tool_name="calculator",
|
|
112
|
+
arguments={"operation": "add", "a": 5, "b": 3},
|
|
113
|
+
result={"result": 8}
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## 💡 Common Use Cases
|
|
118
|
+
|
|
119
|
+
### Web App Conversation Tracking
|
|
120
|
+
```python
|
|
121
|
+
from chuk_ai_session_manager import track_conversation
|
|
122
|
+
|
|
123
|
+
# In your chat endpoint
|
|
124
|
+
session_id = await track_conversation(
|
|
125
|
+
user_message=request.message,
|
|
126
|
+
ai_response=ai_response,
|
|
127
|
+
model="gpt-4",
|
|
128
|
+
provider="openai",
|
|
129
|
+
session_id=request.session_id # Continue existing conversation
|
|
130
|
+
)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### LLM Wrapper with Automatic Tracking
|
|
134
|
+
```python
|
|
135
|
+
from chuk_ai_session_manager import track_llm_call
|
|
136
|
+
import openai
|
|
137
|
+
|
|
138
|
+
async def my_openai_call(prompt):
|
|
139
|
+
response = await openai.chat.completions.create(
|
|
140
|
+
model="gpt-3.5-turbo",
|
|
141
|
+
messages=[{"role": "user", "content": prompt}]
|
|
142
|
+
)
|
|
143
|
+
return response.choices[0].message.content
|
|
144
|
+
|
|
145
|
+
# Automatically tracked
|
|
146
|
+
response, session_id = await track_llm_call(
|
|
147
|
+
user_input="Explain machine learning",
|
|
148
|
+
llm_function=my_openai_call,
|
|
149
|
+
model="gpt-3.5-turbo",
|
|
150
|
+
provider="openai"
|
|
151
|
+
)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Long Conversations with Auto-Segmentation
|
|
155
|
+
```python
|
|
156
|
+
from chuk_ai_session_manager import track_infinite_conversation
|
|
157
|
+
|
|
158
|
+
# Start a conversation
|
|
159
|
+
session_id = await track_infinite_conversation(
|
|
160
|
+
user_message="Tell me about the history of computing",
|
|
161
|
+
ai_response="Computing history begins with ancient calculating devices...",
|
|
162
|
+
model="gpt-4",
|
|
163
|
+
token_threshold=4000 # Auto-segment after 4000 tokens
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
# Continue the conversation - will auto-segment if needed
|
|
167
|
+
session_id = await track_infinite_conversation(
|
|
168
|
+
user_message="What about quantum computers?",
|
|
169
|
+
ai_response="Quantum computing represents a fundamental shift...",
|
|
170
|
+
session_id=session_id,
|
|
171
|
+
model="gpt-4"
|
|
172
|
+
)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## 🔧 Configuration
|
|
176
|
+
|
|
177
|
+
### Storage Configuration
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
# Memory provider (default) - fast, no persistence
|
|
181
|
+
export SESSION_PROVIDER=memory
|
|
182
|
+
|
|
183
|
+
# Redis provider - persistent, production-ready (requires redis extra)
|
|
184
|
+
export SESSION_PROVIDER=redis
|
|
185
|
+
export SESSION_REDIS_URL=redis://localhost:6379/0
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Installation Matrix
|
|
189
|
+
|
|
190
|
+
| Command | Memory | Redis | Token Counting | Use Case |
|
|
191
|
+
|---------|--------|-------|----------------|----------|
|
|
192
|
+
| `pip install chuk-ai-session-manager` | ✅ | ❌ | Basic | Development |
|
|
193
|
+
| `pip install chuk-ai-session-manager[redis]` | ✅ | ✅ | Basic | Production |
|
|
194
|
+
| `pip install chuk-ai-session-manager[tiktoken]` | ✅ | ❌ | Enhanced | Better accuracy |
|
|
195
|
+
| `pip install chuk-ai-session-manager[all]` | ✅ | ✅ | Enhanced | Full features |
|
|
196
|
+
|
|
197
|
+
## 📊 Monitoring & Analytics
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
# Get comprehensive session analytics
|
|
201
|
+
stats = await sm.get_stats(include_all_segments=True)
|
|
202
|
+
|
|
203
|
+
print(f"""
|
|
204
|
+
🚀 Session Analytics Dashboard
|
|
205
|
+
============================
|
|
206
|
+
Session ID: {stats['session_id']}
|
|
207
|
+
Total Messages: {stats['total_messages']}
|
|
208
|
+
User Messages: {stats['user_messages']}
|
|
209
|
+
AI Messages: {stats['ai_messages']}
|
|
210
|
+
Tool Calls: {stats['tool_calls']}
|
|
211
|
+
Total Tokens: {stats['total_tokens']}
|
|
212
|
+
Total Cost: ${stats['estimated_cost']:.6f}
|
|
213
|
+
Session Segments: {stats.get('session_segments', 1)}
|
|
214
|
+
""")
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## 🏗️ Why CHUK AI Session Manager?
|
|
218
|
+
|
|
219
|
+
- **Zero Configuration**: Start tracking conversations in 3 lines of code
|
|
220
|
+
- **Infinite Context**: Never worry about token limits again
|
|
221
|
+
- **Universal**: Works with any LLM provider (OpenAI, Anthropic, etc.)
|
|
222
|
+
- **Production Ready**: Built-in persistence, monitoring, and error handling
|
|
223
|
+
- **Token Aware**: Automatic cost tracking across all providers
|
|
224
|
+
- **Tool Friendly**: Seamless tool call logging and retry mechanisms
|
|
225
|
+
|
|
226
|
+
## 🛡️ Error Handling
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
from chuk_ai_session_manager import (
|
|
230
|
+
SessionManagerError,
|
|
231
|
+
SessionNotFound,
|
|
232
|
+
TokenLimitExceeded
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
try:
|
|
236
|
+
session_id = await track_conversation("Hello", "Hi there")
|
|
237
|
+
except SessionNotFound as e:
|
|
238
|
+
print(f"Session not found: {e}")
|
|
239
|
+
except TokenLimitExceeded as e:
|
|
240
|
+
print(f"Token limit exceeded: {e}")
|
|
241
|
+
except SessionManagerError as e:
|
|
242
|
+
print(f"General session error: {e}")
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## 🔄 Dependencies
|
|
246
|
+
|
|
247
|
+
- **Required**: `chuk-sessions` (session storage), `pydantic` (data models), `chuk-tool-processor` (tool integration)
|
|
248
|
+
- **Optional**: `redis` (Redis storage), `tiktoken` (accurate token counting)
|
|
249
|
+
|
|
250
|
+
## 📄 License
|
|
251
|
+
|
|
252
|
+
MIT License - build amazing AI applications with confidence!
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
**Ready to build better AI applications?**
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
pip install chuk-ai-session-manager
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
**Start tracking conversations in 30 seconds!**
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# CHUK AI Session Manager
|
|
2
|
+
|
|
3
|
+
**A powerful session management system for AI applications**
|
|
4
|
+
|
|
5
|
+
[](https://www.python.org/downloads/)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
Automatic conversation tracking, token usage monitoring, tool call logging, infinite context support with automatic summarization, and hierarchical session relationships. Perfect for AI applications that need reliable session management.
|
|
9
|
+
|
|
10
|
+
## 🚀 Quick Start
|
|
11
|
+
|
|
12
|
+
### Installation Options
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
# Basic installation (memory storage only)
|
|
16
|
+
pip install chuk-ai-session-manager
|
|
17
|
+
|
|
18
|
+
# With Redis support for production
|
|
19
|
+
pip install chuk-ai-session-manager[redis]
|
|
20
|
+
|
|
21
|
+
# With enhanced token counting
|
|
22
|
+
pip install chuk-ai-session-manager[tiktoken]
|
|
23
|
+
|
|
24
|
+
# Full installation with all optional features
|
|
25
|
+
pip install chuk-ai-session-manager[all]
|
|
26
|
+
|
|
27
|
+
# Development installation
|
|
28
|
+
pip install chuk-ai-session-manager[dev]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Quick Example
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from chuk_ai_session_manager import track_conversation
|
|
35
|
+
|
|
36
|
+
# Track any conversation automatically
|
|
37
|
+
session_id = await track_conversation(
|
|
38
|
+
user_message="What's the weather like?",
|
|
39
|
+
ai_response="I don't have access to real-time weather data.",
|
|
40
|
+
model="gpt-3.5-turbo",
|
|
41
|
+
provider="openai"
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
print(f"Conversation tracked in session: {session_id}")
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
That's it! Zero configuration required.
|
|
48
|
+
|
|
49
|
+
## ⚡ Major Features
|
|
50
|
+
|
|
51
|
+
### 🎯 **Zero-Configuration Tracking**
|
|
52
|
+
```python
|
|
53
|
+
from chuk_ai_session_manager import SessionManager
|
|
54
|
+
|
|
55
|
+
# Just start using it
|
|
56
|
+
sm = SessionManager()
|
|
57
|
+
await sm.user_says("Hello!")
|
|
58
|
+
await sm.ai_responds("Hi there!", model="gpt-4")
|
|
59
|
+
|
|
60
|
+
# Get stats instantly
|
|
61
|
+
stats = await sm.get_stats()
|
|
62
|
+
print(f"Tokens: {stats['total_tokens']}, Cost: ${stats['estimated_cost']:.4f}")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 🔄 **Infinite Context**
|
|
66
|
+
```python
|
|
67
|
+
# Automatically handles conversations longer than token limits
|
|
68
|
+
sm = SessionManager(infinite_context=True, token_threshold=4000)
|
|
69
|
+
await sm.user_says("Tell me about the history of computing...")
|
|
70
|
+
await sm.ai_responds("Computing history begins with...", model="gpt-4")
|
|
71
|
+
# Session will auto-segment when limits are reached
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### ⚙️ **Storage Backends**
|
|
75
|
+
|
|
76
|
+
| Installation | Storage | Use Case | Performance |
|
|
77
|
+
|-------------|---------|----------|-------------|
|
|
78
|
+
| `pip install chuk-ai-session-manager` | Memory | Development, testing | 1.8M ops/sec |
|
|
79
|
+
| `pip install chuk-ai-session-manager[redis]` | Redis | Production, persistence | 20K ops/sec |
|
|
80
|
+
|
|
81
|
+
### 🛠️ **Tool Integration**
|
|
82
|
+
```python
|
|
83
|
+
# Automatic tool call tracking
|
|
84
|
+
await sm.tool_used(
|
|
85
|
+
tool_name="calculator",
|
|
86
|
+
arguments={"operation": "add", "a": 5, "b": 3},
|
|
87
|
+
result={"result": 8}
|
|
88
|
+
)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 💡 Common Use Cases
|
|
92
|
+
|
|
93
|
+
### Web App Conversation Tracking
|
|
94
|
+
```python
|
|
95
|
+
from chuk_ai_session_manager import track_conversation
|
|
96
|
+
|
|
97
|
+
# In your chat endpoint
|
|
98
|
+
session_id = await track_conversation(
|
|
99
|
+
user_message=request.message,
|
|
100
|
+
ai_response=ai_response,
|
|
101
|
+
model="gpt-4",
|
|
102
|
+
provider="openai",
|
|
103
|
+
session_id=request.session_id # Continue existing conversation
|
|
104
|
+
)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### LLM Wrapper with Automatic Tracking
|
|
108
|
+
```python
|
|
109
|
+
from chuk_ai_session_manager import track_llm_call
|
|
110
|
+
import openai
|
|
111
|
+
|
|
112
|
+
async def my_openai_call(prompt):
|
|
113
|
+
response = await openai.chat.completions.create(
|
|
114
|
+
model="gpt-3.5-turbo",
|
|
115
|
+
messages=[{"role": "user", "content": prompt}]
|
|
116
|
+
)
|
|
117
|
+
return response.choices[0].message.content
|
|
118
|
+
|
|
119
|
+
# Automatically tracked
|
|
120
|
+
response, session_id = await track_llm_call(
|
|
121
|
+
user_input="Explain machine learning",
|
|
122
|
+
llm_function=my_openai_call,
|
|
123
|
+
model="gpt-3.5-turbo",
|
|
124
|
+
provider="openai"
|
|
125
|
+
)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Long Conversations with Auto-Segmentation
|
|
129
|
+
```python
|
|
130
|
+
from chuk_ai_session_manager import track_infinite_conversation
|
|
131
|
+
|
|
132
|
+
# Start a conversation
|
|
133
|
+
session_id = await track_infinite_conversation(
|
|
134
|
+
user_message="Tell me about the history of computing",
|
|
135
|
+
ai_response="Computing history begins with ancient calculating devices...",
|
|
136
|
+
model="gpt-4",
|
|
137
|
+
token_threshold=4000 # Auto-segment after 4000 tokens
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
# Continue the conversation - will auto-segment if needed
|
|
141
|
+
session_id = await track_infinite_conversation(
|
|
142
|
+
user_message="What about quantum computers?",
|
|
143
|
+
ai_response="Quantum computing represents a fundamental shift...",
|
|
144
|
+
session_id=session_id,
|
|
145
|
+
model="gpt-4"
|
|
146
|
+
)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## 🔧 Configuration
|
|
150
|
+
|
|
151
|
+
### Storage Configuration
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
# Memory provider (default) - fast, no persistence
|
|
155
|
+
export SESSION_PROVIDER=memory
|
|
156
|
+
|
|
157
|
+
# Redis provider - persistent, production-ready (requires redis extra)
|
|
158
|
+
export SESSION_PROVIDER=redis
|
|
159
|
+
export SESSION_REDIS_URL=redis://localhost:6379/0
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Installation Matrix
|
|
163
|
+
|
|
164
|
+
| Command | Memory | Redis | Token Counting | Use Case |
|
|
165
|
+
|---------|--------|-------|----------------|----------|
|
|
166
|
+
| `pip install chuk-ai-session-manager` | ✅ | ❌ | Basic | Development |
|
|
167
|
+
| `pip install chuk-ai-session-manager[redis]` | ✅ | ✅ | Basic | Production |
|
|
168
|
+
| `pip install chuk-ai-session-manager[tiktoken]` | ✅ | ❌ | Enhanced | Better accuracy |
|
|
169
|
+
| `pip install chuk-ai-session-manager[all]` | ✅ | ✅ | Enhanced | Full features |
|
|
170
|
+
|
|
171
|
+
## 📊 Monitoring & Analytics
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
# Get comprehensive session analytics
|
|
175
|
+
stats = await sm.get_stats(include_all_segments=True)
|
|
176
|
+
|
|
177
|
+
print(f"""
|
|
178
|
+
🚀 Session Analytics Dashboard
|
|
179
|
+
============================
|
|
180
|
+
Session ID: {stats['session_id']}
|
|
181
|
+
Total Messages: {stats['total_messages']}
|
|
182
|
+
User Messages: {stats['user_messages']}
|
|
183
|
+
AI Messages: {stats['ai_messages']}
|
|
184
|
+
Tool Calls: {stats['tool_calls']}
|
|
185
|
+
Total Tokens: {stats['total_tokens']}
|
|
186
|
+
Total Cost: ${stats['estimated_cost']:.6f}
|
|
187
|
+
Session Segments: {stats.get('session_segments', 1)}
|
|
188
|
+
""")
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## 🏗️ Why CHUK AI Session Manager?
|
|
192
|
+
|
|
193
|
+
- **Zero Configuration**: Start tracking conversations in 3 lines of code
|
|
194
|
+
- **Infinite Context**: Never worry about token limits again
|
|
195
|
+
- **Universal**: Works with any LLM provider (OpenAI, Anthropic, etc.)
|
|
196
|
+
- **Production Ready**: Built-in persistence, monitoring, and error handling
|
|
197
|
+
- **Token Aware**: Automatic cost tracking across all providers
|
|
198
|
+
- **Tool Friendly**: Seamless tool call logging and retry mechanisms
|
|
199
|
+
|
|
200
|
+
## 🛡️ Error Handling
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
from chuk_ai_session_manager import (
|
|
204
|
+
SessionManagerError,
|
|
205
|
+
SessionNotFound,
|
|
206
|
+
TokenLimitExceeded
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
try:
|
|
210
|
+
session_id = await track_conversation("Hello", "Hi there")
|
|
211
|
+
except SessionNotFound as e:
|
|
212
|
+
print(f"Session not found: {e}")
|
|
213
|
+
except TokenLimitExceeded as e:
|
|
214
|
+
print(f"Token limit exceeded: {e}")
|
|
215
|
+
except SessionManagerError as e:
|
|
216
|
+
print(f"General session error: {e}")
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## 🔄 Dependencies
|
|
220
|
+
|
|
221
|
+
- **Required**: `chuk-sessions` (session storage), `pydantic` (data models), `chuk-tool-processor` (tool integration)
|
|
222
|
+
- **Optional**: `redis` (Redis storage), `tiktoken` (accurate token counting)
|
|
223
|
+
|
|
224
|
+
## 📄 License
|
|
225
|
+
|
|
226
|
+
MIT License - build amazing AI applications with confidence!
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
**Ready to build better AI applications?**
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
pip install chuk-ai-session-manager
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**Start tracking conversations in 30 seconds!**
|
|
@@ -4,30 +4,45 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "chuk-ai-session-manager"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.7"
|
|
8
8
|
description = "Session manager for AI applications"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
11
|
+
|
|
12
|
+
# Core dependencies - only what's absolutely required
|
|
11
13
|
dependencies = [
|
|
12
|
-
"chuk-sessions>=0.
|
|
13
|
-
"chuk-tool-processor>=0.4.1",
|
|
14
|
+
"chuk-sessions>=0.4.1",
|
|
15
|
+
"chuk-tool-processor>=0.4.1",
|
|
14
16
|
"pydantic>=2.11.3",
|
|
15
17
|
]
|
|
16
18
|
|
|
17
19
|
# Optional dependencies
|
|
18
20
|
[project.optional-dependencies]
|
|
21
|
+
# Redis support (passes through to chuk-sessions)
|
|
22
|
+
redis = [
|
|
23
|
+
"chuk-sessions[redis]>=0.4.1",
|
|
24
|
+
"redis>=4.0.0"
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
# Token counting for better accuracy
|
|
19
28
|
tiktoken = ["tiktoken>=0.9.0"]
|
|
20
|
-
|
|
29
|
+
|
|
30
|
+
# Development dependencies
|
|
21
31
|
dev = [
|
|
22
32
|
"pytest>=7.0.0",
|
|
23
33
|
"pytest-cov>=4.0.0",
|
|
24
|
-
"pytest-asyncio>=0.21.0",
|
|
25
|
-
"redis>=4.0.0",
|
|
34
|
+
"pytest-asyncio>=0.21.0",
|
|
26
35
|
"black>=23.0.0",
|
|
27
36
|
"isort>=5.12.0",
|
|
28
37
|
"mypy>=1.0.0",
|
|
29
38
|
]
|
|
30
|
-
|
|
39
|
+
|
|
40
|
+
# All optional features
|
|
41
|
+
all = [
|
|
42
|
+
"chuk-sessions[redis]>=0.4",
|
|
43
|
+
"redis>=4.0.0",
|
|
44
|
+
"tiktoken>=0.9.0",
|
|
45
|
+
]
|
|
31
46
|
|
|
32
47
|
# Configure setuptools for src/ layout
|
|
33
48
|
[tool.setuptools.packages.find]
|