-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.py
226 lines (187 loc) · 8.37 KB
/
pipeline.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
from runtime import GenericRuntime
from modeling import select_model
from prompting import select_prompter
import re
from utils import *
from python_interpreter import *
def select_pipeline(pipeline, **kwargs):
pipeline_map = dict(
prove=ProvePipeline,
prove_math=ProveMathPipeline,
)
model_class = pipeline_map.get(pipeline)
if model_class is None:
raise ValueError(f"{pipeline}. Choose from {list(pipeline_map.keys())}")
return model_class(**kwargs)
class Pipeline(object):
def __init__(self, verbose) -> None:
self.verbose = verbose
def print_statement(self, statement):
if self.verbose:
print("#" * 50)
if isinstance(statement, list):
for i in statement:
print(i)
print("*" * 25)
else:
print(statement)
print("#" * 50)
def run(self, question):
raise NotImplementedError
class ProvePipeline(Pipeline):
def __init__(self, **kwargs):
super().__init__(kwargs.get("verbose"))
self.runtime = GenericRuntime()
self.num_cot = kwargs.get("num_cot")
self.cot_prompter = select_prompter(kwargs.get("cot_prompt"))
self.cot_model = select_model(
model_name=kwargs.get("cot_model"),
temperature=kwargs.get("cot_temperature"),
max_tokens=kwargs.get("cot_max_tokens"),
gpu=kwargs.get("cot_gpu"),
)
self.extract_prompter = select_prompter(kwargs.get("extract_prompt"))
self.extract_model = select_model(
model_name=kwargs.get("extract_model"),
temperature=kwargs.get("extract_temperature"),
max_tokens=kwargs.get("extract_max_tokens"),
gpu=kwargs.get("extract_gpu"),
)
self.program_model = select_model(
model_name=kwargs.get("program_model"),
temperature=kwargs.get("program_temperature"),
max_tokens=kwargs.get("program_max_tokens"),
gpu=kwargs.get("program_gpu"),
)
self.program_prompter_w_plan = select_prompter(kwargs.get("output_to_program"))
def extract_number(self, text):
text = text.replace(",", "")
pred = [s for s in re.findall(r"-?\d+\.?\d*", text)]
if pred:
pred_answer = float(pred[-1])
else:
pred_answer = None
return pred_answer
def run(self, question):
cot_prompt = self.cot_prompter.run(question)
cots = self.cot_model.run(question=cot_prompt, num_outputs=self.num_cot)
all_cots = {}
all_cot_answers = []
all_final_outputs = {}
all_final_answers = []
for cot in cots:
extract_prompt = self.extract_prompter.run(cot_prompt, cot)
output = self.extract_model.run(question=extract_prompt, num_outputs=1)[0]
self.print_statement(f"{extract_prompt} {output}")
answer = self.extract_number(output)
self.print_statement(answer)
if isinstance(answer, int) or isinstance(answer, float):
prompt = cot_prompt + cot
program_prompt = self.program_prompter_w_plan.run(prompt=prompt)
self.print_statement(program_prompt)
codes = self.program_model.run(question=program_prompt, num_outputs=1)
for code in codes:
all_cot_answers.append(answer)
if answer in all_cots:
all_cots[answer].append(f"{extract_prompt} {output}\n\n{code}")
else:
all_cots[answer] = [f"{extract_prompt} {output}\n\n{code}"]
try:
self.print_statement(code)
code, code_answer = self.runtime.run_code(code)
self.print_statement(code)
self.print_statement(code_answer)
if isinstance(code_answer, int) or isinstance(
code_answer, float
):
if abs(float(code_answer) - float(answer)) < 1e-3:
all_final_answers.append(answer)
if answer in all_final_outputs:
all_final_outputs[answer].append(
f"{extract_prompt} {output}\n\n{code}"
)
else:
all_final_outputs[answer] = [
f"{extract_prompt} {output}\n\n{code}"
]
except Exception as e:
self.print_statement(e)
if all_final_answers:
final_answer = max(all_final_answers, key=all_final_answers.count)
final_cots = all_final_outputs[final_answer]
else:
if all_cot_answers:
final_answer = max(all_cot_answers, key=all_cot_answers.count)
final_cots = all_cots[final_answer]
else:
final_answer = None
final_cots = []
return final_cots, final_answer
class ProveMathPipeline(Pipeline):
def __init__(self, **kwargs):
super().__init__(kwargs.get("verbose"))
self.num_cot = kwargs.get("num_cot")
self.cot_prompter = select_prompter(kwargs.get("cot_prompt"))
self.cot_model = select_model(
model_name=kwargs.get("cot_model"),
temperature=kwargs.get("cot_temperature"),
max_tokens=kwargs.get("cot_max_tokens"),
gpu=kwargs.get("cot_gpu"),
)
self.program_model = select_model(
model_name=kwargs.get("program_model"),
temperature=kwargs.get("program_temperature"),
max_tokens=kwargs.get("program_max_tokens"),
gpu=kwargs.get("program_gpu"),
)
self.program_prompter_w_plan = select_prompter(kwargs.get("output_to_program"))
self.executor = PythonREPL()
def run(self, question):
cot_prompt = self.cot_prompter.run(question)
cots = self.cot_model.run(question=cot_prompt, num_outputs=self.num_cot)
all_cots = {}
all_cot_answers = []
all_final_outputs = {}
all_final_answers = []
for cot in cots:
self.print_statement(cot)
answer = remove_boxed(last_boxed_only_string(cot))
self.print_statement(answer)
if answer != None:
prompt = cot_prompt + cot
program_prompt = self.program_prompter_w_plan.run(prompt=prompt)
self.print_statement(program_prompt)
codes = self.program_model.run(question=program_prompt, num_outputs=1)
for code in codes:
all_cot_answers.append(answer)
if answer in all_cots:
all_cots[answer].append(f"{cot}\n\n{code}")
else:
all_cots[answer] = [f"{cot}\n\n{code}"]
try:
self.print_statement(code)
code_answer = postprocess_completion(self.executor, code)
self.print_statement(code_answer)
code_answer = remove_boxed(last_boxed_only_string(code_answer))
self.print_statement(code_answer)
if is_equiv(code_answer, answer):
all_final_answers.append(answer)
if answer in all_final_outputs:
all_final_outputs[answer].append(f"{cot}\n\n{code}")
else:
all_final_outputs[answer] = [f"{cot}\n\n{code}"]
except Exception as e:
self.print_statement(e)
else:
continue
if all_final_answers:
final_answer = max(all_final_answers, key=all_final_answers.count)
final_cots = all_final_outputs[final_answer]
else:
if all_cot_answers:
final_answer = max(all_cot_answers, key=all_cot_answers.count)
final_cots = all_cots[final_answer]
else:
final_answer = None
final_cots = []
return final_cots, final_answer