-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
303 lines (267 loc) · 8.86 KB
/
evaluate.py
File metadata and controls
303 lines (267 loc) · 8.86 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
293
294
295
296
297
298
299
300
301
302
303
import argparse
import datetime
import enum
import json
import os
import re
import time
from typing import Optional
assert os.path.exists(".env"), "Please create a .env file with your API keys!"
with open(".env", "r") as f:
for line in f.read().splitlines():
key, value = line.split("=")
os.environ[key] = value
from systems import (
CanvasRetrieval,
CanvasRetrievalContext,
Gemma3Reasoner,
GPT4Reasoner,
GPT35Reasoner,
Llama3_1_8bReasoner,
Llama3_1_70bReasoner,
Llama3_3Reasoner,
MixtralReasoner,
ContextualRAG,
VanillaRAG,
)
from systems.evaluators import (
BreakdownEvaluator,
F1Evaluator,
LLMEvaluator,
PrecisionEvaluator,
RecallEvaluator,
)
class MethodEnum(enum.Enum):
RANDOM_EVIDENCE = "random_evidence"
CANVAS_RETRIEVE = "canvas_retrieve"
CANVAS_RETRIEVE_CONTEXT = "canvas_retrieve_context"
GPT3 = "gpt3"
GPT4 = "gpt4"
LLAMA3_1_8B = "llama3_1_8b"
LLAMA3_1_70B = "llama3_1_70b"
LLAMA3_3 = "llama3_3"
GEMMA3 = "gemma3"
MIXTRAL = "mixtral"
VANILLA_RAG = "vanilla_rag"
def __str__(self):
return self.name.lower()
def __repr__(self):
return str(self)
@staticmethod
def argparse(s):
try:
return MethodEnum[s.upper()]
except KeyError:
return s
REASONERS = {
"vanilla_rag": VanillaRAG,
"gpt3": GPT35Reasoner,
"gpt4": GPT4Reasoner,
"llama3_1_8b": Llama3_1_8bReasoner,
"llama3_1_70b": Llama3_1_70bReasoner,
"llama3_3": Llama3_3Reasoner,
"gemma3": Gemma3Reasoner,
"mixtral": MixtralReasoner,
"rag_systems": ContextualRAG,
"canvas_retrieve": CanvasRetrieval,
"canvas_retrieve_context": CanvasRetrievalContext,
}
EVALUATORS = {
"recall": RecallEvaluator,
"breakdown": BreakdownEvaluator,
"precision": PrecisionEvaluator,
"f1": F1Evaluator,
"llm": LLMEvaluator,
}
RESULTS_DIRECTORY = "output/results/"
EXPERIMENT_HISTORY_DIRECTORY = "output/experiments/"
USECASE_DATAPATH = {
"hotpot-scrambled": {
"data_path": "data/hotpot-scrambled",
"qa_path": "workloads/hotpot-scrambled.json",
},
"stirpot-300": {
"data_path": "data/stirpot-300",
"qa_path": "workloads/stirpot-300.json",
},
}
def _parse_result_cache(
results_directory: str, system_name: str, usecase: str
) -> Optional[str]:
"""
Returns the filename for the most recent cached experiment result for a system name and usecase
and None if matching cache files are not found.
"""
parsed_filenames = []
filenames = [f for f in os.listdir(results_directory) if f.endswith(".json")]
for fname in filenames:
if fname.startswith(f"{usecase}_{system_name}"):
timestamp = fname.split("_")[-1].split(".")[0]
parsed_filenames.append((timestamp, fname))
if len(parsed_filenames) == 0:
return None
sorted_filenames = sorted(parsed_filenames, key=lambda x: x[0])
latest = sorted_filenames[-1][1] # these are tuples of (timestamp, filename)
return latest
def evaluate_system(
system_name: str,
usecase: Optional[str],
data_path: Optional[str],
workload_path: Optional[str],
evaluator: str,
topk: int,
query_limit: int,
use_system_cache: bool,
use_result_cache: bool,
verbose: bool,
):
timestamp = time.strftime("%Y-%m-%d-%H%M")
# Prepares the workload
assert (usecase is None) ^ (data_path is None)
assert (usecase is None) ^ (workload_path is None)
if usecase is not None:
data_path = USECASE_DATAPATH[usecase]["data_path"]
workload_path = USECASE_DATAPATH[usecase]["qa_path"]
with open(workload_path) as f:
qa_dicts = json.load(f)
queries = qa_dicts[:query_limit]
print(f"Benchmark: {usecase} ({len(queries)} questions) on method {method_name}")
# Prepares the system and evaluator
reasoner = REASONERS[system_name](
data_path=data_path,
topk=topk,
use_cache=use_system_cache,
verbose=verbose,
usecase=usecase,
method_name=system_name,
)
evaluator = EVALUATORS[evaluator](use_cache=use_system_cache, verbose=verbose)
# Attempt to load result cache
result_filename = None
if use_result_cache:
result_filename = _parse_result_cache(
results_directory=RESULTS_DIRECTORY,
system_name=system_name,
usecase=usecase,
)
if result_filename is not None:
print(f"Using cached results from {result_filename}")
with open(f"{RESULTS_DIRECTORY}/{method_name}/{result_filename}") as f:
results = json.load(f)
for idx, result in results.items():
measures = evaluator.evaluate(result)
results[idx] = result | measures
with open(
f"{EXPERIMENT_HISTORY_DIRECTORY}/{method_name}/{result_filename}"
) as f:
experiment_history = json.load(f)
overall_time = experiment_history["overall_time"]
else:
results = {}
start_time = time.time()
for idx, query in enumerate(queries):
if verbose:
print()
print(f"Running query {idx}: {query['question']}")
predicted = reasoner(query["question"])
try:
target = query["answer"]
except KeyError:
target = [answer["value"] for answer in query["answer_set"]]
result = query | {"predicted": predicted, "target": target}
del result["answer"]
measures = evaluator.evaluate(result)
results[idx] = result | measures
end_time = time.time()
if verbose:
print(f"Target: {target} - Accuracy: {measures['accuracy']}")
print()
overall_time = end_time - start_time
aggregate_scores = evaluator.aggregate_scores()
print("Overall time", overall_time)
for k in aggregate_scores:
print(k, ":", aggregate_scores[k])
if not use_result_cache:
experiment_history = {
"date": timestamp,
"usecase": usecase,
"method": method_name,
"overall_time": overall_time,
} | aggregate_scores
experiment_string = f"{usecase}_{method_name}_{timestamp}.json"
if "vanilla_rag" in method_name:
experiment_string = f"{usecase}_{method_name}_topk{topk}_{timestamp}.json"
result_path = f"{RESULTS_DIRECTORY}{method_name}/{experiment_string}"
os.makedirs(f"{RESULTS_DIRECTORY}{method_name}", exist_ok=True)
with open(result_path,"w+") as f:
json.dump(results, f, indent=4, ensure_ascii=False)
if "vanilla_rag" in method_name:
experiment_history["rag_topk"] = topk
history_path = f"{EXPERIMENT_HISTORY_DIRECTORY}{method_name}/{experiment_string}.json"
os.makedirs(f"{EXPERIMENT_HISTORY_DIRECTORY}{method_name}", exist_ok=True)
with open(history_path,"w+") as f:
json.dump(experiment_history, f, indent=4, ensure_ascii=False)
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument(
"--usecase",
help="The workload to be used for the evaluation, e.g., bdf-tiny,openmmqa-medium, ma-tiny",
default="openmmqa-tiny",
)
argparser.add_argument(
"--query-limit",
type=int,
help="Limit the number of queries to run",
default=0,
)
argparser.add_argument(
"--system-cache",
help="Use system cache if it's available",
default=False,
action="store_true",
)
argparser.add_argument(
"--result-cache",
help="Use cached results if they are available",
default=False,
action="store_true",
)
argparser.add_argument(
"--verbose",
help="Print intermediate results",
default=False,
action="store_true",
)
argparser.add_argument(
"--method",
help="Choose method to evaluate for",
type=MethodEnum.argparse,
choices=list(MethodEnum),
default="canvas_systems",
)
argparser.add_argument(
"--evaluator",
help="Choose evaluator class, e.g., recall, breakdown",
default="llm",
)
argparser.add_argument(
"--topk",
help="The number of top-k documents to retrieve if using a RAG method (default 50)",
default=50,
)
args = argparser.parse_args()
### Parsing arguments
method = args.method
method_name = method.name.lower()
evaluate_system(
system_name=method_name,
usecase=args.usecase,
data_path=None,
workload_path=None,
evaluator=args.evaluator,
topk=int(args.topk),
query_limit=int(args.query_limit) or None,
use_system_cache=args.system_cache,
use_result_cache=args.result_cache,
verbose=args.verbose,
)