-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
367 lines (320 loc) · 13.2 KB
/
main.py
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
import argparse
import json
import os
from datetime import datetime
from utils import *
from evaluation.datasets.gsm8k import Gsm8kEvaluator
from evaluation.datasets.aqua import AQuAEvaluator
from evaluation.datasets.hotpotqa import HotpotQaEvaluator
from evaluation.metrics.pass_rate import PassRateEvaluator
from evaluation.multi_evaluator import MultiEvaluator
def parse_and_print_args():
"""
Parse command line arguments and print them.
Returns:
Namespace: Parsed arguments.
"""
args = parse_arguments()
print("*****************************")
print(args)
print("*****************************")
return args
def load_predictions(args):
"""
Load model predictions from a JSON file.
Args:
args (Namespace): Arguments containing the output path.
Returns:
list: List of model predictions.
"""
with open(args.output_path, "r", encoding="utf-8") as f:
return json.load(f)["model_result"]
def handle_model_output(args, question, model_output, decoder):
"""
Handle the model output based on the specified method.
Args:
args (Namespace): Arguments containing various settings and configurations.
question (str): The input question.
model_output (str): The raw model output.
decoder (object): Decoder object used for handling model output.
Returns:
str: Cleaned model output.
"""
if args.method == "zero_shot_cot":
model_output_postprocess = f"Question: {question}\nModel answer: {model_output} {args.direct_answer_trigger_for_zeroshot_cot}"
model_output, _, _ = decoder.decode(args, model_output_postprocess, args.max_length_direct, 0, 2)
return answer_cleansing(args, model_output)
def process_predictions(pred_data, args, decoder, gt_data):
"""
Processes prediction data and compares it with ground truth data.
Args:
pred_data (list): A list of dictionaries containing prediction data.
args (Namespace): Arguments containing various settings and configurations.
decoder (object): Decoder object used for handling model output.
gt_data (dict): Dictionary containing ground truth data.
Returns:
tuple: A tuple containing:
- predictions (list): List of processed predictions.
- references (list): List of ground truth references.
- input_tokens (list): List of input token counts.
- output_tokens (list): List of output token counts.
- total (int): Total number of processed entries.
Note:
If the ground truth answer is not found using the entry_id, it attempts to convert entry_id to an integer and search again.
If args.limit_dataset_size is set to a non-zero value, the processing will stop once the limit is reached.
"""
predictions, references, input_tokens, output_tokens, total_tokens = [], [], [], [], []
total = 0
for i, pre in enumerate(pred_data):
model_output = pre.get("last_output", "")
question = pre.get("question", "")
entry_id = pre["id"]
input_tokens.append(pre.get("prompt_tokens", 0))
output_tokens.append(pre.get("completion_tokens", 0))
total_tokens.append(pre.get("total_tokens", 0))
if model_output:
cleaned_answer = handle_model_output(args, question, model_output, decoder)
else:
cleaned_answer = answer_cleansing(args, model_output)
pred = floatify_ans(cleaned_answer)
predictions.append(pred)
gt_answer = gt_data.get(entry_id)
if gt_answer is None:
try:
entry_id_int = int(entry_id)
gt_answer = gt_data.get(entry_id_int)
except ValueError:
pass
references.append(gt_answer)
print(f"======== id : {entry_id}, pred : {pred}, gt : {gt_answer}")
total += 1
if (args.limit_dataset_size != 0) and ((i + 1) >= args.limit_dataset_size):
break
return predictions, references, input_tokens, output_tokens, total
def calculate_token_statistics(args, input_tokens, output_tokens):
"""
Calculate and print token statistics and cost.
Args:
args (Namespace): Arguments containing various settings and configurations.
input_tokens (list): List of input token counts.
output_tokens (list): List of output token counts.
"""
exchange_rate = 7.3249
if args.model == "Doubao-lite-32k":
input_token_cost = 0.0003 / exchange_rate # dollars / 1K tokens
output_token_cost = 0.0006 / exchange_rate # dollars / 1K tokens
elif args.model == "gpt-3.5-turbo":
input_token_cost = 0.0005 # dollars / 1K tokens
output_token_cost = 0.0015 # dollars / 1K tokens
total_input_tokens = sum(input_tokens)
average_input_tokens = total_input_tokens / len(input_tokens) if input_tokens else 0
total_output_tokens = sum(output_tokens)
average_output_tokens = total_output_tokens / len(output_tokens) if output_tokens else 0
total_tokens_sum = total_input_tokens + total_output_tokens
input_cost = total_input_tokens / 1000 * input_token_cost
output_cost = total_output_tokens / 1000 * output_token_cost
total_cost = input_cost + output_cost # dollars
print("Total input tokens:", total_input_tokens)
print("Average input tokens:", average_input_tokens)
print("Total output tokens:", total_output_tokens)
print("Average output tokens:", average_output_tokens)
print("Total tokens:", total_tokens_sum)
print("Total cost for tokens:", total_cost)
def main():
"""
Main function to run the evaluation process.
"""
args = parse_and_print_args()
fix_seed(args.random_seed)
print("OPENAI_API_KEY:\n", os.getenv("OPENAI_API_KEY"))
# Initialize decoder class (load model and tokenizer) ...
decoder = Decoder(args)
print("setup data loader ...")
dataloader = setup_data_loader(args)
print_now()
# Initialize the evaluator
dataset_evaluator = {
"aqua": AQuAEvaluator(),
"gsm8k": Gsm8kEvaluator(),
"hotpotqa": HotpotQaEvaluator(),
}.get(args.dataset)
metric_evaluator = PassRateEvaluator()
multi_evaluator = MultiEvaluator([dataset_evaluator, metric_evaluator])
json_results = {
"dataset": args.dataset,
"model_id": args.model,
"alg": args.agent,
"model_result": [],
}
if args.output_path:
pred_data = load_predictions(args)
gt_data = load_ground_truth(args.dataset_path, args.dataset)
predictions, references, input_tokens, output_tokens, total = process_predictions(pred_data, args, decoder, gt_data)
else:
total = 0
demo = create_demo_text(args, cot_flag=(args.method == "few_shot_cot")) if args.method in ["few_shot", "few_shot_cot"] else ""
for i, data in enumerate(dataloader):
print(f"*************************\n{i + 1}st data")
x, y, idx = data
x = "Q: " + x[0] + "\nA:"
y = y[0].strip()
if args.method == "zero_shot":
x = x + " " + args.direct_answer_trigger_for_zeroshot
elif args.method == "zero_shot_cot":
x = x + " " + args.cot_trigger
elif args.method in ["few_shot", "few_shot_cot"]:
x = demo + x
max_length = args.max_length if "cot" not in args.method else args.max_length_direct
z, _, raw_full_response = decoder.decode(args, x, max_length, i, 1)
# Answer extraction for zero-shot-cot ...
if args.method == "zero_shot_cot":
z2 = x + z + " " + args.direct_answer_trigger_for_zeroshot_cot
pred, _, _ = decoder.decode(args, z2, args.max_length_direct, i, 2)
print(z2 + pred)
else:
pred = z
print(x + pred)
# Clensing of predicted answer ...
pred = answer_cleansing(args, pred)
predictions.append(pred)
references.append(y)
# Token statistics
input_token = raw_full_response.json()["usage"]["prompt_tokens"]
output_token = raw_full_response.json()["usage"]["completion_tokens"]
input_tokens.append(input_token)
output_tokens.append(output_token)
print("pred : {}".format(pred))
print("GT : " + y)
print("*************************")
result = {
"id": idx[0].item(),
"question": x,
"last_output": z,
"output_postprocess": pred,
"ground_truth": y,
"prompt_tokens": input_token,
"completion_tokens": output_token,
}
json_results["model_result"].append(result)
# Save current results to JSON file
save_dir = os.path.join(args.output_dir, args.agent, args.dataset)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
if not os.path.exists(save_dir):
os.makedirs(save_dir)
with open(os.path.join(save_dir, f"{args.method}_{args.limit_dataset_size}_{args.model}_{timestamp}.json"), "w") as ans_file:
json.dump(json_results, ans_file, indent=4)
ans_file.flush()
ans_file.close()
total += 1
if (args.limit_dataset_size != 0) and ((i + 1) >= args.limit_dataset_size):
break
# Evaluate predictions using the evaluator
evaluation_results = multi_evaluator.evaluate(predictions, references)
print("Evaluation Result:", evaluation_results)
if args.dataset == "hotpotqa":
print("Reward : {}".format(evaluation_results.get("reward", 0)))
print("F1 : {}".format(evaluation_results.get("f1", 0)))
print("EM : {}".format(evaluation_results.get("em", 0)))
else:
# Calculate accuracy ...
print("Accuracy : {}".format(evaluation_results.get("accuracy", 0)))
# Calculate pass rate ...
print("Pass Rate : {}\n".format(evaluation_results.get("pass_rate", 0)))
print(f" ====== Testing {args.agent} on {args.dataset} dataset")
print("Total predictions:", total)
calculate_token_statistics(args, input_tokens, output_tokens)
def parse_arguments():
"""
Parse command line arguments.
Returns:
Namespace: Parsed arguments.
"""
parser = argparse.ArgumentParser(description="OmAgent Evaluation")
parser.add_argument("--random_seed", type=int, default=1, help="random seed")
parser.add_argument(
"--dataset",
type=str,
default="aqua",
choices=[
"aqua",
"gsm8k",
"hotpotqa",
],
help="dataset used for experiment",
)
parser.add_argument(
"--minibatch_size",
type=int,
default=1,
choices=[1],
help="minibatch size (default 1)",
)
parser.add_argument(
"--max_num_worker",
type=int,
default=4,
help="maximum number of workers for dataloader",
)
parser.add_argument(
"--model",
type=str,
default="gpt-3.5-turbo",
choices=[
"gpt-4o-mini",
"gpt-4o",
"gpt-3.5-turbo",
"Doubao-lite-32k",
],
help="model used for decoding.",
)
parser.add_argument(
"--method",
type=str,
default="zero_shot_cot",
choices=["zero_shot", "zero_shot_cot", "few_shot", "few_shot_cot"],
help="method",
)
parser.add_argument(
"--cot_trigger_no",
type=int,
default=1,
help="A trigger sentence that elicits a model to execute chain of thought",
)
parser.add_argument(
"--max_length",
type=int,
default=2048,
help="maximum length of output tokens by model for reasoning extraction",
)
parser.add_argument(
"--max_length_direct",
type=int,
default=32,
help="maximum length of output tokens by model for answer extraction",
)
parser.add_argument(
"--limit_dataset_size",
type=int,
default=0,
help="whether to limit test dataset size. if 0, the dataset size is unlimited and we use all the samples in the dataset for testing.",
)
parser.add_argument(
"--output_dir", type=str, default="./outputs/", help="output directory"
)
parser.add_argument("--output_path", type=str, default=None, help="output path")
parser.add_argument(
"--agent",
type=str,
default="cot",
choices=["cot", "pot", "sc_cot", "react", "dnc"],
help="agent used for experiment",
)
parser.add_argument("--system_prompt", type=str, default="", help="system prompt")
parser.add_argument("--openai_api_key", type=str, default="sk-4zr6uGzVbNfIiq7U513aCc94Af614792938cE9AdB7D0E295", help="openai api key")
parser.add_argument("--openai_url", type=str, default="http://36.133.246.107:11002/v1/chat/completions", help="openai url")
args = parser.parse_args()
configure_dataset(args)
configure_cot_trigger(args)
return args
if __name__ == "__main__":
main()