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标签、字幕时间码)。
专业术语保持统一翻译。
除了输出翻译,其他的什么都不要输出。
"""

效果展示

相关推荐
Henry-SAP16 分钟前
AI与机器人信息新闻
人工智能·云原生·sap·erp
天远数科23 分钟前
零信任架构实战:基于天远二手车VIN估值构建自动化汽车数据网关
人工智能·架构·自动化·汽车
AI码农小姐姐26 分钟前
小说导入AI漫剧赚钱教程:知漫剧批量生成与选型
人工智能
hh95027 分钟前
Agent Plan x DeepSeek Harness — Token 预算管理与成本追踪体系技术
人工智能·学习·adg·火山引擎·adg成都社区
水獭比特40 分钟前
工具都批准了,为什么还不能执行?给 Agent 补上第二道校验
javascript·人工智能·node.js
小小帅呀1 小时前
学习 VLA 第 2 天:深度学习基础
人工智能·深度学习·学习
科技小E1 小时前
国标GB28181视频平台EasyGBS监控为什么会“瞎”:视频质量诊断EasyVQD如何揪出黑屏卡顿偏色
大数据·人工智能·音视频
仙女修炼史1 小时前
ultralytics-yolov8的数据增强
人工智能·opencv·yolo
2601_967097221 小时前
接待机器人推荐:2026年主流品牌多场景选型指南
人工智能