输入
根据如下TEMPLATE和params写一个modelfile文件,TEMPLATE为:{{- $lastUserIdx := -1 -}}
{{- range $idx, $msg := .Messages -}}
{{- if eq $msg.Role “user” }}{{ $lastUserIdx = $idx }}{{ end -}}
{{- end }}
{{- if or .System .Tools }}<|im_start|>system
{{ if .System }}
{{ .System }}
{{- end }}
{{- if .Tools }}
Tools
You may call one or more functions to assist with the user query.
You are provided with function signatures within XML tags:
{{- range .Tools }}
{“type”: “function”, “function”: {{ .Function }}}
{{- end }}
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{“name”: , “arguments”: }
</tool_call>
{{- end -}}
<|im_end|>
{{ end }}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
{{- if eq .Role “user” }}<|im_start|>user
{{ .Content }}
{{- if and $.IsThinkSet (eq $i $lastUserIdx) }}
{{- if $.Think -}}
{{- " "}}/think
{{- else -}}
{{- " "}}/no_think
{{- end -}}
{{- end }}<|im_end|>
{{ else if eq .Role “assistant” }}<|im_start|>assistant
{{ if (and $.IsThinkSet (and .Thinking (or $last (gt $i $lastUserIdx)))) -}}
{{ .Thinking }}
{{ end -}}
{{ if .Content }}{{ .Content }}
{{- else if .ToolCalls }}<tool_call>
{{ range .ToolCalls }}{“name”: “{{ .Function.Name }}”, “arguments”: {{ .Function.Arguments }}}
{{ end }}</tool_call>
{{- end }}{{ if not $last }}<|im_end|>
{{ end }}
{{- else if eq .Role “tool” }}<|im_start|>user
<tool_response>
{{ .Content }}
</tool_response><|im_end|>
{{ end }}
{{- if and (ne .Role “assistant”) $last }}<|im_start|>assistant
{{ if and $.IsThinkSet (not $.Think) -}}
{{ end -}}
{{ end }}
{{- end }}
params为:{
“repeat_penalty”: 1,
“stop”: [
“<|im_start|>”,
“<|im_end|>”
],
“temperature”: 0.6,
“top_k”: 20,
“top_p”: 0.95
}
根据您提供的 TEMPLATE 和 params,我为您编写了一个完整的 Ollama Modelfile 文件。结合搜索结果中关于 Modelfile 的规范,以下是专业实现:
完整 Modelfile 文件
# 基础模型(需替换为实际模型名或路径)
FROM your_base_model:tag# 系统提示(根据工具调用逻辑优化)
SYSTEM """
You are an AI assistant that supports function calling.
Respond concisely and call tools when needed.
"""# 用户提供的模板(完整复制)
TEMPLATE """
{{- $lastUserIdx := -1 -}}
{{- range $idx, $msg := .Messages -}}
{{- if eq $msg.Role "user" }}{{ $lastUserIdx = $idx }}{{ end -}}
{{- end }}
{{- if or .System .Tools }}<|im_start|>system
{{ if .System }}
{{ .System }}
{{- end }}
{{- if .Tools }}# ToolsYou may call one or more functions to assist with the user query.You are provided with function signatures within <tools></tools> XML tags:
<tools>
{{- range .Tools }}
{"type": "function", "function": {{ .Function }}}
{{- end }}
</tools>For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call>
{{- end -}}
<|im_end|>
{{ end }}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
{{- if eq .Role "user" }}<|im_start|>user
{{ .Content }}
{{- if and $.IsThinkSet (eq $i $lastUserIdx) }}{{- if $.Think -}}{{- " "}}/think{{- else -}}{{- " "}}/no_think{{- end -}}
{{- end }}<|im_end|>
{{ else if eq .Role "assistant" }}<|im_start|>assistant
{{ if (and $.IsThinkSet (and .Thinking (or $last (gt $i $lastUserIdx)))) -}}
<think>{{ .Thinking }}</think>
{{ end -}}
{{ if .Content }}{{ .Content }}
{{- else if .ToolCalls }}<tool_call>
{{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
{{ end }}</tool_call>
{{- end }}{{ if not $last }}<|im_end|>
{{ end }}
{{- else if eq .Role "tool" }}<|im_start|>user
<tool_response>
{{ .Content }}
</tool_response><|im_end|>
{{ end }}
{{- if and (ne .Role "assistant") $last }}<|im_start|>assistant
{{ if and $.IsThinkSet (not $.Think) -}}
<think></think>{{ end -}}
{{ end }}
{{- end }}
"""# 用户提供的参数配置
PARAMETER repeat_penalty 1
PARAMETER temperature 0.6
PARAMETER top_k 20
PARAMETER top_p 0.95# 停止词设置(覆盖模板中的XML标签)
PARAMETER stop "<|im_start|>"
PARAMETER stop "<|im_end|>"
PARAMETER stop "<tool_call>"
PARAMETER stop "</tool_call>"
关键配置说明
-
基础模型适配
FROM your_base_model:tag
需替换为实际模型(如qwen:7b
)。模板中的工具调用逻辑(<tools>
/<tool_call>
)要求基础模型支持函数调用(如 Qwen 系列)。 -
模板特性
- 支持多角色对话(user/assistant/tool)
- 包含动态逻辑(如
$lastUserIdx
跟踪) - 支持思维链(
/think
和/no_think
指令) - 完整的工具调用格式规范
-
参数优化
temperature=0.6
:平衡创造性与准确性top_k=20
与top_p=0.95
:协同控制采样多样性- 4 组
stop
参数:确保 XML 标签正确终止生成
-
系统提示优化
添加了默认工具调用说明,与模板中的<tools>
部分逻辑一致。
使用步骤
- 将文件保存为
Modelfile
(无后缀名) - 创建 Ollama 模型:
ollama create custom-model -f ./Modelfile
- 测试工具调用功能:
ollama run custom-model "查询北京今天的天气"
注意:若需发布模型,可通过
ollama push
分享。完整文档参考:Ollama Modelfile 官方说明。