-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstruction_encode_templates.py
More file actions
144 lines (129 loc) · 5.52 KB
/
instruction_encode_templates.py
File metadata and controls
144 lines (129 loc) · 5.52 KB
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
"""
@misc{wang2023far,
title={How Far Can Camels Go? Exploring the State of Instruction Tuning on Open Resources},
author={Yizhong Wang and Hamish Ivison and Pradeep Dasigi and Jack Hessel and Tushar Khot and Khyathi Raghavi Chandu and David Wadden and Kelsey MacMillan and Noah A. Smith and Iz Beltagy and Hannaneh Hajishirzi},
year={2023},
eprint={2306.04751},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
"""
import random
encoding_templates_w_input = [
# input encoding template, output encoding template, weight
("{instruction}\n\n{input}\n\n", "{output}", 0.2),
("{instruction}\n{input}\n\n", "{output}", 0.1),
("{instruction}\n{input}\n", "{output}", 0.1),
("{instruction}\n\nInput: {input}\n\nOutput:", "{output}", 0.05),
("{instruction}\nInput: {input}\nOutput:", "{output}", 0.05),
("{instruction}\n{input}\n\nResponse:", "{output}", 0.05),
("{instruction}\n\nAdditional Context:\n{input}\n\nAnswer:", "{output}", 0.05),
("Task: {instruction}\nInput: {input}\nOutput:", "{output}", 0.05),
("Task: {instruction}\n\n{input}\n\n", "{output}", 0.05),
("Task: {instruction}\n\n{input}\n\nAnswer:", "{output}", 0.05),
(
"You need to complete the following task:\n\n{instruction}\n\n{input}\n\nAnswer:",
"{output}",
0.05,
),
(
"{instruction}\n\nNow complete the following instance -\nInput: {input}\nOutput:",
"{output}",
0.05,
),
("Instruction:{instruction}\n\nInput: {input}\n\n", "{output}", 0.05),
(
"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n"
"### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:",
"{output}",
0.1,
), # alpaca template
]
encoding_templates_wo_input = [
("{instruction}\n\n", "{output}", 0.2),
("{instruction}\n", "{output}", 0.1),
("{instruction}", "\n{output}", 0.1),
("{instruction} Output:", "{output}", 0.05),
("{instruction}\nResponse:", "{output}", 0.05),
("{instruction}\n\nAnswer:", "{output}", 0.05),
("Task: {instruction}\n\n", "{output}", 0.05),
("Instruction: {instruction}\n", "{output}", 0.05),
("Instruction: {instruction}\nOutput:", "{output}", 0.05),
("You need to complete the following task:\n\n{instruction}\n\n", "{output}", 0.05),
("Can you help with this?\n\n{instruction}\n", "{output}", 0.05),
("Plase answer the following request: {instruction}\nAnswer:", "{output}", 0.05),
(
"Tell me how would you respond to the following request.\n{instruction}\n",
"{output}",
0.05,
),
(
"Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:",
"{output}",
0.1,
), # alpaca template
]
def encode_instruction_example(
instruction, input, output, random_template=True, eos_token=None
):
if random_template:
if input is not None and input.strip() != "":
# randomly choose a template with input
prompt_template, completion_template, _ = random.choices(
encoding_templates_w_input,
weights=[w for _, _, w in encoding_templates_w_input],
)[0]
prompt = prompt_template.format(
instruction=instruction.strip(), input=input.strip()
)
completion = completion_template.format(output=output.strip())
else:
# randomly choose a template without input
prompt_template, completion_template, _ = random.choices(
encoding_templates_wo_input,
weights=[w for _, _, w in encoding_templates_wo_input],
)[0]
prompt = prompt_template.format(instruction=instruction.strip())
completion = completion_template.format(output=output.strip())
else:
if input is not None and input.strip() != "":
prompt = instruction.strip() + "\n\n" + input.strip() + "\n\n"
completion = output.strip()
else:
prompt = instruction.strip() + "\n\n"
completion = output.strip()
data = {
"prompt": prompt,
"completion": completion + eos_token if eos_token else completion,
}
return data
def encode_cot_instruction_example(
instruction, input, cot_inputs, cot_outputs, output, eos_token="[END]"
):
if input is not None and input.strip() != "":
prompt = instruction.strip() + "\n\n" + input.strip() + "\n\n"
completion = output.strip()
else:
prompt = instruction.strip() + "\n\n"
completion = output.strip()
cot_prompts = [instruction.strip() for instruction in cot_inputs]
cot_completions = [completion.strip() for completion in cot_outputs]
data = {
"prompt": prompt,
"cot_prompts": cot_prompts,
"cot_completions": cot_completions,
"completion": completion + eos_token if eos_token else completion,
}
return data
def encode_few_shot_example(instruction, examplars, input, output, eos_token=None):
prompt = instruction.strip() + "\n\n"
for examplar in examplars:
prompt += "Input:\n" + examplar["input"].strip() + "\n"
prompt += "Output:\n" + examplar["output"].strip() + "\n\n"
prompt += "Input:\n" + input.strip() + "\n"
prompt += "Output:\n"
data = {
"prompt": prompt,
"completion": output.strip() + eos_token if eos_token else output.strip(),
}
return data