-
Notifications
You must be signed in to change notification settings - Fork 28
/
utils.py
302 lines (276 loc) · 10.7 KB
/
utils.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
# This file contains necessary helper functions
# e.g. GPT request, create_dataloader
import openai
import random
import sys
import numpy as np
import torch
import json
import re
from collections import Counter
import time
# put your API key here
API_KEY = "YOUR_KEY"
# define for no solution if GPT cannot generate a valid solution
# here define a magic number for the convenience of variance calculatio
NO_SOLUTION = '-10086' # use this when calculating numerical results
# set the random seed for reproducibility
def set_random_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
def chatgpt_request(model:str, message_list:list, max_tokens:int, temperature=0.7, sleep=3):
resp = None
done = False
while not done:
try:
openai.api_key = API_KEY
resp = openai.ChatCompletion.create(
model=model,
messages=message_list,
temperature=temperature,
max_tokens=max_tokens,
top_p=1.0,
)
done = True
except:
errno = sys.exc_info()[:2]
if errno[0] == openai.error.InvalidRequestError:
# print(f"Invalid Request\nPrompt: {message_list}\n")
print("Invalid Request")
print(f"Reason: {errno[1]}")
assert False
else:
print(f"Error: {errno[0]}\n")
print(f"Reason: {errno[1]}\n")
# pause between each request to avoid rate limit
time.sleep(sleep)
return resp
# pass in a list of prompts and returns a response body contains a list of responses
def GPT3_request(model:str, input_prompt:list, max_tokens:int, time_interval, temperature=0.7, stop=None):
resp = None
done = False
while not done:
try:
openai.api_key = API_KEY
resp = openai.Completion.create(
model=model,
prompt=input_prompt,
temperature=temperature,
max_tokens=max_tokens,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop = stop
)
done = True
except:
errno = sys.exc_info()[:2]
if errno[0] == openai.error.InvalidRequestError:
print(f"Invalid Request\nPrompt: {input_prompt}\n")
print(f"Reason: {errno[1]}")
assert False
else:
print(f"Error: {errno[0]}\n")
print(f"Reason: {errno[1]}\n")
# pause between each request to avoid rate limit
time.sleep(time_interval)
return resp
def load_data(args):
questions = []
answers = []
decoder = json.JSONDecoder()
if args.dataset == "gsm8k":
with open(args.dataset_path) as f:
lines = f.readlines()
for line in lines:
json_res = decoder.raw_decode(line)[0]
questions.append(json_res["question"].strip())
answers.append(json_res["answer"].split("#### ")[-1].replace(",", ""))
elif args.dataset == "aqua":
with open(args.dataset_path) as f:
lines = f.readlines()
for line in lines:
json_res = decoder.raw_decode(line)[0]
qes = json_res["question"].strip() + " Answer Choices:"
for opt in json_res["options"]:
opt = opt.replace(')', ') ')
qes += f" ({opt}"
questions.append(qes)
answers.append(json_res["correct"])
elif args.dataset == "svamp":
with open(args.dataset_path) as f:
json_data = json.load(f)
for line in json_data:
q = line["Body"].strip() + " " + line["Question"].strip()
a = str(line["Answer"])
if a[-2:] == ".0":
a = a[:-2]
questions.append(q)
answers.append(a)
elif args.dataset == "asdiv":
with open(args.dataset_path) as f:
json_data = json.load(f)["Instances"]
for line in json_data:
q = line['input'].strip()
a = line['output'][0]
questions.append(q)
answers.append(a)
elif args.dataset in ("addsub", "singleeq", "multiarith"):
with open(args.dataset_path) as f:
json_data = json.load(f)
for line in json_data:
q = line["sQuestion"].strip()
a = str(line["lSolutions"][0])
if a[-2:] == ".0":
a = a[:-2]
questions.append(q)
answers.append(a)
elif args.dataset == "csqa":
with open(args.dataset_path) as f:
lines = f.readlines()
for line in lines:
json_res = decoder.raw_decode(line)[0]
choice = "Answer Choices:"
for c in json_res["question"]["choices"]:
choice += " ("
choice += c["label"]
choice += ") "
choice += c["text"]
questions.append(json_res["question"]["stem"].strip() + " " + choice)
answers.append(json_res["answerKey"])
elif args.dataset == "strategyqa":
if 'task' in args.dataset_path:
with open(args.dataset_path) as f:
json_data = json.load(f)["examples"]
for line in json_data:
q = line["input"].strip()
a = int(line["target_scores"]["Yes"])
if a == 1:
a = "yes"
else:
a = "no"
questions.append(q)
answers.append(a)
else:
with open(args.dataset_path, encoding='utf-8') as f:
json_data = json.load(f)
for line in json_data:
q = line["question"].strip()
if line['answer']:
a = 'yes'
else:
a = 'no'
questions.append(q)
answers.append(a)
elif args.dataset in ("coin_flip", "last_letters"):
with open(args.dataset_path) as f:
json_data = json.load(f)
json_data = json_data["examples"]
for line in json_data:
q = line["question"]
a = line["answer"]
questions.append(q)
answers.append(a)
elif args.dataset == 'time_zone':
with open(args.dataset_path) as f:
json_data = json.load(f)
for line in json_data:
q = line['question'].strip()
a = line["answer"]
questions.append(q)
answers.append(a)
else:
raise NotImplementedError
print(f"dataset: {args.dataset}")
print(f"dataset_size: {len(answers)}")
args.dataset_size = len(answers)
return questions, answers
# return a customized dataloader of batches
# Not PyTorch dataloader, it supprts random index(slice) access
def create_dataloader(args)->list:
set_random_seed(args.random_seed)
questions, answers = load_data(args)
dataset = []
for idx in range(len(questions)):
dataset.append({"question":questions[idx], "answer":answers[idx], "question_idx":idx})
random.shuffle(dataset)
print(f"dataloader size: {len(dataset)}")
return dataset
# read the generated/prepared prompt json file
# return a string of prefix prompt before each question
def create_input_prompt(args, cot_flag:bool)->str:
x, z, y = [], [], []
with open(args.prompt_path, encoding="utf-8") as f:
json_data = json.load(f)
json_data = json_data["prompt"]
for line in json_data:
x.append(line["question"])
z.append(line["rationale"])
y.append(line["pred_ans"])
index_list = list(range(len(x)))
prompt_text = ""
for i in index_list:
if cot_flag:
if args.dataset == "strategyqa":
prompt_text += x[i] + " " + z[i] + " " + \
"So the answer is" + " " + y[i] + ".\n\n"
else:
prompt_text += x[i] + " " + z[i] + " " + \
args.direct_answer_trigger_for_fewshot + " " + y[i] + ".\n\n"
else:
prompt_text += x[i] + " " + args.direct_answer_trigger_for_fewshot + " " + y[i] + ".\n\n"
return prompt_text
def answer_extraction(args, responses):
pred_ans = ""
temp = ""
if args.model == 'gpt-3.5-turbo':
temp = responses['choices'][0]['message']['content']
else:
temp = responses['choices'][0].text
if args.dataset in ("gsm8k", "svamp", "asdiv", "addsub", "singleeq", "multiarith"):
temp = temp.replace(",", "")
temp = [s for s in re.findall(r'-?\d+\.?\d*', temp)]
elif args.dataset in ("aqua", "csqa"):
temp = re.findall(r'A|B|C|D|E', temp)
elif args.dataset in ("strategyqa", "coin_flip"):
temp = temp.lower()
temp = re.sub("\"|\'|\n|\.|\s|\:|\,"," ", temp)
temp = temp.split(" ")
temp = [i for i in temp if i in ("yes", "no")]
elif args.dataset in ("last_letters"):
temp = re.sub("\"|\'|\n|\.|\s","", temp)
temp = [temp]
elif args.dataset in ('time_zone'):
temp = temp.split('The answer is ')[-1].replace('.', '')
temp = [temp]
if len(temp) != 0:
answer = temp[-1]
# if there is . at the end of answer, remove it
# e.g. answer = 64.
if answer != "":
if answer[-1] == ".":
answer = answer[:-1]
# round the answer to nearest integer
if args.dataset in ("gsm8k", "svamp"):
try:
answer = str(round(float(answer)))
except:
answer = "" # no sol or sol doesn't have valid format
elif args.dataset in ("last_letters"):
try:
answer = answer[-args.concat_length:]
except:
answer = ""
pred_ans = answer
else:
pred_ans = ""
return pred_ans
def find_most_frequent(arr, n):
# method 1: return max(arr[:n], key=arr.count)
# method 2:
arr_acounts = Counter(arr[:n])
most_frequent_item, frequency = arr_acounts.most_common(1)[0]
return frequency, most_frequent_item