File size: 3,822 Bytes
aa9003d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90c94b5
aa9003d
 
 
 
 
 
 
 
 
 
 
 
90c94b5
aa9003d
90c94b5
 
 
 
aa9003d
90c94b5
aa9003d
 
 
 
 
90c94b5
aa9003d
 
 
90c94b5
aa9003d
90c94b5
aa9003d
90c94b5
 
 
 
 
 
 
 
aa9003d
 
90c94b5
aa9003d
90c94b5
 
 
 
 
 
aa9003d
90c94b5
aa9003d
 
90c94b5
 
 
aa9003d
 
90c94b5
aa9003d
90c94b5
 
 
 
 
 
aa9003d
90c94b5
 
 
 
aa9003d
90c94b5
aa9003d
90c94b5
 
 
 
aa9003d
90c94b5
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Memory System for EdSummariser

This directory contains a clean, modular memory and history management system for the EdSummariser application, designed to provide superior chat continuity and context awareness while maintaining simplicity and efficiency.

## 🚀 Features

### Core Memory Types
- **Conversation Memory**: Stores and retrieves chat history with intelligent summarization
- **Enhanced Memory**: MongoDB-based persistent storage with semantic search (when available)
- **Legacy Memory**: In-memory LRU system for backward compatibility

### Key Capabilities
- **Backward Compatibility**: All existing code works unchanged
- **Enhanced Features**: MongoDB persistence and semantic search when available
- **Graceful Fallback**: Falls back to legacy system if MongoDB unavailable
- **Zero Breaking Changes**: No modifications required to existing code
- **Modular Design**: Clean separation of concerns across files

## 📁 Architecture

```
memo/
├── README.md                    # This documentation
├── core.py                      # Main memory system Legacy memory
├── legacy.py                    # Legacy in-memory LRU system
├── persistent.py                # MongoDB-based persistent storage
├── nvidia.py                    # NVIDIA API integration
├── context.py                   # Context retrieval and management
└── history.py                   # History management functions
```

## 🚀 Core Features

- **Dual Memory System**: Legacy LRU + MongoDB persistent storage
- **Smart Context Selection**: NVIDIA AI + semantic similarity
- **Graceful Fallback**: Works with or without MongoDB
- **Zero Breaking Changes**: Backward compatible with existing code

## 🔧 Quick Start

```python
from memo.core import get_memory_system
from memo.history import get_history_manager

# Initialize
memory = get_memory_system()
history_manager = get_history_manager(memory)

# Basic operations
memory.add("user123", "q: What is AI?\na: AI is artificial intelligence")
recent = memory.recent("user123", 3)

# Enhanced features (when MongoDB available)
if memory.is_enhanced_available():
    await memory.add_conversation_memory(
        user_id="user123",
        question="How to implement auth?",
        answer="Use JWT tokens...",
        project_id="my_project"
    )
```

## 🧠 Memory Types

| Type | Description | Storage |
|------|-------------|---------|
| `conversation` | Chat history & Q&A pairs | Both |
| `user_preference` | User preferences | Enhanced only |
| `project_context` | Project-specific knowledge | Enhanced only |
| `knowledge_fact` | Domain facts | Enhanced only |

## 🔧 Configuration

```bash
MONGO_URI=mongodb://localhost:27017
MONGO_DB=studybuddy
NVIDIA_SMALL=meta/llama-3.1-8b-instruct
```

## 🛠️ Maintenance

### Key Functions
- `get_memory_system()` - Main entry point
- `memory.add()` - Add memory (legacy compatible)
- `memory.get_conversation_context()` - Get context
- `memory.search_memories()` - Semantic search
- `history_manager.files_relevance()` - File relevance detection

### Error Handling
- Multiple fallback mechanisms
- Graceful degradation when services unavailable
- Comprehensive logging for debugging

## 🔬 R&D Notes

### Context Selection Algorithm
1. **Recent Context**: NVIDIA AI selection from recent memories
2. **Semantic Context**: Cosine similarity search across all memories
3. **Fallback**: Direct memory retrieval if AI/semantic fails

### Performance Optimizations
- Shared cosine similarity function
- Efficient MongoDB indexing
- Lazy loading of embeddings
- Memory consolidation and pruning

### Extension Points
- Add new memory types in `persistent.py`
- Enhance context selection in `context.py`
- Add new AI integrations in `nvidia.py`
- Extend memory operations in `core.py`