Spaces:
Runtime error
Runtime error
Create memory_drift_diagnostician.py
Browse files
memory_drift_diagnostician.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import numpy as np
|
| 3 |
+
from typing import Dict, Any, List
|
| 4 |
+
from agentic_reliability_framework.runtime.agents.base import BaseAgent
|
| 5 |
+
from ai_event import AIEvent
|
| 6 |
+
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
+
class MemoryDriftDiagnosticianAgent(BaseAgent):
|
| 10 |
+
"""Detects drift in semantic memory by comparing current retrieval scores with historical distribution."""
|
| 11 |
+
def __init__(self, history_window: int = 100):
|
| 12 |
+
super().__init__(AgentSpecialization.DIAGNOSTICIAN)
|
| 13 |
+
self.history_window = history_window
|
| 14 |
+
self._retrieval_scores_history: List[float] = []
|
| 15 |
+
|
| 16 |
+
async def analyze(self, event: AIEvent) -> Dict[str, Any]:
|
| 17 |
+
try:
|
| 18 |
+
if not event.retrieval_scores:
|
| 19 |
+
return {'specialization': 'ai_memory_drift', 'confidence': 0, 'findings': {}, 'recommendations': []}
|
| 20 |
+
current_avg = float(np.mean(event.retrieval_scores))
|
| 21 |
+
self._retrieval_scores_history.append(current_avg)
|
| 22 |
+
if len(self._retrieval_scores_history) > self.history_window:
|
| 23 |
+
self._retrieval_scores_history.pop(0)
|
| 24 |
+
|
| 25 |
+
if len(self._retrieval_scores_history) < 10:
|
| 26 |
+
drift_detected = False
|
| 27 |
+
confidence = 0.0
|
| 28 |
+
else:
|
| 29 |
+
historical_avg = float(np.mean(self._retrieval_scores_history[:-1]))
|
| 30 |
+
historical_std = float(np.std(self._retrieval_scores_history[:-1])) + 1e-6
|
| 31 |
+
z_score = (current_avg - historical_avg) / historical_std
|
| 32 |
+
drift_detected = abs(z_score) > 2.0
|
| 33 |
+
confidence = min(1.0, abs(z_score) / 5.0)
|
| 34 |
+
|
| 35 |
+
return {
|
| 36 |
+
'specialization': 'ai_memory_drift',
|
| 37 |
+
'confidence': confidence,
|
| 38 |
+
'findings': {
|
| 39 |
+
'drift_detected': drift_detected,
|
| 40 |
+
'current_avg': current_avg,
|
| 41 |
+
'historical_avg': historical_avg if len(self._retrieval_scores_history) > 1 else None,
|
| 42 |
+
'z_score': float(z_score) if len(self._retrieval_scores_history) > 1 else None
|
| 43 |
+
},
|
| 44 |
+
'recommendations': [
|
| 45 |
+
"Reindex knowledge base",
|
| 46 |
+
"Adjust embedding model",
|
| 47 |
+
"Update context window"
|
| 48 |
+
] if drift_detected else []
|
| 49 |
+
}
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logger.error(f"MemoryDriftDiagnostician error: {e}", exc_info=True)
|
| 52 |
+
return {'specialization': 'ai_memory_drift', 'confidence': 0.0, 'findings': {}, 'recommendations': []}
|