-
Notifications
You must be signed in to change notification settings - Fork 3
/
evaluate.py
203 lines (177 loc) · 7.1 KB
/
evaluate.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
import argparse
import os
import json
import openai
import random
from pathlib import Path
from itertools import combinations
from string import Template
from tqdm import tqdm
from threading import get_ident
from concurrent.futures import ThreadPoolExecutor
from eval_utils import (
retry_handler,
openai_chat_request,
)
import numpy as np
from data_utils import concept_list_str
from datasets import load_dataset
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--mode", type=str, default="pairwise", required=True)
parser.add_argument("--model_output_file", type=str, required=False)
parser.add_argument("--eval_output_file", type=str, required=True)
parser.add_argument("--start_idx", type=int, default=0)
parser.add_argument("--end_idx", type=int, default=-1)
parser.add_argument("--save_interval", type=int, default=3)
# Prompt configs
parser.add_argument("--max_words_to_eval", type=int, default=-1)
# OpenAI Configs
parser.add_argument("--api_key", type=str, default=None)
parser.add_argument("--model", type=str, default="gpt-4-0314")
parser.add_argument("--engine", type=str, default=None)
parser.add_argument("--temperature", type=float, default=0.0)
parser.add_argument("--max_tokens", type=int, default=1024)
args = parser.parse_args()
if args.api_key is not None:
openai.api_key = args.api_key
return args
def parse_result(result_str):
if "neither" in result_str.lower():
return "neither"
elif "A" in result_str:
return "A"
elif "B" in result_str:
return "B"
elif "tie" in result_str:
return "tie"
else:
return "Not Matched"
def gpt_eval(results, args):
# try to load the existing results from args.eval_output_file
if os.path.exists(args.eval_output_file):
cnt = 0
with open(args.eval_output_file, "r") as f:
existing_results = json.load(f)
for i in range(len(existing_results)):
e = existing_results[i]
t = results[i]
if e["prompt"] != t["prompt"]:
continue
# if e["prompt"] == t["prompt"] and e["result"] != "N/A":
# results[i]["result"] = e["result"]
# cnt += 1
if "result" in e:
t["result"] = e["result"]
if "parsed_result" in e:
t["parsed_result"] = e["parsed_result"]
cnt += 1
print(f"loading {cnt} results from {args.eval_output_file}")
openai_args = {
"prompt": "TODO",
"temperature": args.temperature,
"max_tokens": args.max_tokens,
"stop": []
}
if args.model:
openai_args['model'] = args.model
if args.engine:
openai_args['engine'] = args.engine
@retry_handler(retry_limit=10)
def api(ind, item, **kwargs):
result = openai_chat_request(**kwargs)
result = result[0]
return result
# results = results[args.start_idx:args.end_idx] # for debug
for ind, item in tqdm(enumerate(results), total=len(results), desc=f"Evaluating: {args.eval_output_file} "):
if item["result"] != "N/A":
results[ind]["parsed_result"] = parse_result(results[ind]["result"])
print(f"Skipping {ind} for {args.eval_output_file}")
# skip the existing results
continue
openai_args["prompt"] = item["prompt"]
try:
result = api(ind, item, **openai_args)
results[ind]["result"] = result
results[ind]["parsed_result"] = parse_result(results[ind]["result"])
r = results[ind]["parsed_result"]
if r in ["A", "B"]:
results[ind]["winner"] = item["assignment"][r]
else:
results[ind]["winner"] = r
except Exception as e:
print(e)
raise Exception("Failed!")
# print("Done!")
if ind % args.save_interval == 0 or ind == len(results)-1:
with open(args.eval_output_file, "w") as f:
json.dump(results, f, indent=2)
with open(args.eval_output_file, "w") as f:
json.dump(results, f, indent=2)
return results
def shorten(text, K=-1):
# if K > 0 and len(text.split(" ")) > K:
# text = " ".join(text.split(" ")[:K]) + "... (truncated)"
pass
return text
def placeholder_generation(args):
commongen_data = load_dataset("allenai/commongen_lite_eval", split="train")
with open("eval_template.md") as f:
eval_template = f.read()
results = []
with open(args.model_output_file, 'r') as f:
candidates = json.load(f)
id_to_references = {x["id"]: x["human_annotations"] for x in commongen_data}
candidates = [c for c in candidates if c["id"] in id_to_references]
references = [id_to_references[c["id"]] for c in candidates]
assert len(candidates) == len(references)
L = len(candidates)
if args.end_idx < 0:
args.end_idx = L
print(f"# examples in candidates: {len(candidates)}; We take {args.end_idx-args.start_idx} for evaluation.")
candidates = candidates[args.start_idx:args.end_idx]
references = references[args.start_idx:args.end_idx]
results = []
for item, human_annoations in zip(candidates, references):
instruction = item["instruction"]
for ref_id, ref in enumerate(human_annoations):
o = item["output"][0]
# random decide which is A and which is B
d = {}
d["id"] = item["id"]
d["ref_index"] = ref_id
d["input"] = instruction
d["concept_set"] = item["concept_set"]
d["human_ref"] = ref
d["model_output"] = item["output"]
d["generator"] = item["generator"]
d["eval_config"] = {"mode": args.mode, "gpt": args.model, "max_words": args.max_words_to_eval}
if random.random() < 0.5:
A = o
B = ref["ref"]
d["assignment"] = {"A": d["generator"], "B": "human"}
else:
A = ref["ref"]
B = o
d["assignment"] = {"A": "human", "B": d["generator"]}
cs_str = concept_list_str(d["concept_set"])
prompt = eval_template.replace("{$concept_list}", cs_str).replace("{$candidate_A}", A).replace("{$candidate_B}", B)
d["prompt"] = prompt
d["result"] = "N/A"
results.append(d)
return results
def main():
random.seed(42)
args = get_args()
if args.mode.startswith("trial"):
results = placeholder_generation(args)
print(f"We have {len(results)} examples to evaluate!")
with open(args.eval_output_file, "w") as f:
json.dump(results, f, indent=2)
elif args.mode.startswith("compare"):
results = placeholder_generation(args)
results = gpt_eval(results, args)
else:
print("Not implemented yet!")
if __name__ == "__main__":
main()