-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
289 lines (250 loc) · 11.4 KB
/
template.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
from API import decoder
import random as random
import json
import pandas as pd
def generate_cot(args, original_prompt):
propmpt_generator = "Q: " + original_prompt
propmpt_generator = propmpt_generator + "\"\n" + "A: Let's think step by step."
cot = decoder(args, original_prompt)
return cot
def combine_query_cot(question_ls, plan_ls):
# sumamrize a general chain-of-thought by n examples
input = "Q: "
for i in range(len(question_ls)):
input = input + "\n\n"
input = input + "Question " + str(i + 1) + ": " + question_ls[i]
input = input + "\n" + "Chain of thought " + str(i + 1) + ": " + plan_ls[i]
# add trigger
trigger = "Given the questions and chain of thoughts above, we wish to have a general chain of thought for all questions instead of one chain of thoughts for each question. Can you generate one?"
input1 = input + "\n\n" + trigger
input1 = input1 + "\n" + "A: "
return input1
def Instruction_generate(args):
# retireved_num: number of demonstrtaions to generate instruction, default=5
# retirve queries from the same dataset
if args.dataset in ["aqua", "gsm8k", "multialrith", "object_tracking", "cycle_letters", "TOMI1", "TOMI2", "socialIQA", "coin_flip", "last_letters"]:
retrieved_queries, _ = data_reader(args)
else:
raise ValueError("Include your own dataset in `data_reader` function")
# Generate COT for each query
COTs = [generate_cot(args, i) for i in retrieved_queries]
# Combine query and COTs
instrutcion_template = combine_query_cot(retrieved_queries, COTs)
instruction = decoder(args, instrutcion_template)
return instruction
def Instruction_demo_generate(args, original_prompt):
### Get demo ###
planning = "Q: Generate " + str(args.generated_num) + " questions with the same structure as the given question: "
propmpt_generator = planning + original_prompt + "\n\n" + "A: "
examples = decoder(args, original_prompt).strip().strip("\"")
example_ls = examples.split("\n")
demo_cot = ""
demo_example = ""
for exa in example_ls:
prompt = "Q: " + exa + "\nAnswer: Let's think step by step."
answer = decoder(args, prompt)
demo_cot += "Question: " + exa + "\nChain of thought: Let's think step by step." + answer + "\n\n"
demo_example += exa + "\nAnswer: Let's think step by step." + answer + "\n\n"
### Get instruction ###
prompt_plan = demo_cot + "Given the questions and chain of thoughts above, generate a general chain of thought to help model to solve questions."
cot = decoder(args, prompt_plan)
new_prompt = "Q: Examples: " + demo_example + "Plan:" + cot + "\n\n" + "Question: " + original_prompt + "\n\n" + "Please solve the question step by step based on the provided examples and the plan."
new_prompt = new_prompt + "\n\nA: "
return new_prompt
def Auto_ICL(args, query):
if args.method == "Auto-ICL-retrieving":
insturction = Instruction_generate(args)
new_prompt = "Chain of thought: " + insturction + "\n" + "Question: " + query + "\n" + "Solve the question step by step based on the chain of thought."
new_prompt = "Q: " + new_prompt + "\nA: "
elif args.method == "Auto-ICL-generating":
new_prompt= Instruction_demo_generate(args, query)
return new_prompt
def shuffleDict(d):
keys = list(d.keys())
random.shuffle(keys)
[(key, d[key]) for key in keys]
random.shuffle(keys)
[(key, d[key]) for key in keys]
random.shuffle(keys)
keys = [(key, d[key]) for key in keys]
#keys = d(keys)
return dict(keys)
def data_reader(args):
questions = []
answers = []
decoder = json.JSONDecoder()
if args.dataset == "aqua":
with open(args.dataset_path) as f:
lines = f.readlines()
for line in lines:
json_res = decoder.raw_decode(line)[0]
choice = "(" + "(".join(json_res["options"])
choice = choice.replace("(", " (").replace(")", ") ")
choice = "Answer Choices:" + choice
questions.append(json_res["question"].strip() + " " + choice)
answers.append(json_res["correct"])
elif 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])
elif args.dataset == "commonsensqa":
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 in ("addsub", "multiarith", "singleeq"):
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 == "strategyqa":
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)
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 == "object_tracking":
# elif args.dataset == "object_tracking":
with open(args.dataset_path) as f:
json_data = json.load(f)
json_data = json_data["examples"]
if args.dataset == "bigbench_date":
choice_index = ['A', 'B', 'C', 'D', 'E', 'F']
elif args.dataset in ("object_tracking"):
choice_index = ['A', 'B', 'C']
else:
raise ValueError("dataset is not properly defined ...")
for line in json_data:
q = line["input"].strip()
if args.dataset == "bigbench_date":
choice = "Answer Choices:"
# Randomly shuffle the answer choice dictionary because the original answer is always A ...
choice_dic = shuffleDict(line["target_scores"])
elif args.dataset == "object_tracking":
choice = "\nWhich choice is true ? Answer Choices:"
choice_dic = line["target_scores"]
else:
raise ValueError("dataset is not properly defined ...")
for i, key_value in enumerate(choice_dic.items()):
key, value = key_value
choice += " ("
choice += choice_index[i]
choice += ") "
choice += key
if value == 1:
a = choice_index[i]
# a = key
q = q + " " + choice
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 == "cycle_letters":
with open(args.dataset_path) as f:
lines = f.readlines()
for line in lines:
json_res = decoder.raw_decode(line)[0]
q = "Please unscramble the letters into a word, and write that word: " + json_res["context"]
a = json_res["completion"]
questions.append(q)
answers.append(a)
elif args.dataset == "socialIQA":
with open(args.dataset_path) as f:
lines = f.readlines()
for line in lines:
json_res = decoder.raw_decode(line)[0]
# only include others
if json_res["promptQuestionFocusChar"] == "o":
q = json_res['context'] + " " + json_res['question']
q += " Answer Choices: (A) " + json_res['answerA'] + " (B) " + json_res[
'answerB'] + " (C) " + \
json_res['answerC']
questions.append(q)
answers.append(json_res["label_letter"])
elif args.dataset == "TOMI2":
data = pd.read_csv(args.dataset_path)
second_idx = [i for i in range(data["qOrder"].shape[0]) if data["qOrder"][i] == "second_order"]
for i in second_idx:
q = data["story"][i] + " " + data["question"][i]
choice = []
choice.append(data["answerMem"][i])
choice.append(data["answerReal"][i])
choice.append("Unknown")
random.shuffle(choice)
q = q + " Answer Choices: (A) " + choice[0] + " (B) " + choice[1] + " (C) " + choice[2]
questions.append(q)
if choice[0] == data["answer"][i]:
answers.append("A")
elif choice[1] == data["answer"][i]:
answers.append("B")
elif choice[2] == data["answer"][i]:
answers.append("C")
elif args.dataset == "TOMI1":
data = pd.read_csv(args.dataset_path)
second_idx = [i for i in range(data["qOrder"].shape[0]) if data["qOrder"][i] == "first_order"]
for i in second_idx:
q = data["story"][i] + " " + data["question"][i]
choice = []
choice.append(data["answerMem"][i])
choice.append(data["answerReal"][i])
choice.append("Unknown")
random.shuffle(choice)
q = q + " Answer Choices: (A) " + choice[0] + " (B) " + choice[1] + " (C) " + choice[2]
questions.append(q)
if choice[0] == data["answer"][i]:
answers.append("A")
elif choice[1] == data["answer"][i]:
answers.append("B")
elif choice[2] == data["answer"][i]:
answers.append("C")
else:
raise ValueError("dataset is not properly defined ...")
# q_len_list = []
# for q in questions:
# q_len_list.append(len(q.split(" ")))
# q_len_mean = mean(q_len_list)
#
# print("dataset : {}".format(args.dataset))
# print("data size : {}".format(len(answers)))
# print("average num of words for each sample : {}".format(q_len_mean))
questions = random.sample(questions, args.retrieved_num)
return questions, answers