File size: 10,316 Bytes
d0fdbcd | 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 | """
Evaluation framework to measure system performance on test dataset.
Tracks success rate, retries, failure types, and latency.
"""
import json
import time
from typing import Any, Dict, List, Optional
from datetime import datetime
import sys
import os
# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
from pipeline import Pipeline
from runtime_simulator import validate_config_executable
from test_dataset import get_test_dataset, get_real_prompts, get_edge_cases
class EvaluationFramework:
"""Comprehensive evaluation of the system."""
def __init__(self, use_llm: bool = True):
self.pipeline = Pipeline(use_llm=use_llm)
self.results = []
self.summary = {
"total_prompts": 0,
"successful": 0,
"failed": 0,
"executable": 0,
"total_retries": 0,
"total_latency": 0.0,
"by_category": {},
"failure_types": {},
"timestamp": datetime.now().isoformat(),
}
def evaluate_prompt(self, prompt: Dict[str, Any], max_retries: int = 1) -> Dict[str, Any]:
"""Evaluate a single prompt."""
prompt_id = prompt.get("id", "unknown")
prompt_text = prompt.get("prompt", "")
category = prompt.get("category", prompt.get("type", "unknown"))
result = {
"prompt_id": prompt_id,
"category": category,
"prompt_summary": prompt_text[:100],
"success": False,
"executable": False,
"retries": 0,
"latency": 0.0,
"errors": [],
"warnings": [],
"config_size": 0,
}
start_time = time.time()
# Try generation with retries
for attempt in range(max_retries):
result["retries"] = attempt + 1
try:
config, exec_log = self.pipeline.generate(prompt_text)
if not config:
result["errors"].append("Empty config generated")
continue
# Check if executable
is_executable, exec_report = validate_config_executable(config)
result["success"] = True
result["executable"] = is_executable
result["config_size"] = len(json.dumps(config))
if not is_executable:
result["warnings"].extend(exec_report.get("warnings", []))
result["errors"].extend(exec_report.get("errors", []))
# Store execution log
result["execution_log"] = exec_log
result["execution_report"] = exec_report
break
except Exception as e:
error_msg = str(e)
result["errors"].append(error_msg)
# Categorize error
error_type = self._categorize_error(error_msg)
if error_type not in self.summary["failure_types"]:
self.summary["failure_types"][error_type] = 0
self.summary["failure_types"][error_type] += 1
result["latency"] = time.time() - start_time
return result
def _categorize_error(self, error: str) -> str:
"""Categorize error type."""
error_lower = error.lower()
if "json" in error_lower:
return "json_error"
elif "validation" in error_lower:
return "validation_error"
elif "field" in error_lower:
return "field_error"
elif "api" in error_lower:
return "api_error"
elif "database" in error_lower or "table" in error_lower:
return "database_error"
else:
return "unknown_error"
def run_evaluation(self, dataset_size: str = "full") -> Dict[str, Any]:
"""Run full evaluation on test dataset."""
if dataset_size == "full":
prompts = get_real_prompts() + get_edge_cases()
elif dataset_size == "real":
prompts = get_real_prompts()
elif dataset_size == "edge":
prompts = get_edge_cases()
else:
prompts = get_real_prompts()[:int(dataset_size)]
self.summary["total_prompts"] = len(prompts)
print(f"\nπ Running evaluation on {len(prompts)} prompts...")
for i, prompt in enumerate(prompts):
print(f" [{i+1}/{len(prompts)}] {prompt.get('name', prompt.get('id'))}", end=" ")
result = self.evaluate_prompt(prompt)
self.results.append(result)
# Update summary
if result["success"]:
self.summary["successful"] += 1
print("β")
else:
self.summary["failed"] += 1
print("β")
if result["executable"]:
self.summary["executable"] += 1
self.summary["total_retries"] += result["retries"]
self.summary["total_latency"] += result["latency"]
# Track by category
category = result["category"]
if category not in self.summary["by_category"]:
self.summary["by_category"][category] = {"success": 0, "total": 0}
self.summary["by_category"][category]["total"] += 1
if result["success"]:
self.summary["by_category"][category]["success"] += 1
# Calculate metrics
self.summary["success_rate"] = (self.summary["successful"] / self.summary["total_prompts"]) * 100 if self.summary["total_prompts"] > 0 else 0
self.summary["executable_rate"] = (self.summary["executable"] / self.summary["total_prompts"]) * 100 if self.summary["total_prompts"] > 0 else 0
self.summary["avg_retries"] = self.summary["total_retries"] / self.summary["total_prompts"] if self.summary["total_prompts"] > 0 else 0
self.summary["avg_latency"] = self.summary["total_latency"] / self.summary["total_prompts"] if self.summary["total_prompts"] > 0 else 0
return self.get_report()
def get_report(self) -> Dict[str, Any]:
"""Generate evaluation report."""
return {
"summary": self.summary,
"detailed_results": self.results,
"cost_analysis": self._calculate_cost_analysis(),
}
def _calculate_cost_analysis(self) -> Dict[str, Any]:
"""Analyze cost vs quality tradeoff."""
if not self.results:
return {}
successful_configs = [r for r in self.results if r["success"]]
if not successful_configs:
return {"note": "No successful generations to analyze"}
avg_config_size = sum(r["config_size"] for r in successful_configs) / len(successful_configs)
avg_latency = sum(r["latency"] for r in successful_configs) / len(successful_configs)
return {
"avg_config_size_bytes": avg_config_size,
"avg_generation_latency_seconds": round(avg_latency, 2),
"estimated_api_calls_per_prompt": 4, # 4 stages
"estimated_tokens_per_generation": int(avg_config_size / 4), # Rough estimate
"quality_score": (self.summary["success_rate"] * 0.6) + (self.summary["executable_rate"] * 0.4),
"efficiency_score": 100 - (avg_latency * 10), # Arbitrary scale
"recommendation": self._get_recommendation(),
}
def _get_recommendation(self) -> str:
"""Get recommendation based on metrics."""
success_rate = self.summary.get("success_rate", 0)
executable_rate = self.summary.get("executable_rate", 0)
if success_rate >= 80 and executable_rate >= 75:
return "Production-ready with monitoring"
elif success_rate >= 60 and executable_rate >= 50:
return "Ready for limited production use"
elif success_rate >= 40:
return "Needs refinement before production"
else:
return "Requires significant improvements"
def print_report(self):
"""Print formatted report."""
print("\n" + "="*70)
print("π EVALUATION REPORT")
print("="*70)
s = self.summary
print(f"\nπ SUMMARY METRICS:")
print(f" Total Prompts Evaluated: {s['total_prompts']}")
print(f" Successful Generations: {s['successful']}/{s['total_prompts']} ({s.get('success_rate', 0):.1f}%)")
print(f" Executable Configs: {s['executable']}/{s['total_prompts']} ({s.get('executable_rate', 0):.1f}%)")
print(f" Average Retries: {s.get('avg_retries', 0):.2f}")
print(f" Average Latency: {s.get('avg_latency', 0):.2f}s")
print(f"\nπ RESULTS BY CATEGORY:")
for category, stats in s.get("by_category", {}).items():
success_pct = (stats["success"] / stats["total"] * 100) if stats["total"] > 0 else 0
print(f" {category}: {stats['success']}/{stats['total']} ({success_pct:.0f}%)")
print(f"\nβ ERROR TYPES:")
if s.get("failure_types"):
for error_type, count in s["failure_types"].items():
print(f" {error_type}: {count}")
else:
print(" None (all prompts succeeded!)")
print(f"\nπ° COST vs QUALITY ANALYSIS:")
cost_analysis = self._calculate_cost_analysis()
for key, value in cost_analysis.items():
if key != "note":
print(f" {key}: {value}")
print(f"\nβ
RECOMMENDATION: {cost_analysis.get('recommendation', 'Unknown')}")
print("="*70 + "\n")
def run_evaluation_suite():
"""Run the complete evaluation suite."""
evaluator = EvaluationFramework(use_llm=False) # Use rule-based for faster testing
report = evaluator.run_evaluation(dataset_size="full")
evaluator.print_report()
return report
if __name__ == "__main__":
run_evaluation_suite()
|