Prompts
Prompt
dataclass
Represents a prompt function.
We return a Prompt
class instead of a simple function so the
template defined in prompt functions can be accessed.
Source code in outlines/prompts.py
__call__(*args, **kwargs)
Render and return the template.
Returns
The rendered template as a Python str
.
Source code in outlines/prompts.py
get_fn_args(fn)
Returns the arguments of a function with annotations and default values if provided.
Source code in outlines/prompts.py
get_fn_description(fn)
Returns the first line of a callable's docstring.
Source code in outlines/prompts.py
get_fn_name(fn)
Returns the name of a callable.
Source code in outlines/prompts.py
get_fn_signature(fn)
Return the signature of a callable.
Source code in outlines/prompts.py
get_fn_source(fn)
Return the source code of a callable.
Source code in outlines/prompts.py
get_schema_dict(model)
get_schema_pydantic(model)
Return the schema of a Pydantic model.
Source code in outlines/prompts.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/prompts.py
prompt(fn)
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")
Returns
A Prompt
callable class which will render the template when called.
Source code in outlines/prompts.py
render(template, **values)
Parse a Jinaj2 template and translate it into an Outlines graph.
This function removes extra whitespaces and linebreaks from templates to allow users to enter prompts more naturally than if they used Python's constructs directly. See the examples for a detailed explanation.
Examples
Outlines follow Jinja2's syntax
import outlines outline = outlines.render("I like {{food}} and {{sport}}", food="tomatoes", sport="tennis") I like tomatoes and tennis
If the first line of the template is empty, render
removes it
from outlines import render
tpl = ''' ... A new string''' tpl ... '\nA new string' render(tpl) ... 'a new string'
Similarly, render
ignores linebreaks introduced by placing the closing quotes
underneath the text:
tpl = ''' ... A new string ... ''' tpl ... '\nA new string\n' render(tpl) ... 'A new string'
If you want to insert a linebreak at the end of the rendered template, you will need to leave an empty line at the end of the template:
tpl = ''' ... A new string ... ... ''' tpl ... '\nA new string\n\n' render(tpl) ... 'A new string\n'
render
removes the identation in docstrings. This is particularly important
when using prompt functions
tpl = ''' ... a string ... and another string''' tpl ... '\n a string\n and another string' render(tpl) ... 'a string\nand another string'
The indentation of the first line is assumed to be the same as the second line's
tpl = '''a string ... and another''' tpl ... 'a string\n and another' render(tpl) ... 'a string\nand another'
To get a different indentation for the first and the second line, we can start the prompt on the string's second line:
tpl = ''' ... First line ... Second line''' render(tpl) ... 'First Line\n Second Line'
Parameters
template A string that contains a template written with the Jinja2 syntax. **values Map from the variables in the template to their value.
Returns
A string that contains the rendered template.
Source code in outlines/prompts.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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 |
|