-
Notifications
You must be signed in to change notification settings - Fork 4
/
demo.py
292 lines (241 loc) · 10.3 KB
/
demo.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
import json
import random
import re
from pathlib import Path
from typing import List, TextIO
import openai
from datasets import load_dataset
from fire import Fire
from pydantic import BaseModel
from tqdm import tqdm
def remove_substrings_with_double_angle_brackets(input_string):
# Define the pattern to match substrings within double angled brackets
pattern = r"<<[^>]+>>"
# Use the sub() function from the re module to replace matching substrings with an empty string
result = re.sub(pattern, "", input_string)
return result
class ReasonSample(BaseModel):
question: str
explanation: str = ""
answer: str = ""
wrong_explanation: str = ""
wrong_answer: str = ""
pred: str = ""
class ReasonData(BaseModel):
samples: List[ReasonSample]
@classmethod
def load(cls, path: str):
samples = []
with open(path) as f:
for line in f:
raw = json.loads(line)
samples.append(ReasonSample(**raw))
return cls(samples=samples)
@classmethod
def load_gsm8k_test(cls, path: str = "gsm8k", subset: str = "main", split="test"):
samples = []
for raw in load_dataset(path, subset, split=split):
explanation, answer = raw["answer"].split("####")
explanation = remove_substrings_with_double_angle_brackets(explanation)
samples.append(
ReasonSample(
question=raw["question"].strip(),
explanation=explanation.strip(),
answer=answer.strip(),
)
)
return cls(samples=samples)
@classmethod
def load_gsm8k_incoherent_objects(cls, split: str = "test", sample: bool = False):
if split == "train" and sample:
samples = [
ReasonSample(
question="There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done, there will be 21 trees. How many trees did the grove workers plant today?",
explanation="There are 15 trees originally. Then there were 21 trees after the Grove workers planted some more. So there must have been 21 - 15 = 6 trees that were planted.",
answer="6",
wrong_explanation="There are 21 - 15 = 6 trees originally. Then there were 15 trees after the Grove workers planted some more. So there must have been 21 trees that were planted.",
wrong_answer="21",
),
ReasonSample(
question="If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot??",
explanation="There are originally 3 cars. Then 2 more cars arrive. Now 3 + 2 = 5 cars are in the parking lot.",
answer="5",
wrong_explanation="There are originally 3 + 2 = 5 cars. Then 3 more cars arrive. Now 2 cars are in the parking lot",
wrong_answer="2",
),
ReasonSample(
question="Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?",
explanation="Originally, Leah had 32 chocolates and her sister had 42. So in total they had 32 + 42 = 74. After eating 35, they had 74 - 35 = 39 pieces left in total.",
answer="39",
wrong_explanation="Originally, Leah had 32 + 42 = 74 chocolates and her sister had 32. So in total they had 74 - 35 = 39. After eating 35, they had 42 pieces left in total",
wrong_answer="42",
),
ReasonSample(
question="Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?",
explanation="Jason had 20 lollipops originally. Then he had 12 after giving some to Denny. So he gave Denny 20 - 12 = 8 lollipops.",
answer="8",
wrong_explanation="Jason had 20 - 12 = 8 lollipops originally. Then he had 20 after giving some to Denny. So he gave Denny 12 lollipops",
wrong_answer="12",
),
]
return cls(samples=samples)
else:
return cls.load_gsm8k_test()
@classmethod
def load_from_name(cls, name: str, **kwargs):
if name == "gsm8k":
return cls.load_gsm8k_incoherent_objects(**kwargs)
else:
raise KeyError(name)
class Prompter(BaseModel):
def run(self, data_train: ReasonData, sample_test: ReasonSample) -> str:
prompt = ""
for sample in data_train.samples:
prompt += f"Question: {sample.question}\n"
prompt += f"Answer: {sample.answer}\n\n"
prompt += f"Question: {sample_test.question}\n"
prompt += "Answer: "
return prompt
@staticmethod
def get_answer(text: str) -> str:
parts = text.split("Answer: ")
if len(parts) >= 2:
return parts[1]
else:
return text
class ChainThoughtPrompter(Prompter):
def run(self, data_train: ReasonData, sample_test: ReasonSample) -> str:
prompt = ""
for sample in data_train.samples:
prompt += f"Question: {sample.question}\n"
prompt += f"Explanation: {sample.explanation}\n"
prompt += f"Answer: {sample.answer}\n\n"
prompt += f"Question: {sample_test.question}\n"
prompt += "Explanation: "
return prompt
def get_explanation(self, text: str) -> str:
assert self is not None
return text.split("\nAnswer: ")[0]
class ContrastiveChainThoughtPrompter(Prompter):
def run(self, data_train: ReasonData, sample_test: ReasonSample) -> str:
prompt = ""
for sample in data_train.samples:
prompt += f"Question: {sample.question}\n"
prompt += f"Explanation: {sample.explanation}\n"
prompt += f"Answer: {sample.answer}\n"
prompt += f"Wrong explanation: {sample.wrong_explanation}\n"
prompt += f"Wrong Answer: {sample.wrong_answer}\n\n"
prompt += f"Question: {sample_test.question}\n"
prompt += "Explanation: "
return prompt
def select_prompter(name: str):
if name == "standard":
return Prompter()
if name == "cot":
return ChainThoughtPrompter()
if name == "contrast_cot":
return ContrastiveChainThoughtPrompter()
else:
raise KeyError(name)
class OpenAIModel(BaseModel):
model_path: str
engine: str = ""
use_azure: bool = False
timeout: int = 60
temperature: float = 0.0
def load(self):
with open(self.model_path) as f:
info = json.load(f)
openai.api_key = info["key"]
self.engine = info["engine"]
def run(self, prompt: str) -> str:
self.load()
output = ""
error_message = "The response was filtered"
while not output:
try:
key = "engine" if self.use_azure else "model"
kwargs = {key: self.engine}
response = openai.ChatCompletion.create(
messages=[{"role": "user", "content": prompt}],
timeout=self.timeout,
request_timeout=self.timeout,
temperature=self.temperature, # this is the degree of randomness of the model's output
**kwargs,
)
if response.choices[0].finish_reason == "content_filter":
raise ValueError(error_message)
output = response.choices[0].message.content
except Exception as e:
print(e)
if error_message in str(e):
output = error_message
if not output:
print("OpenAIModel request failed, retrying.")
return output
class Scorer(BaseModel):
@staticmethod
def run(pred: str, gold: str) -> float:
pred = pred.replace("$", "")
gold = gold.replace("$", "")
return float(pred.startswith(gold))
def evaluate(
model: OpenAIModel,
data_train: ReasonData,
data_test: ReasonData,
prompter: Prompter,
file: TextIO,
) -> dict:
is_correct = []
score = 0
progress = tqdm(data_test.samples)
sample: ReasonSample
for sample in progress:
prompt = prompter.run(data_train, sample)
raw = model.run(prompt).strip()
sample.pred = prompter.get_answer(raw)
is_correct.append(Scorer.run(sample.pred, sample.answer))
score = sum(is_correct) / len(is_correct)
progress.set_postfix(score=score)
print(dict(prompt=prompt, raw=raw, gold=sample.answer, pred=sample.pred))
print(sample.json(), file=file)
return dict(score=score)
def main(
data_name: str,
prompt_name: str,
data_path: str = None,
num_shots: int = 4,
max_test_samples: int = 500,
seed: int = 0,
**kwargs,
):
model = OpenAIModel(**kwargs)
print(locals())
data_train = ReasonData.load_from_name(data_name, split="train", sample=True)
name = f"{prompt_name}_{num_shots=}_{max_test_samples=}"
data_test = ReasonData.load_from_name(data_name, split="test")
path_out = f"outputs/{data_name}/{kwargs.get('model_name')}/{name}.json"
if num_shots >= 0:
data_train.samples = data_train.samples[:num_shots]
if 0 < max_test_samples < len(data_test.samples):
random.seed(seed)
data_test.samples = random.sample(data_test.samples, k=max_test_samples)
print(dict(train=len(data_train.samples), test=len(data_test.samples)))
Path(path_out).parent.mkdir(exist_ok=True, parents=True)
with open(path_out, "w") as f:
prompter = select_prompter(prompt_name)
result = evaluate(model, data_train, data_test, prompter, file=f)
print(path_out)
print(result)
return result["score"]
def infer(prompt_name: str, question: str, **kwargs):
model = OpenAIModel(**kwargs)
data = ReasonData.load_from_name("gsm8k", split="train", sample=True)
sample = ReasonSample(question=question)
prompt = select_prompter(prompt_name).run(data, sample)
output = model.run(prompt)
info = dict(question=question, prompt=prompt, output=output)
print(json.dumps(info, indent=2))
# return info
if __name__ == "__main__":
Fire()