-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPromptFactory.py
282 lines (260 loc) · 13.9 KB
/
PromptFactory.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
# Simple Factory pattern that builds multiple types of prompts from a string and data object as this
# gets more complicated we can add more types of prompts and break it into a true Factory pattern
# Side note: Factories are often singletons as well
def get_question_topic(item):
# Yaya
topic = "Math"
if (item["construct_info"]["construct1"][1]):
topic = item["construct_info"]["construct1"][1]
if (item["construct_info"]["construct2"][1]):
topic = item["construct_info"]["construct2"][1]
return topic
def get_question_full_info(q_data):
return f"Question: {q_data['question']}\n" +\
f"Topic: {get_question_topic(q_data)}\n" +\
f"Concept: {q_data['construct_info']['construct3'][1]}\n" +\
f"Explanation: {q_data['correct_option']['explanation']}\n" +\
f"Answer: {q_data['correct_option']['option']}"
class PromptFactory():
STOP_TOKEN = "[stop]"
@classmethod
def producePrompt(cls, questionData, promptType, num_distractors, examples=None):
# Add to this ladder and create an internal method
if promptType == "distractor_only":
return cls._disOnlyPrompt(questionData, examples)
elif promptType == "distractor_and_answer":
return cls._disAnsPrompt(questionData, examples)
elif promptType == "distractor_and_answer_with_feedback":
return cls._disAnsFeedPrompt(questionData, examples)
elif promptType == "distractor_all_info":
return cls._disAllInfoPrompt(questionData, examples, num_distractors)
elif promptType == "distractor_all_info_with_feedback":
return cls._disAllInfoFeedPrompt(questionData, examples, num_distractors)
elif promptType == "distractor_all_info_with_errors":
return cls._disAllInfoErrorPrompt(questionData, examples, num_distractors)
elif promptType == "zero_shot":
return cls._zeroShotPrompt(questionData, examples)
elif promptType == "zero_shot_all_info_error":
return cls._zeroShotAllInfoErrorPrompt(questionData, num_distractors)
elif promptType == "rule_based_random":
return cls.rule_based_random_prompt(questionData, examples)
elif promptType == "rule_based_selection":
return cls.rule_based_selection_prompt(questionData, examples, num_distractors)
else:
raise ValueError(promptType + " is not an available prompt type")
@classmethod
def _disOnlyPrompt(cls, questionData, examples):
"""
=== EXAMPLE ===
Question: XXX\n
Distractor1: XXX\n
Distractor2: XXX\n
Distractor3: XXX\n
[STOP]
=== PROMPT ===
Question: XXX\n
"""
examples_text = ""
for _, row in examples.iterrows():
distractors_text_list = [f"Distractor{i+1}: {x['option']}\n" for i, x in enumerate(row["distractors"])]
distractor_text = ''.join(distractors_text_list)
examples_text += f"Question: {row['question']}\n" + distractor_text
examples_text += PromptFactory.STOP_TOKEN
prompt = examples_text + f"\nQuestion: {questionData['question']}\n"
return prompt
@classmethod
def _disAnsPrompt(cls, questionData, examples):
"""
=== EXAMPLE ===
Question: XXX\n
Answer: XXX\n
Distractor1: XXX\n
Distractor2: XXX\n
Distractor3: XXX\n
[STOP]
=== PROMPT ===
Question: XXX\n
Answer: XXX\n
"""
examples_text = ""
for _, row in examples.iterrows():
distractors_text_list = [f"Distractor{i+1}: {x['option']}\n" for i, x in enumerate(row["distractors"])]
distractor_text = ''.join(distractors_text_list)
examples_text += f"Question: {row['question']}\n" + f"Answer: {row['correct_option']['option']}\n" + distractor_text
examples_text += PromptFactory.STOP_TOKEN
prompt = examples_text + f"\nQuestion: {questionData['question']}\nAnswer: {questionData['correct_option']['option']}\n"
return prompt
@classmethod
def _disAnsFeedPrompt(cls, questionData, examples):
"""
=== EXAMPLE ===
Question: XXX\n
Explanation: XXX\n
Answer: XXX\n
Distractor1 Feedback: XXX\n
Distractor1: XXX\n
Distractor2 Feedback: XXX\n
Distractor2: XXX\n
Distractor3 Feedback: XXX\n
Distractor3: XXX\n
[STOP]
=== PROMPT ===
Question: XXX\n
Explanation: XXX\n
Answer: XXX\n
"""
examples_text = ""
for _, row in examples.iterrows():
distractors_text_list = [f"Distractor{i+1} Feedback: {x['explanation']}\nDistractor{i+1}: {x['option']}\n" for i, x in enumerate(row["distractors"])]
distractor_text = ''.join(distractors_text_list)
examples_text += f"Question: {row['question']}\n" + f"Explanation: {row['correct_option']['explanation']}\nAnswer: {row['correct_option']['option']}\n" + distractor_text
examples_text += PromptFactory.STOP_TOKEN
prompt = examples_text + f"\nQuestion: {questionData['question']}\nExplanation: {questionData['correct_option']['explanation']}\nAnswer: {questionData['correct_option']['option']}\n"
return prompt
@classmethod
def _disAllInfoPrompt(cls, questionData, examples, num_distractors):
instructions = "You will be given a math question along with the correct answer and explanation. " +\
"You will be also provided with several example questions that include incorrect distractor answers. " +\
f"Please generate {num_distractors} incorrect distractor answers for the current question to be used as " +\
"multiple-choice options in a multiple-choice exam." +\
"\n[Template]\n" +\
"Distractor1:\n" +\
"...\n" +\
f"Distractor{num_distractors}:\n"
examples_text = ""
for _, row in examples.iterrows():
distractors_text_list = [f"Distractor{i+1}: {x['option']}\n" for i, x in enumerate(row["distractors"])]
distractor_text = ''.join(distractors_text_list)
examples_text += get_question_full_info(row) + "\n" + distractor_text
examples_text += PromptFactory.STOP_TOKEN + "\n"
prompt = instructions + "\n" + examples_text + get_question_full_info(questionData) + "\n"
return prompt
@classmethod
def _disAllInfoFeedPrompt(cls, questionData, examples, num_distractors):
instructions = "You will be given a math question along with the correct answer and explanation. " +\
"You will be also provided with several example questions that include incorrect distractor answers. " +\
f"Please generate {num_distractors} incorrect distractor answers for the current question to be used as " +\
"multiple-choice options in a multiple-choice exam." +\
"\n[Template]\n" +\
"Distractor1 Feedback:\n" +\
"Distractor1:\n" +\
"...\n" +\
f"Distractor{num_distractors} Feedback:\n" +\
f"Distractor{num_distractors}:\n"
examples_text = ""
for _, row in examples.iterrows():
distractors_text_list = [f"Distractor{i+1} Feedback: {x['explanation']}\nDistractor{i+1}: {x['option']}\n" for i, x in enumerate(row["distractors"])]
distractor_text = ''.join(distractors_text_list)
examples_text += get_question_full_info(row) + "\n" + distractor_text
examples_text += PromptFactory.STOP_TOKEN + "\n"
prompt = instructions + "\n" + examples_text + get_question_full_info(questionData) + "\n"
return prompt
@classmethod
def _disAllInfoErrorPrompt(cls, questionData, examples, num_distractors):
instructions = "You will be given a math question along with the correct answer and explanation. " +\
"You will be also provided with several example questions that include incorrect distractor answers. " +\
f"Please generate {num_distractors} incorrect distractor answers for the current question to be used as " +\
"multiple-choice options in a multiple-choice exam." +\
"\n[Template]\n" +\
"Distractor1 Error:\n" +\
"Distractor1:\n" +\
"...\n" +\
f"Distractor{num_distractors} Error:\n" +\
f"Distractor{num_distractors}:\n"
examples_text = ""
for _, row in examples.iterrows():
distractors_text_list = [f"Distractor{i+1} Error: {x['misconception']}\nDistractor{i+1}: {x['option']}\n" for i, x in enumerate(row["distractors"])]
distractor_text = ''.join(distractors_text_list)
examples_text += get_question_full_info(row) + "\n" + distractor_text
examples_text += PromptFactory.STOP_TOKEN + "\n"
prompt = instructions + "\n" + examples_text + get_question_full_info(questionData) + "\n"
return prompt
@classmethod
def _zeroShotPrompt(cls, questionData, examples):
"""
=== EXAMPLE ===
<Instructions>
=== PROMPT ===
Question: XXX\n
Explanation: XXX\n
Answer: XXX\n
"""
instructions="You are given the following math question along with the correct answer and explanation. Please use the following template to give three alternative incorrect answers to be used as multiple-choice options in a multiple-choice exam. Prior to the incorrect answer, provide feedback to be displayed to the student as an explanation of why that is not the correct answer.\n \
[Template]\n \
Distractor1 Feedback: \
Distractor1: \
Distractor2 Feedback: \
Distractor2: \
Distractor3 Feedback: \
Distractor3:"
prompt = f"{instructions}\nQuestion: {questionData['question'].strip()}\nExplanation: {questionData['correct_option']['explanation'].strip()}\nAnswer: {questionData['correct_option']['option'].strip()}"
return prompt
@classmethod
def _zeroShotAllInfoErrorPrompt(cls, questionData, num_distractors):
instructions = "You are given the following math question along with the correct answer and explanation. " +\
f"Please use the following template to give {num_distractors} alternative incorrect answers to be used as " +\
"multiple-choice options in a multiple-choice exam. " +\
"Prior to the incorrect answer, provide the underlying error corresponding to that incorrect answer. " +\
"These errors should be conceptual in nature and should not refer to numbers, variables, or names in the question." +\
"\n[Template]\n" +\
"Distractor1 Error:\n" +\
"Distractor1:\n" +\
"...\n" +\
f"Distractor{num_distractors} Error:\n" +\
f"Distractor{num_distractors}:\n"
prompt = instructions + "\n" + get_question_full_info(questionData)
return prompt
@classmethod
def rule_based_random_prompt(cls, questionData, examples):
"""
=== EXAMPLE ===
<Instructions>
=== PROMPT ===
Question: XXX\n
Explanation: XXX\n
Answer: XXX\n
Error1: XXX\n
Error2: XXX\n
Error3: XXX\n
"""
instructions="You are given the following math question along with the correct answer, explanation, and three errors. Please use the following template to give three alternative incorrect answers to be used as multiple-choice options in a multiple-choice exam based on the given three errors. Prior to the incorrect answer, provide feedback to be displayed to the student as an explanation of why that is not the correct answer.\n \
[Template]\n \
Distractor1 Feedback: \
Distractor1: \
Distractor2 Feedback: \
Distractor2: \
Distractor3 Feedback: \
Distractor3:"
examples_text = ""
for idx, example in enumerate(examples):
examples_text += f"Error{idx+1}: {example}\n"
prompt = f"{instructions}\nQuestion: {questionData['question'].strip()}\nExplanation: {questionData['correct_option']['explanation'].strip()}\nAnswer: {questionData['correct_option']['option'].strip()}\n{examples_text}"
prompt = prompt[:-1]
return prompt
@classmethod
def rule_based_selection_prompt(cls, questionData, examples, num_distractors):
"""
=== EXAMPLE ===
<Instructions>
=== PROMPT ===
Question: XXX\n
Explanation: XXX\n
Answer: XXX\n
Error list: ...
"""
instructions = f"You are given the following math question along with the correct answer, explanation, and a list of errors. Please follow the template to first select {num_distractors} most likely errors for this question and use the selected errors to generate {num_distractors} alternative incorrect answers to be used as multiple-choice options in a multiple-choice exam. Prior to the incorrect answer, provide feedback to be displayed to the student as an explanation of why that is not the correct answer. If the list of errors is not given, generate {num_distractors} errors instead and do not contain any explanation in the {num_distractors} incorrect answer.\n" +\
"[Template]\n" +\
"Error 1:\n" +\
"...\n" +\
f"Error {num_distractors}\n" +\
"Distractor1 Feedback:\n" +\
"Distractor1:\n" +\
"...\n" +\
f"Distractor{num_distractors} Feedback:\n" +\
f"Distractor{num_distractors}:\n"
examples_text = "Error list:\n"
for idx, example in enumerate(examples):
examples_text += f"{example}\n"
prompt = f"{instructions}\nQuestion: {questionData['question'].strip()}\nExplanation: {questionData['correct_option']['explanation'].strip()}\nAnswer: {questionData['correct_option']['option'].strip()}\n{examples_text}"
prompt = prompt[:-1]
return prompt