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

效果展示

相关推荐
XMAIPC_Robot1 分钟前
RK3588四路1080P工业MIPI相机视觉网关,同时支持RK3588+FPGA的24路相机同步方案
人工智能·数码相机
决战灬2 分钟前
langgraph之interrupt(事例篇)
人工智能·python·agent
czzxxxxxx3 分钟前
创客匠人AI智能体:知识付费从“重人力”走向“自动化”的拐点
大数据·人工智能
依然范特东8 分钟前
强化学习笔记2--bellman equation
人工智能·笔记
张申傲12 分钟前
拆解 harness9(9):Observability 可观测性
人工智能·aigc·agent·deepseek·harness
ZKNOW甄知科技16 分钟前
燕千云深度集成飞书:以AI之力,开启无感IT运维体验
大数据·运维·网络·数据库·人工智能·低代码·集成学习
京和动物医院·总院20 分钟前
2026年未央区宠物医院:如何挑选最适合您爱宠的健康守护者
大数据·人工智能·python
液态不合群21 分钟前
AI×低代码工作流:破解制造业数字化流程异构困局
人工智能·低代码
武子康21 分钟前
Copilot Code Review 从固定 Reviewer 演进为可编程 Runtime,仓库控制面、Setup 供应链、Runner 资源和 MCP 工具同时被纳入审查决策
人工智能·github·aigc
吠品22 分钟前
五子棋AI对战功能实现与多难度策略设计
人工智能