Spaces:
Build error
Build error
File size: 7,548 Bytes
d9d0a60 a6df84a d9d0a60 a6df84a d9d0a60 | 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 | """
Promptfoo Evaluation Runner
Python wrapper for running Promptfoo evaluations and processing results.
"""
import subprocess
import json
import logging
from pathlib import Path
from typing import Dict, Any, Optional
from datetime import datetime
logger = logging.getLogger(__name__)
class PromptfooRunner:
"""
Wrapper for running Promptfoo evaluations.
Provides Python interface for:
- Running evaluations
- Parsing results
- Generating reports
"""
def __init__(self, config_path: str = "eval/promptfooconfig.yaml"):
"""
Initialize the runner.
Args:
config_path: Path to promptfoo config file
"""
self.config_path = Path(config_path)
self.results_dir = Path("eval/results")
self.results_dir.mkdir(parents=True, exist_ok=True)
def run_eval(
self,
output_path: Optional[str] = None,
no_cache: bool = False,
verbose: bool = True,
) -> Dict[str, Any]:
"""
Run Promptfoo evaluation.
Args:
output_path: Custom output path for results
no_cache: Disable caching
verbose: Show detailed output
Returns:
Evaluation results dictionary
"""
if not output_path:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_path = self.results_dir / f"eval_{timestamp}.json"
cmd = [
"npx", "promptfoo", "eval",
"-c", str(self.config_path),
"-o", str(output_path),
"--json",
]
if no_cache:
cmd.append("--no-cache")
logger.info(f"Running Promptfoo evaluation: {' '.join(cmd)}")
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=300, # 5 minute timeout
)
if result.returncode != 0:
logger.error(f"Promptfoo failed: {result.stderr}")
return {"error": result.stderr}
# Parse results
with open(output_path, "r") as f:
results = json.load(f)
return results
except subprocess.TimeoutExpired:
logger.error("Promptfoo evaluation timed out")
return {"error": "Evaluation timed out"}
except Exception as e:
logger.error(f"Failed to run Promptfoo: {e}")
return {"error": str(e)}
def parse_results(self, results: Dict[str, Any]) -> Dict[str, Any]:
"""
Parse and summarize evaluation results.
Args:
results: Raw Promptfoo results
Returns:
Summary dictionary
"""
if "error" in results:
return results
summary = {
"total_tests": 0,
"passed": 0,
"failed": 0,
"pass_rate": 0.0,
"avg_latency_ms": 0,
"total_cost": 0.0,
"failures": [],
}
tests = results.get("results", [])
summary["total_tests"] = len(tests)
latencies = []
costs = []
for test in tests:
if test.get("success"):
summary["passed"] += 1
else:
summary["failed"] += 1
summary["failures"].append({
"description": test.get("description", "Unknown"),
"error": test.get("error", "Assertion failed"),
})
if "latencyMs" in test:
latencies.append(test["latencyMs"])
if "cost" in test:
costs.append(test["cost"])
if summary["total_tests"] > 0:
summary["pass_rate"] = summary["passed"] / summary["total_tests"]
if latencies:
summary["avg_latency_ms"] = sum(latencies) / len(latencies)
if costs:
summary["total_cost"] = sum(costs)
return summary
def generate_report(
self,
results: Dict[str, Any],
output_format: str = "markdown",
) -> str:
"""
Generate human-readable report.
Args:
results: Evaluation results
output_format: Output format (markdown, text)
Returns:
Formatted report string
"""
summary = self.parse_results(results)
if output_format == "markdown":
return self._generate_markdown_report(summary)
else:
return self._generate_text_report(summary)
def _generate_markdown_report(self, summary: Dict[str, Any]) -> str:
"""Generate Markdown report."""
report = [
"# RAG Evaluation Report",
"",
f"**Date:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
"",
"## Summary",
"",
"| Metric | Value |",
"|--------|-------|",
f"| Total Tests | {summary['total_tests']} |",
f"| Passed | {summary['passed']} |",
f"| Failed | {summary['failed']} |",
f"| Pass Rate | {summary['pass_rate']:.1%} |",
f"| Avg Latency | {summary['avg_latency_ms']:.0f}ms |",
f"| Total Cost | ${summary['total_cost']:.4f} |",
"",
]
if summary["failures"]:
report.extend([
"## Failures",
"",
])
for failure in summary["failures"]:
report.append(f"- **{failure['description']}**: {failure['error']}")
report.append("")
return "\n".join(report)
def _generate_text_report(self, summary: Dict[str, Any]) -> str:
"""Generate plain text report."""
lines = [
"=== RAG Evaluation Report ===",
"",
f"Total Tests: {summary['total_tests']}",
f"Passed: {summary['passed']}",
f"Failed: {summary['failed']}",
f"Pass Rate: {summary['pass_rate']:.1%}",
f"Avg Latency: {summary['avg_latency_ms']:.0f}ms",
f"Total Cost: ${summary['total_cost']:.4f}",
]
if summary["failures"]:
lines.extend(["", "Failures:"])
for failure in summary["failures"]:
lines.append(f" - {failure['description']}: {failure['error']}")
return "\n".join(lines)
def run_evaluation(
config_path: str = "eval/promptfooconfig.yaml",
verbose: bool = True,
) -> Dict[str, Any]:
"""
Convenience function to run evaluation.
Args:
config_path: Path to config file
verbose: Show detailed output
Returns:
Evaluation summary
"""
runner = PromptfooRunner(config_path)
results = runner.run_eval(verbose=verbose)
return runner.parse_results(results)
if __name__ == "__main__":
# Run evaluation when executed directly
import sys
logging.basicConfig(level=logging.INFO)
runner = PromptfooRunner()
results = runner.run_eval()
if "error" in results:
print(f"Evaluation failed: {results['error']}")
sys.exit(1)
report = runner.generate_report(results)
print(report)
|