Spaces:
Sleeping
Sleeping
File size: 24,292 Bytes
f884e6e |
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
"""
Evaluation Tracking and Monitoring System
Provides continuous evaluation tracking, trend analysis, and performance monitoring
for the RAG system with automated alerts and quality regression detection.
"""
import json
import os
import statistics
import time
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
class EvaluationTracker:
"""Track evaluation results over time and detect performance trends."""
def __init__(self, tracking_dir: str = "evaluation_tracking"):
"""Initialize evaluation tracker."""
self.tracking_dir = Path(tracking_dir)
self.tracking_dir.mkdir(exist_ok=True)
self.metrics_file = self.tracking_dir / "metrics_history.json"
self.alerts_file = self.tracking_dir / "alerts.json"
self.trends_file = self.tracking_dir / "trends.json"
self._load_history()
def _load_history(self):
"""Load historical tracking data."""
try:
with open(self.metrics_file, "r") as f:
self.metrics_history = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
self.metrics_history = []
try:
with open(self.alerts_file, "r") as f:
self.alerts = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
self.alerts = []
def record_evaluation(self, results_file: str) -> Dict[str, Any]:
"""Record a new evaluation run."""
try:
with open(results_file, "r") as f:
results = json.load(f)
except Exception as e:
return {"error": f"Failed to load results: {e}"}
# Extract key metrics
summary = results.get("summary", {})
timestamp = time.time()
evaluation_record = {
"timestamp": timestamp,
"date": datetime.fromtimestamp(timestamp).isoformat(),
"metrics": {
"total_questions": summary.get("n_questions", 0),
"success_rate": summary.get("success_rate", 0.0),
"avg_latency_s": summary.get("avg_latency_s", 0.0),
"avg_groundedness_score": summary.get("avg_groundedness_score", 0.0),
"avg_citation_accuracy": summary.get("avg_citation_accuracy", 0.0),
"perfect_citations": summary.get("perfect_citations", 0),
"no_citations": summary.get("no_citations", 0),
},
"performance_score": self._calculate_performance_score(summary),
"quality_grade": self._calculate_quality_grade(summary),
"evaluation_file": results_file,
}
# Add to history
self.metrics_history.append(evaluation_record)
# Keep only last 100 evaluations
if len(self.metrics_history) > 100:
self.metrics_history = self.metrics_history[-100:]
# Save updated history
self._save_history()
# Check for alerts
alerts = self._check_alerts(evaluation_record)
# Update trends
trends = self._update_trends()
return {
"recorded": True,
"timestamp": timestamp,
"performance_score": evaluation_record["performance_score"],
"quality_grade": evaluation_record["quality_grade"],
"alerts": alerts,
"trends": trends,
}
def _calculate_performance_score(self, summary: Dict) -> float:
"""Calculate composite performance score."""
success_rate = summary.get("success_rate", 0.0)
latency = summary.get("avg_latency_s", 10.0)
groundedness = summary.get("avg_groundedness_score", 0.0)
citation = summary.get("avg_citation_accuracy", 0.0)
# Normalize latency (assume 10s worst, 1s best)
latency_score = max(0, min(1, (10 - latency) / 9))
# Weighted composite score
score = (
success_rate * 0.25 # System reliability
+ latency_score * 0.25 # Response speed
+ groundedness * 0.30 # Content accuracy
+ citation * 0.20 # Source attribution
)
return round(score, 3)
def _calculate_quality_grade(self, summary: Dict) -> str:
"""Calculate quality grade from metrics."""
score = self._calculate_performance_score(summary)
if score >= 0.95:
return "A+"
elif score >= 0.90:
return "A"
elif score >= 0.80:
return "B+"
elif score >= 0.70:
return "B"
elif score >= 0.60:
return "C+"
elif score >= 0.50:
return "C"
else:
return "D"
def _check_alerts(self, current_evaluation: Dict) -> List[Dict[str, Any]]:
"""Check for performance alerts and quality regressions."""
alerts = []
current_metrics = current_evaluation["metrics"]
timestamp = current_evaluation["timestamp"]
# Define alert thresholds
thresholds = {
"success_rate_critical": 0.90,
"success_rate_warning": 0.95,
"latency_critical": 10.0,
"latency_warning": 6.0,
"groundedness_critical": 0.80,
"groundedness_warning": 0.90,
"citation_critical": 0.20,
"citation_warning": 0.50,
}
# Check current values against thresholds
success_rate = current_metrics["success_rate"]
if success_rate < thresholds["success_rate_critical"]:
alerts.append(
{
"level": "critical",
"category": "reliability",
"title": "Critical System Reliability Issue",
"message": f"Success rate dropped to {success_rate*100:.1f}% "
f"(threshold: {thresholds['success_rate_critical']*100:.1f}%)",
"timestamp": timestamp,
"value": success_rate,
}
)
elif success_rate < thresholds["success_rate_warning"]:
alerts.append(
{
"level": "warning",
"category": "reliability",
"title": "System Reliability Warning",
"message": f"Success rate at {success_rate*100:.1f}% "
f"(threshold: {thresholds['success_rate_warning']*100:.1f}%)",
"timestamp": timestamp,
"value": success_rate,
}
)
# Check latency
latency = current_metrics["avg_latency_s"]
if latency > thresholds["latency_critical"]:
alerts.append(
{
"level": "critical",
"category": "performance",
"title": "Critical Performance Degradation",
"message": f"Average latency at {latency:.1f}s (threshold: {thresholds['latency_critical']:.1f}s)",
"timestamp": timestamp,
"value": latency,
}
)
elif latency > thresholds["latency_warning"]:
alerts.append(
{
"level": "warning",
"category": "performance",
"title": "Performance Warning",
"message": f"Average latency at {latency:.1f}s (threshold: {thresholds['latency_warning']:.1f}s)",
"timestamp": timestamp,
"value": latency,
}
)
# Check groundedness
groundedness = current_metrics["avg_groundedness_score"]
if groundedness < thresholds["groundedness_critical"]:
alerts.append(
{
"level": "critical",
"category": "quality",
"title": "Critical Content Quality Issue",
"message": f"Groundedness score at {groundedness*100:.1f}% "
f"(threshold: {thresholds['groundedness_critical']*100:.1f}%)",
"timestamp": timestamp,
"value": groundedness,
}
)
elif groundedness < thresholds["groundedness_warning"]:
alerts.append(
{
"level": "warning",
"category": "quality",
"title": "Content Quality Warning",
"message": (
f"Groundedness score at {groundedness*100:.1f}% "
f"(threshold: {thresholds['groundedness_warning']*100:.1f}%)"
),
"timestamp": timestamp,
"value": groundedness,
}
)
# Check citation accuracy
citation = current_metrics["avg_citation_accuracy"]
if citation < thresholds["citation_critical"]:
alerts.append(
{
"level": "critical",
"category": "attribution",
"title": "Critical Citation Accuracy Issue",
"message": (
f"Citation accuracy at {citation*100:.1f}% "
f"(threshold: {thresholds['citation_critical']*100:.1f}%)"
),
"timestamp": timestamp,
"value": citation,
}
)
elif citation < thresholds["citation_warning"]:
alerts.append(
{
"level": "warning",
"category": "attribution",
"title": "Citation Accuracy Warning",
"message": (
f"Citation accuracy at {citation*100:.1f}% "
f"(threshold: {thresholds['citation_warning']*100:.1f}%)"
),
"timestamp": timestamp,
"value": citation,
}
)
# Check for trend-based alerts (regression detection)
if len(self.metrics_history) >= 3:
trend_alerts = self._check_trend_alerts(current_evaluation)
alerts.extend(trend_alerts)
# Save alerts
self.alerts.extend(alerts)
# Keep only alerts from last 30 days
cutoff_time = timestamp - (30 * 24 * 3600)
self.alerts = [a for a in self.alerts if a["timestamp"] > cutoff_time]
with open(self.alerts_file, "w") as f:
json.dump(self.alerts, f, indent=2)
return alerts
def _check_trend_alerts(self, current_evaluation: Dict) -> List[Dict[str, Any]]:
"""Check for negative trends and regressions."""
alerts = []
if len(self.metrics_history) < 3:
return alerts
# Get recent history for trend analysis
recent_history = self.metrics_history[-3:] # Last 3 evaluations
current_metrics = current_evaluation["metrics"]
# Check for performance degradation trends
recent_scores = [eval_record["performance_score"] for eval_record in recent_history]
current_score = current_evaluation["performance_score"]
# Check if performance is consistently declining
if len(recent_scores) >= 2:
declining_trend = all(recent_scores[i] > recent_scores[i + 1] for i in range(len(recent_scores) - 1))
score_drop = recent_scores[0] - current_score
if declining_trend and score_drop > 0.1:
alerts.append(
{
"level": "warning",
"category": "trend",
"title": "Performance Degradation Trend",
"message": (
f"Performance score declining over last {len(recent_scores)+1} "
f"evaluations (drop: {score_drop:.3f})"
),
"timestamp": current_evaluation["timestamp"],
"value": current_score,
}
)
# Check specific metric trends
metrics_to_check = [
"avg_latency_s",
"avg_groundedness_score",
"avg_citation_accuracy",
]
for metric in metrics_to_check:
recent_values = [eval_record["metrics"][metric] for eval_record in recent_history]
current_value = current_metrics[metric]
if metric == "avg_latency_s":
# For latency, increasing is bad
if all(recent_values[i] < recent_values[i + 1] for i in range(len(recent_values) - 1)):
value_increase = current_value - recent_values[0]
if value_increase > 1.0: # 1 second increase
alerts.append(
{
"level": "warning",
"category": "trend",
"title": "Latency Increase Trend",
"message": f"Response time increasing over recent evaluations (+{value_increase:.1f}s)",
"timestamp": current_evaluation["timestamp"],
"value": current_value,
}
)
else:
# For other metrics, decreasing is bad
if all(recent_values[i] > recent_values[i + 1] for i in range(len(recent_values) - 1)):
value_decrease = recent_values[0] - current_value
if value_decrease > 0.05: # 5% decrease
alerts.append(
{
"level": "warning",
"category": "trend",
"title": f"{metric.replace('_', ' ').title()} Decline Trend",
"message": f"{metric} declining over recent evaluations (-{value_decrease:.3f})",
"timestamp": current_evaluation["timestamp"],
"value": current_value,
}
)
return alerts
def _update_trends(self) -> Dict[str, Any]:
"""Update trend analysis."""
if len(self.metrics_history) < 2:
return {"error": "Insufficient data for trend analysis"}
# Calculate trends over different time windows
trends = {
"overall_performance": self._calculate_metric_trend("performance_score"),
"system_reliability": self._calculate_metric_trend("success_rate"),
"response_time": self._calculate_metric_trend("avg_latency_s"),
"content_quality": self._calculate_metric_trend("avg_groundedness_score"),
"citation_accuracy": self._calculate_metric_trend("avg_citation_accuracy"),
"last_updated": time.time(),
}
# Save trends
with open(self.trends_file, "w") as f:
json.dump(trends, f, indent=2)
return trends
def _calculate_metric_trend(self, metric_path: str) -> Dict[str, Any]:
"""Calculate trend for a specific metric."""
if len(self.metrics_history) < 2:
return {"trend": "insufficient_data"}
# Extract values
if metric_path in ["performance_score", "quality_grade"]:
values = [record[metric_path] for record in self.metrics_history[-10:]] # Last 10 evaluations
else:
values = [record["metrics"][metric_path] for record in self.metrics_history[-10:]]
if metric_path == "quality_grade":
# Convert grades to numeric for trend analysis
grade_values = {
"A+": 4.0,
"A": 3.7,
"B+": 3.3,
"B": 3.0,
"C+": 2.7,
"C": 2.3,
"D": 2.0,
}
values = [grade_values.get(v, 2.0) for v in values]
# Calculate trend
if len(values) < 2:
return {"trend": "insufficient_data"}
# Simple linear trend calculation
x = list(range(len(values)))
mean_x = statistics.mean(x)
mean_y = statistics.mean(values)
numerator = sum((x[i] - mean_x) * (values[i] - mean_y) for i in range(len(values)))
denominator = sum((x[i] - mean_x) ** 2 for i in range(len(values)))
if denominator == 0:
slope = 0
else:
slope = numerator / denominator
# Determine trend direction
if abs(slope) < 0.01:
trend_direction = "stable"
elif slope > 0:
trend_direction = "improving" if metric_path != "avg_latency_s" else "degrading"
else:
trend_direction = "degrading" if metric_path != "avg_latency_s" else "improving"
return {
"trend": trend_direction,
"slope": slope,
"current_value": values[-1],
"previous_value": values[-2] if len(values) >= 2 else values[-1],
"change": values[-1] - (values[-2] if len(values) >= 2 else values[-1]),
"data_points": len(values),
}
def _save_history(self):
"""Save metrics history to file."""
with open(self.metrics_file, "w") as f:
json.dump(self.metrics_history, f, indent=2)
def get_current_status(self) -> Dict[str, Any]:
"""Get current system status and recent trends."""
if not self.metrics_history:
return {"error": "No evaluation history available"}
latest_evaluation = self.metrics_history[-1]
recent_alerts = [a for a in self.alerts if a["timestamp"] > time.time() - (24 * 3600)] # Last 24h
try:
with open(self.trends_file, "r") as f:
trends = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
trends = {}
return {
"current_performance": {
"score": latest_evaluation["performance_score"],
"grade": latest_evaluation["quality_grade"],
"timestamp": latest_evaluation["timestamp"],
"date": latest_evaluation["date"],
},
"current_metrics": latest_evaluation["metrics"],
"recent_alerts": recent_alerts,
"alert_summary": {
"critical": len([a for a in recent_alerts if a["level"] == "critical"]),
"warning": len([a for a in recent_alerts if a["level"] == "warning"]),
},
"trends": trends,
"evaluation_count": len(self.metrics_history),
}
def generate_monitoring_report(self) -> Dict[str, Any]:
"""Generate comprehensive monitoring report."""
if not self.metrics_history:
return {"error": "No evaluation data available"}
current_status = self.get_current_status()
# Calculate statistics over different time periods
last_7_days = [e for e in self.metrics_history if e["timestamp"] > time.time() - (7 * 24 * 3600)]
last_30_days = [e for e in self.metrics_history if e["timestamp"] > time.time() - (30 * 24 * 3600)]
report = {
"report_timestamp": time.time(),
"report_date": datetime.now().isoformat(),
"current_status": current_status,
"historical_analysis": {
"total_evaluations": len(self.metrics_history),
"evaluations_last_7_days": len(last_7_days),
"evaluations_last_30_days": len(last_30_days),
"average_performance_7d": (
statistics.mean([e["performance_score"] for e in last_7_days]) if last_7_days else None
),
"average_performance_30d": (
statistics.mean([e["performance_score"] for e in last_30_days]) if last_30_days else None
),
},
"alert_analysis": {
"total_alerts": len(self.alerts),
"critical_alerts_30d": len(
[
a
for a in self.alerts
if a["level"] == "critical" and a["timestamp"] > time.time() - (30 * 24 * 3600)
]
),
"most_frequent_alert_category": self._get_most_frequent_alert_category(),
},
"recommendations": self._generate_monitoring_recommendations(current_status),
}
return report
def _get_most_frequent_alert_category(self) -> Optional[str]:
"""Get the most frequent alert category."""
if not self.alerts:
return None
categories = {}
for alert in self.alerts:
category = alert["category"]
categories[category] = categories.get(category, 0) + 1
return max(categories.items(), key=lambda x: x[1])[0] if categories else None
def _generate_monitoring_recommendations(self, current_status: Dict) -> List[str]:
"""Generate monitoring-based recommendations."""
recommendations = []
alert_summary = current_status["alert_summary"]
if alert_summary["critical"] > 0:
recommendations.append(f"π΄ Address {alert_summary['critical']} critical alert(s) immediately")
if alert_summary["warning"] > 2:
recommendations.append(f"π‘ Investigate {alert_summary['warning']} warning alert(s) to prevent degradation")
current_score = current_status["current_performance"]["score"]
if current_score < 0.7:
recommendations.append("π Performance score below acceptable threshold - implement improvement plan")
evaluation_count = current_status["evaluation_count"]
if evaluation_count < 5:
recommendations.append("π Increase evaluation frequency for better trend analysis")
return recommendations
def main():
"""Demonstrate evaluation tracking system."""
print("π Initializing evaluation tracking system...")
# Initialize tracker
tracker = EvaluationTracker("evaluation_tracking")
# Record latest evaluation
results_file = "/Users/sethmcknight/Developer/msse-ai-engineering/evaluation/enhanced_results.json"
if os.path.exists(results_file):
print("π Recording latest evaluation...")
record_result = tracker.record_evaluation(results_file)
if "error" in record_result:
print(f"β Error: {record_result['error']}")
return
print("β
Evaluation recorded successfully")
print(f" Performance Score: {record_result['performance_score']}")
print(f" Quality Grade: {record_result['quality_grade']}")
if record_result["alerts"]:
print(f" β οΈ Generated {len(record_result['alerts'])} alert(s)")
# Get current status
print("\nπ Current System Status:")
status = tracker.get_current_status()
if "error" in status:
print(f"β Error: {status['error']}")
return
current_perf = status["current_performance"]
print(f" Grade: {current_perf['grade']}")
print(f" Score: {current_perf['score']}")
print(f" Last Evaluation: {current_perf['date'][:19]}")
alert_summary = status["alert_summary"]
print(f" Recent Alerts: {alert_summary['critical']} critical, {alert_summary['warning']} warnings")
# Generate monitoring report
print("\nπ Generating monitoring report...")
report = tracker.generate_monitoring_report()
# Save report
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
report_file = f"evaluation_tracking/monitoring_report_{timestamp}.json"
with open(report_file, "w") as f:
json.dump(report, f, indent=2)
print(f"π Monitoring report saved: {report_file}")
recommendations = report.get("recommendations", [])
if recommendations:
print("\nπ‘ RECOMMENDATIONS:")
for rec in recommendations:
print(f" {rec}")
print("\nβ
Evaluation tracking system ready!")
if __name__ == "__main__":
main()
|