模板

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

基本模板结构

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

  • 布局:模板的整体结构。
  • 变量:动态数据的占位符,在呈现模板时将替换为实际值。
  • 函数:可用于作模板内容的自定义函数或逻辑。

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

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

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

  • 基本消息结构 (布局)
  • 三个变量:Messages,RoleContent(变量)
  • 一个自定义函数 (action),用于迭代项目数组 (range .Messages) 并显示每个项目

向模型添加模板

默认情况下,导入 Ollama 的模型的默认模板为{{ .Prompt }},即用户输入将逐字发送到 LLM。这适用于文本或代码完成模型,但缺少用于聊天或指令模型的基本标记。ollama.cadn.net.cn

在这些模型中省略模板会将正确模板输入的责任交给用户。添加模板使用户能够轻松地从模型中获得最佳结果。ollama.cadn.net.cn

要在模型中添加模板,您需要添加一个TEMPLATE命令添加到 Modelfile 中。下面是一个使用 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(string):系统提示符ollama.cadn.net.cn

Prompt(string):用户提示ollama.cadn.net.cn

Response(字符串):助理响应ollama.cadn.net.cn

Suffix(字符串):在 Google 助理的响应后插入的文本ollama.cadn.net.cn

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

Messages[].Role(string):角色,可以是以下之一system,user,assistanttoolollama.cadn.net.cn

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

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

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

Messages[].ToolCalls[].Function.Name(string):函数名称ollama.cadn.net.cn

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

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

Tools[].Type(string):架构类型。type总是functionollama.cadn.net.cn

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

Tools[].Function.Name(string):函数名称ollama.cadn.net.cn

Tools[].Function.Description(string):函数说明ollama.cadn.net.cn

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

Tools[].Function.Parameters.Type(string):架构类型。type总是objectollama.cadn.net.cn

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

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

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

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

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

提示和最佳实践

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

  • 请注意 dot: Control flow 结构,如rangewith更改值.
  • 范围外变量:使用$.引用当前不在范围内的变量,从根
  • 空格控件:aabbccdd

示例

示例消息

ChatML

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

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

示例工具

可以通过添加{{ .Tools }}node 添加到模板中。此功能对于训练为调用外部工具的模型非常有用,并且可以成为检索实时数据或执行复杂任务的强大工具。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 }}

示例:Fill-in-Middle

可以通过添加{{ .Suffix }}node 添加到模板中。此功能对于经过训练以在用户输入中间生成文本的模型(如代码完成模型)非常有用。ollama.cadn.net.cn

CodeLlama

CodeLlama 7B13B 代码完成模型支持中间填充。ollama.cadn.net.cn

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

[!注意] CodeLlama 34B 和 70B 代码补全以及所有 instruct 和 Python 微调模型都不支持中间填充。ollama.cadn.net.cn

代码

Codestral 22B 支持中间填充。ollama.cadn.net.cn

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

结果 匹配 ”"

    没有匹配 “ 的结果"