Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make funtion templates return templates to be rendered #22

Merged
merged 4 commits into from
Sep 27, 2024

Conversation

rlouf
Copy link
Member

@rlouf rlouf commented Sep 26, 2024

Prompts templates are currently contained in the docstring of decorated functions. The main issue with this is that prompt templates cannot be composed.

In this commit we instead require users to return the prompt template from the function. The template will then automatically be rendered using the values passed to the function. This is very flexible: some variables can be used inside the functions and not be present in the Jinja2 template that is returned, for instance:

import prompts

@prompts.template
def my_template(a, b):
    prompt = f'This is a first variable {a}'
    return prompt + "and a second {{b}}"

Closes #17.

Potential issue with performance

I have one reservation related to this change. dottxt-ai/outlines#1162 showed that we can dramatically increase rendering speed by compiling the template only once. Here we have no guarantee that the template being rendered will be the same between different calls.

We run a comparison benchmark between using @prompt.template and a simple function. The performance hit is real (using the prompt template is 1,000 times slower), but we are sub-microsecond so I am not sure this really matters. We added a note in the documentation.

@rlouf rlouf added the enhancement New feature or request label Sep 26, 2024
Prompts remplates are currently contained in the docstring of decorated
functions. The main issue with this is that prompt templates cannot be
composed.

In this commit we instead require users to return the prompt
template from the function. The template will then automatically be
rendered using the values passed to the function. This is very flexible:
some variables can be used inside the functions and not be present in
the Jinja2 template that is returned, for instance:

```python
import prompts

@prompts.template
def my_template(a, b):
    prompt = f'This is a first variable {a}'
    return prompt + "and a second {{b}}"
```
@rlouf rlouf force-pushed the template-as-functions branch 2 times, most recently from 981f8cd to 8ccc783 Compare September 26, 2024 14:45
A concern is that rendering the template this way could be much slower,
assuming Jinja2 makes extensive use of caching. We thus write a
comparative benchmark between using Jinja2 templates or simply
concatenating strings.
@rlouf rlouf merged commit 3d2689a into dottxt-ai:master Sep 27, 2024
5 checks passed
@rlouf rlouf deleted the template-as-functions branch September 27, 2024 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simplify prompts.template and make it composable
1 participant