-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheval_rag.py
More file actions
251 lines (199 loc) · 7.95 KB
/
eval_rag.py
File metadata and controls
251 lines (199 loc) · 7.95 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
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
import datetime
import time
from pathlib import Path
from typing import List, Dict, Any
from deepeval.models.llms import OllamaModel
from deepeval.metrics import ContextualPrecisionMetric, ContextualRecallMetric
from deepeval.test_case import LLMTestCase
import yaml
from components.rag import pipeline
from haystack.components.builders.answer_builder import AnswerBuilder
TEST_FILE = "test/questions20.yaml"
try:
with open(TEST_FILE, "r", encoding="utf-8") as file:
questions_dict = yaml.safe_load(file)
# You can now access data like a Python dictionary
print(f"Success read {len(questions_dict)} questions.")
except FileNotFoundError:
print("Error: The file was not found.")
except yaml.YAMLError as e:
print(f"Error parsing YAML file: {e}")
chat_history_id = "test_1"
pipeline.add_component("answer_builder", AnswerBuilder())
pipeline.connect("llm.replies", "answer_builder.replies")
pipeline.connect("retriever", "answer_builder.documents")
def get_contexts_and_responses(questions, pipeline):
all_contexts = []
responses = []
for i, question in enumerate(questions):
response = pipeline.run(
data={
"embedder": {"text": question},
"prompt_builder": {"query": question},
"message_retriever": {"chat_history_id": f"chat_{i}"},
"message_writer": {"chat_history_id": f"chat_{i}"},
"answer_builder": {"query": question},
},
include_outputs_from={"llm", "retriever"},
)
docs = response["retriever"]["documents"]
ranked_contexts = []
for rank, d in enumerate(docs, start=1):
ranked_contexts.append({
"rank": rank,
"score": getattr(d, "score", None),
"content": d.content,
"meta": d.meta,
})
all_contexts.append(ranked_contexts)
responses.append(response["answer_builder"]["answers"][0].data)
# print(f"\nQuestion {i+1}: {question}")
# for c in ranked_contexts:
# print(f" Rank {c['rank']} | Score={c['score']} | meta={c['meta']}")
# print("Response:", responses[-1])
# print("-" * 60)
return all_contexts, responses
gold_ids = [list(map(int, q['gold_chunks_id'].split(", "))) for q in questions_dict]
ground_truths = [q['answer'] for q in questions_dict]
questions = [q['question'] for q in questions_dict]
contexts, responses = get_contexts_and_responses(questions, pipeline)
# ----------------------------
# IR metrics by chunk_id
# ----------------------------
def first_relevant_rank_by_chunks_id(ranked_contexts: List[Dict], gold_chunks_id: List[int]) -> int | None:
for item in ranked_contexts:
meta = item.get("meta") or {}
if meta.get("chunk_id") in gold_chunks_id:
return int(item["rank"])
return None
def hit_at_k_by_chunks_id(ranked_contexts: List[Dict], gold_chunks_id: List[int], k: int) -> float:
return 1.0 if first_relevant_rank_by_chunks_id(ranked_contexts[:k], gold_chunks_id) else 0.0
def precision_at_k_by_chunks_id(ranked_contexts: List[Dict], gold_chunks_id: List[int], k: int) -> float:
relevant = 0
for item in ranked_contexts[:k]:
meta = item.get("meta") or {}
if meta.get("chunk_id") in gold_chunks_id:
relevant += 1
return relevant / k
def recall_at_k_by_chunks_id(ranked_contexts: List[Dict], gold_chunks_id: List[int], k: int) -> float:
relevant = 0
for item in ranked_contexts[:k]:
meta = item.get("meta") or {}
if meta.get("chunk_id") in gold_chunks_id:
relevant += 1
return relevant / len(gold_chunks_id)
def reciprocal_rank_by_chunks_id(ranked_contexts: List[Dict], gold_chunks_id: List[int]) -> float:
r = first_relevant_rank_by_chunks_id(ranked_contexts, gold_chunks_id)
return 0.0 if r is None else 1.0 / r
# ----------------------------
# LLM-as-judge metrics
# ----------------------------
model_name = "yandex/YandexGPT-5-Lite-8B-instruct-GGUF"
judge_model = OllamaModel(model=model_name, timeout=180)
contextual_precision_metric = ContextualPrecisionMetric(
model=judge_model, async_mode=False
)
contextual_recall_metric = ContextualRecallMetric(
model=judge_model, async_mode=False
)
# ----------------------------
# Evaluation loop
# ----------------------------
all_results: List[Dict[str, Any]] = []
# For summary stats
K_VALUES = (3, 5)
hit_at_k = {k: [] for k in K_VALUES}
precision_at_k = {k: [] for k in K_VALUES}
recall_at_k = {k: [] for k in K_VALUES}
mrr = 0.0
for i, question in enumerate(questions):
print(f"\nEvaluating question {i + 1}/{len(questions)}")
ranked_ctx = contexts[i]
ctx_texts = [c["content"] for c in ranked_ctx]
q_gold_ids = gold_ids[i]
# --- IR metrics ---
ir_metrics: Dict[str, Any] = {
"gold_chunks_id": q_gold_ids,
}
if q_gold_ids is not None:
reciprocal_rank = reciprocal_rank_by_chunks_id(ranked_ctx, q_gold_ids)
mrr += reciprocal_rank
ir_metrics['reciprocal_rank'] = reciprocal_rank
for k in K_VALUES:
ir_metrics.update({
f"hit@{k}": hit_at_k_by_chunks_id(ranked_ctx, q_gold_ids, k),
f"precision@{k}": precision_at_k_by_chunks_id(ranked_ctx, q_gold_ids, k),
f"recall@{k}": recall_at_k_by_chunks_id(ranked_ctx, q_gold_ids, k)
})
print(f"Reciprocal Rank: {ir_metrics['reciprocal_rank']}")
for k in K_VALUES:
print(
f"Hit@{k}={ir_metrics[f"hit@{k}"]} | "
f"Precision@{k}={ir_metrics[f"precision@{k}"]} | "
f"Recall@{k}={ir_metrics[f"recall@{k}"]}"
)
# --- LLM-as-judge metrics ---
judge_metrics: Dict[str, Any] = {
"contextual_precision": {"score": None, "reason": None},
"contextual_recall": {"score": None, "reason": None},
}
result = {
"id": i,
"question": question,
"response": responses[i],
"ground_truth": ground_truths[i],
"ir_metrics": ir_metrics,
"judge_metrics": judge_metrics,
}
try:
test_case = LLMTestCase(
input=question,
actual_output=responses[i],
expected_output=ground_truths[i],
retrieval_context=ctx_texts,
)
contextual_precision_metric.measure(test_case)
contextual_recall_metric.measure(test_case)
judge_metrics["contextual_precision"] = {
"score": contextual_precision_metric.score,
"reason": contextual_precision_metric.reason,
}
judge_metrics["contextual_recall"] = {
"score": contextual_recall_metric.score,
"reason": contextual_recall_metric.reason,
}
print(f"✅ ContextualPrecision: {contextual_precision_metric.score:.3f}")
print(f"🧠 Reason: {contextual_precision_metric.reason}")
print(f"✅ ContextualRecall: {contextual_recall_metric.score:.3f}")
print(f"🧠 Reason: {contextual_recall_metric.reason}")
except Exception as e:
print(f"❌ Error evaluating LLM-as-a-judge metric: {e}")
result["error"] = str(e)
all_results.append(result)
time.sleep(1)
mrr = mrr / len(questions)
# ----------------------------
# Results
# ----------------------------
print("\n" + "=" * 80)
print("FINAL RESULTS (per question)")
print("=" * 80)
print("MRR:", mrr)
for r in all_results:
print("─" * 50)
print("Question:", r["question"])
if "error" in r:
print("Error:", r["error"])
print("IR metrics:", r.get("ir_metrics"))
print("LLM-as-a-judge metrics:", r.get("judge_metrics"))
all_results.insert(0, {"MRR": mrr})
report_name = f"{datetime.datetime.today().strftime('%Y-%m-%d-%H-%M-%S')}_" + model_name + Path(TEST_FILE).stem + ".yaml"
with open(f"reports/{report_name}", "w", encoding="utf-8") as f:
yaml.dump(
all_results,
f,
allow_unicode=True,
sort_keys=False,
indent=4,
default_flow_style=False,
)