outlines
Outlines is a Generative Model Programming Framework.
Application
Application is a class that encapsulates a prompt template and an output type. It can be called to generate a response by providing a model, the values to be substituted in the template in a dictionary and optional inference parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
Union[Template, Callable]
|
A callable that takes arguments and returns a prompt string. |
required |
output_type
|
Any
|
The expected output type of the generated response. |
None
|
Examples:
from pydantic import BaseModel
from transformers import AutoModelForCausalLM, AutoTokenizer
from outlines import models, Application
from outlines.types import JsonType
from outlines.templates import Template
class OutputModel(BaseModel):
result: int
model = models.from_transformers(
AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct"),
AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
)
template_string = "What is 2 times {{ num }}?"
template = Template.from_string(template_string)
application = Application(template, JsonType(OutputModel))
result = application(model, {"num": 3}, max_new_tokens=20)
print(result) # Expected output: { "result" : 6 }
Source code in outlines/applications.py
__call__(model, template_vars, **inference_kwargs)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Model
|
The model to use to generate the response. |
required |
template_vars
|
Dict[str, Any]
|
The variables to be substituted in the template. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
The generated response. |
Source code in outlines/applications.py
__init__(template, output_type=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
Union[Template, Callable]
|
The template to use to build the prompt. |
required |
output_type
|
Optional[Any]
|
The output type provided to the generator. |
None
|
Source code in outlines/applications.py
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
Generator(model, output_type=None, *, processor=None)
Create a generator for the given model and output parameters.
The 2 parameters output_type and processor are mutually exclusive. The parameters processor is only supported for SteerableModel instances (typically local models) and is intended to be only used by advanced users.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Union[Model, AsyncModel]
|
An instance of an Outlines model. |
required |
output_type
|
Optional[Any]
|
The output type expressed as a Python type or a type defined in the outlines.types.dsl module. |
None
|
processor
|
Optional[OutlinesLogitsProcessor]
|
An instance of an OutlinesLogitsProcessor. |
None
|
Returns:
Type | Description |
---|---|
Union[SteerableGenerator, BlackBoxGenerator, AsyncBlackBoxGenerator]
|
A generator instance. |
Source code in outlines/generator.py
clear_cache()
disable_cache()
Disable the cache for this session.
Generative models output different results each time they are called when
sampling. This can be a desirable property for some workflows, in which case
one can call outlines.call.disable
to disable the cache for the session.
This function does not delete the cache, call outlines.cache.clear
instead. It also does not overwrite the cache with the values returned
during the session.
Example
outlines.cache.disable
should be called right after importing outlines:
import outlines.caching as cache cache.disable_cache()
Source code in outlines/caching.py
get_cache()
cached
Get the context object that contains previously-computed return values.
The cache is used to avoid unnecessary computations and API calls, which can be long and expensive for large models.
The cache directory defaults to HOMEDIR/.cache/outlines
, but this choice
can be overridden by the user by setting the value of the OUTLINES_CACHE_DIR
environment variable.
Source code in outlines/caching.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 |
|
grammars
A few common Lark grammars.
read_grammar(grammar_file_name, base_grammar_path=GRAMMAR_PATH)
Read grammar file from default grammar path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grammar_file_name
|
str
|
The name of the grammar file to read. |
required |
base_grammar_path
|
Path
|
The path to the directory containing the grammar file. |
GRAMMAR_PATH
|
Returns:
Type | Description |
---|---|
str
|
The contents of the grammar file. |