Spaces:
Sleeping
Sleeping
Commit
·
6c7d228
1
Parent(s):
4341ed8
Upd memo README
Browse files- memo/README.md +195 -60
memo/README.md
CHANGED
|
@@ -1,111 +1,246 @@
|
|
| 1 |
# Memory System for EdSummariser
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
##
|
| 6 |
|
| 7 |
-
###
|
| 8 |
-
- **
|
| 9 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
- **Legacy Memory**: In-memory LRU system for backward compatibility
|
|
|
|
| 11 |
|
| 12 |
-
###
|
| 13 |
-
- **
|
| 14 |
-
- **
|
| 15 |
-
- **
|
| 16 |
-
- **
|
| 17 |
-
- **Modular Design**: Clean separation of concerns across files
|
| 18 |
|
| 19 |
## 📁 Architecture
|
| 20 |
|
| 21 |
```
|
| 22 |
memo/
|
| 23 |
├── README.md # This documentation
|
| 24 |
-
├── core.py # Main memory system
|
| 25 |
-
├──
|
| 26 |
├── persistent.py # MongoDB-based persistent storage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
├── nvidia.py # NVIDIA API integration
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
```
|
| 31 |
|
| 32 |
-
## 🚀 Core
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
## 🔧 Quick Start
|
| 40 |
|
| 41 |
```python
|
| 42 |
from memo.core import get_memory_system
|
| 43 |
-
from memo.
|
| 44 |
|
| 45 |
-
# Initialize
|
| 46 |
memory = get_memory_system()
|
| 47 |
-
|
| 48 |
|
| 49 |
-
# Basic operations
|
| 50 |
memory.add("user123", "q: What is AI?\na: AI is artificial intelligence")
|
| 51 |
recent = memory.recent("user123", 3)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
```
|
| 62 |
|
| 63 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
## 🔧 Configuration
|
| 73 |
|
| 74 |
```bash
|
|
|
|
| 75 |
MONGO_URI=mongodb://localhost:27017
|
| 76 |
MONGO_DB=studybuddy
|
|
|
|
|
|
|
| 77 |
NVIDIA_SMALL=meta/llama-3.1-8b-instruct
|
| 78 |
```
|
| 79 |
|
| 80 |
-
## 🛠️
|
| 81 |
|
| 82 |
-
###
|
| 83 |
- `get_memory_system()` - Main entry point
|
| 84 |
-
- `memory.
|
| 85 |
-
- `memory.
|
|
|
|
| 86 |
- `memory.search_memories()` - Semantic search
|
| 87 |
-
- `history_manager.files_relevance()` - File relevance detection
|
| 88 |
|
| 89 |
-
###
|
| 90 |
-
-
|
| 91 |
-
-
|
| 92 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
###
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
-
### Performance Optimizations
|
| 102 |
-
- Shared cosine similarity function
|
| 103 |
- Efficient MongoDB indexing
|
| 104 |
- Lazy loading of embeddings
|
| 105 |
- Memory consolidation and pruning
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
- Add new memory types in `persistent.py`
|
| 109 |
-
- Enhance context selection in `context.py`
|
| 110 |
-
- Add new AI integrations in `nvidia.py`
|
| 111 |
-
- Extend memory operations in `core.py`
|
|
|
|
| 1 |
# Memory System for EdSummariser
|
| 2 |
|
| 3 |
+
A sophisticated memory management system that provides intelligent context retrieval, conversation continuity, and enhancement-focused memory planning for the EdSummariser application.
|
| 4 |
|
| 5 |
+
## 🧠 Key Features
|
| 6 |
|
| 7 |
+
### **Memory Planning System**
|
| 8 |
+
- **Intent Detection**: Automatically detects user intent (enhancement, clarification, comparison, etc.)
|
| 9 |
+
- **Strategy Planning**: Selects optimal memory retrieval strategy based on user intent
|
| 10 |
+
- **Enhancement Focus**: Specialized handling for "Enhance...", "Be more detailed" requests
|
| 11 |
+
- **Q&A Prioritization**: Focuses on past Q&A data for enhancement requests
|
| 12 |
+
|
| 13 |
+
### **Dual Memory Architecture**
|
| 14 |
+
- **Enhanced Memory**: MongoDB-based persistent storage with semantic search
|
| 15 |
- **Legacy Memory**: In-memory LRU system for backward compatibility
|
| 16 |
+
- **Graceful Fallback**: Automatically falls back when MongoDB unavailable
|
| 17 |
|
| 18 |
+
### **Smart Context Retrieval**
|
| 19 |
+
- **Semantic Search**: Cosine similarity-based memory selection
|
| 20 |
+
- **AI-Powered Selection**: NVIDIA model integration for intelligent memory filtering
|
| 21 |
+
- **Session Management**: Tracks conversation continuity and context switches
|
| 22 |
+
- **Memory Consolidation**: Prevents information overload through intelligent pruning
|
|
|
|
| 23 |
|
| 24 |
## 📁 Architecture
|
| 25 |
|
| 26 |
```
|
| 27 |
memo/
|
| 28 |
├── README.md # This documentation
|
| 29 |
+
├── core.py # Main memory system with planning integration
|
| 30 |
+
├── planning.py # Memory planning and strategy system
|
| 31 |
├── persistent.py # MongoDB-based persistent storage
|
| 32 |
+
├── legacy.py # In-memory LRU system
|
| 33 |
+
├── retrieval.py # Context retrieval manager
|
| 34 |
+
├── conversation.py # Conversation management orchestrator
|
| 35 |
+
├── sessions.py # Session tracking and context switching
|
| 36 |
+
├── consolidation.py # Memory consolidation and pruning
|
| 37 |
+
├── context.py # Context management utilities
|
| 38 |
+
├── history.py # History management functions
|
| 39 |
├── nvidia.py # NVIDIA API integration
|
| 40 |
+
└── plan/ # Modular planning components
|
| 41 |
+
├── intent.py # Intent detection
|
| 42 |
+
├── strategy.py # Strategy planning
|
| 43 |
+
└── execution.py # Execution engine
|
| 44 |
```
|
| 45 |
|
| 46 |
+
## 🚀 Core Capabilities
|
| 47 |
|
| 48 |
+
### **Enhancement Request Handling**
|
| 49 |
+
```python
|
| 50 |
+
# Automatically detects and handles enhancement requests
|
| 51 |
+
question = "Enhance the previous answer about machine learning"
|
| 52 |
+
# System uses FOCUSED_QA strategy with Q&A prioritization
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
### **Intent-Based Memory Planning**
|
| 56 |
+
- **ENHANCEMENT**: Uses FOCUSED_QA strategy for detailed responses
|
| 57 |
+
- **CLARIFICATION**: Uses RECENT_FOCUS strategy for context
|
| 58 |
+
- **COMPARISON**: Uses BROAD_CONTEXT strategy for comprehensive data
|
| 59 |
+
- **REFERENCE**: Uses FOCUSED_QA strategy for specific past content
|
| 60 |
+
- **NEW_TOPIC**: Uses SEMANTIC_DEEP strategy for discovery
|
| 61 |
+
|
| 62 |
+
### **Memory Types**
|
| 63 |
+
| Type | Description | Storage | Usage |
|
| 64 |
+
|------|-------------|---------|-------|
|
| 65 |
+
| `conversation` | Chat history & Q&A pairs | Both | Primary context source |
|
| 66 |
+
| `user_preference` | User preferences | Enhanced only | Personalization |
|
| 67 |
+
| `project_context` | Project-specific knowledge | Enhanced only | Project continuity |
|
| 68 |
+
| `knowledge_fact` | Domain facts | Enhanced only | Knowledge base |
|
| 69 |
|
| 70 |
## 🔧 Quick Start
|
| 71 |
|
| 72 |
```python
|
| 73 |
from memo.core import get_memory_system
|
| 74 |
+
from memo.planning import get_memory_planner
|
| 75 |
|
| 76 |
+
# Initialize memory system
|
| 77 |
memory = get_memory_system()
|
| 78 |
+
planner = get_memory_planner(memory, embedder)
|
| 79 |
|
| 80 |
+
# Basic operations (backward compatible)
|
| 81 |
memory.add("user123", "q: What is AI?\na: AI is artificial intelligence")
|
| 82 |
recent = memory.recent("user123", 3)
|
| 83 |
|
| 84 |
+
# Smart context with planning
|
| 85 |
+
recent_context, semantic_context, metadata = await memory.get_smart_context(
|
| 86 |
+
user_id="user123",
|
| 87 |
+
question="Enhance the previous answer about deep learning",
|
| 88 |
+
nvidia_rotator=rotator
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Enhancement-specific context
|
| 92 |
+
enhancement_context = await memory.get_enhancement_context(
|
| 93 |
+
user_id="user123",
|
| 94 |
+
question="Be more detailed about neural networks",
|
| 95 |
+
nvidia_rotator=rotator
|
| 96 |
+
)
|
| 97 |
```
|
| 98 |
|
| 99 |
+
## 🎯 Memory Planning Strategies
|
| 100 |
+
|
| 101 |
+
### **FOCUSED_QA** (Enhancement Requests)
|
| 102 |
+
- Prioritizes past Q&A pairs
|
| 103 |
+
- Uses very low similarity threshold (0.05) for maximum recall
|
| 104 |
+
- AI-powered selection of most relevant Q&A memories
|
| 105 |
+
- Optimized for detailed, comprehensive responses
|
| 106 |
|
| 107 |
+
### **RECENT_FOCUS** (Clarification Requests)
|
| 108 |
+
- Focuses on recent conversation context
|
| 109 |
+
- Balances recent and semantic context
|
| 110 |
+
- Ideal for follow-up questions
|
| 111 |
+
|
| 112 |
+
### **BROAD_CONTEXT** (Comparison Requests)
|
| 113 |
+
- Retrieves wide range of memories
|
| 114 |
+
- Higher similarity threshold for relevance
|
| 115 |
+
- Suitable for comparative analysis
|
| 116 |
+
|
| 117 |
+
### **SEMANTIC_DEEP** (New Topics)
|
| 118 |
+
- Deep semantic search across all memories
|
| 119 |
+
- AI-powered selection for discovery
|
| 120 |
+
- Comprehensive knowledge retrieval
|
| 121 |
+
|
| 122 |
+
### **MIXED_APPROACH** (Continuation)
|
| 123 |
+
- Combines recent and semantic context
|
| 124 |
+
- Balanced approach for ongoing conversations
|
| 125 |
+
- Adaptive based on conversation state
|
| 126 |
|
| 127 |
## 🔧 Configuration
|
| 128 |
|
| 129 |
```bash
|
| 130 |
+
# MongoDB Configuration
|
| 131 |
MONGO_URI=mongodb://localhost:27017
|
| 132 |
MONGO_DB=studybuddy
|
| 133 |
+
|
| 134 |
+
# NVIDIA API Configuration
|
| 135 |
NVIDIA_SMALL=meta/llama-3.1-8b-instruct
|
| 136 |
```
|
| 137 |
|
| 138 |
+
## 🛠️ Key Functions
|
| 139 |
|
| 140 |
+
### **Core Memory System**
|
| 141 |
- `get_memory_system()` - Main entry point
|
| 142 |
+
- `memory.get_smart_context()` - Intelligent context with planning
|
| 143 |
+
- `memory.get_enhancement_context()` - Enhancement-specific context
|
| 144 |
+
- `memory.add_conversation_memory()` - Add structured memories
|
| 145 |
- `memory.search_memories()` - Semantic search
|
|
|
|
| 146 |
|
| 147 |
+
### **Memory Planning**
|
| 148 |
+
- `planner.plan_memory_strategy()` - Plan retrieval strategy
|
| 149 |
+
- `planner.execute_memory_plan()` - Execute planned strategy
|
| 150 |
+
- `planner._detect_user_intent()` - Detect user intent
|
| 151 |
+
|
| 152 |
+
### **Session Management**
|
| 153 |
+
- `session_manager.get_or_create_session()` - Session tracking
|
| 154 |
+
- `session_manager.detect_context_switch()` - Context switching
|
| 155 |
+
- `session_manager.get_conversation_insights()` - Conversation analytics
|
| 156 |
+
|
| 157 |
+
## 🧪 Enhancement Request Examples
|
| 158 |
+
|
| 159 |
+
The system automatically handles various enhancement patterns:
|
| 160 |
+
|
| 161 |
+
```python
|
| 162 |
+
# These all trigger FOCUSED_QA strategy:
|
| 163 |
+
"Enhance the previous answer about machine learning"
|
| 164 |
+
"Be more detailed about neural networks"
|
| 165 |
+
"Elaborate on the explanation of deep learning"
|
| 166 |
+
"Tell me more about what we discussed"
|
| 167 |
+
"Go deeper into the topic"
|
| 168 |
+
"Provide more context about..."
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
## 🔬 Technical Details
|
| 172 |
|
| 173 |
+
### **Intent Detection**
|
| 174 |
+
- Pattern-based detection using regex
|
| 175 |
+
- AI-powered detection using NVIDIA models
|
| 176 |
+
- Fallback to continuation for ambiguous cases
|
| 177 |
|
| 178 |
+
### **Memory Selection**
|
| 179 |
+
- Cosine similarity for semantic matching
|
| 180 |
+
- AI-powered selection for optimal relevance
|
| 181 |
+
- Configurable similarity thresholds per strategy
|
| 182 |
|
| 183 |
+
### **Performance Optimizations**
|
|
|
|
| 184 |
- Efficient MongoDB indexing
|
| 185 |
- Lazy loading of embeddings
|
| 186 |
- Memory consolidation and pruning
|
| 187 |
+
- Cached context for session continuity
|
| 188 |
+
|
| 189 |
+
### **Error Handling**
|
| 190 |
+
- Multiple fallback mechanisms
|
| 191 |
+
- Graceful degradation when services unavailable
|
| 192 |
+
- Comprehensive logging for debugging
|
| 193 |
+
- Backward compatibility maintained
|
| 194 |
+
|
| 195 |
+
## 🚀 Advanced Usage
|
| 196 |
+
|
| 197 |
+
### **Custom Memory Planning**
|
| 198 |
+
```python
|
| 199 |
+
# Create custom execution plan
|
| 200 |
+
execution_plan = {
|
| 201 |
+
"intent": QueryIntent.ENHANCEMENT,
|
| 202 |
+
"strategy": MemoryStrategy.FOCUSED_QA,
|
| 203 |
+
"retrieval_params": {
|
| 204 |
+
"recent_limit": 5,
|
| 205 |
+
"semantic_limit": 10,
|
| 206 |
+
"qa_focus": True,
|
| 207 |
+
"enhancement_mode": True,
|
| 208 |
+
"similarity_threshold": 0.05
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
# Execute custom plan
|
| 213 |
+
recent, semantic, metadata = await planner.execute_memory_plan(
|
| 214 |
+
user_id, question, execution_plan, nvidia_rotator
|
| 215 |
+
)
|
| 216 |
+
```
|
| 217 |
+
|
| 218 |
+
### **Memory Consolidation**
|
| 219 |
+
```python
|
| 220 |
+
# Consolidate and prune memories
|
| 221 |
+
consolidation_result = await memory.consolidate_memories(
|
| 222 |
+
user_id="user123",
|
| 223 |
+
nvidia_rotator=rotator
|
| 224 |
+
)
|
| 225 |
+
```
|
| 226 |
+
|
| 227 |
+
## 🔄 Integration Points
|
| 228 |
+
|
| 229 |
+
The memory system integrates seamlessly with:
|
| 230 |
+
- **Chat Routes**: Automatic context retrieval
|
| 231 |
+
- **Report Generation**: Enhanced instruction processing
|
| 232 |
+
- **File Processing**: Relevance detection
|
| 233 |
+
- **User Sessions**: Continuity tracking
|
| 234 |
+
- **API Rotators**: AI-powered enhancements
|
| 235 |
+
|
| 236 |
+
## 📊 Monitoring
|
| 237 |
+
|
| 238 |
+
The system provides comprehensive metadata:
|
| 239 |
+
- Intent detection results
|
| 240 |
+
- Strategy selection rationale
|
| 241 |
+
- Memory retrieval statistics
|
| 242 |
+
- Enhancement focus indicators
|
| 243 |
+
- Session continuity tracking
|
| 244 |
+
- Performance metrics
|
| 245 |
|
| 246 |
+
This memory system ensures that enhancement requests like "Enhance..." or "Be more detailed" are handled with maximum effectiveness by focusing on past Q&A data and using intelligent memory planning strategies.
|
|
|
|
|
|
|
|
|
|
|
|