templates
Create templates to easily build prompts.
Template
dataclass
Represents a prompt template.
We return a Template
class instead of a simple function so the
template can be accessed by callers.
Source code in outlines/templates.py
__call__(*args, **kwargs)
Render and return the template.
Returns:
Type | Description |
---|---|
str
|
The rendered template as a Python string. |
Source code in outlines/templates.py
from_file(path, filters={})
classmethod
Create a Template
instance from a file containing a Jinja
template.
Note: This method does not allow to include and inheritance to
reference files that are outside the folder or subfolders of the file
given to from_file
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path
|
The path to the file containing the Jinja template. |
required |
Returns:
Type | Description |
---|---|
Template
|
An instance of the Template class with the template loaded from the file. |
Source code in outlines/templates.py
from_string(content, filters={})
classmethod
Create a Template
instance from a string containing a Jinja
template.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content
|
str
|
The string content to be converted into a template. |
required |
Returns:
Type | Description |
---|---|
Template
|
An instance of the class with the provided content as a template. |
Source code in outlines/templates.py
Vision
dataclass
Contains the input for a vision model.
Provide an instance of this class as the model_input
argument to a model
that supports vision.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The prompt to use to generate the response. |
required |
image
|
Image
|
The image to use to generate the response. |
required |
Source code in outlines/templates.py
create_jinja_env(loader, filters)
Create a new Jinja environment.
The Jinja environment is loaded with a set of pre-defined filters:
- name
: get the name of a function
- description
: get a function's docstring
- source
: get a function's source code
- signature
: get a function's signature
- args
: get a function's arguments
- schema
: display a JSON Schema
Users may pass additional filters, and/or override existing ones.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loader
|
Optional[BaseLoader]
|
An optional |
required |
filters
|
Dict[str, Callable]
|
A dictionary of filters, map between the filter's name and the corresponding function. |
required |
Source code in outlines/templates.py
get_fn_args(fn)
Returns the arguments of a function with annotations and default values if provided.
Source code in outlines/templates.py
get_fn_description(fn)
Returns the first line of a callable's docstring.
Source code in outlines/templates.py
get_fn_name(fn)
Returns the name of a callable.
Source code in outlines/templates.py
get_fn_signature(fn)
Return the signature of a callable.
Source code in outlines/templates.py
get_fn_source(fn)
Return the source code of a callable.
Source code in outlines/templates.py
get_schema_dict(model)
get_schema_pydantic(model)
Return the schema of a Pydantic model.
Source code in outlines/templates.py
parse_pydantic_schema(raw_schema, definitions)
Parse the output of Basemodel.[schema|model_json_schema]()
.
This recursively follows the references to other schemas in case of nested models. Other schemas are stored under the "definitions" key in the schema of the top-level model.
Source code in outlines/templates.py
prompt(fn=None, filters={})
Decorate a function that contains a prompt template.
This allows to define prompts in the docstring of a function and simplify their
manipulation by providing some degree of encapsulation. It uses the render
function internally to render templates.
>>> import outlines
>>>
>>> @outlines.prompt
>>> def build_prompt(question):
... "I have a ${question}"
...
>>> prompt = build_prompt("How are you?")
This API can also be helpful in an "agent" context where parts of the prompt are set when the agent is initialized and never modified later. In this situation we can partially apply the prompt function at initialization.
>>> import outlines
>>> import functools as ft
...
>>> @outlines.prompt
... def solve_task(name: str, objective: str, task: str):
... """Your name is {{name}}.
... Your overall objective is to {{objective}}.
... Please solve the following task: {{task}}
... """
...
>>> hal = ft.partial(solve_task, "HAL", "Travel to Jupiter")
Additional Jinja2 filters can be provided as keyword arguments to the decorator.
>>> def reverse(s: str) -> str:
... return s[::-1]
...
>>> @outlines.prompt(filters={ 'reverse': reverse })
... def reverse_prompt(text):
... """{{ text | reverse }}"""
...
>>> prompt = reverse_prompt("Hello")
>>> print(prompt)
... "olleH"
Returns:
Type | Description |
---|---|
A `Template` callable class which will render the template when called.
|
|
Source code in outlines/templates.py
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 |
|