File size: 4,828 Bytes
5a6a2cc |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# Long-Term Context Memory Fix
## Problem
After 2-3 interactions, the system loses context and gives factually incorrect answers. In the user's example:
- Discussed Sachin Tendulkar (cricket)
- Lost context of sport and gave gaming journalist advice about Tom Bramwell
## Root Cause Analysis
### Issue 1: Limited Context Window
- Only showing **last 3 interactions** in prompts
- With longer conversations, early context gets lost
### Issue 2: Incomplete Context Storage
- **OLD**: Only stored `user_input`, not the response
- Context looked like this:
```
interactions: [
{"user_input": "Who is Sachin?", "timestamp": "..."},
{"user_input": "Is he the greatest?", "timestamp": "..."}
]
```
- **PROBLEM**: LLM doesn't know what was answered before!
### Issue 3: No Response Tracking
- When retrieving context from DB, only user questions were available
- Missing the actual conversation flow (Q&A pairs)
## Solution Implemented
### 1. Increased Context Window (3 β 5 interactions)
```python
# OLD:
recent_interactions = context.get('interactions', [])[:3]
# NEW:
recent_interactions = context.get('interactions', [])[:5] # Last 5 interactions
```
### 2. Added Response Storage
```python
# OLD:
new_interaction = {
"user_input": user_input,
"timestamp": datetime.now().isoformat()
}
# NEW:
new_interaction = {
"user_input": user_input,
"timestamp": datetime.now().isoformat(),
"response": response # Store the response text β
}
```
### 3. Enhanced Conversation History in Prompts
```python
# OLD format:
"1. User asked: Who is Sachin?\n"
# NEW format:
"Q1: Who is Sachin?
A1: Sachin Ramesh Tendulkar is a legendary Indian cricketer...
Q2: Is he the greatest?
A2: The question of who is the greatest..."
```
### 4. Updated Orchestrator to Save Responses
```python
# After generating response, update context:
response_text = str(result.get('response', ''))
if response_text:
self.context_manager._update_context(context, user_input, response_text)
```
## Files Modified
1. **`src/agents/synthesis_agent.py`**:
- Increased context window from 3 to 5
- Enhanced conversation history format to include Q&A pairs
- Added support for displaying responses in prompts
2. **`context_manager.py`**:
- Updated `_update_context()` to accept `response` parameter
- Now stores full interaction (user_input + response)
3. **`orchestrator_engine.py`**:
- Added call to update context with response after processing
- Ensures responses are saved for future context retrieval
4. **Duplicates in `Research_AI_Assistant/`**: Applied same fixes
## Expected Behavior
### Before Fix:
```
Q1: "Who is Sachin?"
A1: (Cricket info)
Q2: "Is he the greatest?"
A2: (Compares Sachin to Bradman)
Q3: "Define greatness parameters"
A3: β Lost context, gives generic answer
Q4: "Name a cricket journalist"
A4: β Switches to gaming journalist (wrong sport!)
```
### After Fix:
```
Q1: "Who is Sachin?"
A1: (Cricket info) β Saved to context
Q2: "Is he the greatest?"
A2: (Compares Sachin to Bradman) β Saved to context
Context includes: Q1+A1, Q2+A2
Q3: "Define greatness parameters"
A3: β Knows we're talking about CRICKET greatness
Context includes: Q1+A1, Q2+A2, Q3+A3
Q4: "Name a cricket journalist"
A4: β Suggests cricket journalists (Harsha Bhogle, etc.)
Context includes: Q1+A1, Q2+A2, Q3+A3, Q4+A4
```
## Technical Details
### Context Structure Now:
```json
{
"session_id": "d5e8171f",
"interactions": [
{
"user_input": "Who is Sachin?",
"timestamp": "2025-10-27T15:39:32",
"response": "Sachin Ramesh Tendulkar is a legendary Indian cricketer..."
},
{
"user_input": "Is he the greatest?",
"timestamp": "2025-10-27T15:40:04",
"response": "The question of who is the greatest cricketer..."
}
]
}
```
### Prompt Format:
```
User Question: Define greatness parameters
Previous conversation:
Q1: Who is Sachin?
A1: Sachin Ramesh Tendulkar is a legendary Indian cricketer...
Q2: Is he the greatest? What about Don Bradman?
A2: The question of who is the greatest cricketer...
Instructions: Provide a comprehensive, helpful response that directly addresses the question. If there's conversation context, use it to answer the current question appropriately.
```
## Testing
To verify the fix:
1. Ask about a specific topic: "Who is Sachin Tendulkar?"
2. Ask 3-4 follow-up questions without mentioning the sport
3. Verify the system still knows you're talking about cricket
4. Check logs for "context has X interactions"
## Impact
- β
Better context retention (5 vs 3 interactions)
- β
Complete conversation history (Q&A pairs)
- β
Reduced factual errors due to context loss
- β
More coherent multi-turn conversations
- β
Sport/domain awareness maintained across turns
|