-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpt4_as_judge_gsm8k.py
178 lines (145 loc) · 5.48 KB
/
gpt4_as_judge_gsm8k.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
'''
How to run?
python gpt4_as_judge.py --response_file "path to response file" --save_path results
'''
# input()
import json
import time
import openai
import argparse
import pandas as pd
from tqdm import tqdm
import datasets
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--response_file", help='json path where responses from LM Eval are saved', type=str)
parser.add_argument('--save_path', help='path where results are saved', type=str, required=False, default='results')
args = parser.parse_args()
file = args.response_file
save_path = args.save_path
key_path = "<Path to GPT4 API KEY JSON>"
with open(key_path, 'r') as f:
keys = json.load(f)
openai.api_type = keys['api_type']
openai.api_base = keys['api_base']
openai.api_version = keys['api_version']
openai.api_key=keys['api_key']
model_engine = keys['model_engine']
model_family = keys['model_family']
# API setting constants
API_MAX_RETRY = 5
API_RETRY_SLEEP = 15
API_ERROR_OUTPUT = "$ERROR$"
data_r = json.load(open(file, 'r'))
def extract_json_labels(json_list):
new_json_list = []
for item in json_list:
if item['exact_match'] == 1:
new_json_list.append("positive")
elif item['exact_match'] == 0:
new_json_list.append("negative")
else:
print(item['exact_match'])
assert False, "item mismatch"
return new_json_list
labels = extract_json_labels(data_r)
def extract_json_data(json_list):
new_json_list = []
for item in json_list:
new_item = {
"instruction": item["doc"]["question"],
"target": item["target"],
"output": item["resps"][0][0]
}
new_json_list.append(new_item)
return new_json_list
data_r = extract_json_data(data_r[:len(data_r)//2])
##define chat completion function##
def chat_completion(system, prompt):
for _ in range(API_MAX_RETRY):
try:
response = openai.ChatCompletion.create(
model="gpt-4",
engine="GPT4",
messages=[
{"role": "system", "content": system},
{"role": "user", "content": prompt},
]
)
response = response['choices'][0]['message']['content']
count = response.count('[[A]]')+response.count('[[B]]')
if count > 1 or count < 1:
print('--More (or less) than one tags--')
print(response)
print('Retry...')
continue
return response
except Exception as e:
if 'policy' in str(e):
print("Skipping due to openai policy")
return '[[N]]'
print(type(e), e)
print("trying again")
time.sleep(API_RETRY_SLEEP)
#[[N]] if can not label the response
return '[[N]]'
print("Running GSM8k evals")
system = (
"You are a helpful assistant, that evaluates models by the quality of their answers to math questions."
"To do so, I will give you the instructions (prompts) given to the models, and the response of the model and a reference solution. Please evaluate the models based on mathematical correctness by referring to the reference solution."
"Be as objective as possible. After providing your explanation, output your final verdict by strictly following this format:"
"\"[[A]]\" if the response is correct , \"[[B]]\" if the response is wrong."
)
template = (
"Question: {question}\n"
"Reference Solution: {target}\n\n"
"Response: {response}\n\n"
"Final Verdict:<verdict>."
)
##just tags##
def annotate_tag(r):
if '[[A]]' in r:
k = 'positive'
elif '[[B]]' in r:
k = 'negative'
elif '[[N]]' in r:
k = 'CantAnnotate'
else:
return 'CantAnnotate'
return k
##Running evaluations##
judge = {k:'0' for k in range(len(data_r))}
comments = {k:'-' for k in range(len(data_r))}
count_dict = {'positive':0, 'negative':0, 'responseNA':0, 'CantAnnotate':0}
file_ = file.replace(".json",'').split('/')[-1]
save_name = f"{save_path}/{file_}_gpt4_labelled.csv"
data_pdr = pd.DataFrame(data_r).rename(columns={"response": file_})
data_pdr['template_label'] = labels[:len(labels)//2]
data_pdr['last_num_label'] = labels[len(labels)//2:]
for i in tqdm(range(len(data_r))):
question = data_r[i]['instruction']
response = data_r[i]['output']
reference_output = data_r[i]['target']
#[[N]] if the response contains [[N]]
if '[[N]]' in response:
k = 'responseNA'
r = '[[N]]'
else:
templateA = template.format(question=question, response=response, target=reference_output)
r=chat_completion(system=system, prompt=templateA)
k = annotate_tag(r)
judge[i] = k
comments[i] = r+f'\n\n[Evaluated text] {response}'
count_dict[k] += 1
print(f"\n\n[Question]: {question}")
print(f"\n[response]: {r}")
print(f"\n\n[Total counts]: \n{json.dumps(count_dict, indent=4)}")
data_pdr['comments'] = comments
data_pdr['gpt4_annotation'] = judge.values()
data_pdr.to_csv(f"{save_name}")
with open(f"{save_path}/all_results.txt",'a+') as f:
f.write(f"-----------------{save_name}-----------------\n")
f.write(f"\n[Total counts]: \n{json.dumps(count_dict, indent=4)}\n")
f.write(f"\n[p/(p+np)]: {count_dict['positive']/(count_dict['positive']+count_dict['negative'])}")
f.write("\n\n")
print(f"\nCompleted, please check {save_name}")