smart_prompt 0.4.4 → 0.5.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/README.cn.md +305 -11
- data/README.md +309 -11
- data/Rakefile +10 -1
- data/config/anthropic_config.yml +151 -0
- data/config/image_generation_config.yml +22 -0
- data/config/multimodal_config.yml +85 -0
- data/config/sensenova_config.yml +63 -0
- data/config/zhipu_config.yml +73 -0
- data/docs/ANTHROPIC_EXAMPLES.md +559 -0
- data/docs/CONVERSATION_INTEGRATION_SUMMARY.md +155 -0
- data/docs/HISTORY_EXAMPLES_README.md +533 -0
- data/docs/HISTORY_MANAGEMENT_GUIDE.md +797 -0
- data/docs/MONITORING_GUIDE.md +278 -0
- data/docs/MULTIMODAL_README.md +265 -0
- data/docs/RELEVANCE_BASED_STRATEGY_IMPLEMENTATION.md +124 -0
- data/docs/STT_README.md +302 -0
- data/docs/TTS_README.md +303 -0
- data/docs/VIDEO_GENERATION_README.md +246 -0
- data/docs/delete_files_list.md +124 -0
- data/examples/anthropic_basic_chat.rb +143 -0
- data/examples/anthropic_example.rb +232 -0
- data/examples/anthropic_multimodal.rb +212 -0
- data/examples/anthropic_streaming.rb +312 -0
- data/examples/anthropic_tool_calling.rb +393 -0
- data/examples/automatic_cleanup_example.rb +109 -0
- data/examples/history_management_examples.rb +522 -0
- data/examples/image_generation_example.rb +130 -0
- data/examples/monitoring_example.rb +121 -0
- data/examples/multimodal_example.rb +63 -0
- data/examples/relevance_based_strategy_example.rb +87 -0
- data/examples/sensenova_example.rb +129 -0
- data/examples/stt_example.rb +287 -0
- data/examples/tts_example.rb +244 -0
- data/examples/video_generation_example.rb +189 -0
- data/examples/zhipu_example.rb +151 -0
- data/lib/smart_prompt/anthropic_adapter.rb +407 -298
- data/lib/smart_prompt/compression_engine.rb +201 -0
- data/lib/smart_prompt/context_strategy.rb +22 -0
- data/lib/smart_prompt/conversation.rb +47 -4
- data/lib/smart_prompt/engine.rb +29 -2
- data/lib/smart_prompt/history_manager.rb +596 -0
- data/lib/smart_prompt/hybrid_strategy.rb +222 -0
- data/lib/smart_prompt/image_generation_adapter.rb +297 -0
- data/lib/smart_prompt/lru_cache.rb +133 -0
- data/lib/smart_prompt/message.rb +57 -0
- data/lib/smart_prompt/multimodal_adapter.rb +277 -0
- data/lib/smart_prompt/persistence_layer.rb +197 -0
- data/lib/smart_prompt/relevance_based_strategy.rb +221 -0
- data/lib/smart_prompt/sensenova_adapter.rb +410 -0
- data/lib/smart_prompt/session.rb +140 -0
- data/lib/smart_prompt/sliding_window_strategy.rb +100 -0
- data/lib/smart_prompt/stt_adapter.rb +381 -0
- data/lib/smart_prompt/summary_based_strategy.rb +152 -0
- data/lib/smart_prompt/token_counter.rb +74 -0
- data/lib/smart_prompt/tts_adapter.rb +403 -0
- data/lib/smart_prompt/version.rb +1 -1
- data/lib/smart_prompt/video_generation_adapter.rb +330 -0
- data/lib/smart_prompt/worker.rb +28 -3
- data/lib/smart_prompt/zhipu_adapter.rb +616 -0
- data/lib/smart_prompt.rb +21 -0
- data/workers/history_management_examples.rb +407 -0
- data/workers/image_generation_workers.rb +119 -0
- data/workers/multimodal_workers.rb +110 -0
- data/workers/sensenova_workers.rb +62 -0
- data/workers/stt_workers.rb +195 -0
- data/workers/tts_workers.rb +388 -0
- data/workers/video_generation_workers.rb +264 -0
- data/workers/zhipu_workers.rb +113 -0
- metadata +88 -1
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Conversation Integration with HistoryManager - Implementation Summary
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Successfully integrated the HistoryManager with the Conversation class to provide session-based history management while maintaining backward compatibility with the existing `with_history` parameter.
|
|
5
|
+
|
|
6
|
+
## Changes Made
|
|
7
|
+
|
|
8
|
+
### 1. Conversation Class (`lib/smart_prompt/conversation.rb`)
|
|
9
|
+
|
|
10
|
+
#### Modified `initialize` method:
|
|
11
|
+
- Added optional `session_id` parameter
|
|
12
|
+
- Added `@session_id` and `@use_history_manager` instance variables
|
|
13
|
+
|
|
14
|
+
#### Modified `history_messages` method:
|
|
15
|
+
- Now checks if HistoryManager is available and being used
|
|
16
|
+
- If using HistoryManager, retrieves messages from the session and converts them to hash format
|
|
17
|
+
- Falls back to old implementation (`@engine.history_messages`) if HistoryManager is not available
|
|
18
|
+
|
|
19
|
+
#### Modified `add_message` method:
|
|
20
|
+
- When `with_history` is true and HistoryManager is available:
|
|
21
|
+
- Sets `@use_history_manager` flag to true
|
|
22
|
+
- Generates a default session ID if none exists
|
|
23
|
+
- Adds message to HistoryManager session
|
|
24
|
+
- Falls back to old implementation if HistoryManager is not available
|
|
25
|
+
- Always adds message to local `@messages` array for current conversation
|
|
26
|
+
|
|
27
|
+
#### Added `generate_default_session_id` private method:
|
|
28
|
+
- Generates unique session IDs in format: `default_{timestamp}_{random}`
|
|
29
|
+
|
|
30
|
+
### 2. Engine Class (`lib/smart_prompt/engine.rb`)
|
|
31
|
+
|
|
32
|
+
#### Added `history_manager` attribute reader:
|
|
33
|
+
- Exposes HistoryManager instance to other components
|
|
34
|
+
|
|
35
|
+
#### Modified `initialize` method:
|
|
36
|
+
- Added `@history_manager = nil` initialization
|
|
37
|
+
|
|
38
|
+
#### Modified `load_config` method:
|
|
39
|
+
- Checks for `history` configuration section
|
|
40
|
+
- Initializes HistoryManager if configuration is present
|
|
41
|
+
- Logs initialization success
|
|
42
|
+
|
|
43
|
+
### 3. Worker Class (`lib/smart_prompt/worker.rb`)
|
|
44
|
+
|
|
45
|
+
#### Modified `execute` method:
|
|
46
|
+
- Generates default session ID when:
|
|
47
|
+
- `with_history` is true
|
|
48
|
+
- No `session_id` is provided in params
|
|
49
|
+
- HistoryManager is available
|
|
50
|
+
- Session ID format: `worker_{worker_name}_{timestamp}`
|
|
51
|
+
- Passes session ID to Conversation constructor
|
|
52
|
+
|
|
53
|
+
#### Modified `execute_by_stream` method:
|
|
54
|
+
- Same session ID generation logic as `execute`
|
|
55
|
+
- Ensures streaming workers also support session management
|
|
56
|
+
|
|
57
|
+
## Backward Compatibility
|
|
58
|
+
|
|
59
|
+
### Maintained Features:
|
|
60
|
+
1. **`with_history` parameter**: Still works as before
|
|
61
|
+
2. **Old history implementation**: Falls back automatically when HistoryManager is not configured
|
|
62
|
+
3. **Existing worker definitions**: No changes required to existing workers
|
|
63
|
+
4. **API compatibility**: All existing methods maintain their signatures
|
|
64
|
+
|
|
65
|
+
### Migration Path:
|
|
66
|
+
1. **Without configuration**: System uses old `@engine.history_messages` array
|
|
67
|
+
2. **With configuration**: System automatically uses HistoryManager
|
|
68
|
+
3. **Explicit session IDs**: Can be provided via `session_id` parameter for fine-grained control
|
|
69
|
+
|
|
70
|
+
## Configuration Example
|
|
71
|
+
|
|
72
|
+
To enable HistoryManager, add to your config YAML:
|
|
73
|
+
|
|
74
|
+
```yaml
|
|
75
|
+
history:
|
|
76
|
+
cache_size: 100
|
|
77
|
+
session_defaults:
|
|
78
|
+
max_messages: 50
|
|
79
|
+
max_tokens: 2000
|
|
80
|
+
persistence:
|
|
81
|
+
enabled: true
|
|
82
|
+
storage_path: "./history_data"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Usage Examples
|
|
86
|
+
|
|
87
|
+
### 1. Basic Usage with Auto-Generated Session:
|
|
88
|
+
```ruby
|
|
89
|
+
conversation = SmartPrompt::Conversation.new(engine)
|
|
90
|
+
conversation.add_message({ role: "user", content: "Hello" }, true)
|
|
91
|
+
# Session ID is auto-generated
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 2. Explicit Session ID:
|
|
95
|
+
```ruby
|
|
96
|
+
conversation = SmartPrompt::Conversation.new(engine, nil, "my_session_123")
|
|
97
|
+
conversation.add_message({ role: "user", content: "Hello" }, true)
|
|
98
|
+
# Uses "my_session_123"
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 3. Worker with Default Session:
|
|
102
|
+
```ruby
|
|
103
|
+
engine.call_worker("my_worker", with_history: true)
|
|
104
|
+
# Session ID: "worker_my_worker_{timestamp}"
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### 4. Worker with Custom Session:
|
|
108
|
+
```ruby
|
|
109
|
+
engine.call_worker("my_worker", with_history: true, session_id: "custom_session")
|
|
110
|
+
# Uses "custom_session"
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Testing
|
|
114
|
+
|
|
115
|
+
Created comprehensive integration tests:
|
|
116
|
+
|
|
117
|
+
### `test/conversation_integration_test.rb`:
|
|
118
|
+
- Tests Conversation with HistoryManager
|
|
119
|
+
- Tests Conversation without HistoryManager (backward compatibility)
|
|
120
|
+
- Tests backward compatibility with `with_history` parameter
|
|
121
|
+
- Tests default session creation
|
|
122
|
+
- Tests session isolation between conversations
|
|
123
|
+
- Tests message format conversion
|
|
124
|
+
|
|
125
|
+
### `test/worker_history_integration_test.rb`:
|
|
126
|
+
- Tests worker with default session creation
|
|
127
|
+
- Tests worker with explicit session ID
|
|
128
|
+
- Tests worker without history
|
|
129
|
+
- Tests multiple worker calls to same session
|
|
130
|
+
- Tests worker session isolation
|
|
131
|
+
|
|
132
|
+
All tests pass successfully, confirming:
|
|
133
|
+
- ✅ Integration works correctly
|
|
134
|
+
- ✅ Backward compatibility is maintained
|
|
135
|
+
- ✅ Session isolation is enforced
|
|
136
|
+
- ✅ Default session creation works
|
|
137
|
+
- ✅ Existing tests continue to pass
|
|
138
|
+
|
|
139
|
+
## Requirements Validated
|
|
140
|
+
|
|
141
|
+
This implementation satisfies the following requirements from the spec:
|
|
142
|
+
|
|
143
|
+
- **Requirement 5.1**: Supports existing `with_history: true` parameter
|
|
144
|
+
- **Requirement 5.2**: Creates default session for workers when no session ID provided
|
|
145
|
+
- **Requirement 5.4**: Maintains backward compatibility with existing API
|
|
146
|
+
|
|
147
|
+
## Next Steps
|
|
148
|
+
|
|
149
|
+
The integration is complete and ready for use. The next task in the implementation plan is:
|
|
150
|
+
|
|
151
|
+
**Task 14**: Integrate with Engine class
|
|
152
|
+
- Add HistoryManager initialization to Engine ✅ (Already done)
|
|
153
|
+
- Add history configuration loading from YAML ✅ (Already done)
|
|
154
|
+
- Expose history_manager accessor ✅ (Already done)
|
|
155
|
+
- Add deprecation warning for old history_messages (Optional)
|
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
# History Management Examples
|
|
2
|
+
|
|
3
|
+
This directory contains comprehensive examples demonstrating the new intelligent history management features in SmartPrompt.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The history management system provides:
|
|
8
|
+
|
|
9
|
+
- **Session Isolation**: Each conversation has its own isolated history
|
|
10
|
+
- **Intelligent Context Management**: Multiple strategies for selecting relevant messages
|
|
11
|
+
- **Automatic Compression**: Summarize old messages to save tokens
|
|
12
|
+
- **Persistence**: Save and restore conversations from disk
|
|
13
|
+
- **Monitoring**: Track usage statistics and metrics
|
|
14
|
+
- **Backward Compatibility**: Works seamlessly with existing code
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
### Running the Examples
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
# Run all examples
|
|
22
|
+
ruby examples/history_management_examples.rb
|
|
23
|
+
|
|
24
|
+
# Or run specific sections by modifying the file
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Basic Usage
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
require 'smart_prompt'
|
|
31
|
+
|
|
32
|
+
# Simple chat with automatic session management
|
|
33
|
+
engine.call_worker(:basic_chat, {
|
|
34
|
+
message: "Hello! How are you?"
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
# Chat with explicit session ID
|
|
38
|
+
engine.call_worker(:explicit_session_chat, {
|
|
39
|
+
session_id: "user_123",
|
|
40
|
+
message: "Hello! How are you?"
|
|
41
|
+
})
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Examples Included
|
|
45
|
+
|
|
46
|
+
### 1. Explicit Session Management
|
|
47
|
+
|
|
48
|
+
Demonstrates how to create and manage isolated conversation sessions.
|
|
49
|
+
|
|
50
|
+
**Key Features:**
|
|
51
|
+
- Unique session IDs for each conversation
|
|
52
|
+
- Session isolation (no cross-contamination)
|
|
53
|
+
- Multi-user support
|
|
54
|
+
|
|
55
|
+
**Requirements Validated:** 1.1, 1.2, 1.3, 1.4, 5.2
|
|
56
|
+
|
|
57
|
+
**Example:**
|
|
58
|
+
```ruby
|
|
59
|
+
# User 1's conversation
|
|
60
|
+
SmartPrompt.run_worker(:explicit_session_chat, {
|
|
61
|
+
session_id: "user_alice",
|
|
62
|
+
message: "My name is Alice."
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
# User 2's conversation (completely isolated)
|
|
66
|
+
SmartPrompt.run_worker(:explicit_session_chat, {
|
|
67
|
+
session_id: "user_bob",
|
|
68
|
+
message: "My name is Bob."
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
# Continue User 1's conversation
|
|
72
|
+
SmartPrompt.run_worker(:explicit_session_chat, {
|
|
73
|
+
session_id: "user_alice",
|
|
74
|
+
message: "What's my name?" # Will remember "Alice"
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 2. Different Context Strategies
|
|
79
|
+
|
|
80
|
+
Demonstrates the four available context strategies for managing conversation history.
|
|
81
|
+
|
|
82
|
+
#### 2.1 Sliding Window Strategy
|
|
83
|
+
|
|
84
|
+
Keeps the most recent N messages. Best for real-time chat and customer support.
|
|
85
|
+
|
|
86
|
+
**Requirements Validated:** 8.2
|
|
87
|
+
|
|
88
|
+
**Example:**
|
|
89
|
+
```ruby
|
|
90
|
+
SmartPrompt.run_worker(:sliding_window_chat, {
|
|
91
|
+
session_id: "support_001",
|
|
92
|
+
message: "I need help with my account."
|
|
93
|
+
})
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**Configuration:**
|
|
97
|
+
- `max_messages: 20` - Keep last 20 messages
|
|
98
|
+
- `max_tokens: 2000` - Maximum 2000 tokens
|
|
99
|
+
- `preserve_system_messages: true` - Always keep system messages
|
|
100
|
+
|
|
101
|
+
#### 2.2 Relevance-Based Strategy
|
|
102
|
+
|
|
103
|
+
Selects semantically relevant messages based on importance scoring. Best for Q&A systems and knowledge bases.
|
|
104
|
+
|
|
105
|
+
**Requirements Validated:** 8.1, 8.3
|
|
106
|
+
|
|
107
|
+
**Example:**
|
|
108
|
+
```ruby
|
|
109
|
+
SmartPrompt.run_worker(:relevance_chat, {
|
|
110
|
+
session_id: "qa_001",
|
|
111
|
+
message: "Tell me about Ruby programming."
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
# Later in the conversation...
|
|
115
|
+
SmartPrompt.run_worker(:relevance_chat, {
|
|
116
|
+
session_id: "qa_001",
|
|
117
|
+
message: "What were the Ruby features we discussed?"
|
|
118
|
+
# Will include relevant earlier messages even if not recent
|
|
119
|
+
})
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**Configuration:**
|
|
123
|
+
- `max_messages: 100` - Consider up to 100 messages
|
|
124
|
+
- `max_tokens: 4000` - Maximum 4000 tokens
|
|
125
|
+
- Importance scoring based on recency and relevance
|
|
126
|
+
|
|
127
|
+
#### 2.3 Summary-Based Strategy
|
|
128
|
+
|
|
129
|
+
Automatically compresses old messages into summaries. Best for long conversations and extended dialogues.
|
|
130
|
+
|
|
131
|
+
**Requirements Validated:** 9.1, 9.2, 9.3
|
|
132
|
+
|
|
133
|
+
**Example:**
|
|
134
|
+
```ruby
|
|
135
|
+
SmartPrompt.run_worker(:summary_chat, {
|
|
136
|
+
session_id: "long_conv_001",
|
|
137
|
+
message: "Let's discuss the history of computing."
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
# After many messages, old ones are automatically summarized
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
**Configuration:**
|
|
144
|
+
- `max_messages: 200` - Can handle up to 200 messages
|
|
145
|
+
- `max_tokens: 8000` - Maximum 8000 tokens
|
|
146
|
+
- Automatic summarization when threshold is reached
|
|
147
|
+
|
|
148
|
+
#### 2.4 Hybrid Strategy
|
|
149
|
+
|
|
150
|
+
Adaptively combines multiple strategies based on conversation characteristics. Best for general-purpose applications.
|
|
151
|
+
|
|
152
|
+
**Requirements Validated:** 6.1
|
|
153
|
+
|
|
154
|
+
**Example:**
|
|
155
|
+
```ruby
|
|
156
|
+
SmartPrompt.run_worker(:hybrid_chat, {
|
|
157
|
+
session_id: "general_001",
|
|
158
|
+
message: "Let's talk about various topics."
|
|
159
|
+
})
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
**Configuration:**
|
|
163
|
+
- `max_messages: 150` - Balanced message limit
|
|
164
|
+
- `max_tokens: 6000` - Balanced token limit
|
|
165
|
+
- Automatically switches between strategies based on conversation state
|
|
166
|
+
|
|
167
|
+
### 3. Session Operations
|
|
168
|
+
|
|
169
|
+
Demonstrates all available session management operations.
|
|
170
|
+
|
|
171
|
+
**Requirements Validated:** 4.1, 4.2, 4.3, 4.4, 4.5
|
|
172
|
+
|
|
173
|
+
#### Get Statistics
|
|
174
|
+
|
|
175
|
+
```ruby
|
|
176
|
+
stats = SmartPrompt.run_worker(:session_operations, {
|
|
177
|
+
session_id: "user_123",
|
|
178
|
+
operation: :stats
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
# Returns:
|
|
182
|
+
# {
|
|
183
|
+
# session_id: "user_123",
|
|
184
|
+
# message_count: 15,
|
|
185
|
+
# total_tokens: 1234,
|
|
186
|
+
# created_at: <timestamp>,
|
|
187
|
+
# updated_at: <timestamp>,
|
|
188
|
+
# config: {...}
|
|
189
|
+
# }
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
#### Search Messages
|
|
193
|
+
|
|
194
|
+
```ruby
|
|
195
|
+
results = SmartPrompt.run_worker(:session_operations, {
|
|
196
|
+
session_id: "user_123",
|
|
197
|
+
operation: :search,
|
|
198
|
+
query: "Ruby programming"
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
# Returns: "Found 3 messages matching 'Ruby programming'"
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
#### Export Session
|
|
205
|
+
|
|
206
|
+
```ruby
|
|
207
|
+
data = SmartPrompt.run_worker(:session_operations, {
|
|
208
|
+
session_id: "user_123",
|
|
209
|
+
operation: :export
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
# Returns JSON with all session data
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
#### Clear Session
|
|
216
|
+
|
|
217
|
+
```ruby
|
|
218
|
+
SmartPrompt.run_worker(:session_operations, {
|
|
219
|
+
session_id: "user_123",
|
|
220
|
+
operation: :clear
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
# Removes all messages except system messages
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
#### Delete Session
|
|
227
|
+
|
|
228
|
+
```ruby
|
|
229
|
+
SmartPrompt.run_worker(:session_operations, {
|
|
230
|
+
session_id: "user_123",
|
|
231
|
+
operation: :delete
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
# Completely removes session from memory and disk
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### 4. Backward Compatibility
|
|
238
|
+
|
|
239
|
+
Demonstrates that existing code continues to work without modifications.
|
|
240
|
+
|
|
241
|
+
**Requirements Validated:** 5.1, 5.2, 5.3, 5.4
|
|
242
|
+
|
|
243
|
+
**Example:**
|
|
244
|
+
```ruby
|
|
245
|
+
# Old API - still works!
|
|
246
|
+
SmartPrompt.run_worker(:legacy_chat, {
|
|
247
|
+
message: "Hello!"
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
# Automatically creates a default session
|
|
251
|
+
# Benefits from new features without code changes
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### 5. Advanced Use Cases
|
|
255
|
+
|
|
256
|
+
#### Multi-User Chat Application
|
|
257
|
+
|
|
258
|
+
```ruby
|
|
259
|
+
# User 1
|
|
260
|
+
SmartPrompt.run_worker(:multi_user_chat, {
|
|
261
|
+
user_id: "001",
|
|
262
|
+
user_name: "Alice",
|
|
263
|
+
message: "Hi! I'm working on a Ruby project."
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
# User 2 (completely isolated)
|
|
267
|
+
SmartPrompt.run_worker(:multi_user_chat, {
|
|
268
|
+
user_id: "002",
|
|
269
|
+
user_name: "Bob",
|
|
270
|
+
message: "Hello! I need help with Python."
|
|
271
|
+
})
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
#### Streaming with History
|
|
275
|
+
|
|
276
|
+
```ruby
|
|
277
|
+
SmartPrompt.run_worker(:streaming_history_chat, {
|
|
278
|
+
session_id: "stream_001",
|
|
279
|
+
message: "Tell me a long story."
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
# Streams response while maintaining history
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
#### Custom Configuration
|
|
286
|
+
|
|
287
|
+
```ruby
|
|
288
|
+
SmartPrompt.run_worker(:custom_config_chat, {
|
|
289
|
+
session_id: "code_review_001",
|
|
290
|
+
message: "Please review this code: def hello; puts 'world'; end"
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
# Uses fine-tuned configuration for code review
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
#### Persistent Sessions
|
|
297
|
+
|
|
298
|
+
```ruby
|
|
299
|
+
SmartPrompt.run_worker(:persistent_chat, {
|
|
300
|
+
session_id: "persistent_001",
|
|
301
|
+
message: "This conversation will be saved to disk."
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
# Session is automatically saved and can be restored later
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### 6. Monitoring and Debugging
|
|
308
|
+
|
|
309
|
+
#### System-Wide Statistics
|
|
310
|
+
|
|
311
|
+
```ruby
|
|
312
|
+
stats = SmartPrompt.run_worker(:system_stats, {})
|
|
313
|
+
|
|
314
|
+
# Returns:
|
|
315
|
+
# {
|
|
316
|
+
# active_sessions: 5,
|
|
317
|
+
# total_messages: 123,
|
|
318
|
+
# total_tokens: 12345,
|
|
319
|
+
# cache_hit_rate: 0.85,
|
|
320
|
+
# messages_added: 150,
|
|
321
|
+
# context_retrievals: 75
|
|
322
|
+
# }
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
#### Export Prometheus Metrics
|
|
326
|
+
|
|
327
|
+
```ruby
|
|
328
|
+
metrics = SmartPrompt.run_worker(:export_metrics, {})
|
|
329
|
+
|
|
330
|
+
# Returns Prometheus-formatted metrics:
|
|
331
|
+
# smart_prompt_active_sessions 5
|
|
332
|
+
# smart_prompt_total_messages 123
|
|
333
|
+
# smart_prompt_cache_hit_rate 0.85
|
|
334
|
+
# ...
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
## Configuration Options
|
|
338
|
+
|
|
339
|
+
### Session Configuration
|
|
340
|
+
|
|
341
|
+
```ruby
|
|
342
|
+
session_config = {
|
|
343
|
+
max_messages: 100, # Maximum number of messages to keep
|
|
344
|
+
max_tokens: 4000, # Maximum token count
|
|
345
|
+
context_strategy: :sliding_window, # Strategy: :sliding_window, :relevance_based, :summary_based, :hybrid
|
|
346
|
+
preserve_system_messages: true # Always keep system messages
|
|
347
|
+
}
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
### Global Configuration
|
|
351
|
+
|
|
352
|
+
Configure the history manager in your `config/anthropic_config.yml`:
|
|
353
|
+
|
|
354
|
+
```yaml
|
|
355
|
+
history:
|
|
356
|
+
cache_size: 100 # Maximum sessions in cache
|
|
357
|
+
|
|
358
|
+
session_defaults:
|
|
359
|
+
max_messages: 100
|
|
360
|
+
max_tokens: 4000
|
|
361
|
+
context_strategy: sliding_window
|
|
362
|
+
preserve_system_messages: true
|
|
363
|
+
|
|
364
|
+
persistence:
|
|
365
|
+
enabled: true
|
|
366
|
+
backend: filesystem
|
|
367
|
+
storage_path: "./history_data"
|
|
368
|
+
async: true
|
|
369
|
+
|
|
370
|
+
cleanup:
|
|
371
|
+
auto_cleanup: true
|
|
372
|
+
cleanup_interval: 3600 # 1 hour
|
|
373
|
+
session_ttl: 86400 # 24 hours
|
|
374
|
+
|
|
375
|
+
monitoring:
|
|
376
|
+
enabled: true
|
|
377
|
+
log_level: info
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
## Best Practices
|
|
381
|
+
|
|
382
|
+
### 1. Choose the Right Strategy
|
|
383
|
+
|
|
384
|
+
- **Sliding Window**: Use for real-time chat, customer support, or when you only need recent context
|
|
385
|
+
- **Relevance-Based**: Use for Q&A systems, knowledge bases, or when semantic relevance matters
|
|
386
|
+
- **Summary-Based**: Use for long conversations, documentation, or when you need to preserve history
|
|
387
|
+
- **Hybrid**: Use for general-purpose applications or when conversation patterns vary
|
|
388
|
+
|
|
389
|
+
### 2. Set Appropriate Limits
|
|
390
|
+
|
|
391
|
+
```ruby
|
|
392
|
+
# For short conversations
|
|
393
|
+
max_messages: 20
|
|
394
|
+
max_tokens: 2000
|
|
395
|
+
|
|
396
|
+
# For medium conversations
|
|
397
|
+
max_messages: 50
|
|
398
|
+
max_tokens: 4000
|
|
399
|
+
|
|
400
|
+
# For long conversations
|
|
401
|
+
max_messages: 100
|
|
402
|
+
max_tokens: 8000
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### 3. Use Meaningful Session IDs
|
|
406
|
+
|
|
407
|
+
```ruby
|
|
408
|
+
# Good: Descriptive and unique
|
|
409
|
+
session_id: "user_#{user_id}"
|
|
410
|
+
session_id: "conversation_#{conversation_id}"
|
|
411
|
+
session_id: "support_ticket_#{ticket_id}"
|
|
412
|
+
|
|
413
|
+
# Bad: Generic or non-unique
|
|
414
|
+
session_id: "session"
|
|
415
|
+
session_id: "default"
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### 4. Clean Up Old Sessions
|
|
419
|
+
|
|
420
|
+
```ruby
|
|
421
|
+
# Manually trigger cleanup
|
|
422
|
+
engine.history_manager.cleanup_expired_sessions
|
|
423
|
+
|
|
424
|
+
# Or enable automatic cleanup
|
|
425
|
+
cleanup: {
|
|
426
|
+
auto_cleanup: true,
|
|
427
|
+
cleanup_interval: 3600, # Check every hour
|
|
428
|
+
session_ttl: 86400 # Delete after 24 hours
|
|
429
|
+
}
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
### 5. Monitor Performance
|
|
433
|
+
|
|
434
|
+
```ruby
|
|
435
|
+
# Regularly check statistics
|
|
436
|
+
stats = engine.history_manager.get_stats
|
|
437
|
+
|
|
438
|
+
# Monitor cache hit rate (should be > 0.7)
|
|
439
|
+
puts "Cache hit rate: #{stats[:cache_hit_rate]}"
|
|
440
|
+
|
|
441
|
+
# Monitor token usage
|
|
442
|
+
puts "Average tokens per session: #{stats[:tokens_per_session_avg]}"
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
## Troubleshooting
|
|
446
|
+
|
|
447
|
+
### Session Not Found
|
|
448
|
+
|
|
449
|
+
If a session is not found, it may have been evicted from cache or deleted:
|
|
450
|
+
|
|
451
|
+
```ruby
|
|
452
|
+
# Check if session exists
|
|
453
|
+
if engine.history_manager.session_exists?(session_id)
|
|
454
|
+
# Session exists
|
|
455
|
+
else
|
|
456
|
+
# Session was deleted or never created
|
|
457
|
+
end
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
### High Memory Usage
|
|
461
|
+
|
|
462
|
+
If memory usage is high:
|
|
463
|
+
|
|
464
|
+
1. Reduce `cache_size` in configuration
|
|
465
|
+
2. Enable automatic cleanup
|
|
466
|
+
3. Reduce `max_messages` and `max_tokens` per session
|
|
467
|
+
4. Use summary-based strategy for long conversations
|
|
468
|
+
|
|
469
|
+
### Slow Performance
|
|
470
|
+
|
|
471
|
+
If performance is slow:
|
|
472
|
+
|
|
473
|
+
1. Check cache hit rate (should be > 0.7)
|
|
474
|
+
2. Reduce `max_messages` to speed up context retrieval
|
|
475
|
+
3. Use sliding window strategy for faster selection
|
|
476
|
+
4. Enable async persistence
|
|
477
|
+
|
|
478
|
+
## Migration Guide
|
|
479
|
+
|
|
480
|
+
### From Old History API
|
|
481
|
+
|
|
482
|
+
The new system is backward compatible. No changes required!
|
|
483
|
+
|
|
484
|
+
```ruby
|
|
485
|
+
# Old code - still works
|
|
486
|
+
SmartPrompt.define_worker :old_worker do
|
|
487
|
+
prompt(params[:message], with_history: true)
|
|
488
|
+
send_msg(with_history: true)
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
# New code - with explicit session management
|
|
492
|
+
SmartPrompt.define_worker :new_worker do
|
|
493
|
+
session_id = params[:session_id]
|
|
494
|
+
prompt(params[:message], with_history: true, session_id: session_id)
|
|
495
|
+
send_msg
|
|
496
|
+
end
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
### Gradual Migration
|
|
500
|
+
|
|
501
|
+
1. Start using explicit session IDs for new features
|
|
502
|
+
2. Keep existing workers unchanged
|
|
503
|
+
3. Gradually migrate to new API as needed
|
|
504
|
+
4. Both APIs work simultaneously
|
|
505
|
+
|
|
506
|
+
## Requirements Coverage
|
|
507
|
+
|
|
508
|
+
This example set validates the following requirements:
|
|
509
|
+
|
|
510
|
+
- **Requirement 1**: Session Isolation (1.1, 1.2, 1.3, 1.4)
|
|
511
|
+
- **Requirement 2**: Message and Token Limits (2.1, 2.2, 2.3, 2.4, 2.5)
|
|
512
|
+
- **Requirement 3**: Persistence (3.1, 3.2, 3.3)
|
|
513
|
+
- **Requirement 4**: Session Operations (4.1, 4.2, 4.3, 4.4, 4.5)
|
|
514
|
+
- **Requirement 5**: Backward Compatibility (5.1, 5.2, 5.3, 5.4, 5.5)
|
|
515
|
+
- **Requirement 6**: Configuration (6.1)
|
|
516
|
+
- **Requirement 8**: Intelligent Context Management (8.1, 8.2, 8.3)
|
|
517
|
+
- **Requirement 9**: Automatic Compression (9.1, 9.2, 9.3)
|
|
518
|
+
- **Requirement 11**: Monitoring (11.3, 11.5)
|
|
519
|
+
|
|
520
|
+
## Additional Resources
|
|
521
|
+
|
|
522
|
+
- [Design Document](.kiro/specs/history-optimization/design.md)
|
|
523
|
+
- [Requirements Document](.kiro/specs/history-optimization/requirements.md)
|
|
524
|
+
- [Implementation Tasks](.kiro/specs/history-optimization/tasks.md)
|
|
525
|
+
- [Main README](../README.md)
|
|
526
|
+
|
|
527
|
+
## Support
|
|
528
|
+
|
|
529
|
+
For questions or issues:
|
|
530
|
+
1. Check the design document for detailed architecture
|
|
531
|
+
2. Review the requirements document for expected behavior
|
|
532
|
+
3. Examine the worker examples in `workers/history_management_examples.rb`
|
|
533
|
+
4. Run the comprehensive examples in this directory
|