-
Notifications
You must be signed in to change notification settings - Fork 3
/
prompt_templates.py
744 lines (669 loc) · 36.7 KB
/
prompt_templates.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# This file contains the prompt templates used in chat completion.
import re
# parent class
class ConversationPrompt(object):
def __init__(self):
self.system = (
"You are a creative assistant. " +
"Your responsibility is to understand the user's instructions and help brainstorm novel ideas."
)
# filter based on keywords that are not suitable for language models.
self.blacklist = [
"image",
"images",
"graph",
"graphs",
"picture",
"pictures",
"file",
"files",
"map",
"maps",
"draw",
"plot",
"go to",
"video",
"audio",
"poster",
"music",
"flowchart",
"diagram",
# "media",
# "media post",
]
def extract_content(self, content:str):
# Remove the "None" at the end
# content = re.sub(r"(None\.|None|none\.|none)", "", content)
content = re.sub(r"(None\.?|none\.?)+$", "", content)
items = re.split(self.seperator, content)
# Remove any empty elements from the list
items = [item.strip() for item in items if item.strip() != ""]
# Remove any elements that has words in the blacklist
items = [item for item in items if not any([word in item for word in self.blacklist])]
return items
# used for generating textual facets (attributes)
class ConversationPromptAttribute(ConversationPrompt):
def __init__(self):
super().__init__()
self.system = (
"You are an expert in Natural Language Processing (NLP)."
)
self.seperator = r"Attribute \d+:" ## Attribute 1: Attribute 2: .. Attribute n:
self.requirement_prompt = (
"1. Please brainstorm as many textual attributes as possible. If you think there are no more suitable attributes, end up with 'None'.\n" +
"2. Be creative. Any interesting perspectives are welcome!\n" +
"3. Each attribute must concisely summarize one specific aspect of this input.\n" +
"4. Feel free to ignore the tedious and specific content. Just focus on some general textual attributes!\n" +
"5. Please prioritize your most confident predictions.\n"
)
self.query_prompt = (
"You are an expert in Natural Language Processing (NLP). Carefully read the given input (consists of one or several pieces of text), and find out the textual attributes of this input.\n\n" +
"### Input:\n" +
"{input}\n\n" +
"### Requirements:\n" +
self.requirement_prompt + "\n" +
"Attribute 1:\n"
)
# used for generating textual attributes (generally more attributes than the first template)
class ConversationPromptAttribute_2(ConversationPrompt):
def __init__(self):
super().__init__()
self.seperator = r"Attribute \d+:" ## Attribute 1: Attribute 2: .. Attribute n:
self.requirement_prompt = (
"1. Please brainstorm as many textual attributes as possible. If you think there are no more suitable attributes, end up with 'None'.\n" +
"2. Be creative. Any interesting perspectives are welcome!\n" +
"3. Each attribute must concisely summarize one specific aspect of this input, such as language, length, intent, etc.\n" +
"4. Feel free to ignore the tedious and specific content. Just focus on some general textual attributes!\n" +
"5. Please prioritize your most confident predictions.\n"
)
self.query_prompt = (
"### Input:\n" +
"{input}\n\n" +
"### Instruction:\n" +
"Given the above input, what kind of textual attributes it has?\n\n" +
"### Requirements:\n" +
self.requirement_prompt + "\n" +
"Attribute 1:\n"
)
# used for generating task instructions
class ConversationPromptTask(ConversationPrompt):
''' following the hint to brainstorm novel task instructions '''
def __init__(self):
super().__init__()
self.seperator = r"Task \d+:" ## Task 1: Task 2: .. Task n:
self.requirement_prompt = (
"1. Brainstorm as many textual tasks as possible. If you think there are no more suitable tasks, end up with 'None'.\n" +
"2. You'll need to look at the hint as a direction to guide your thinking. Your responses should strictly be based on the hint!\n" +
"3. Each task must be indivisible (one task, one intention).\n" +
"4. Avoid tasks requiring additional context, i.e., tasks must be easily solved/answered using only the input.\n" +
"5. Please prioritize your most confident predictions.\n"
"6. You should explicitly mention any necessary output constraints, such as answer options or length limitations.\n" +
"7. But do not disclose the final answer!\n"
)
self.query_prompt = (
"### Input:\n" +
"{input}\n\n" +
"### Instruction:\n" +
"What kind of textual tasks can you develop that can be applied to the input?\n\n" +
"### Hint:\n" +
"{hint}\n\n" +
"### Requirements:\n" +
self.requirement_prompt + "\n" +
"Task 1:\n"
)
class ConversationPromptTask_2(ConversationPrompt):
''' shifting the attribute to generate task instructions '''
def __init__(self):
super().__init__()
self.seperator = r"Task \d+:" ## Task 1: Task 2: .. Task n:
self.requirement_prompt = (
"1. Brainstorm as many textual tasks as possible. If you think there are no more suitable tasks, end up with 'None'.\n" +
"2. Your tasks can be applied to the input to shift its attribute.\n" +
"3. Each task must be indivisible (one task, one intention).\n" +
"4. Avoid tasks requiring additional context, i.e., tasks must be easily solved/answered using only the input.\n" +
"5. Please prioritize your most confident predictions.\n"
"6. You should explicitly mention any necessary output constraints, such as answer options or length limitations.\n" +
"7. But do not disclose the final answer!\n"
)
self.query_prompt = (
"### Input:\n" +
"{input}\n\n" +
"### Attribute:\n" +
"{hint}\n\n" +
"### Instruction:\n" +
"Based on the above information, what kind of textual tasks can you develop that can shift the input's attribute?\n\n" +
"### Requirements:\n" +
self.requirement_prompt + "\n" +
"Task 1:\n"
)
class ConversationPromptTask_5(ConversationPrompt):
''' following the hint to brainstorm novel task instructions, also add 3-shot in-domain demonstrations '''
def __init__(self):
super().__init__()
self.seperator = r"Task \d+:" ## Task 1: Task 2: .. Task n:
self.requirement_prompt = (
"1. Brainstorm as many textual tasks as possible. If you think there are no more suitable tasks, end up with 'None'.\n" +
"2. You'll need to look at the hint as a direction to guide your thinking. Your responses should strictly be based on this hint!\n" +
"3. Each task must be indivisible (one task, one intention).\n" +
"4. Please prioritize your most confident predictions.\n" +
"5. Avoid tasks requiring additional context, i.e., tasks must be easily solved/answered using only the input.\n" +
"6. Your tasks should sufficiently describe how this input is expected to be mapped to an output text, i.e., elaborating the tasks in detail.\n" +
"7. But do not disclose the final answer!\n"
)
self.demonstrations = (
"1. {example_1}\n" +
"2. {example_2}\n" +
"3. {example_3}\n\n" +
"### In a word, you should first describe what the input is, and what textual attribute it has, then elaborate on the task intent, and finally exactly describe what kind of output you expect and mention any necessary output constraints (e.g., formats, options).\n"
)
self.query_prompt = (
"### Input:\n" +
"{input}\n\n" +
"### Hint:\n" +
"{hint}\n\n" +
"### Instruction:\n" +
"Based on the above hint, what kind of textual tasks can you develop that can be applied to the input?\n\n" +
"### Format Examples (Imitate their formats, ignore the contents):\n" +
self.demonstrations + "\n" +
"### Requirements:\n" +
self.requirement_prompt + "\n" +
"Task 1:\n"
)
class ConversationPromptTask_6(ConversationPrompt):
''' shifting the attribute to generate task instructions, also add 3-shot in-domain demonstrations '''
def __init__(self):
super().__init__()
self.seperator = r"Task \d+:" ## Task 1: Task 2: .. Task n:
self.requirement_prompt = (
"1. Brainstorm as many textual tasks as possible. If you think there are no more suitable tasks, end up with 'None'.\n" +
"2. You'll need to look at the attribute as a direction to guide your thinking. \n" +
"3. Each task must be indivisible (one task, one intention).\n" +
"4. Please prioritize your most confident predictions.\n" +
"5. Avoid tasks requiring additional context, i.e., tasks must be easily solved/answered using only the input.\n" +
"6. Your tasks should sufficiently describe how this input is expected to be mapped to an output text, i.e., elaborating the tasks in detail.\n" +
"7. But do not disclose the final answer!\n"
)
self.demonstrations = (
"1. {example_1}\n" +
"2. {example_2}\n" +
"3. {example_3}\n\n" +
"### In a word, you should first describe what the input is, and what textual attribute it has, then elaborate on the task intent, and finally exactly describe what kind of output you expect and mention any necessary output constraints (e.g., formats, options).\n"
)
self.query_prompt = (
"### Input:\n" +
"{input}\n\n" +
"### Attribute:\n" +
"{hint}\n\n" +
"### Instruction:\n" +
"Based on the above information, what kind of textual tasks can you develop that can shift the input's attribute?\n\n" +
"### Format Examples (Imitate their formats, ignore the contents):\n" +
self.demonstrations + "\n" +
"### Requirements:\n" +
self.requirement_prompt + "\n" +
"Task 1:\n"
)
class ConversationPromptTask_3(ConversationPrompt):
''' following the hint to brainstorm novel task instructions, also add 3-shot in-domain demonstrations '''
def __init__(self):
super().__init__()
self.system = (
"You are a creative prompt engineer."
)
self.seperator = r"Task \d+:" ## Task 1: Task 2: .. Task n:
self.requirement_prompt = (
"1. Brainstorm as many textual tasks as possible. If you think there are no more suitable tasks, end up with 'None'.\n" +
"2. Please prioritize your most confident predictions.\n" +
"3. Avoid generating tasks that require accessing external tools or APIs.\n" +
"4. Ensure your textual tasks are detailed and sufficient, e.g., including any necessary contexts beyond the given input.\n" +
"5. Do not disclose the output (answer) of your tasks.\n"
)
self.demonstrations = (
"1. {example_1}\n" +
"2. {example_2}\n" +
"3. {example_3}\n\n" +
"### In a word, you should first describe what the input is, and what textual attribute it has, then elaborate on the task intent, and finally exactly describe what kind of output you expect and mention any necessary output constraints (e.g., formats, options).\n"
)
self.query_prompt = (
"You are a creative prompt engineer responsible for designing textual tasks to test the readers' problem-solving ability.\n" +
"A 'textual task' can be simply understood as a 'mapping function', which defines how to process an 'input' (source text) into a certain 'output' (target text).\n\n" +
"### Input (waits to be processed):\n" +
"{input}\n\n" +
"### Requirements:\n" +
self.requirement_prompt + "\n" +
"### Hint:\n" +
"{hint}\n\n" +
"What kind of textual tasks can you develop to process the input?\n\n" +
"Task 1: "
)
class ConversationPromptTask_4(ConversationPrompt):
''' shifting the attribute to generate task instructions, also add 3-shot in-domain demonstrations '''
def __init__(self):
super().__init__()
self.system = (
"You are a creative prompt engineer."
)
self.seperator = r"Task \d+:" ## Task 1: Task 2: .. Task n:
self.requirement_prompt = (
"1. Brainstorm as many textual tasks as possible. If you think there are no more suitable tasks, end up with 'None'.\n" +
"2. Please prioritize your most confident predictions.\n" +
"3. Avoid generating tasks that require accessing external tools or APIs.\n" +
"4. Ensure your textual tasks are detailed and sufficient, e.g., including any necessary contexts beyond the given input.\n" +
"5. Do not disclose the output (answer) of your tasks.\n"
)
self.demonstrations = (
"1. {example_1}\n" +
"2. {example_2}\n" +
"3. {example_3}\n\n" +
"### In a word, you should first describe what the input is, and what textual attribute it has, then elaborate on the task intent, and finally exactly describe what kind of output you expect and mention any necessary output constraints (e.g., formats, options).\n"
)
self.query_prompt = (
"You are a creative prompt engineer responsible for designing textual tasks to test the readers' knowledge.\n" +
"A 'textual task' can be simply understood as a 'mapping function', which defines how to process an 'input' (source text) into a certain 'output' (target text).\n\n" +
"### Input (waits to be processed):\n" +
"{input}\n\n" +
"### Requirements:\n" +
self.requirement_prompt + "\n" +
"### Attribute:\n" +
"{hint}\n\n" +
"Your tasks should also try to shift the above attribute of the input. So, what kind of textual tasks can you develop?\n\n" +
"Task 1: "
)
# used for generating classification task instructions
class ConversationPromptTask_CLS(ConversationPrompt):
''' use 3-shot demonstrations '''
def __init__(self):
super().__init__()
self.system = (
"You are an expert in Natural Language Processing (NLP)."
)
self.seperator = r"Task \d+:" ## Task 1: Task 2: .. Task n:
self.requirement_prompt = ()
self.demonstrations = (
"1. {example_1}\n" +
"2. {example_2}\n" +
"3. {example_3}\n"
)
self.query_prompt = (
"You are an expert in Natural Language Processing (NLP). I want you to design a high-quality classification task based on the given information.\n" +
"A good classification task should clearly include any necessary information, e.g., all the possible answer options (in random order) and how to decide the category of a given input. If the input has already included the answer options, you don't have to repeat them.\n\n" +
"### Input:\n" +
"{input}\n\n" +
"### Attribute:\n" +
"{hint}\n\n" +
"Using the attribute as a clue, what classification task can you develop for this input?\n\n" +
"### Example Tasks (your task should imitate their formats):\n" +
self.demonstrations + "\n" +
"### Your task (do not disclose the answer):\n"
)
def extract_content(self, content:str):
content = content.strip()
return [content] # return a list to ensure the consistency with other templates
# used for generating classification task instructions
class ConversationPromptTask_CLS_2(ConversationPrompt):
''' use 3-shot demonstrations (slightly tune the prompt) '''
def __init__(self):
super().__init__()
self.system = (
"You are an expert in Natural Language Processing (NLP)."
)
self.seperator = r"Task \d+:" ## Task 1: Task 2: .. Task n:
self.requirement_prompt = ()
self.demonstrations = (
"1. {example_1}\n" +
"2. {example_2}\n" +
"3. {example_3}\n"
)
self.query_prompt = (
"You are an expert in Natural Language Processing (NLP). I want you to design a high-quality classification task based on the given information.\n" +
"A good classification task should clearly include any necessary information, e.g., all the possible answer options and how to decide the category of a given input. If the input has already included the answer options, you don't have to repeat them.\n\n" +
"### Input:\n" +
"{input}\n\n" +
"### Attribute:\n" +
"{hint}\n\n" +
"Using the attribute as a clue (just use it to guide your thinking but do not mention it in your task), what classification task can you develop for this input?\n\n" +
"### Example Tasks (your task should imitate their formats):\n" +
self.demonstrations + "\n" +
"### Your task (do not disclose the answer):\n"
)
def extract_content(self, content:str):
content = content.strip()
return [content] # return a list to ensure the consistency with other templates
# used for annotating the output
class ConversationPromptAnswer(ConversationPrompt):
''' generate the answer by following the instruction '''
def __init__(self):
super().__init__()
self.system = (
"You are a helpful assistant. " +
"Your responsibility is to follow the user's instruction and generate the output."
)
# don't need to filter the keywords when annotating the output
self.blacklist = [
]
self.requirement_prompt = (
"1. Conclude your final output without reasoning.\n" +
"2. If you think it is impossible to answer the instruction by giving the existing information (e.g., require additional context, not a textual task), simply generate 'None'.\n"
)
self.query_prompt = (
# "### Input:\n" +
# "{input}\n\n" +
# "### Instruction:\n" +
# "{instruction}\n\n" +
# "### Requirements:\n" +
# self.requirement_prompt + "\n" +
# "### Output:\n"
"Given a task instruction and an input, please generate the output (answer) according to the requirements mentioned in the instruction.\n" +
"If you cannot answer the instruction base on the given information, simply generate 'None'.\n\n" +
"### Instruction:\n" +
"{instruction}\n\n" +
"### Input:\n" +
"{input}\n\n" +
"### Output:\n"
)
def extract_content(self, content:str):
# Remove the "None" at the end
# content = re.sub(r"(None\.|None|none\.|none)", "", content)
content = re.sub(r"(None\.?|none\.?)+$", "", content)
content = content.strip()
return content
# used for annotating the output (the original version, that asks the chatgpt to not reasoning)
class ConversationPromptAnswer_2(ConversationPrompt):
''' generate the answer by following the instruction '''
def __init__(self):
super().__init__()
self.system = (
"You are a helpful assistant. " +
"Your responsibility is to follow the user's instruction and generate the output."
)
# don't need to filter the keywords when annotating the output
self.blacklist = [
]
self.requirement_prompt = (
"1. Conclude your final output without reasoning.\n" +
"2. If you think it is impossible to answer the instruction by giving the existing information (e.g., require additional context, not a textual task), simply generate 'None'.\n"
)
self.query_prompt = (
"### Input:\n" +
"{input}\n\n" +
"### Instruction:\n" +
"{instruction}\n\n" +
"### Requirements:\n" +
self.requirement_prompt + "\n" +
"### Output:\n"
)
def extract_content(self, content:str):
# Remove the "None" at the end
# content = re.sub(r"(None\.|None|none\.|none)", "", content)
content = re.sub(r"(None\.?|none\.?)+$", "", content)
content = content.strip()
return content
# the prompt used in evaluating GPT performances on eval benchmarks (baseline prompt)
class ConversationPromptAnswer_GPT_EVAL(ConversationPrompt):
def __init__(self):
super().__init__()
self.system = (
"You are a helpful assistant. " +
"Your responsibility is to follow the user's instruction and generate the output."
)
# don't need to filter the keywords when annotating the output
self.blacklist = [
]
self.query_prompt = (
"### Instruction:\n" +
"{instruction}\n\n" +
"### Input:\n" +
"{input}\n\n" +
"### Output:\n"
)
def extract_content(self, content:str):
# Remove the "None" at the end
# content = re.sub(r"(None\.|None|none\.|none)", "", content)
content = re.sub(r"(None\.?|none\.?)+$", "", content)
content = content.strip()
return content
# used for adding output constraints
class ConversationPromptConstraint(ConversationPrompt):
''' summarize an output constraint from the instruction '''
def __init__(self):
super().__init__()
self.system = (
"You are a helpful assistant. "
)
self.demonstrations_num = 8
self.query_prompt = (
"Read and summarize the output constraints from the given instruction. Generate 'None', if there are no clear constraints required by the instruction.\n\n" +
"### Example 1:\n" +
"Instruction: {instruction_1}\n" +
"Constraints: {constraint_1}\n\n" +
"### Example 2:\n" +
"Instruction: {instruction_2}\n" +
"Constraints: {constraint_2}\n\n" +
"### Example 3:\n" +
"Instruction: {instruction_3}\n" +
"Constraints: {constraint_3}\n\n" +
"### Example 4:\n" +
"Instruction: {instruction_4}\n" +
"Constraints: {constraint_4}\n\n" +
"### Example 5:\n" +
"Instruction: {instruction_5}\n" +
"Constraints: {constraint_5}\n\n" +
"### Example 6:\n" +
"Instruction: {instruction_6}\n" +
"Constraints: {constraint_6}\n\n" +
"### Example 7:\n" +
"Instruction: {instruction_7}\n" +
"Constraints: {constraint_7}\n\n" +
"### Example 8:\n" +
"Instruction: {instruction_8}\n" +
"Constraints: {constraint_8}\n\n" +
"### Example 9:\n" +
"Instruction: {target_instruction}\n" +
"Constraints: "
)
def extract_content(self, content:str):
# just simply return back the content
content = content.strip()
return content
class ConversationPromptConstraint_2(ConversationPrompt):
''' also add Input to the query prompt, since some of the constraints are coming from the input '''
def __init__(self):
super().__init__()
self.system = (
"You are a helpful assistant. "
)
self.demonstrations_num = 8
self.query_prompt = (
"Read the given task instruction and input, and summarize output constraints for this task. Generate 'None', if there are no clear constraints required by this task.\n\n" +
"### Example 1:\n" +
"- Instruction: {instruction_1}\n" +
"- Input: {input_1}\n" +
"- Constraints: {constraint_1}\n\n" +
"### Example 2:\n" +
"- Instruction: {instruction_2}\n" +
"- Input: {input_2}\n" +
"- Constraints: {constraint_2}\n\n" +
"### Example 3:\n" +
"- Instruction: {instruction_3}\n" +
"- Input: {input_3}\n" +
"- Constraints: {constraint_3}\n\n" +
"### Example 4:\n" +
"- Instruction: {instruction_4}\n" +
"- Input: {input_4}\n" +
"- Constraints: {constraint_4}\n\n" +
"### Example 5:\n" +
"- Instruction: {instruction_5}\n" +
"- Input: {input_5}\n" +
"- Constraints: {constraint_5}\n\n" +
"### Example 6:\n" +
"- Instruction: {instruction_6}\n" +
"- Input: {input_6}\n" +
"- Constraints: {constraint_6}\n\n" +
"### Example 7:\n" +
"- Instruction: {instruction_7}\n" +
"- Input: {input_7}\n" +
"- Constraints: {constraint_7}\n\n" +
"### Example 8:\n" +
"- Instruction: {instruction_8}\n" +
"- Input: {input_8}\n" +
"- Constraints: {constraint_8}\n\n" +
"### Example 9:\n" +
"- Instruction: {target_instruction}\n" +
"- Input: {target_input}\n" +
"- Constraints: "
)
def extract_content(self, content:str):
# just simply return back the content
content = content.strip()
return content
# used for generating wrong output candidates, to expand more classifcation options
class ConversationPromptWrongOutputs(ConversationPrompt):
''' develop more outputs that are worse than the given output '''
def __init__(self):
super().__init__()
self.seperator = r"Wrong Output \d+:" ## Wrong Output 1: Wrong Output 2: .. Wrong Output n:
self.system = (
"You are a helpful assistant. "
)
self.requirement_prompt = (
"1. The output candidates you generate should be worse than the given correct output, e.g., wrong or imperfect answers.\n" +
"2. You are encouraged to generate some challenging output candidates, that are close to the correct output but not the most desired one (i.e., containing certain errors).\n" +
"3. You are encouraged to generate as many output candidates as possible; If you think there are no more suitable output candidates, end up with 'None'.\n"
)
self.query_prompt = (
"Given a task, a task input, and a corresponding correct output, generate more output candidates for this task.\n\n" +
"### Requirements:\n" +
self.requirement_prompt + "\n" +
"### Task:\n" +
"{instruction}\n\n" +
"### Input:\n" +
"{input}\n\n" +
"### Output:\n" +
"{output}\n\n" +
"Wrong Output 1:\n"
)
def extract_content(self, content:str):
# Remove the "None" at the end
# content = re.sub(r"(None\.|None|none\.|none)", "", content)
content = re.sub(r"(None\.?|none\.?)+$", "", content)
items = re.split(self.seperator, content)
# Remove any empty elements from the list
items = [item.strip() for item in items if item.strip() != ""]
return items
# used for classify whether an exsiting instruction can be applied to a given input
class ClassificationValidationPrompt(ConversationPrompt):
''' judge valid instruction or not (tested on gpt-4 and gpt-3.5-turbo-0613) '''
def __init__(self):
super().__init__()
self.system = (
"You are an expert in Natural Language Processing (NLP) tasks."
)
self.query_prompt = (
"You are an expert in Natural Language Processing (NLP) tasks.\n" +
"Given a task description and a piece of text, your job is to determine whether this text can be used as input for this task.\n" +
"If the text satisfies the input expectation of this task, answer 'Yes'; otherwise, if the text doesn't match the input description, answer 'No'.\n\n" +
"### Text:\n" +
"{input}\n\n" +
"### Task Description:\n" +
"{instruction}\n\n" +
"### Your Answer:\n"
)
def extract_content(self, content:str):
# just simply return back the content
content = content.strip()
content = re.sub(r'[^\w\s]', '', content) # remove all punctuations
content = content.lower() # convert to lower case
return content
if __name__ == "__main__":
# prompt = ConversationPromptAttribute()
# test_input = {"input": "This is a test input."}
# print(prompt.query_prompt.format_map(test_input))
# test_content = "Identify the total number of characters in the input.\n\n" + \
# "Attribute 2:\n" + \
# "Determine the number of uppercase letters in the input.\n\n" + \
# "Attribute 3:\n" + \
# "Count the number of lowercase letters in the input.\n\n" + \
# "Attribute 17:\n" + \
# "Count the number of characters that are neither vowels nor consonants (i.e., digits and special characters) in the input.\n\n" + \
# "None."
# print(test_content)
# print("\n\nExtracted attributes:")
# print(prompt.extract_content(test_content))
# test_content = "Identify the total number of characters in the input.\n\n" + \
# "Task 2:\n" + \
# "Determine the number of uppercase letters (such as 'None') in the input.\n\n" + \
# "Task 3:\n" + \
# "Count the number of lowercase letters in the input.\n\n" + \
# "Task 17:\n" + \
# "Count the number of characters that are neither vowels nor consonants (i.e., digits and special characters) in the input, e.g., none.\n\n" + \
# "None."
# test_content = "None."
# prompt = ConversationPromptTask()
# prompt = ConversationPromptTask_2()
# print(prompt.extract_content(test_content))
# test_content = "Person A: Well that's sad. It might've been funny if it was fake.\n" + \
# "Person B: Oh yes, because nothing brings joy to the world like a good fake tragedy. Maybe next time we can all gather around and laugh at a staged car accident.\n" + \
# "Person B: Oh, my apologies. I thought you were suggesting we should all be entertained by the misfortunes of others. Silly me.\n" + \
# "None."
# prompt = ConversationPromptAnswer()
# test_input = {"input": "This is a test input.", "instruction": "This is a test instruction."}
# print(prompt.query_prompt.format_map(test_input))
# print(prompt.extract_content(test_content))
# test_input = {"input": "This is a test input.", "hint": "This is a test hint."}
# print(prompt.query_prompt.format_map(test_input))
# prompt = ConversationPromptTask_3()
# test_input = {"input": "This is a test input.", "hint": "This is a test hint.", "example_1": "This is a test example 1.", "example_2": "This is a test example 2.", "example_3": "This is a test example 3."}
# print(prompt.query_prompt.format_map(test_input))
# print(10*"="+"\n\n")
# prompt = ConversationPromptTask_4()
# test_input = {"input": "This is a test input.", "hint": "This is a test hint.", "example_1": "This is a test example 1.", "example_2": "This is a test example 2.", "example_3": "This is a test example 3."}
# print(prompt.query_prompt.format_map(test_input))
# print(10*"="+"\n\n")
# prompt = ConversationPromptTask_CLS()
# test_input = {"input": "This is a test input.", "hint": "This is a test hint.", "example_1": "This is a test example 1.", "example_2": "This is a test example 2.", "example_3": "This is a test example 3."}
# print(prompt.query_prompt.format_map(test_input))
# prompt = ClassificationValidationPrompt()
# test_input = {"input": "This is a test input.", "instruction": "This is a test instruction."}
# print(prompt.query_prompt.format_map(test_input))
# prompt = ConversationPromptAttribute_2()
# test_input = {"input": "This is a test input.", "hint": "This is a test hint."}
# print(prompt.query_prompt.format_map(test_input))
# prompt = ConversationPromptTask_5()
# test_input = {"input": "This is a test input.", "hint": "This is a test hint.", "example_1": "This is a test example 1.", "example_2": "This is a test example 2.", "example_3": "This is a test example 3."}
# print(prompt.query_prompt.format_map(test_input))
# prompt = ConversationPromptTask_6()
# test_input = {"input": "This is a test input.", "hint": "This is a test hint.", "example_1": "This is a test example 1.", "example_2": "This is a test example 2.", "example_3": "This is a test example 3."}
# print(prompt.query_prompt.format_map(test_input))
# prompt = ConversationPromptConstraint()
# test_input = {"instruction_1": "This is a test instruction 1.", "constraint_1": "This is a test constraint 1.",
# "instruction_2": "This is a test instruction 2.", "constraint_2": "This is a test constraint 2.",
# "instruction_3": "This is a test instruction 3.", "constraint_3": "This is a test constraint 3.",
# "instruction_4": "This is a test instruction 4.", "constraint_4": "This is a test constraint 4.",
# "instruction_5": "This is a test instruction 5.", "constraint_5": "This is a test constraint 5.",
# "instruction_6": "This is a test instruction 6.", "constraint_6": "This is a test constraint 6.",
# "instruction_7": "This is a test instruction 7.", "constraint_7": "This is a test constraint 7.",
# "instruction_8": "This is a test instruction 8.", "constraint_8": "This is a test constraint 8.",
# "target_instruction": "This is a test target instruction."}
# print(prompt.query_prompt.format_map(test_input))
# prompt = ConversationPromptConstraint_2()
# test_input = {"instruction_1": "This is a test instruction 1.", "constraint_1": "This is a test constraint 1.", "input_1": "This is a test input 1.",
# "instruction_2": "This is a test instruction 2.", "constraint_2": "This is a test constraint 2.", "input_2": "This is a test input 2.",
# "instruction_3": "This is a test instruction 3.", "constraint_3": "This is a test constraint 3.", "input_3": "This is a test input 3.",
# "instruction_4": "This is a test instruction 4.", "constraint_4": "This is a test constraint 4.", "input_4": "This is a test input 4.",
# "instruction_5": "This is a test instruction 5.", "constraint_5": "This is a test constraint 5.", "input_5": "This is a test input 5.",
# "instruction_6": "This is a test instruction 6.", "constraint_6": "This is a test constraint 6.", "input_6": "This is a test input 6.",
# "instruction_7": "This is a test instruction 7.", "constraint_7": "This is a test constraint 7.", "input_7": "This is a test input 7.",
# "instruction_8": "This is a test instruction 8.", "constraint_8": "This is a test constraint 8.", "input_8": "This is a test input 8.",
# "target_instruction": "This is a test target instruction.", "target_input": "This is a test target input."}
# print(prompt.query_prompt.format_map(test_input))
prompt = ConversationPromptWrongOutputs()
test_input = {"instruction": "This is a test instruction.", "input": "This is a test input.", "output": "This is a test output."}
print(prompt.query_prompt.format_map(test_input))
# test_content = "test content 1\n\n" + \
# "Wrong Output 2:\n" + \
# "test content 2\n\n" + \
# "Wrong Output 3:\n" + \
# "test content 3\n\n" + \
# "None."
# test_content = "frience." + \
# "None."
# print(prompt.extract_content(test_content))