-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromptor.py
238 lines (184 loc) · 9.28 KB
/
promptor.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
from IPython import embed
import re
def build_promptor(prompt_type):
if prompt_type == "Rewrite":
return RewritePrompter()
elif prompt_type == "Rewrite-And-Response":
return RewriteAndResponsePromptor()
elif prompt_type == "CoT-Rewrite":
return CoTRewritePromptor()
elif prompt_type == "CoT-Rewrite-And-Response":
return CoTRewriteAndResponsePromptor()
elif prompt_type == "Rewrite-Then-Response":
return RewriteThenResponsePromptor()
else:
raise NotImplementedError
class RewritePrompter:
def __init__(self) -> None:
self.instruction = "Reformulate the current question into a de-contextualized rewrite under the multi-turn information-seeking dialog context."
self.stop_tokens = ['\n']
def build_demo_prompt(self, dialogs: list):
demo_prompt = []
demo_prompt.append(self.instruction)
for dialog in dialogs:
dialog_prompt = []
turns = dialog['turns']
for turn in turns:
turn_prompt = self.build_turn_prompt(turn, is_demo=True)
dialog_prompt.append(turn_prompt)
demo_prompt.append("\n".join(dialog_prompt))
return "\n\n".join(demo_prompt)
def build_turn_prompt(self, turn, is_demo):
turn_prompt = []
turn_prompt.append("Turn: {}".format(turn['turn_id']))
turn_prompt.append("Question: {}".format(turn['question']))
if is_demo:
turn_prompt.append("Rewrite: {}".format(turn['manual_rewrite']))
turn_prompt.append("Response: {}".format(turn['response']))
else: # for test
turn_prompt.append("Rewrite: ")
return "\n".join(turn_prompt)
def build_this_turn_prompt_for_prediction(self, pre_prompt, this_turn, last_predicted_rewrite, last_response):
pre_prompt_components = pre_prompt.split("\n\n")
# update the last turn of the last dialog's info in the prompt
if last_predicted_rewrite is not None:
last_dialog_prompt = pre_prompt_components[-1]
pre_prompt_components.pop()
last_dialog_prompt_turns = last_dialog_prompt.split('\n')
last_dialog_prompt_turns[-1] = "Rewrite: {}".format(last_predicted_rewrite)
if last_response == "":
last_response = "Unavailable." # for cast19
last_dialog_prompt_turns.append("Response: {}".format(last_response))
else:
last_dialog_prompt_turns = []
this_turn_prompt = self.build_turn_prompt(this_turn, is_demo=False)
last_dialog_prompt_turns.append(this_turn_prompt)
pre_prompt_components.append("\n".join(last_dialog_prompt_turns))
return "\n\n".join(pre_prompt_components)
def parse_returned_text(self, text):
return text.strip()
class RewriteAndResponsePromptor(RewritePrompter):
def __init__(self) -> None:
super().__init__()
self.instruction = "Reformulate the current question into a de-contextualized rewrite under the multi-turn information-seeking dialog context and generate a correct response to the current question."
self.stop_tokens = ["\n\n", "\nTurn"]
def parse_returned_text(self, text):
response, rewrite = None, None
response_pattern = r"Response:\s*(.*)"
match = re.search(response_pattern, text)
if match:
response = match.group(1)
else:
raise ValueError("Response not found in returned text: {}".format(text))
rewrite_pattern = r'^(.*?)\nResponse:'
match = re.search(rewrite_pattern, text, re.DOTALL)
if match:
rewrite = match.group(1)
else:
raise ValueError("Rewrite not found in returned text: {}".format(text))
return rewrite, response
class RewriteThenResponsePromptor(RewritePrompter):
def __init__(self) -> None:
super().__init__()
self.instruction = "Generate a correct response to the current question rewrite under the multi-turn information-seeking dialog context."
def build_turn_prompt(self, turn, is_demo, predicted_rewrite=None):
turn_prompt = []
turn_prompt.append("Turn: {}".format(turn['turn_id']))
turn_prompt.append("Question: {}".format(turn['question']))
if is_demo:
turn_prompt.append("Rewrite: {}".format(turn['manual_rewrite']))
turn_prompt.append("Response: {}".format(turn['response']))
else: # for test
turn_prompt.append("Rewrite: {}".format(predicted_rewrite))
turn_prompt.append("Response: ")
return "\n".join(turn_prompt)
def build_this_turn_prompt_for_prediction(self, pre_prompt, this_turn, this_turn_predicted_rewrite, last_response):
pre_prompt_components = pre_prompt.split("\n\n")
# update the last turn of the last dialog's info in the prompt
if last_response is not None:
last_dialog_prompt = pre_prompt_components[-1]
pre_prompt_components.pop()
last_dialog_prompt_turns = last_dialog_prompt.split('\n')
if last_response == "":
last_response = "Unavailable." # for cast19
last_dialog_prompt_turns[-1] = "Response: {}".format(last_response)
else:
last_dialog_prompt_turns = []
this_turn_prompt = self.build_turn_prompt(this_turn, is_demo=False, predicted_rewrite=this_turn_predicted_rewrite)
last_dialog_prompt_turns.append(this_turn_prompt)
pre_prompt_components.append("\n".join(last_dialog_prompt_turns))
return "\n\n".join(pre_prompt_components)
def parse_returned_text(self, text):
return text.strip()
class CoTRewritePromptor(RewritePrompter):
def __init__(self) -> None:
super().__init__()
def build_turn_prompt(self, turn, is_demo):
turn_prompt = []
turn_prompt.append("Turn: {}".format(turn['turn_id']))
turn_prompt.append("Question: {}".format(turn['question']))
if is_demo:
output = turn['cot'] + " So the question should be rewritten as: {}".format(turn['manual_rewrite'])
turn_prompt.append("Rewrite: {}".format(output))
turn_prompt.append("Response: {}".format(turn['response']))
else: # for test
turn_prompt.append("Rewrite: ")
return "\n".join(turn_prompt)
def parse_returned_text(self, text):
cot, rewrite = None, None
cot_pattern = r'^(.*?) So the question should be rewritten as:'
match = re.search(cot_pattern, text, re.DOTALL)
if match:
cot = match.group(1)
else:
raise ValueError("Chain-of-Thought not found in returned text: {}".format(text))
rewrite_pattern = r"So the question should be rewritten as:\s*(.*)"
match = re.search(rewrite_pattern, text, re.DOTALL)
if match:
rewrite = match.group(1)
else:
raise ValueError("Rewrite not found in returned text: {}".format(text))
rewrite_part_text = text
return rewrite, cot, rewrite_part_text
class CoTRewriteAndResponsePromptor(RewriteAndResponsePromptor):
def __init__(self) -> None:
super().__init__()
def build_turn_prompt(self, turn, is_demo):
turn_prompt = []
turn_prompt.append("Turn: {}".format(turn['turn_id']))
turn_prompt.append("Question: {}".format(turn['question']))
if is_demo:
output = turn['cot'] + " So the question should be rewritten as: {}".format(turn['manual_rewrite'])
turn_prompt.append("Rewrite: {}".format(output))
turn_prompt.append("Response: {}".format(turn['response']))
else: # for test
turn_prompt.append("Rewrite: ")
return "\n".join(turn_prompt)
def parse_returned_text(self, text):
# text: "cot, so the question should be rewritten as: rewrite\nResponse: response\n..."
cot, rewrite, response = None, None, None
cot_pattern = r'^(.*?) So the question should be rewritten as:'
match = re.search(cot_pattern, text, re.DOTALL)
if match:
cot = match.group(1)
else:
raise ValueError("Chain-of-Thought not found in returned text: {}".format(text))
rewrite_pattern = r'So the question should be rewritten as: (.*?)\nResponse:'
match = re.search(rewrite_pattern, text, re.DOTALL)
if match:
rewrite = match.group(1)
else:
raise ValueError("Rewrite not found in returned text: {}".format(text))
response_pattern = r"Response:\s*(.*)"
match = re.search(response_pattern, text)
if match:
response = match.group(1)
else:
raise ValueError("Response not found in returned text: {}".format(text))
rewrite_part_text_pattern = r'^(.*?)\nResponse:'
match = re.search(rewrite_part_text_pattern, text, re.DOTALL)
if match:
rewrite_part_text = match.group(1)
else:
raise ValueError("rewrite_part_text not found in returned text: {}".format(text))
return rewrite, response, cot, rewrite_part_text