Ollama官方仓库的问题
Ollama官方仓库国内访问不稳定,有时下载一半突然断开连接就很烦。
解决方法:
我们可以在国内魔塔上下载。
避坑反例教程
如果你按照这个教程做,你的模型很有可能抽风。比如他会顺着你的话说,如图

原因是没有配置合适的模板(Template)。
当前生态下,大模型开发以 Python 为主,因此多数大模型官方提供的 Templat 是面向 python 的 jinja2(如下图) ,而Ollama是使用 Go 写的。
有些时候这种方式部署的模型不会抽风,那是因为Ollama 内部写了一个"粗糙的翻译器",有概率将 jinja2 Template 成功翻译为 Go Template,但是这种翻译理论上是不可行的,就好比你无法将 C++ 的代码完美的翻译为 Python。
jinjia2的Template
正确方法
先在网页上下载这个 GGUF 模型,并在本地目录创建一个 Modelfile 文件,在文件中配置 Template。如图。

下载注意:不要下载代 -of- 标识的(如红色所示),下载绿色的这种。

对于千问2.5而言, Modelfile 文件内容如下:
dockerfile
from Qwen2.5-7B-Instruct-1M-Q4_K_M.gguf
# 设置千问的参数
PARAMETER temperature 0.7
PARAMETER top_p 0.8
PARAMETER repeat_penalty 1.05
PARAMETER top_k 20
# 配置Template(不同模型的Template大不相同)
TEMPLATE """{{ if .Messages }}
{{- if or .System .Tools }}<|im_start|>system
{{ .System }}
{{- if .Tools }}
# Tools
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 }}<|im_end|>
{{ else if eq .Role "assistant" }}<|im_start|>assistant
{{ 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
{{ end }}
{{- end }}
{{- else }}
{{- if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>user
{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
{{ end }}{{ .Response }}{{ if .Response }}<|im_end|>{{ end }}"""
内容来源
[Ollama - Qwen](https://qwen.readthedocs.io/en/latest/run_locally/ollama.html)\]([Ollama - Qwen](https://qwen.readthedocs.io/en/latest/run_locally/ollama.html)) 但是,像这阿里种官方直接提供了使用文档的还是比较少见的。即便是强如阿里,文档的更新也十分不及时,只覆盖到了 qwen2.5。 如果找不到,可以拿着jinja2 Template 让 AI 生成一个功能一致的 Go Template。 #### 实战展示 以腾讯 `HY-MT1.5-7B-GGUF` 为例,只有官方提供的 jinja2 Template。  将 `tokenizer.chat_template` 的内容丢给 AI ,告诉AI: 根据jinja2的template写一个用于Ollama Modelfile的Go template: {% set ns = namespace(has_head=true) %}{% set loop_messages = messages %}{% for message in loop_messages %}{% set content = message['content'] %}{% if loop.index0 == 0 %}{% if content == '' %}{% set ns.has_head = false %}{% elif message['role'] == 'system' %}{% set content = '<|startoftext|>' + content + '<|extra_4|>' %}{% endif %}{% endif %}{% if message['role'] == 'user' %}{% if loop.index0 == 1 and ns.has_head %}{% set content = content + '<|extra_0|>' %}{% else %}{% set content = '<|startoftext|>' + content + '<|extra_0|>' %}{% endif %}{% elif message['role'] == 'assistant' %}{% set content = content + '<|eos|>' %}{% endif %}{{ content }}{% endfor %} AI输出 {{- /* 处理 System 逻辑 */ -}} {{- if .System -}} <|startoftext|>{{ .System }}<|extra_4|> {{- end -}} {{- /* 循环处理对话消息 */ -}} {{- range $i, $_ := .Messages -}} {{- if eq .Role "user" -}} {{- /* 如果存在 System 且当前是第一条 user 消息,省略开始标记 */ -}} {{- if and $.System (eq $i 0) -}} {{ .Content }}<|extra_0|> {{- else -}} <|startoftext|>{{ .Content }}<|extra_0|> {{- end -}} {{- else if eq .Role "assistant" -}} {{ .Content }}<|eos|> {{- end -}} {{- end -}} `Modelfile` 文件内容如下: ```dockerfile FROM ./HY-MT1.5-7B-Q4_K_M.gguf PARAMETER num_ctx 4096 PARAMETER num_batch 512 TEMPLATE """ {{- /* 处理 System 逻辑 */ -}} {{- if .System -}} <|startoftext|>{{ .System }}<|extra_4|> {{- end -}} {{- /* 循环处理对话消息 */ -}} {{- range $i, $_ := .Messages -}} {{- if eq .Role "user" -}} {{- /* 如果存在 System 且当前是第一条 user 消息,省略开始标记 */ -}} {{- if and $.System (eq $i 0) -}} {{ .Content }}<|extra_0|> {{- else -}} <|startoftext|>{{ .Content }}<|extra_0|> {{- end -}} {{- else if eq .Role "assistant" -}} {{ .Content }}<|eos|> {{- end -}} {{- end -}} """ SYSTEM """ 你是一个高效的多语言翻译引擎。自动检测输入语言并翻译。 保留原始格式(如HTML标签、字幕时间码)。 专业术语保持统一翻译。 除了输出翻译,其他的什么都不要输出。 """ ``` 效果展示 