-
Notifications
You must be signed in to change notification settings - Fork 484
/
test_prompts.py
314 lines (241 loc) · 7.51 KB
/
test_prompts.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
from typing import Dict, List
import pytest
from pydantic import BaseModel, Field
import outlines
from outlines.prompts import render
def test_render():
tpl = """
A test string"""
assert render(tpl) == "A test string"
tpl = """
A test string
"""
assert render(tpl) == "A test string"
tpl = """
A test
Another test
"""
assert render(tpl) == "A test\nAnother test"
tpl = """A test
Another test
"""
assert render(tpl) == "A test\nAnother test"
tpl = """
A test line
An indented line
"""
assert render(tpl) == "A test line\n An indented line"
tpl = """
A test line
An indented line
"""
assert render(tpl) == "A test line\n An indented line\n"
def test_render_escaped_linebreak():
tpl = """
A long test \
that we break \
in several lines
"""
assert render(tpl) == "A long test that we break in several lines"
tpl = """
Break in \
several lines \
But respect the indentation
on line breaks.
And after everything \
Goes back to normal
"""
assert (
render(tpl)
== "Break in several lines But respect the indentation\n on line breaks.\nAnd after everything Goes back to normal"
)
def test_render_jinja():
"""Make sure that we can use basic Jinja2 syntax, and give examples
of how we can use it for basic use cases.
"""
# Notice the newline after the end of the loop
examples = ["one", "two"]
prompt = render(
"""
{% for e in examples %}
Example: {{e}}
{% endfor -%}""",
examples=examples,
)
assert prompt == "Example: one\nExample: two\n"
# We can remove the newline by cloing with -%}
examples = ["one", "two"]
prompt = render(
"""
{% for e in examples %}
Example: {{e}}
{% endfor -%}
Final""",
examples=examples,
)
assert prompt == "Example: one\nExample: two\nFinal"
# Same for conditionals
tpl = """
{% if is_true %}
true
{% endif -%}
final
"""
assert render(tpl, is_true=True) == "true\nfinal"
assert render(tpl, is_true=False) == "final"
def test_prompt_basic():
@outlines.prompt
def test_tpl(variable):
"""{{variable}} test"""
assert test_tpl.template == "{{variable}} test"
assert test_tpl.parameters == ["variable"]
with pytest.raises(TypeError):
test_tpl(v="test")
p = test_tpl("test")
assert p == "test test"
p = test_tpl(variable="test")
assert p == "test test"
@outlines.prompt
def test_single_quote_tpl(variable):
"${variable} test"
p = test_tpl("test")
assert p == "test test"
def test_prompt_kwargs():
@outlines.prompt
def test_kwarg_tpl(var, other_var="other"):
"""{{var}} and {{other_var}}"""
assert test_kwarg_tpl.template == "{{var}} and {{other_var}}"
assert test_kwarg_tpl.parameters == ["var", "other_var"]
p = test_kwarg_tpl("test")
assert p == "test and other"
p = test_kwarg_tpl("test", other_var="kwarg")
assert p == "test and kwarg"
p = test_kwarg_tpl("test", "test")
assert p == "test and test"
def test_no_prompt():
with pytest.raises(TypeError, match="template"):
@outlines.prompt
def test_empty(variable):
pass
with pytest.raises(TypeError, match="template"):
@outlines.prompt
def test_only_code(variable):
return variable
def test_prompt_function():
def empty_fn():
pass
def with_description():
"""A description.
But this is ignored.
"""
pass
@outlines.prompt
def name_description_ppt(fn):
"""
{{fn|name}}: {{fn|description}}
"""
rendered = name_description_ppt(empty_fn)
assert rendered == "empty_fn: "
rendered = name_description_ppt(with_description)
assert rendered == "with_description: A description."
def with_signature(one: int, two: List[str], three: float = 1.0):
pass
@outlines.prompt
def name_signature_ppt(fn):
"""
{{fn|name}}: {{fn|signature}}
"""
rendered = name_signature_ppt(with_signature)
assert rendered == "with_signature: one: int, two: List[str], three: float = 1.0"
def test_function_call(one, two=2):
return one + two
@outlines.prompt
def source_ppt(fn):
"""
{{fn|source}}
"""
rendered = source_ppt(test_function_call)
assert rendered == "def test_function_call(one, two=2):\n return one + two\n"
def test_prompt_pydantic_response():
class SimpleResponse(BaseModel):
one: str = Field(description="a description")
two: str
@outlines.prompt
def source_ppt(model):
"{{model | schema }}"
prompt = source_ppt(SimpleResponse)
assert prompt == '{\n "one": "a description",\n "two": "<two>"\n}'
class NestedResponse(BaseModel):
answer: str
thought: SimpleResponse
prompt = source_ppt(NestedResponse)
assert (
prompt
== '{\n "answer": "<answer>",\n "thought": {\n "one": "a description",\n "two": "<two>"\n }\n}'
)
class ConvolutedResponse(BaseModel):
part_one: NestedResponse
part_two: SimpleResponse
prompt = source_ppt(ConvolutedResponse)
assert (
prompt
== '{\n "part_one": {\n "answer": "<answer>",\n "thought": {\n "one": "a description",\n "two": "<two>"\n }\n },\n "part_two": {\n "one": "a description",\n "two": "<two>"\n }\n}'
)
def test_prompt_dict_response():
response = {"one": "a description", "two": ""}
@outlines.prompt
def source_ppt(model):
"{{model | schema }}"
prompt = source_ppt(response)
assert prompt == '{\n "one": "a description",\n "two": ""\n}'
def test_prompt_args():
def no_args():
pass
def with_args(x, y, z):
pass
def with_annotations(x: bool, y: str, z: Dict[int, List[str]]):
pass
def with_defaults(x=True, y="Hi", z={4: ["I", "love", "outlines"]}):
pass
def with_annotations_and_defaults(
x: bool = True,
y: str = "Hi",
z: Dict[int, List[str]] = {4: ["I", "love", "outlines"]},
):
pass
def with_all(
x1,
y1,
z1,
x2: bool,
y2: str,
z2: Dict[int, List[str]],
x3=True,
y3="Hi",
z3={4: ["I", "love", "outlines"]},
x4: bool = True,
y4: str = "Hi",
z4: Dict[int, List[str]] = {4: ["I", "love", "outlines"]},
):
pass
@outlines.prompt
def args_prompt(fn):
"""args: {{ fn | args }}"""
assert args_prompt(no_args) == "args: "
assert args_prompt(with_args) == "args: x, y, z"
assert (
args_prompt(with_annotations)
== "args: x: bool, y: str, z: Dict[int, List[str]]"
)
assert (
args_prompt(with_defaults)
== "args: x=True, y='Hi', z={4: ['I', 'love', 'outlines']}"
)
assert (
args_prompt(with_annotations_and_defaults)
== "args: x: bool = True, y: str = 'Hi', z: Dict[int, List[str]] = {4: ['I', 'love', 'outlines']}"
)
assert (
args_prompt(with_all)
== "args: x1, y1, z1, x2: bool, y2: str, z2: Dict[int, List[str]], x3=True, y3='Hi', z3={4: ['I', 'love', 'outlines']}, x4: bool = True, y4: str = 'Hi', z4: Dict[int, List[str]] = {4: ['I', 'love', 'outlines']}"
)