Templates
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 |
---|---|
The rendered template as a Python ``str``.
|
|
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 |
---|---|
An instance of the class with the provided content as a template.
|
|
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
: isplay a JSON Schema
Users may pass additional filters, and/or override existing ones.
Arguments
loader
An optional BaseLoader
instance
filters
A dictionary of filters, map between the filter's name and the
corresponding function.
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
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 |
|