1
1
import yaml
2
- from jinja2 import BaseLoader , Environment , PackageLoader , select_autoescape
2
+ from jinja2 import BaseLoader , Environment
3
3
4
4
env = Environment (loader = BaseLoader )
5
5
6
6
7
- def get_sample_template_data ():
8
- data = TemplateCollection ()
9
-
10
- ag_news_template = Template (
11
- "basic" ,
12
- "Example template." ,
13
- 'return example["text"] + "\n \n Is this an example of news about world politics, sports, business, or technology?"' ,
14
- "label_map = {\n "
15
- ' 0: "World politics",\n '
16
- ' 1: "Sports",\n '
17
- ' 2: "Business",\n '
18
- ' 3: "Technology"}\n '
19
- 'return label_map[example["label"]]' ,
20
- )
21
-
22
- data .add_template ("ag_news" , ag_news_template )
23
-
24
- trec_template = Template (
25
- "basic" ,
26
- "Example template." ,
27
- 'return example["text"] + "\n \n Is this asking about a description, an entity, '
28
- 'an abbreviation, a person, or a quantity?"' ,
29
- "label_map = {\n "
30
- ' 0: "A description",\n '
31
- ' 1: "An entity",\n '
32
- ' 2: "An abbreviation",\n '
33
- ' 3: "A person",\n '
34
- ' 4: "A quantity"}\n '
35
- 'return label_map[example["label-coarse"]]' ,
36
- )
37
-
38
- data .add_template ("trec" , trec_template )
39
-
40
- return data
41
-
42
-
43
7
class TemplateCollection :
44
8
"""
45
9
Collection of prompt templates.
@@ -133,34 +97,25 @@ class Template(yaml.YAMLObject):
133
97
134
98
yaml_tag = "!Template"
135
99
136
- def __init__ (self , name , reference , prompt_fn = None , output_fn = None , jinja_tpl = None ):
100
+ def __init__ (self , name , jinja , reference ):
137
101
"""
138
102
Creates a prompt template.
139
103
140
- A prompt template is made up three main pieces: strings that define
141
- three functions, one each for generating the input, the prompt, and the
142
- output given an example. These strings should not include the function
143
- signature, but should assume that there is an input called "example".
144
- Each function should return a string.
104
+ A prompt template is expressed in Jinja. It is rendered using an example
105
+ from the corresponding Hugging Face datasets library (a dictionary). The
106
+ separator ||| should appear once to divide the template into prompt and
107
+ output. Generally, the prompt should provide information on the desired
108
+ behavior, e.g., text passage and instructions, and the output should be
109
+ a desired response.
145
110
146
111
:param name: unique name (per dataset) for template
112
+ :param jinja: template expressed in Jinja
147
113
:param reference: string metadata describing author or paper reference
148
114
for template
149
- :param prompt_fn: string defining function that creates prompt from example
150
- :param output_fn: string defining function that creates output from example
151
115
"""
152
116
self .name = name
153
- self .prompt_fn = prompt_fn
154
- self .output_fn = output_fn
117
+ self .jinja = jinja
155
118
self .reference = reference
156
- self .jinja = jinja_tpl
157
-
158
- @property
159
- def jinja_tpl (self ):
160
- if hasattr (self , "jinja" ):
161
- return self .jinja
162
- else :
163
- return ""
164
119
165
120
def get_name (self ):
166
121
"""
@@ -183,29 +138,7 @@ def apply(self, example):
183
138
Creates a prompt by applying this template to an example
184
139
185
140
:param example: the dataset example to create a prompt for
186
- :return: tuple of 3 strings, for input, prompt, and output
187
- """
188
- if self .jinja_tpl :
189
- rtemplate = env .from_string (self .jinja_tpl )
190
- return rtemplate .render (** example ).split ("|||" )
191
-
192
- else :
193
- fns = {}
194
- exec (self ._make_fun_str ("prompt_fn" , ["example" ], self .prompt_fn ), fns )
195
- exec (self ._make_fun_str ("output_fn" , ["example" ], self .output_fn ), fns )
196
- return (fns ["prompt_fn" ](example ), fns ["output_fn" ](example ))
197
-
198
- @classmethod
199
- def _make_fun_str (cls , name , args , body ):
200
- """
201
- Creates a string representation of a Python function.
202
-
203
- :param name: the name of the function
204
- :param args: iterable of strings naming function arguments
205
- :param body: the function definition. The outermost context should be unindented.
206
- :return: full function definition that can be parsed by exec
141
+ :return: tuple of 2 strings, for prompt and output
207
142
"""
208
- arg_str = ", " .join (args )
209
- signature = f"def { name } ({ arg_str } ):\n "
210
- body = "\n " .join ([(" " + line ) for line in body .split ("\n " )])
211
- return signature + body
143
+ rtemplate = env .from_string (self .jinja )
144
+ return rtemplate .render (** example ).split ("|||" )
0 commit comments