-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompting.py
423 lines (342 loc) · 16.9 KB
/
prompting.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
from typing import Optional, List
from fire import Fire
from pydantic import BaseModel
from data_loading import ArgumentSample, ArgumentData
class Prompter(BaseModel):
def run(self, data_train: ArgumentSample, sample_test: ArgumentSample) -> str:
raise NotImplementedError
def get_answer(self, text: str) -> str:
raise NotImplementedError
class ConclugenBasePrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Based on the evidence presented, what is the most logical and justifiable stance to take on the issue at hand?\n"
for sample in data_train.samples:
prompt += f"Argument: {sample.src}\n"
prompt += f"Conclusion: {sample.tgt}\n\n"
prompt += f"Argument: {sample_test.src}\n"
prompt += "Conclusion: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n\n')[0]
return text.strip()
class ConclugenTopicPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Based on the evidence presented, what is the most logical and justifiable stance to take on the issue at hand?\n"
for sample in data_train.samples:
topic = sample.src.split('<|TOPIC|>')[1].split('<|ARGUMENT|>')[0]
argument = sample.src.split('<|ARGUMENT|>')[1].split('<|CONCLUSION|>')[0]
prompt += f"Topic: {topic}\n"
prompt += f"Argument: {argument}\n"
prompt += f"Conclusion: {sample.tgt}\n\n"
topic = sample_test.src.split('<|TOPIC|>')[1].split('<|ARGUMENT|>')[0]
argument = sample_test.src.split('<|ARGUMENT|>')[1].split('<|CONCLUSION|>')[0]
prompt += f"Topic: {topic}\n"
prompt += f"Argument: {argument}\n"
prompt += "Conclusion: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n\n')[0]
return text.strip()
class ConclugenAspectsPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Based on the evidence presented, what is the most logical and justifiable stance to take on the issue at hand?\n"
for sample in data_train.samples:
topic = sample.src.split('<|TOPIC|>')[1].split('<|ARGUMENT|>')[0]
argument = sample.src.split('<|ARGUMENT|>')[1].split('<|ASPECTS|>')[0]
aspects = sample.src.split('<|ASPECTS|>')[1].split('<|CONCLUSION|>')[0]
prompt += f"Topic: {topic}\n"
prompt += f"Argument: {argument}\n"
prompt += f"Aspects: {aspects}\n"
prompt += f"Conclusion: {sample.tgt}\n\n"
topic = sample_test.src.split('<|TOPIC|>')[1].split('<|ARGUMENT|>')[0]
argument = sample_test.src.split('<|ARGUMENT|>')[1].split('<|ASPECTS|>')[0]
aspects = sample_test.src.split('<|ASPECTS|>')[1].split('<|CONCLUSION|>')[0]
prompt += f"Topic: {topic}\n"
prompt += f"Argument: {argument}\n"
prompt += f"Aspects: {aspects}\n"
prompt += "Conclusion: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n\n')[0]
return text.strip()
class ConclugenTargetsPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Based on the evidence presented, what is the most logical and justifiable stance to take on the issue at hand?\n"
for sample in data_train.samples:
topic = sample.src.split('<|TOPIC|>')[1].split('<|ARGUMENT|>')[0]
argument = sample.src.split('<|ARGUMENT|>')[1].split('<|TARGETS|>')[0]
targets = sample.src.split('<|TARGETS|>')[1].split('<|CONCLUSION|>')[0]
prompt += f"Topic: {topic}\n"
prompt += f"Argument: {argument}\n"
prompt += f"Targets: {targets}\n"
prompt += f"Conclusion: {sample.tgt}\n\n"
topic = sample_test.src.split('<|TOPIC|>')[1].split('<|ARGUMENT|>')[0]
argument = sample_test.src.split('<|ARGUMENT|>')[1].split('<|TARGETS|>')[0]
targets = sample_test.src.split('<|TARGETS|>')[1].split('<|CONCLUSION|>')[0]
prompt += f"Topic: {topic}\n"
prompt += f"Argument: {argument}\n"
prompt += f"Targets: {targets}\n"
prompt += "Conclusion: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n\n')[0]
return text.strip()
class DebatesumAbstractivePrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "What is the main idea or argument presented in the document?\n"
for sample in data_train.samples:
prompt += f"Document: {sample.src}\n"
prompt += f"Abstractive Summary: {sample.tgt}\n\n"
prompt += f"Document: {sample_test.src}\n"
prompt += "Abstractive Summary: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n\n')[0]
return text.strip()
class DebatesumExtractivePrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify the main points and supporting evidence in the document that support the argument being made.\n"
for sample in data_train.samples:
prompt += f"Document: {sample.src}\n"
prompt += f"Extractive Summary: {sample.tgt}\n\n"
prompt += f"Document: {sample_test.src}\n"
prompt += "Extractive Summary: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n\n')[0]
return text.strip()
class CounterargumentPremisesPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify a premise for the claim and come up with a counter-argument that challenges the validity of that premise.\n"
for sample in data_train.samples:
claim, premises = sample.src.split('\t')
prompt += f"Claim: {claim}\n"
prompt += f"Premises: {premises}\n"
prompt += f"Counter Argument: {sample.tgt}\n\n"
claim, premises = sample_test.src.split('\t')
prompt += f"Claim: {claim}\n"
prompt += f"Premises: {premises}\n"
prompt += "Counter Argument: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n\n')[0]
return text.strip()
class CounterargumentWeakPremisesPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify a weak premise for the claim and formulate a counter-argument to challenge it.\n"
for sample in data_train.samples:
claim, premises = sample.src.split('\t')
prompt += f"Claim: {claim}\n"
prompt += f"Weak Premises: {premises}\n"
prompt += f"Counter Argument: {sample.tgt}\n\n"
claim, premises = sample_test.src.split('\t')
prompt += f"Claim: {claim}\n"
prompt += f"Weak Premises: {premises}\n"
prompt += "Counter Argument: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n\n')[0]
return text.strip()
class ClaimDetectionPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify whether the given sentence is a claim towards the given topic. Choose from 'claim' or 'non claim'.\n"
for sample in data_train.samples:
topic, sentence = sample.src.split('\t')
prompt += f"Topic: {topic}\n"
prompt += f"Sentence: {sentence}\n"
prompt += f"Label: {sample.tgt}\n\n"
topic, sentence = sample_test.src.split('\t')
prompt += f"Topic: {topic}\n"
prompt += f"Sentence: {sentence}\n"
prompt += "Label: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n')[0]
text = text.replace('-', ' ')
return text.strip().lower()
class ArgumentDetectionPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify whether the given sentence is an argument towards the given topic. Choose from 'argument' or 'non argument'.\n"
for sample in data_train.samples:
topic, sentence = sample.src.split('\t')
prompt += f"Topic: {topic}\n"
prompt += f"Sentence: {sentence}\n"
prompt += f"Label: {sample.tgt}\n\n"
topic, sentence = sample_test.src.split('\t')
prompt += f"Topic: {topic}\n"
prompt += f"Sentence: {sentence}\n"
prompt += "Label: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n')[0]
text = text.replace('-', ' ')
return text.strip().lower()
class EvidenceDetectionIAMPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify whether the given sentence is a piece of evidence towards the given claim. Choose from 'evidence' or 'non evidence'.\n"
for sample in data_train.samples:
claim, sentence = sample.src.split('\t')
prompt += f"Claim: {claim}\n"
prompt += f"Sentence: {sentence}\n"
prompt += f"Label: {sample.tgt}\n\n"
claim, sentence = sample_test.src.split('\t')
prompt += f"Claim: {claim}\n"
prompt += f"Sentence: {sentence}\n"
prompt += "Label: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n')[0]
text = text.replace('-', ' ')
return text.strip().lower()
class EvidenceDetectionIBMPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify whether the given sentence is a piece of evidence towards the given topic. Choose from 'evidence' or 'non evidence'.\n"
for sample in data_train.samples:
topic, sentence = sample.src.split('\t')
prompt += f"Topic: {topic}\n"
prompt += f"Sentence: {sentence}\n"
prompt += f"Label: {sample.tgt}\n\n"
topic, sentence = sample_test.src.split('\t')
prompt += f"Topic: {topic}\n"
prompt += f"Sentence: {sentence}\n"
prompt += "Label: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n')[0]
text = text.replace('-', ' ')
return text.strip().lower()
class StanceDetectionPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify the stance of the given sentence towards the given topic. Choose from 'support' or 'attack'.\n"
for sample in data_train.samples:
topic, sentence = sample.src.split('\t')
prompt += f"Topic: {topic}\n"
prompt += f"Sentence: {sentence}\n"
prompt += f"Label: {sample.tgt}\n\n"
topic, sentence = sample_test.src.split('\t')
prompt += f"Topic: {topic}\n"
prompt += f"Sentence: {sentence}\n"
prompt += "Label: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n')[0]
return text.strip().lower()
class StanceDetectionFeverPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify the stance of the given sentence. Choose from 'support', 'attack', or 'neutral'.\n"
for sample in data_train.samples:
prompt += f"Sentence: {sample.src}\n"
prompt += f"Label: {sample.tgt}\n\n"
prompt += f"Sentence: {sample_test.src}\n"
prompt += "Label: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n')[0]
return text.strip().lower()
class StanceDetectionMTSDPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify the stance of the given sentence towards each given target. Choose from 'support', 'attack', or 'neutral' for each target in the target pair. Format the output as a label pair: label1, label2.\n"
for sample in data_train.samples:
sentence, target1, target2 = sample.src.split('\t')
label1, label2 = sample.tgt.split('\t')
prompt += f"Sentence: {sentence}\n"
prompt += f"Target Pair: {target1}, {target2}\n"
prompt += f"Label Pair: {label1}, {label2}\n\n"
sentence, target1, target2 = sample_test.src.split('\t')
prompt += f"Sentence: {sentence}\n"
prompt += f"Target Pair: {target1}, {target2}\n"
prompt += "Label Pair: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n')[0]
text = text.replace(', ', '\t')
return text.strip().lower()
class EvidenceClassificationIBMPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify the evidence type of the given sentence. Choose from 'study', 'anecdotal' or 'expert'.\n"
for sample in data_train.samples:
prompt += f"Sentence: {sample.src}\n"
prompt += f"Evidence Type: {sample.tgt}\n\n"
prompt += f"Sentence: {sample_test.src}\n"
prompt += "Evidence Type: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n')[0]
return text.strip().lower()
class EvidenceClassificationAQEPrompter(Prompter):
def run(self, data_train: ArgumentData, sample_test: ArgumentSample) -> str:
prompt = "Identify the evidence type of the given sentence. Choose from 'research', 'case', 'expert', 'explanation' or 'others'.\n"
for sample in data_train.samples:
prompt += f"Sentence: {sample.src}\n"
prompt += f"Evidence Type: {sample.tgt}\n\n"
prompt += f"Sentence: {sample_test.src}\n"
prompt += "Evidence Type: "
return prompt
def get_answer(self, text: str) -> str:
text = text.split('\n')[0]
return text.strip().lower()
def select_prompter(task: str, data_name: str) -> Prompter:
if task == "conclugen":
if data_name == "base":
return ConclugenBasePrompter()
elif data_name == "aspects":
return ConclugenAspectsPrompter()
elif data_name == "targets":
return ConclugenTargetsPrompter()
elif data_name == "topic":
return ConclugenTopicPrompter()
else:
raise ValueError(f"Invalid data name: {data_name}")
elif task == "debatesum":
if data_name == "abstract":
return DebatesumAbstractivePrompter()
elif data_name == "extract":
return DebatesumExtractivePrompter()
else:
raise ValueError(f"Invalid data name: {data_name}")
elif task == "counter_arg_gen":
if data_name == "weak_premises":
return CounterargumentWeakPremisesPrompter()
elif data_name == "premises":
return CounterargumentPremisesPrompter()
else:
raise ValueError(f"Invalid data name: {data_name}")
elif task == "claim_detection":
if data_name == "ibm_argument":
return ArgumentDetectionPrompter()
elif data_name in ["iam_claims", "ibm_claims"]:
return ClaimDetectionPrompter()
else:
raise ValueError(f"Invalid data name: {data_name}")
elif task == "evidence_detection":
if data_name == "iam_evidence":
return EvidenceDetectionIAMPrompter()
elif data_name == "ibm_evidence":
return EvidenceDetectionIBMPrompter()
else:
raise ValueError(f"Invalid data name: {data_name}")
elif task == "stance_detection":
if data_name == "fever":
return StanceDetectionFeverPrompter()
elif data_name == "mtsd":
return StanceDetectionMTSDPrompter()
elif data_name in ["ibm_stance", "iam_stance"]:
return StanceDetectionPrompter()
else:
raise ValueError(f"Invalid data name: {data_name}")
elif task == "evidence_classification":
if data_name == "aqe_type":
return EvidenceClassificationAQEPrompter()
elif data_name == "ibm_type":
return EvidenceClassificationIBMPrompter()
else:
raise ValueError(f"Invalid data name: {data_name}")
else:
raise ValueError(f"Invalid task: {task}")
def test_prompt(task: str, data_name: str, num_train: int, seed: int):
data_train, data_test = ArgumentData.load(task, data_name, num_train, seed)
prompter = select_prompter(task, data_name)
prompt = prompter.run(data_train, data_test.samples[0])
print(prompt)
if __name__ == "__main__":
Fire()