TEMPLATE

本文内容均由Ollama官方文档翻译,仅供个人学习,如有差异请以官网文档为准(https://ollama.com)ollama.cadn.net.cn

Ollama 提供了一个功能强大的模板引擎,该引擎基于 Go 语言内置的模板引擎,用于为您的大语言模型构建提示。此功能是充分发挥模型性能的宝贵工具。ollama.cadn.net.cn

基本模板结构

一个基本的 Go 模板由三个主要部分组成:ollama.cadn.net.cn

  • 布局:模板的整体结构。
  • 变量:用于动态数据的占位符,当模板被渲染时,这些占位符将被实际值替换。
  • 功能:可用于操作模板内容的自定义函数或逻辑。

这是一个简单的聊天模板示例:ollama.cadn.net.cn

{{- range .Messages }}
{{ .Role }}: {{ .Content }}
{{- end }}

在此示例中,我们有:ollama.cadn.net.cn

  • 一个基本的消息结构(布局)
  • 三个变量:MessagesRoleContent(变量)
  • 一个自定义函数(操作),用于遍历项目数组(range .Messages)并显示每个项目

向您的模型添加模板

默认情况下,导入到 Ollama 的模型采用默认模板 {{ .Prompt }},即用户输入将逐字发送给大语言模型(LLM)。这对于文本或代码补全类模型是合适的,但对于聊天或指令类模型则缺少必要的标记。ollama.cadn.net.cn

在这些模型中省略模板,会将正确模板化输入的责任交由用户承担。添加模板则可帮助用户轻松获得模型的最佳效果。ollama.cadn.net.cn

要向您的模型中添加模板,您需要在 Modelfile 中添加一个 TEMPLATE 命令。以下是一个使用 Meta 的 Llama 3 的示例。ollama.cadn.net.cn

FROM llama3.2

TEMPLATE """{{- if .System }}<|start_header_id|>system<|end_header_id|>

{{ .System }}<|eot_id|>
{{- end }}
{{- range .Messages }}<|start_header_id|>{{ .Role }}<|end_header_id|>

{{ .Content }}<|eot_id|>
{{- end }}<|start_header_id|>assistant<|end_header_id|>

"""

变量

System(字符串):系统提示ollama.cadn.net.cn

Prompt(字符串):用户提示ollama.cadn.net.cn

Response(字符串):助手回复ollama.cadn.net.cn

Suffix(字符串):插入在助手回复之后的文本ollama.cadn.net.cn

Messages(列表):消息列表ollama.cadn.net.cn

Messages[].Role(字符串):角色,可取值为 systemuserassistanttool 之一ollama.cadn.net.cn

Messages[].Content(字符串):消息内容ollama.cadn.net.cn

Messages[].ToolCalls(列表):模型希望调用的工具列表ollama.cadn.net.cn

Messages[].ToolCalls[].Function(对象):要调用的函数ollama.cadn.net.cn

Messages[].ToolCalls[].Function.Name(字符串):函数名ollama.cadn.net.cn

Messages[].ToolCalls[].Function.Arguments(映射):参数名到参数值的映射ollama.cadn.net.cn

Tools(列表):模型可以访问的工具列表ollama.cadn.net.cn

Tools[].Type(字符串):模式类型。type 始终为 functionollama.cadn.net.cn

Tools[].Function(对象):函数定义ollama.cadn.net.cn

Tools[].Function.Name(字符串):函数名ollama.cadn.net.cn

Tools[].Function.Description(字符串):函数描述ollama.cadn.net.cn

Tools[].Function.Parameters(对象):函数参数ollama.cadn.net.cn

Tools[].Function.Parameters.Type(字符串):模式类型。type 始终为 objectollama.cadn.net.cn

Tools[].Function.Parameters.Required(列表):必需属性的列表ollama.cadn.net.cn

Tools[].Function.Parameters.Properties(映射):属性名称到属性定义的映射ollama.cadn.net.cn

Tools[].Function.Parameters.Properties[].Type(字符串):属性类型ollama.cadn.net.cn

Tools[].Function.Parameters.Properties[].Description(字符串):属性描述ollama.cadn.net.cn

Tools[].Function.Parameters.Properties[].Enum(列表):有效值列表ollama.cadn.net.cn

提示与最佳实践

使用 Go 模板时,请注意以下提示和最佳实践:ollama.cadn.net.cn

  • 请注意点号:控制流结构如 rangewith 会改变值 .
  • 超出作用域的变量:使用 $. 引用当前不在作用域内的变量,从根级开始计数
  • 空白字符控制:aabbccdd

示例

示例消息

ChatML

ChatML 是一种流行的模板格式。它可以用于 Databricks 的 DBRX、Intel 的 Neural Chat 和微软的 Orca 2 等模型。ollama.cadn.net.cn

{{- range .Messages }}<|im_start|>{{ .Role }}
{{ .Content }}<|im_end|>
{{ end }}<|im_start|>assistant

示例工具

可以通过在模板中添加一个 {{ .Tools }} 节点,为模型添加工具支持。该功能适用于训练用于调用外部工具的模型,可用于检索实时数据或执行复杂任务,是强大的工具能力来源。ollama.cadn.net.cn

Mistral

Mistral v0.3 和 Mixtral 8x22B 支持工具调用。ollama.cadn.net.cn

{{- range $index, $_ := .Messages }}
{{- if eq .Role "user" }}
{{- if and (le (len (slice $.Messages $index)) 2) $.Tools }}[AVAILABLE_TOOLS] {{ json $.Tools }}[/AVAILABLE_TOOLS]
{{- end }}[INST] {{ if and (eq (len (slice $.Messages $index)) 1) $.System }}{{ $.System }}

{{ end }}{{ .Content }}[/INST]
{{- else if eq .Role "assistant" }}
{{- if .Content }} {{ .Content }}</s>
{{- else if .ToolCalls }}[TOOL_CALLS] [
{{- range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ json .Function.Arguments }}}
{{- end }}]</s>
{{- end }}
{{- else if eq .Role "tool" }}[TOOL_RESULTS] {"content": {{ .Content }}}[/TOOL_RESULTS]
{{- end }}
{{- end }}

示例:中间填充

可以通过在模板中添加 {{ .Suffix }} 节点,为模型添加中间填充(Fill-in-middle)支持。此功能适用于那些经过训练、能够在用户输入中间生成文本的模型,例如代码补全模型。ollama.cadn.net.cn

Code Llama

CodeLlama 7B13B 代码补全模型支持中间填充(fill-in-middle)。ollama.cadn.net.cn

<PRE> {{ .Prompt }} <SUF>{{ .Suffix }} <MID>

[!NOTE] CodeLlama 34B 和 70B 的代码补全功能,以及所有 instruct 模型和 Python 微调模型均不支持 fill-in-middle(中间填充)功能。ollama.cadn.net.cn

Codestral

Codestral 22B 支持中间填充(fill-in-middle)。ollama.cadn.net.cn

[SUFFIX]{{ .Suffix }}[PREFIX] {{ .Prompt }}

匹配结果:""

    未找到匹配“"