模板
Template
数据类
表示一个 prompt 模板。
我们返回一个 Template
类而非一个简单的函数,以便调用者可以访问模板。
源代码位于 outlines/templates.py
__call__(*args, **kwargs)
渲染并返回模板。
返回值
类型 | 描述 |
---|---|
渲染后的模板,为一个 Python ``str`` 字符串。
|
|
源代码位于 outlines/templates.py
from_file(path, filters={})
类方法
从包含 Jinja 模板的文件创建 Template
实例。
注意:此方法不允许通过 include 和 inheritance 引用位于提供给 from_file
的文件所在文件夹或其子文件夹之外的文件。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
path
|
Path
|
包含 Jinja 模板的文件的路径。 |
必需 |
返回值
类型 | 描述 |
---|---|
Template
|
一个 Template 类实例,其中模板已从文件中加载。 |
源代码位于 outlines/templates.py
from_string(content, filters={})
类方法
从包含 Jinja 模板的字符串创建 Template
实例。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
content
|
str
|
要转换为模板的字符串内容。 |
必需 |
返回值
类型 | 描述 |
---|---|
一个类实例,其中提供的内容作为模板。
|
|
源代码位于 outlines/templates.py
create_jinja_env(loader, filters)
创建一个新的 Jinja 环境。
Jinja 环境加载了一系列预定义过滤器:- name
:获取函数名称 - description
:获取函数的 docstring - source
:获取函数的源代码 - signature
:获取函数的签名 - args
:获取函数的参数 - schema
:显示 JSON Schema
用户可以传递额外的过滤器,和/或覆盖现有过滤器。
参数
loader 一个可选的 BaseLoader
实例 filters 一个过滤器字典,映射过滤器名称和对应的函数。
源代码位于 outlines/templates.py
get_fn_args(fn)
返回一个函数的参数,包括注解和提供的默认值(如果存在)。
源代码位于 outlines/templates.py
get_fn_description(fn)
返回一个可调用对象的 docstring 的第一行。
源代码位于 outlines/templates.py
get_fn_name(fn)
返回一个可调用对象的名称。
源代码位于 outlines/templates.py
get_fn_signature(fn)
返回一个可调用对象的签名。
源代码位于 outlines/templates.py
get_fn_source(fn)
返回一个可调用对象的源代码。
源代码位于 outlines/templates.py
get_schema_dict(model)
get_schema_pydantic(model)
返回一个 Pydantic 模型的 schema。
源代码位于 outlines/templates.py
parse_pydantic_schema(raw_schema, definitions)
解析 Basemodel.[schema|model_json_schema]()
的输出。
对于嵌套模型,这会递归地跟踪对其他 schema 的引用。其他 schema 存储在顶级模型 schema 的“definitions”键下。
源代码位于 outlines/templates.py
prompt(fn=None, filters={})
装饰一个包含 prompt 模板的函数。
这允许在函数的 docstring 中定义 prompt,并通过提供一定程度的封装来简化其操作。它内部使用 render
函数来渲染模板。
>>> import outlines
>>>
>>> @outlines.prompt
>>> def build_prompt(question):
... "I have a ${question}"
...
>>> prompt = build_prompt("How are you?")
此 API 在“agent”场景下也很有用,其中 prompt 的部分内容在 agent 初始化时设置,之后不再修改。在这种情况下,我们可以在初始化时部分应用 prompt 函数。
>>> 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")
额外的 Jinja2 过滤器可以作为关键字参数提供给装饰器。
>>> 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"
返回值
类型 | 描述 |
---|---|
一个 `Template` 可调用类,在被调用时将渲染模板。
|
|
源代码位于 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 |
|