Ollama+其他模型仓库

Ollama官方仓库的问题

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)

但是,像这阿里种官方直接提供了使用文档的还是比较少见的。即便是强如阿里,文档的更新也十分不及时,只覆盖到了 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标签、字幕时间码)。
专业术语保持统一翻译。
除了输出翻译,其他的什么都不要输出。
"""

效果展示

相关推荐
tianxiaxue117 小时前
企微如何使用AI生成推荐话术?
人工智能·企业微信
团象科技17 小时前
梳理中小出海独立站落地阶段关于WordPress 海外主机的实操参考路径
人工智能·深度学习
朴马丁17 小时前
构建日化数字创新平台:PLM如何融合AI、物联网数据,驱动智能研发与精准营销
人工智能·物联网·流程行业plm·日化行业
我不介意孤独17 小时前
04-记忆系统为什么向量数据库不够用
数据库·人工智能·资源隔离·agent infra
小程故事多_8017 小时前
从人工编写到自主迭代进化,SkillEvolver重构大模型智能体技能生成新范式
人工智能·重构
wengad17 小时前
机器学习实践理论基础|算法、模型和数据集
人工智能·算法·机器学习
kishu_iOS&AI17 小时前
LLM —— Prompt提示词工程
人工智能·prompt
li-xun17 小时前
2026年6月7日博客精选
人工智能·chatgpt·每日阅读
人工智能AI技术18 小时前
【VibeCoding系列教程12】 AI代码编辑器
人工智能
zhangfeng113318 小时前
ai训练 顿悟“总数据量是 m²,训练所需要的数据量是 log m
人工智能