Skip to content

Commit 3d2689a

Browse files
committed
Update the docstrings
1 parent e03901d commit 3d2689a

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

prompts/templates.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,40 @@
1111
class Template:
1212
"""Represents a prompt template.
1313
14-
A prompt template is a callable that, given a Jinja2 template and a set of values,
15-
renders the template using those values. It is recommended to instantiate `Template`
16-
using the `template` decorator, which extracts the template from the function's
17-
docstring and its variables from the function's signature.
14+
A prompt template is a callable with renders the template returned by the
15+
function using the values that are passed to it. It is recommended to
16+
instantiate `Template` using the `template` decorator.
17+
18+
>>> import prompts
19+
...
20+
... @prompts.template
21+
... def prompt(name: str) -> str:
22+
... return "My name is {{name}}"
1823
1924
It is not uncommon that, for the same taks, different models will perform
2025
better with different prompt. Here we thus allow to dispatch to associate a
2126
prompt with a task and dispatch the prompt based on the model being used; a
2227
`Template` instance is thus also a registry that associates model names to
2328
other templates.
2429
30+
>>> @prompt.register("gpt2")
31+
... def prompt_gpt2(name: str) -> str:
32+
... return "Hi GPT2! My name is {{name}}"
33+
34+
The name of the model can then be passed to the render function along with
35+
the model name and the values of the arguments:
36+
37+
>>> from prompts import render
38+
...
39+
... render(prompt, "gpt2", name="Dan")
40+
>>> "Hi GPT2! My name is Dan"
2541
2642
Attributes
2743
----------
28-
template
29-
The template to render.
44+
fn
45+
The function that returns a template.
3046
signature
31-
The prompt function's signature.
47+
The function's signature.
3248
model
3349
The model the `Template` is associated with. Defaults to `None`.
3450
registry
@@ -81,7 +97,7 @@ def __getitem__(self, model_name: str):
8197

8298
def register(self, model_name: str):
8399
"""Register the prompt template, as represented by a prompt function,
84-
for the model name.
100+
for a given model `model_name`.
85101
86102
"""
87103

@@ -95,11 +111,11 @@ def wrapper(fn: Callable):
95111

96112

97113
def template(fn: Callable) -> Template:
98-
"""Decorate a function that contains a prompt template.
114+
"""Decorate a function that returns a prompt template.
99115
100-
This allows to define prompts in the docstring of a function and simplify their
101-
manipulation by providing some degree of encapsulation. It uses the `render`
102-
function internally to render templates.
116+
This allows to define prompts as the return value of a function and simplify
117+
their manipulation by providing some degree of encapsulation. It uses the
118+
`render` function internally to render templates.
103119
104120
>>> import prompts
105121
>>>
@@ -125,7 +141,7 @@ def template(fn: Callable) -> Template:
125141
126142
Returns
127143
-------
128-
A `Prompt` callable class which will render the template when called.
144+
A `Template` callable class which will render the template when called.
129145
130146
"""
131147
signature = inspect.signature(fn)
@@ -139,7 +155,7 @@ def render(
139155
model_name: Optional[str] = None,
140156
**values: Optional[Dict[str, Hashable]],
141157
) -> str:
142-
r"""Parse a Jinaj2 template and translate it into an Outlines graph.
158+
r"""Parse a Jinaj2 template and renders it using the passed values.
143159
144160
This function removes extra whitespaces and linebreaks from templates to
145161
allow users to enter prompts more naturally than if they used Python's
@@ -150,13 +166,13 @@ def render(
150166
151167
Outlines follow Jinja2's syntax
152168
153-
>>> import outlines
154-
>>> outline = outlines.render("I like {{food}} and {{sport}}", food="tomatoes", sport="tennis")
169+
>>> from prompts import render
170+
>>> render("I like {{food}} and {{sport}}", food="tomatoes", sport="tennis")
155171
I like tomatoes and tennis
156172
157173
If the first line of the template is empty, `render` removes it
158174
159-
>>> from outlines import render
175+
>>> from prompts import render
160176
>>>
161177
>>> tpl = '''
162178
... A new string'''

0 commit comments

Comments
 (0)