-
Notifications
You must be signed in to change notification settings - Fork 255
/
_instruct_templates.py
177 lines (142 loc) · 5.85 KB
/
_instruct_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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from abc import ABC, abstractmethod
from typing import Any, Dict, Mapping, Optional
class InstructTemplate(ABC):
"""
Interface for instruction templates. Each template should include the template
prompt with placeholders for the data inputs.
"""
template = ""
@classmethod
@abstractmethod
def format(
cls, sample: Mapping[str, Any], column_map: Optional[Dict[str, str]] = None
) -> str:
"""
Format the prompt template with the given arguments.
Args:
sample (Mapping[str, Any]): a single data sample with various fields
column_map (Optional[Dict[str, str]]): a mapping from the expected
placeholder names in the template to the column names in the sample.
If None, assume these are identical. Note: if the sample output is not named
as "output" in the dataset, you always need to map it to "output" in column_map.
Returns:
The formatted prompt
"""
pass
class AlpacaInstructTemplate(InstructTemplate):
"""
Prompt template for Alpaca-style datasets. Template prompt changes slightly depending
on if there's an instruction + input or just an instruction.
"""
template = {
"prompt_input": (
"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:\n"
),
"prompt_no_input": (
"Below is an instruction that describes a task. "
"Write a response that appropriately completes the request.\n\n"
"### Instruction:\n{instruction}\n\n### Response:\n"
),
}
@classmethod
def format(
cls, sample: Mapping[str, Any], column_map: Optional[Dict[str, str]] = None
) -> str:
"""
Generate prompt from instruction and input.
Args:
sample (Mapping[str, Any]): a single data sample with instruction
column_map (Optional[Dict[str, str]]): a mapping from the expected
placeholder names in the template to the column names in the sample.
If None, assume these are identical.
Returns:
The formatted prompt
"""
column_map = column_map or {}
key_input = column_map.get("input", "input")
key_instruction = column_map.get("instruction", "instruction")
if key_input in sample and sample[key_input]:
prompt = cls.template["prompt_input"].format(
instruction=sample[key_instruction], input=sample[key_input]
)
else:
prompt = cls.template["prompt_no_input"].format(
instruction=sample[key_instruction]
)
return prompt
class GrammarErrorCorrectionTemplate(InstructTemplate):
"""
Prompt template for grammar correction datasets.
"""
template = "Correct this to standard English: {sentence}\n---\nCorrected: "
@classmethod
def format(
cls, sample: Mapping[str, Any], column_map: Optional[Dict[str, str]] = None
) -> str:
"""
Generate prompt from sentence.
Args:
sample (Mapping[str, Any]): a single data sample with sentence
column_map (Optional[Dict[str, str]]): a mapping from the expected
placeholder names in the template to the column names in the sample.
If None, assume these are identical.
Returns:
The formatted prompt
"""
column_map = column_map or {}
key_sentence = column_map.get("sentence", "sentence")
prompt = cls.template.format(sentence=sample[key_sentence])
return prompt
class SummarizeTemplate(InstructTemplate):
"""
Prompt template to format datasets for summarization tasks.
"""
template = "Summarize this dialogue:\n{dialogue}\n---\nSummary:\n"
@classmethod
def format(
cls, sample: Mapping[str, Any], column_map: Optional[Dict[str, str]] = None
) -> str:
"""
Generate prompt from dialogue.
Args:
sample (Mapping[str, Any]): a single data sample with dialog
column_map (Optional[Dict[str, str]]): a mapping from the expected
placeholder names in the template to the column names in the sample.
If None, assume these are identical.
Returns:
The formatted prompt
"""
column_map = column_map or {}
key_dialogue = column_map.get("dialogue", "dialogue")
prompt = cls.template.format(dialogue=sample[key_dialogue])
return prompt
class StackExchangedPairedTemplate(InstructTemplate):
"""
Prompt template for preference datasets similar to StackExchangedPaired.
"""
template = "Question: {question}\n\nAnswer: "
@classmethod
def format(
cls, sample: Mapping[str, Any], column_map: Optional[Dict[str, str]] = None
) -> str:
"""
Generate prompt from instruction and input.
Args:
sample (Mapping[str, Any]): a single data sample with instruction
column_map (Optional[Dict[str, str]]): a mapping from the expected
placeholder names in the template to the column names in the sample.
If None, assume these are identical.
Returns:
The formatted prompt
"""
column_map = column_map or {}
key_prompt = column_map.get("prompt", "prompt")
prompt = cls.template.format(question=sample[key_prompt])
return prompt