ollama导入huggingface下载的大模型并量化

1. 导入GGUF 类型的模型

1.1 先在huggingface 下载需要ollama部署的大模型
1.2 编写modelfile

在ollama 里面输入

sh 复制代码
ollama show --modelfile <你有的模型名称>
eg:
ollama show --modelfile qwen2.5:latest

修改其中的from 路径为自己的模型下载路径

modelfile 复制代码
FROM /Users/lzx/AI/llm/lmstudio-community/Llama-3-Groq-8B-Tool-Use-GGUF
TEMPLATE """{{- if .Messages }}
{{- if or .System .Tools }}<|im_start|>system
{{- if .System }}
{{ .System }}
{{- end }}
{{- if .Tools }}

# Tools

You may call one or more functions to assist with the user query.

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 }}"""
SYSTEM You are watt-tool-8B, a helpful assistant with tool calling capabilities.
复制代码
ollama create Llama-3-Groq-8B-Tool -f config.modelfile

执行创建就完成了

2.导入 safetensors 模型导入Ollama

2.1 先在huggingface 下载需要ollama部署的大模型
这里以llama-3为例子,下载所有的文件,或者使用git 拉代码库,需要git lfs
2.2 安装llama.cpp
git 复制代码
git clone https://github.com/ggerganov/llama.cpp.git
pip install -r requirements.txt
2.3 格式转换

在llama.cpp的安装目录下,执行

python 复制代码
python convert_hf_to_gguf.py "<你下载的模型路径>" --outtype f16 --verbose --outfile "<你要输出的文件位置>"
2.4 然后他就转换为GGUF格式了,就按GGUF格式的来就好啦

3.ollama量化

我发现ollama pull下来的模型大小比直接从huggingface下载下来的SAFE类型小很多,于是去ollama看了下大模型量化
git 复制代码
https://github.com/ollama/ollama/blob/b51e0f397ced70bbfa7f22e9b3c94953967cb8e5/ml/backend/ggml/ggml/src/ggml-quants.h#L17
这个里面是一些常见的量化支持,这里我们选择的是q4_K_M
执行创建命令的时候带上
sh 复制代码
ollama create --quantize q4_K_M <你要在ollama中显示的模型名称> -f Modelfile
相关推荐
大刘讲IT5 小时前
面向中小企业的企业AI Agent未来3年构建蓝图规划
人工智能·经验分享·ai·开源·制造
是开心的栗子呀5 小时前
阿里云天池:预测二手车交易价格的机器学习项目-高效实现MAE低于500分
人工智能·机器学习·阿里云·ai·云计算
高冷滴互联网农民工8 小时前
Win11 系统 Trae 无法运行问题
windows·ai·ai编程
冷yan~10 小时前
GPT-5.2 深度技术解析:OpenAI 最强模型的架构与性能突破
java·gpt·ai·架构·ai编程
Lululaurel12 小时前
AI编程文本挖掘提示词实战
人工智能·python·机器学习·ai·ai编程·提示词
乐鑫科技 Espressif14 小时前
乐鑫私有化智能体平台介绍与应用
ai·语言模型·iot·乐鑫科技
爱笑的眼睛1114 小时前
强化学习组件:超越Hello World的架构级思考与实践
java·人工智能·python·ai
爱笑的眼睛1114 小时前
深入解析PyTorch nn模块:超越基础模型构建的高级技巧与实践
java·人工智能·python·ai
阿杰学AI15 小时前
AI核心知识52——大语言模型之Model Quantization(简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·aigc·模型量化·ai-native
阿杰学AI15 小时前
AI核心知识51——大语言模型之LLM Inference(简洁且通俗易懂版)
人工智能·ai·语言模型·aigc·模型推理·大语言模型推理·llm inference