-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_evaluation.py
More file actions
executable file
·193 lines (156 loc) · 7.96 KB
/
Copy pathrun_evaluation.py
File metadata and controls
executable file
·193 lines (156 loc) · 7.96 KB
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
#!/usr/bin/env python
"""
Run complete evaluation of the RAG system.
This script generates evaluation reports similar to the research paper.
"""
import sys
from pathlib import Path
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))
from backend import load_models, query_documents
from tests.evaluation_metrics import EvaluationReport, RetrievalMetrics, GenerationMetrics
from tests.test_queries import TEST_QUERIES
from tests.reference_answers import get_reference_answer, has_reference_answer
def run_complete_evaluation(include_advanced=False):
"""
Run complete evaluation on all test queries.
Generates report similar to Tables III and IV in the paper.
Args:
include_advanced: If True, calculate BERTScore, ROUGE-L, and BLEU metrics
"""
print("\n" + "="*100)
print("EDURAG SYSTEM EVALUATION")
if include_advanced:
print("(with Advanced Metrics: BERTScore, ROUGE-L, BLEU)")
print("="*100)
print("\nLoading models...")
try:
vectorstore, chain = load_models()
print("✓ Models loaded successfully")
except Exception as e:
print(f"✗ Error loading models: {e}")
print("\nMake sure you have:")
print(" 1. Run 'python ingest.py' to create the vector database")
print(" 2. Set GOOGLE_API_KEY in your .env file")
print(" 3. Installed all requirements: pip install -r requirements.txt")
return
print(f"\nEvaluating {len(TEST_QUERIES)} test queries...\n")
results = []
query_ids = []
for i, query_data in enumerate(TEST_QUERIES, 1):
query = query_data["query"]
query_id = query_data["id"]
print(f"[{i}/{len(TEST_QUERIES)}] Processing {query_id}: {query[:60]}...")
try:
# Retrieve documents
retrieved_docs = vectorstore.similarity_search_with_score(query, k=10)
# Generate response
response = query_documents(query, vectorstore, chain)
# Prepare context from retrieved docs
context = [doc.page_content for doc, score in retrieved_docs[:3]]
# For evaluation, we assume top 3 docs are relevant (in production, use human labels)
retrieved_with_relevance = [
{
"content": doc.page_content,
"score": score,
"is_relevant": i < 3 # Top 3 marked as relevant
}
for i, (doc, score) in enumerate(retrieved_docs)
]
# Get reference answer if available
reference = get_reference_answer(query_id) if include_advanced else None
# Evaluate
evaluation = EvaluationReport.evaluate_single_query(
query=query,
response=response,
context=context,
retrieved_docs=retrieved_with_relevance,
reference=reference,
include_advanced=include_advanced
)
results.append(evaluation)
query_ids.append(query_id)
# Print progress
if include_advanced and reference:
print(f" ✓ MRR: {evaluation['mrr']:.3f} | "
f"Faithfulness: {evaluation['faithfulness']:.3f} | "
f"BERTScore: {evaluation.get('bertscore_f1', 0):.3f}")
else:
print(f" ✓ MRR: {evaluation['mrr']:.3f} | "
f"Faithfulness: {evaluation['faithfulness']:.3f} | "
f"Relevancy: {evaluation['relevancy']:.3f}")
except Exception as e:
print(f" ✗ Error: {e}")
continue
# Print comprehensive evaluation table
print("\n")
EvaluationReport.print_evaluation_table(results, query_ids)
# Print advanced metrics table if requested
if include_advanced:
EvaluationReport.print_advanced_metrics_table(results, query_ids)
# Print aggregated statistics
aggregated = EvaluationReport.aggregate_results(results)
print("\nDETAILED STATISTICS")
print("="*100)
print(f"Number of queries evaluated: {len(results)}")
print(f"\nRetrieval Metrics:")
print(f" Average MRR: {aggregated.get('avg_mrr', 0):.4f} ± {aggregated.get('std_mrr', 0):.4f}")
print(f" Average Hit@10: {aggregated.get('avg_hit@10', 0):.4f} ± {aggregated.get('std_hit@10', 0):.4f}")
print(f" Average Hit@5: {aggregated.get('avg_hit@5', 0):.4f} ± {aggregated.get('std_hit@5', 0):.4f}")
print(f"\nGeneration Metrics:")
print(f" Average Faithfulness: {aggregated.get('avg_faithfulness', 0):.4f} ± {aggregated.get('std_faithfulness', 0):.4f}")
print(f" Average Relevancy: {aggregated.get('avg_relevancy', 0):.4f} ± {aggregated.get('std_relevancy', 0):.4f}")
print(f" Average Response Length: {aggregated.get('avg_response_length', 0):.0f} ± {aggregated.get('std_response_length', 0):.0f} words")
# Print advanced metrics if calculated
if include_advanced:
print(f"\nAdvanced NLG Metrics:")
print(f" Average BERTScore F1: {aggregated.get('avg_bertscore_f1', 0):.4f} ± {aggregated.get('std_bertscore_f1', 0):.4f}")
print(f" Average ROUGE-L F1: {aggregated.get('avg_rouge_l_f1', 0):.4f} ± {aggregated.get('std_rouge_l_f1', 0):.4f}")
print(f" Average BLEU: {aggregated.get('avg_bleu', 0):.4f} ± {aggregated.get('std_bleu', 0):.4f}")
print("="*100)
# Interpretation
print("\nINTERPRETATION")
print("="*100)
avg_faithfulness = aggregated.get('avg_faithfulness', 0)
avg_relevancy = aggregated.get('avg_relevancy', 0)
avg_mrr = aggregated.get('avg_mrr', 0)
if avg_faithfulness >= 0.7:
print("✓ FAITHFULNESS: Excellent - Responses are well-grounded in context")
elif avg_faithfulness >= 0.5:
print("⚠ FAITHFULNESS: Good - Some responses may include unsupported information")
else:
print("✗ FAITHFULNESS: Needs Improvement - Responses may hallucinate or drift from context")
if avg_relevancy >= 0.7:
print("✓ RELEVANCY: Excellent - Responses address queries well")
elif avg_relevancy >= 0.5:
print("⚠ RELEVANCY: Good - Most responses address queries adequately")
else:
print("✗ RELEVANCY: Needs Improvement - Responses may not fully address queries")
if avg_mrr >= 0.7:
print("✓ RETRIEVAL: Excellent - Relevant documents ranked highly")
elif avg_mrr >= 0.5:
print("⚠ RETRIEVAL: Good - Relevant documents usually found in top results")
else:
print("✗ RETRIEVAL: Needs Improvement - Relevant documents may be missed or ranked low")
print("="*100 + "\n")
return results, aggregated
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Run EduRAG system evaluation")
parser.add_argument("--advanced", action="store_true",
help="Include advanced metrics (BERTScore, ROUGE-L, BLEU)")
args = parser.parse_args()
try:
results, aggregated = run_complete_evaluation(include_advanced=args.advanced)
print("\n✓ Evaluation complete!")
print("\nTo run individual tests, use: pytest tests/ -v")
print("To run only fast tests, use: pytest tests/ -m 'not slow' -v")
if not args.advanced:
print("\nTo include advanced metrics (BERTScore, ROUGE-L, BLEU), run:")
print(" python run_evaluation.py --advanced")
except KeyboardInterrupt:
print("\n\n✗ Evaluation interrupted by user")
except Exception as e:
print(f"\n✗ Evaluation failed: {e}")
import traceback
traceback.print_exc()