将 safetensors 模型转换为 GGUF,导入Ollama

Ollama 默认 pull 到的模型都是量化过的

image

 

要使用非量化的模型就需要自己导入

 

但是 Hugging Face 不用魔法是打不开的

我们找个镜像站下载模型:

比如这个吧:https://hf-mirror.com/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B

 

首先安装 git lfs

git lfs install

 

然后下载模型:

git clone https://hf-mirror.com/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B

 

 

ollama(旧版本)貌似只能直接导入GGUF格式的模型

我们下面用 llama.cpp 来转换下

 

首先下载 llama.cpp

git clone https://github.com/ggerganov/llama.cpp.git

 

进行安装:

cd llama.cpp pip install -r requirements.txt

 

看下转换语句的参数:

python convert_hf_to_gguf.py -h

image

 

 

然后开始转换:

python convert_hf_to_gguf.py ./DeepSeek-R1-Distill-Qwen-7B --outfile ./DeepSeek-R1-Distill-Qwen-7B/DeepSeek-R1-Distill-Qwen-7B.gguf --outtype f16

image

 

转换完成后,要将模型导入到 Ollama

首先建立一个 Modelfile 文件,文件内容:

FROM ./DeepSeek-R1-Distill-Qwen-7B/DeepSeek-R1-Distill-Qwen-7B.gguf

image

 

看下模型:

ollama list

image

 

看下模型详情:

ollama show deepseek-r1-qwen:7b

image

 

 

但是有个问题

你运行模型后,会发现它在胡言乱语…

image

 

这个时候我们需要改下 Modelfile 文件

复制代码
FROM ./DeepSeek-R1-Distill-Qwen-7B/DeepSeek-R1-Distill-Qwen-7B.gguf
 TEMPLATE """{{- if .System }}{{ .System }}{{ end }}
 {{- range $i, $_ := .Messages }}
 {{- $last := eq (len (slice $.Messages $i)) 1}}
 {{- if eq .Role "user" }}<|User|>{{ .Content }}
 {{- else if eq .Role "assistant" }}<|Assistant|>{{ .Content }}{{- if not $last }}<|end▁of▁sentence|>{{- end }}
 {{- end }}
 {{- if and $last (ne .Role "assistant") }}<|Assistant|>{{- end }}
 {{- end }}"""
 PARAMETER stop "<|begin▁of▁sentence|>"
 PARAMETER stop "<|end▁of▁sentence|>"
 PARAMETER stop "<|User|>"
 PARAMETER stop "<|Assistant|>"
复制代码

 

 

删除模型后,重新导入模型

image

 

再运行模型,这看起来正常多了….

image

 

再看下模型详情:

ollama show deepseek-r1-qwen:7b

image

posted @   sun8134  阅读(531)  评论(0编辑  收藏  举报
努力加载评论中...
分享按钮
点击右上角即可分享
微信分享提示