OLLAMA加载本地GGUF模型教程:以Qwen2.5-0.5B模型为例
OLLAMA加载本地GGUF模型教程:以Qwen2.5-0.5B模型为例
本文是一篇针对Windows环境的技术教程,详细介绍了如何在内网环境下使用OLLAMA加载本地GGUF模型。文章以Qwen2.5-0.5B模型为例,从模型下载到最终运行的全过程进行了详细的步骤说明。
一、网络问题导致无法直接OLLAMA拉取模型
准备在办公机下个小模型跑跑,初步拟定仍然使用OLLAMA,毕竟可以直接使用CPU跑模型。
OLLAMA正常安装后,因为内网环境,无法直接通过ollama run xxx指令下载模型。
报错:Error: pull model manifest: Get “https://registry.ollama.ai/v2/library/qwen2.5/manifests/latest”: dial tcp 172.67.182.229:443: i/o timeout
只能通过modelscope等其他网站下载。(例:https://modelscope.cn/models/second-state/Qwen2.5-0.5B-Instruct-GGUF/files)
二、下载GGUF版本模型
经过查询,为直接适配ollama,优选下载gguf版本的模型。(非gguf需要llama.cpp转化,有点复杂先放弃了×)
modelscope下载方式网站有指示,按照操作即可:
pip install modelscope
modelscope download --model second-state/Qwen2.5-0.5B-Instruct-GGUF
哪怕是0.5B的模型实际上也有5G资源?闭着眼睛全下,应该是下多了QWQ啥也不会就是勇。
(注:Modelfile不是下载的,下一步会说。)
三、创建Modelfile文档并使用,运行大模型
按千问的文档指示创建Modelfile文档:
https://qwen.readthedocs.io/zh-cn/latest/run_locally/ollama.html
文件名:Modelfile
存储路径:我把它和模型放在了一个文件夹中(OllamaModels存储路径\hub\Qwen\Qwen2___5-0___5B-Instruct-GGUF)
文件内容:(注意,第一行的名称需改成和下载模型的文件名一致。其他内容暂时无脑抄示例。)
FROM qwen2.5-0.5b-instruct-q5_0.gguf
# set the temperature to 1 [higher is more creative, lower is more coherent]
PARAMETER temperature 0.7
PARAMETER top_p 0.8
PARAMETER repeat_penalty 1.05
PARAMETER top_k 20
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 }}"""
# set the system message
SYSTEM """You are Qwen, created by Alibaba Cloud. You are a helpful assistant."""
随后在该路径下运行:(别像我一样直接命名模型名称为model-name了……尴尬。)
ollama create 模型名称 -f Modelfile
这时再用ollama list即可查询到本地的模型。
然后就可以开启ollama,使用指令开始玩耍大模型啦!
ollama run 模型名称
四、碎碎念后记
发现0.5B的小模型+CPU在这种办公机上还挺快,惊喜……但内网下载限速真的是要了命了,还老断线,得重复刷刷刷。
工作迫使我进步,即使我只是个虚假的技术工(×)真实的杂工(√)
被迫赶鸭子上架哈哈哈哈哈。后面也许还会不定时有AI相关的尝试。
2024.10.23(程序员节之前w)
12.31补,把GGUF写错了现在才发现【摔】