-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_helper.py
177 lines (153 loc) · 6.9 KB
/
task_helper.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
from utils import *
def load_train_test_set(args):
train_data = read_json("data/{}_{}.json".format(args.task, "train"))
test_data = read_json("data/{}_{}.json".format(args.task, "test"))
train_data = train_data[args.slice_train:args.slice_train+args.num_train]
test_data = test_data[args.slice_dev:args.slice_dev+args.num_dev]
return train_data, test_data
class TaskHelper:
style_to_completion_length = {}
def __init__(self, style):
self.style = style
@classmethod
def from_taskname(cls, taskname, style):
if taskname == "gsm":
return GSMTaskHelper(style)
elif taskname == "ecqa":
return ECQATaskHelper(style)
elif taskname == "esnli":
return ESNLITaskHelper(style)
else:
raise RuntimeError("Not Implemented Yet")
def prompt_func(self, test_ex, shots):
raise RuntimeError("Not Implemented Yet")
def get_completion_length(self):
return self.style_to_completion_length[self.style]
class GSMTaskHelper(TaskHelper):
style_to_completion_length = {
"default": 160,
"standard": 32,
}
def prompt_func(self, test_ex, shots):
if self.style == "default":
return self.default_prompt(test_ex, shots)
elif self.style == "standard":
return self.standard_prompt(test_ex, shots)
else:
raise RuntimeError("Not Implemented Yet")
def default_prompt(self, test_ex, shots):
showcase_examples = [
"Q: {}\nA: {}\n".format(x["question"], x["completion"]) for x in shots
]
test_example = ["Q: {}\nA:".format(test_ex["question"])]
return "\n".join(showcase_examples + test_example)
def strip_explanation(self, comp):
return comp[comp.find("The answer is"):]
def standard_prompt(self, test_ex, shots):
showcase_examples = [
"Q: {}\nA: {}\n".format(x["question"], self.strip_explanation(x["completion"])) for x in shots
]
test_example = ["Q: {}\nA:".format(test_ex["question"])]
return "\n".join(showcase_examples + test_example)
class ECQATaskHelper(TaskHelper):
style_to_completion_length = {
"default": 96,
"standard": 24,
}
def prompt_func(self, test_ex, shots):
if self.style == "default":
return self.default_prompt(test_ex, shots)
elif self.style == "standard":
return self.standard_prompt(test_ex, shots)
else:
raise RuntimeError("Not Implemented Yet")
def strip_explanation(self, comp):
return comp[comp.find("the answer is"):]
def standard_prompt(self, test_ex, shots):
showcase_examples = [
"Q: {}\nA: {}\n".format(x["question"], self.strip_explanation(x["completion"])) for x in shots
]
test_example = ["Q: {}\nA:".format(test_ex["question"])]
return "\n".join(showcase_examples + test_example)
def default_prompt(self, test_ex, shots):
showcase_examples = [
"Q: {}\nA: {}\n".format(x["question"], x["completion"]) for x in shots
]
test_example = ["Q: {}\nA:".format(test_ex["question"])]
return "\n".join(showcase_examples + test_example)
class ESNLITaskHelper(TaskHelper):
style_to_completion_length = {
"qaneither": 96,
"qanotpossible": 96,
"psource": 96,
"psourcestandard": 16,
}
style_to_label_mapping = {
"qaneither": {
'entailment': 'true',
'contradiction': 'false',
'neutral': 'neither',
},
"qanotpossible": {
'entailment': 'true',
'contradiction': 'false',
'neutral': 'not possible to tell',
},
"psource": {
'entailment': 'yes',
'contradiction': 'no',
'neutral': 'not possible to tell',
},
"psourcestandard": {
'entailment': 'yes',
'contradiction': 'no',
'neutral': 'not possible to tell',
},
}
def __init__(self, style):
self.style = style
self.label_mapping = ESNLITaskHelper.style_to_label_mapping[style]
def prompt_func(self, test_ex, shots):
if self.style == "qaneither":
return self.qaneither_prompt(test_ex, shots)
elif self.style == "qanotpossible":
return self.qanotpossible_prompt(test_ex, shots)
elif self.style == "psource":
return self.psource_prompt(test_ex, shots)
elif self.style == "psourcestandard":
return self.psourcestandard_prompt(test_ex, shots)
else:
raise RuntimeError("Not Implemented Yet")
def psourcestandard_prompt(self, test_ex, shots):
showcase_examples = [
'Premise:\n"{}"\nBased on this premise, can we conclude the hypothesis "{}" is true?\nOPTIONS:\n- yes\n- no \n- not possible to tell\nA: The answer is {}.\n'.format(
s["premise"], s["hypothesis"], self.label_mapping[s["label"]]) for s in shots
]
input_example = 'Premise:\n"{}"\nBased on this premise, can we conclude the hypothesis "{}" is true?\nOPTIONS:\n- yes\n- no \n- not possible to tell\nA:'.format(test_ex["premise"], test_ex["hypothesis"])
prompt = "\n".join(showcase_examples + [input_example])
return prompt
def qaneither_prompt(self, test_ex, shots):
showcase_examples = [
"{}\nQ: {} True, False, or Neither?\nA: {} The answer is {}.\n".format(s["premise"], s["hypothesis"],
s["explanations"][0], self.label_mapping[s["label"]]) for s in shots
]
input_example = "{}\nQ: {} True, False, or Neither?\nA:".format(test_ex["premise"], test_ex["hypothesis"])
prompt = "\n".join(showcase_examples + [input_example])
return prompt
def qanotpossible_prompt(self, test_ex, shots):
showcase_examples = [
"{}\nQ: {} True or False?\nA: {} The answer is {}.\n".format(s["premise"], s["hypothesis"],
s["explanations"][0], self.label_mapping[s["label"]]) for s in shots
]
input_example = "{}\nQ: {} True, or False?\nA:".format(test_ex["premise"], test_ex["hypothesis"])
prompt = "\n".join(showcase_examples + [input_example])
return prompt
def psource_prompt(self, test_ex, shots):
showcase_examples = [
'Premise:\n"{}"\nBased on this premise, can we conclude the hypothesis "{}" is true?\nOPTIONS:\n- yes\n- no \n- not possible to tell\nA: {} The answer is {}.\n'.format(
s["premise"], s["hypothesis"],
s["explanations"][0], self.label_mapping[s["label"]]) for s in shots
]
input_example = 'Premise:\n"{}"\nBased on this premise, can we conclude the hypothesis "{}" is true?\nOPTIONS:\n- yes\n- no \n- not possible to tell\nA:'.format(test_ex["premise"], test_ex["hypothesis"])
prompt = "\n".join(showcase_examples + [input_example])
return prompt