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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
__call__(*args, **kwargs)
Render and return the template.
Returns:
Type | Description |
---|---|
The rendered template as a Python ``str``.
|
|
Source code in outlines/prompts.py
from_file(path, filters={})
classmethod
Create a Prompt 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 |
---|---|
Prompt
|
An instance of the Prompt class with the template loaded from the file. |
Source code in outlines/prompts.py
from_str(content, filters={})
classmethod
Create an instance of the class from a string.
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/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=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 `Prompt` callable class which will render the template when called.
|
|