-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.py
More file actions
292 lines (247 loc) · 9.37 KB
/
metrics.py
File metadata and controls
292 lines (247 loc) · 9.37 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
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
from typing import List, Optional
import pandas as pd
from langchain_core.language_models import BaseChatModel
from aidial_rag_eval.dataframe.match_facts import (
DEFAULT_MATCHER,
apply_matcher_to_merged_dataframe,
)
from aidial_rag_eval.dataframe.merge import merge_ground_truth_and_answers
from aidial_rag_eval.generation.metric_binds import metric_binds_dict
from aidial_rag_eval.generation.types import MetricBind, inference_column
from aidial_rag_eval.retrieval.metrics import (
calculate_metrics as calculate_metrics_by_row,
)
from aidial_rag_eval.retrieval.types import Matcher
from aidial_rag_eval.types import MergedColumns
def apply_metrics_to_matched_results(match_result_data: pd.DataFrame) -> pd.DataFrame:
retrieval_metrics = match_result_data.apply(
calculate_metrics_by_row,
axis=1,
result_type="expand",
)
assert isinstance(retrieval_metrics, pd.DataFrame)
return retrieval_metrics
def calculate_metrics(match_result_data: pd.DataFrame) -> pd.DataFrame:
return pd.merge(
match_result_data,
apply_metrics_to_matched_results(match_result_data),
left_index=True,
right_index=True,
)
def calculate_retrieval_metrics(
df_merged: pd.DataFrame,
matcher: Matcher = DEFAULT_MATCHER,
) -> pd.DataFrame:
"""
Calculates RAG evaluation retrieval metrics from df_merged dataframe.
Parameters
-----------
df_merged : pd.DataFrame
merged ground truth answers and collected answers dataframes
The structure of the df_merged
is described in `aidial_rag_eval.types.MergedColumns`.
matcher : Matcher, default=DEFAULT_MATCHER
An object responsible for matching facts
from the ground truth to the context in the answers.
Returns
------------
pd.DataFrame
Returns retrieval metrics dataframe.
"""
matched_result = apply_matcher_to_merged_dataframe(df_merged, matcher)
matched_result_with_df = pd.merge(
df_merged, matched_result, left_index=True, right_index=True
)
retrieval_metrics = apply_metrics_to_matched_results(matched_result_with_df)
return pd.merge(
matched_result, retrieval_metrics, left_index=True, right_index=True
)
def create_retrieval_metrics_report(
ground_truth: pd.DataFrame,
answers: pd.DataFrame,
matcher: Matcher = DEFAULT_MATCHER,
) -> pd.DataFrame:
"""
Calculates RAG evaluation retrieval metrics from input dataframes.
Parameters
-----------
ground_truth : pd.DataFrame
contains the ground truth answers
The structure of the ground truth
is described in `aidial_rag_eval.types.GroundTruthAnswers`.
answers : pd.DataFrame
contains the collected answers
The structure of the collected answers
is described in `aidial_rag_eval.types.CollectedAnswers`.
matcher : Matcher, default=DEFAULT_MATCHER
An object responsible for matching facts
from the ground truth to the context in the answers.
Returns
------------
pd.DataFrame
Returns merged ground_truth dataframe,
answers dataframe with retrieval metrics dataframe.
"""
df_merged = merge_ground_truth_and_answers(ground_truth, answers)
return pd.merge(
df_merged,
calculate_retrieval_metrics(df_merged, matcher),
left_index=True,
right_index=True,
)
def calculate_generation_metrics(
df_merged: pd.DataFrame,
llm: BaseChatModel,
metric_binds: List[MetricBind],
max_concurrency: int = 8,
show_progress_bar: bool = True,
auto_download_nltk: bool = True,
) -> pd.DataFrame:
"""
Calculates RAG evaluation generation metrics from df_merged dataframe.
Parameters
-----------
df_merged : pd.DataFrame
merged ground truth answers and collected answers dataframes
The structure of the df_merged
is described in `aidial_rag_eval.types.MergedColumns`.
llm : BaseChatModel, optional, default=None
A Langchain chat model used for calculating generation metrics.
metric_binds : List[MetricBind], optional, default=None
A list of string constants from `aidial_rag_eval.metric_binds`
specifying the generation metrics.
max_concurrency : int, default=8
The maximum number of concurrent requests to the LLM.
show_progress_bar : bool, default=True
To display a progress bar during LLM requests.
Returns
------------
pd.DataFrame
Returns generation metrics dataframe.
"""
df_merged_copy = df_merged.copy()
df_merged_copy[MergedColumns.JOINED_CONTEXT] = df_merged_copy[
MergedColumns.CONTEXT
].apply(lambda x: "\n".join(x))
metric_results = dict()
for metric_bind in metric_binds:
metric_results.update(
metric_binds_dict[metric_bind](
df_merged=df_merged_copy,
llm=llm,
max_concurrency=max_concurrency,
show_progress_bar=show_progress_bar,
auto_download_nltk=auto_download_nltk,
).to_dict(orient="series")
)
df_metrics = pd.DataFrame(data=metric_results)
nli_columns = [
column for column in metric_results.keys() if column.endswith(inference_column)
]
if nli_columns:
sub_df_nli = df_metrics[nli_columns]
df_metrics["mean_" + inference_column] = sub_df_nli.mean(1)
df_metrics["median_" + inference_column] = sub_df_nli.median(
1
) # pyright: ignore # noqa
return df_metrics
def create_generation_metrics_report(
ground_truth: pd.DataFrame,
answers: pd.DataFrame,
llm: BaseChatModel,
metric_binds: List[MetricBind],
max_concurrency: int = 8,
show_progress_bar: bool = True,
auto_download_nltk: bool = True,
) -> pd.DataFrame:
"""
Calculates RAG evaluation generation metrics from input dataframes.
Parameters
-----------
ground_truth : pd.DataFrame
contains the ground truth answers
The structure of the ground truth
is described in `aidial_rag_eval.types.GroundTruthAnswers`.
answers : pd.DataFrame
contains the collected answers
The structure of the collected answers
is described in `aidial_rag_eval.types.CollectedAnswers`.
llm : BaseChatModel, optional, default=None
A Langchain chat model used for calculating generation metrics.
metric_binds : List[MetricBind], optional, default=None
A list of string constants from `aidial_rag_eval.metric_binds`
specifying the generation metrics.
max_concurrency : int, default=8
The maximum number of concurrent requests to the LLM.
show_progress_bar : bool, default=True
To display a progress bar during LLM requests.
Returns
------------
pd.DataFrame
Returns merged ground_truth dataframe,
answers dataframe with generation metrics dataframe.
"""
df_merged = merge_ground_truth_and_answers(ground_truth, answers)
df_metrics = calculate_generation_metrics(
df_merged,
llm,
metric_binds,
max_concurrency,
show_progress_bar,
auto_download_nltk,
)
return pd.merge(df_merged, df_metrics, left_index=True, right_index=True)
def create_rag_eval_metrics_report(
ground_truth: pd.DataFrame,
answers: pd.DataFrame,
matcher: Matcher = DEFAULT_MATCHER,
llm: Optional[BaseChatModel] = None,
metric_binds: Optional[List[MetricBind]] = None,
max_concurrency: int = 8,
show_progress_bar: bool = True,
auto_download_nltk: bool = True,
) -> pd.DataFrame:
"""
Calculates RAG evaluation metrics from input dataframes.
Parameters
-----------
ground_truth : pd.DataFrame
contains the ground truth answers
The structure of the ground truth
is described in `aidial_rag_eval.types.GroundTruthAnswers`.
answers : pd.DataFrame
contains the collected answers
The structure of the collected answers
is described in `aidial_rag_eval.types.CollectedAnswers`.
matcher : Matcher, default=DEFAULT_MATCHER
An object responsible for matching facts
from the ground truth to the context in the answers.
llm : BaseChatModel, optional, default=None
A Langchain chat model used for calculating generation metrics.
metric_binds : List[MetricBind], optional, default=None
A list of string constants from `aidial_rag_eval.metric_binds`
specifying the generation metrics.
max_concurrency : int, default=8
The maximum number of concurrent requests to the LLM.
show_progress_bar : bool, default=True
To display a progress bar during LLM requests.
Returns
------------
pd.DataFrame
Returns merged ground_truth dataframe, answers dataframe with metrics dataframe.
"""
df_merged = merge_ground_truth_and_answers(ground_truth, answers)
retrieval_metrics = calculate_retrieval_metrics(df_merged, matcher)
if not metric_binds:
return pd.merge(df_merged, retrieval_metrics, left_index=True, right_index=True)
if llm is None:
raise ValueError("Argument 'llm' is required.")
generation_metrics = calculate_generation_metrics(
df_merged,
llm,
metric_binds,
max_concurrency,
show_progress_bar,
auto_download_nltk,
)
return pd.concat([df_merged, retrieval_metrics, generation_metrics], axis=1)