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. |
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(prompt, image)
This factory function replaces the deprecated Vision
class until it is
fully removed in outlines v1.2.0.
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 |
Returns:
Type | Description |
---|---|
list
|
A list containing the prompt and Image instance. |
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.