輸入
根據如下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 官方說明。