File size: 21,298 Bytes
702ea87 | 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 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 | #!/usr/bin/env python3
"""
Evaluation script for EyeWiki RAG system.
Evaluates the system on a set of test questions and measures:
- Retrieval recall (relevant sources retrieved)
- Answer relevance (expected topics covered)
- Source citation accuracy
Usage:
python scripts/evaluate.py
python scripts/evaluate.py --questions tests/custom_questions.json
python scripts/evaluate.py --output results/eval_results.json
"""
import argparse
import json
import sys
import time
from pathlib import Path
from typing import Dict, List, Any
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TimeElapsedColumn
from rich.table import Table
from rich.panel import Panel
# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
from config.settings import Settings
from src.llm.ollama_client import OllamaClient
from src.rag.query_engine import EyeWikiQueryEngine
from src.rag.reranker import CrossEncoderReranker
from src.rag.retriever import HybridRetriever
from src.vectorstore.qdrant_store import QdrantStoreManager
console = Console()
# ============================================================================
# Evaluation Metrics
# ============================================================================
def calculate_retrieval_recall(
retrieved_sources: List[str],
expected_sources: List[str],
) -> float:
"""
Calculate retrieval recall.
Recall = (# of expected sources retrieved) / (# of expected sources)
Args:
retrieved_sources: List of retrieved source titles
expected_sources: List of expected source titles
Returns:
Recall score (0-1)
"""
if not expected_sources:
return 1.0
# Normalize for case-insensitive matching
retrieved_lower = {s.lower() for s in retrieved_sources}
expected_lower = {s.lower() for s in expected_sources}
# Count matches (allow partial matching)
matches = 0
for expected in expected_lower:
for retrieved in retrieved_lower:
# Check if expected source name is in retrieved source or vice versa
if expected in retrieved or retrieved in expected:
matches += 1
break
recall = matches / len(expected_sources) if expected_sources else 0.0
return recall
def calculate_answer_relevance(
answer: str,
expected_topics: List[str],
) -> float:
"""
Calculate answer relevance based on topic coverage.
Relevance = (# of expected topics found) / (# of expected topics)
Args:
answer: Generated answer text
expected_topics: List of expected topic keywords
Returns:
Relevance score (0-1)
"""
if not expected_topics:
return 1.0
answer_lower = answer.lower()
# Count how many expected topics appear in answer
topics_found = sum(1 for topic in expected_topics if topic.lower() in answer_lower)
relevance = topics_found / len(expected_topics) if expected_topics else 0.0
return relevance
def calculate_citation_accuracy(
answer: str,
cited_sources: List[str],
expected_sources: List[str],
) -> Dict[str, float]:
"""
Calculate citation accuracy metrics.
Args:
answer: Generated answer text
cited_sources: Sources returned by system
expected_sources: Expected sources
Returns:
Dictionary with citation metrics
"""
# Check if answer contains explicit citations
has_citations = "[Source:" in answer or "According to" in answer
# Calculate precision and recall
if cited_sources and expected_sources:
cited_set = {s.lower() for s in cited_sources}
expected_set = {s.lower() for s in expected_sources}
# Allow partial matching
true_positives = 0
for cited in cited_set:
for expected in expected_set:
if expected in cited or cited in expected:
true_positives += 1
break
precision = true_positives / len(cited_sources) if cited_sources else 0.0
recall = true_positives / len(expected_sources) if expected_sources else 0.0
# F1 score
f1 = (
2 * (precision * recall) / (precision + recall)
if (precision + recall) > 0
else 0.0
)
else:
precision = 0.0
recall = 0.0
f1 = 0.0
return {
"has_explicit_citations": has_citations,
"precision": precision,
"recall": recall,
"f1": f1,
}
# ============================================================================
# Question Evaluation
# ============================================================================
def evaluate_question(
question_data: Dict[str, Any],
query_engine: EyeWikiQueryEngine,
) -> Dict[str, Any]:
"""
Evaluate a single question.
Args:
question_data: Question data with expected answers
query_engine: Query engine instance
Returns:
Evaluation results
"""
question_id = question_data["id"]
question = question_data["question"]
expected_topics = question_data["expected_topics"]
expected_sources = question_data["expected_sources"]
# Query the system
start_time = time.time()
try:
response = query_engine.query(
question=question,
include_sources=True,
)
query_time = time.time() - start_time
# Extract retrieved sources
retrieved_sources = [s.title for s in response.sources]
# Calculate metrics
retrieval_recall = calculate_retrieval_recall(
retrieved_sources, expected_sources
)
answer_relevance = calculate_answer_relevance(
response.answer, expected_topics
)
citation_metrics = calculate_citation_accuracy(
response.answer, retrieved_sources, expected_sources
)
# Detailed topic analysis
topics_found = [
topic for topic in expected_topics if topic.lower() in response.answer.lower()
]
topics_missing = [
topic
for topic in expected_topics
if topic.lower() not in response.answer.lower()
]
# Source analysis
sources_retrieved = []
sources_missing = []
for expected in expected_sources:
found = False
for retrieved in retrieved_sources:
if expected.lower() in retrieved.lower() or retrieved.lower() in expected.lower():
sources_retrieved.append(expected)
found = True
break
if not found:
sources_missing.append(expected)
result = {
"id": question_id,
"question": question,
"category": question_data.get("category", "unknown"),
"answer": response.answer,
"confidence": response.confidence,
"query_time": query_time,
"metrics": {
"retrieval_recall": retrieval_recall,
"answer_relevance": answer_relevance,
"citation_precision": citation_metrics["precision"],
"citation_recall": citation_metrics["recall"],
"citation_f1": citation_metrics["f1"],
},
"details": {
"retrieved_sources": retrieved_sources,
"expected_sources": expected_sources,
"sources_retrieved": sources_retrieved,
"sources_missing": sources_missing,
"topics_found": topics_found,
"topics_missing": topics_missing,
"has_explicit_citations": citation_metrics["has_explicit_citations"],
},
"success": True,
}
except Exception as e:
result = {
"id": question_id,
"question": question,
"category": question_data.get("category", "unknown"),
"error": str(e),
"query_time": time.time() - start_time,
"success": False,
}
return result
# ============================================================================
# Aggregate Analysis
# ============================================================================
def calculate_aggregate_metrics(results: List[Dict[str, Any]]) -> Dict[str, Any]:
"""
Calculate aggregate metrics across all questions.
Args:
results: List of evaluation results
Returns:
Aggregate metrics
"""
successful_results = [r for r in results if r["success"]]
if not successful_results:
return {"error": "No successful evaluations"}
# Average metrics
avg_retrieval_recall = sum(
r["metrics"]["retrieval_recall"] for r in successful_results
) / len(successful_results)
avg_answer_relevance = sum(
r["metrics"]["answer_relevance"] for r in successful_results
) / len(successful_results)
avg_citation_precision = sum(
r["metrics"]["citation_precision"] for r in successful_results
) / len(successful_results)
avg_citation_recall = sum(
r["metrics"]["citation_recall"] for r in successful_results
) / len(successful_results)
avg_citation_f1 = sum(
r["metrics"]["citation_f1"] for r in successful_results
) / len(successful_results)
avg_confidence = sum(r["confidence"] for r in successful_results) / len(
successful_results
)
avg_query_time = sum(r["query_time"] for r in successful_results) / len(
successful_results
)
# Citation statistics
citations_present = sum(
1 for r in successful_results if r["details"]["has_explicit_citations"]
)
# Category breakdown
categories = {}
for result in successful_results:
category = result["category"]
if category not in categories:
categories[category] = {
"count": 0,
"retrieval_recall": 0,
"answer_relevance": 0,
}
categories[category]["count"] += 1
categories[category]["retrieval_recall"] += result["metrics"]["retrieval_recall"]
categories[category]["answer_relevance"] += result["metrics"]["answer_relevance"]
# Average by category
for category, data in categories.items():
count = data["count"]
data["retrieval_recall"] /= count
data["answer_relevance"] /= count
return {
"total_questions": len(results),
"successful": len(successful_results),
"failed": len(results) - len(successful_results),
"metrics": {
"retrieval_recall": avg_retrieval_recall,
"answer_relevance": avg_answer_relevance,
"citation_precision": avg_citation_precision,
"citation_recall": avg_citation_recall,
"citation_f1": avg_citation_f1,
"avg_confidence": avg_confidence,
"avg_query_time": avg_query_time,
"citation_rate": citations_present / len(successful_results),
},
"by_category": categories,
}
# ============================================================================
# Output Functions
# ============================================================================
def print_question_result(result: Dict[str, Any]):
"""Print result for a single question."""
if not result["success"]:
console.print(
f"\n[red]✗ {result['id']}: {result['question']}[/red]",
f"[red]Error: {result['error']}[/red]",
)
return
# Create metrics table
table = Table(show_header=False, box=None, padding=(0, 1))
table.add_column(style="cyan")
table.add_column(style="yellow")
metrics = result["metrics"]
table.add_row("Retrieval Recall", f"{metrics['retrieval_recall']:.2%}")
table.add_row("Answer Relevance", f"{metrics['answer_relevance']:.2%}")
table.add_row("Citation F1", f"{metrics['citation_f1']:.2%}")
table.add_row("Confidence", f"{result['confidence']:.2%}")
table.add_row("Query Time", f"{result['query_time']:.2f}s")
# Determine overall status
avg_score = (metrics["retrieval_recall"] + metrics["answer_relevance"]) / 2
if avg_score >= 0.8:
status = "[green]✓ PASS[/green]"
elif avg_score >= 0.6:
status = "[yellow]~ PARTIAL[/yellow]"
else:
status = "[red]✗ FAIL[/red]"
console.print(f"\n{status} [bold]{result['id']}:[/bold] {result['question']}")
console.print(table)
# Print missing items
details = result["details"]
if details["topics_missing"]:
console.print(
f" [dim]Missing topics: {', '.join(details['topics_missing'])}[/dim]"
)
if details["sources_missing"]:
console.print(
f" [dim]Missing sources: {', '.join(details['sources_missing'])}[/dim]"
)
def print_aggregate_results(aggregate: Dict[str, Any]):
"""Print aggregate results."""
console.print("\n")
console.print(
Panel.fit(
"[bold cyan]Evaluation Summary[/bold cyan]",
border_style="cyan",
)
)
# Overall metrics table
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Metric", style="cyan")
table.add_column("Score", style="yellow", justify="right")
table.add_column("Grade", style="green", justify="center")
metrics = aggregate["metrics"]
def get_grade(score: float) -> str:
if score >= 0.9:
return "[green]A[/green]"
elif score >= 0.8:
return "[green]B[/green]"
elif score >= 0.7:
return "[yellow]C[/yellow]"
elif score >= 0.6:
return "[yellow]D[/yellow]"
else:
return "[red]F[/red]"
table.add_row(
"Retrieval Recall",
f"{metrics['retrieval_recall']:.2%}",
get_grade(metrics["retrieval_recall"]),
)
table.add_row(
"Answer Relevance",
f"{metrics['answer_relevance']:.2%}",
get_grade(metrics["answer_relevance"]),
)
table.add_row(
"Citation Precision",
f"{metrics['citation_precision']:.2%}",
get_grade(metrics["citation_precision"]),
)
table.add_row(
"Citation Recall",
f"{metrics['citation_recall']:.2%}",
get_grade(metrics["citation_recall"]),
)
table.add_row(
"Citation F1",
f"{metrics['citation_f1']:.2%}",
get_grade(metrics["citation_f1"]),
)
console.print(table)
# Statistics
console.print(f"\n[bold]Statistics:[/bold]")
console.print(
f" Total Questions: {aggregate['total_questions']}",
f" Successful: [green]{aggregate['successful']}[/green]",
f" Failed: [red]{aggregate['failed']}[/red]",
f" Avg Confidence: {metrics['avg_confidence']:.2%}",
f" Avg Query Time: {metrics['avg_query_time']:.2f}s",
f" Citation Rate: {metrics['citation_rate']:.2%}",
)
# Category breakdown
if aggregate["by_category"]:
console.print(f"\n[bold]Performance by Category:[/bold]")
cat_table = Table(show_header=True, header_style="bold magenta")
cat_table.add_column("Category", style="cyan")
cat_table.add_column("Count", justify="right")
cat_table.add_column("Retrieval", justify="right")
cat_table.add_column("Relevance", justify="right")
for category, data in sorted(aggregate["by_category"].items()):
cat_table.add_row(
category,
str(data["count"]),
f"{data['retrieval_recall']:.2%}",
f"{data['answer_relevance']:.2%}",
)
console.print(cat_table)
# ============================================================================
# Main Evaluation
# ============================================================================
def load_test_questions(questions_file: Path) -> List[Dict[str, Any]]:
"""Load test questions from JSON file."""
if not questions_file.exists():
console.print(f"[red]Error: Questions file not found: {questions_file}[/red]")
sys.exit(1)
with open(questions_file, "r") as f:
questions = json.load(f)
console.print(f"[green]✓[/green] Loaded {len(questions)} test questions")
return questions
def initialize_system() -> EyeWikiQueryEngine:
"""Initialize the RAG system."""
console.print("[bold]Initializing RAG system...[/bold]")
# Load settings
settings = Settings()
# Initialize components
ollama_client = OllamaClient(
base_url=settings.ollama_base_url,
llm_model=settings.llm_model,
embedding_model=settings.embedding_model,
)
qdrant_manager = QdrantStoreManager(
collection_name=settings.qdrant_collection_name,
qdrant_path=settings.qdrant_path,
vector_size=settings.embedding_dim,
)
retriever = HybridRetriever(
qdrant_manager=qdrant_manager,
ollama_client=ollama_client,
)
reranker = CrossEncoderReranker(
model_name=settings.reranker_model,
)
# Load prompts
prompts_dir = project_root / "prompts"
system_prompt_path = prompts_dir / "system_prompt.txt"
query_prompt_path = prompts_dir / "query_prompt.txt"
disclaimer_path = prompts_dir / "medical_disclaimer.txt"
query_engine = EyeWikiQueryEngine(
retriever=retriever,
reranker=reranker,
llm_client=ollama_client,
system_prompt_path=system_prompt_path if system_prompt_path.exists() else None,
query_prompt_path=query_prompt_path if query_prompt_path.exists() else None,
disclaimer_path=disclaimer_path if disclaimer_path.exists() else None,
max_context_tokens=settings.max_context_tokens,
retrieval_k=20,
rerank_k=5,
)
console.print("[green]✓[/green] System initialized\n")
return query_engine
def run_evaluation(
questions_file: Path,
output_file: Path = None,
verbose: bool = False,
):
"""
Run evaluation on test questions.
Args:
questions_file: Path to test questions JSON
output_file: Optional path to save results
verbose: Print detailed results
"""
console.print(
Panel.fit(
"[bold blue]EyeWiki RAG Evaluation[/bold blue]",
border_style="blue",
)
)
# Load questions
questions = load_test_questions(questions_file)
# Initialize system
query_engine = initialize_system()
# Evaluate questions
results = []
console.print("[bold]Evaluating questions...[/bold]\n")
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeElapsedColumn(),
console=console,
) as progress:
task = progress.add_task("Processing...", total=len(questions))
for question_data in questions:
result = evaluate_question(question_data, query_engine)
results.append(result)
if verbose:
print_question_result(result)
progress.update(task, advance=1)
# Calculate aggregate metrics
aggregate = calculate_aggregate_metrics(results)
# Print results
if not verbose:
console.print("\n[bold]Per-Question Results:[/bold]")
for result in results:
print_question_result(result)
print_aggregate_results(aggregate)
# Save results
if output_file:
output_data = {
"results": results,
"aggregate": aggregate,
"timestamp": time.time(),
}
output_file.parent.mkdir(parents=True, exist_ok=True)
with open(output_file, "w") as f:
json.dump(output_data, f, indent=2)
console.print(f"\n[green]✓[/green] Results saved to {output_file}")
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Evaluate EyeWiki RAG system on test questions"
)
parser.add_argument(
"--questions",
type=Path,
default=project_root / "tests" / "test_questions.json",
help="Path to test questions JSON file",
)
parser.add_argument(
"--output",
type=Path,
default=None,
help="Path to save evaluation results (JSON)",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Print detailed results for each question",
)
args = parser.parse_args()
try:
run_evaluation(
questions_file=args.questions,
output_file=args.output,
verbose=args.verbose,
)
except KeyboardInterrupt:
console.print("\n[yellow]Evaluation interrupted by user[/yellow]")
sys.exit(1)
except Exception as e:
console.print(f"\n[red]Error: {e}[/red]")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()
|