~/ / AI 文章 / 在 VPS 上跑本機 LLM / 把 Hugging Face 模型匯入 Ollama:GGUF 與 Safetensors 的逐步說明
把 Hugging Face 模型匯入 Ollama:GGUF 與 Safetensors 的逐步說明
不是每個模型都在 Ollama 模型庫裡,但許多模型都能在 Hugging Face 找到。Ollama 有兩種方式可以執行它們:用 ollama run hf.co 直接從 GGUF 儲存庫執行,或是先下載檔案,再用 Modelfile 與 ollama create 建立。本文兩種方式都會走一遍,內容只採用 Ollama 與 Hugging Face 文件以及 Ollama 原始碼所說的,並說明匯入前要先在檔案中檢查什麼。本頁有四個工具,可在瀏覽器中讀取檔案與 Modelfile。
發布: · 約 8 分鐘讀完 · 已於 2026-11-16 對照 Ollama 與 Hugging Face 的文件,以及 Ollama v0.40.16 的原始碼核對。匯入的支援會隨版本改變,使用前請先確認最新的文件。
把模型帶進來的兩種方式
如果模型是以 GGUF 發布,你可能完全不需要 Modelfile。Hugging Face 的文件指出,可以用 ollama run hf.co/{username}/{repository} 執行 GGUF 儲存庫,網域也可以用 huggingface.co。
另一種方式是 Ollama 匯入頁面所說明的:下載檔案,寫一個 FROM 那一行指向這些檔案的 Modelfile,再用 ollama create 建立模型。Safetensors 資料夾,或檔案已經在你的伺服器上時,就需要這種方式。
| 項目 | ollama run hf.co/... | Modelfile 與 ollama create |
|---|---|---|
| 需要什麼 | Hugging Face 上的 GGUF 儲存庫 | GGUF 檔案,或一個 Safetensors 權重資料夾 |
| 量化 | 儲存庫有 Q4_K_M 時用 Q4_K_M,否則由 Hub 挑選一個;加上 :{quantization} 即可自行選擇 | 檔案中本來的量化。Ollama 在匯入時不會量化 GGUF 模型 |
| 聊天範本 | 依據 GGUF 中儲存的 tokenizer.chat_template 選擇,或使用儲存庫中名為 template 的檔案 | Modelfile 中的 TEMPLATE;若省略,則使用 GGUF 中的範本 |
| 私人模型 | 把你的 Ollama 公開金鑰以 SSH 金鑰的形式加入 Hugging Face 帳號 | 下載時先登入,例如使用 hf auth login |
Hugging Face 的文件還指出,儲存庫中名為 system 的檔案會設定系統提示,名為 params 的 JSON 檔案會設定取樣參數,而且 template 檔案必須是 Go 範本,不能是 Jinja 範本。
下載檔案
Hugging Face 的命令列工具是 hf。它的指南在 macOS 與 Linux 上以 curl -LsSf https://hf.co/cli/install.sh | bash 安裝,也可以用 pip install -U "huggingface_hub" 安裝。
hf download <repository> <file> --local-dir <folder> 會把檔案放進你指定的資料夾,而不是快取。用 --include 與 --exclude 可以依樣式選擇檔案,例如 --include "*.safetensors" 可取得模型的所有權重。若是私人儲存庫,請先用 hf auth login 登入。
Safetensors 模型通常由多個檔案組成。請把 config.json 與權重一起下載,因為 Ollama 會在資料夾中尋找它;若儲存庫有 model.safetensors.index.json,也請一併下載:下方的 Safetensors Metadata Viewer 會用它找出缺少的檔案。
匯入前先檢查 GGUF
GGUF 檔案會把中繼資料與權重放在一起。GGUF 規格定義了 general.architecture 表示架構、general.file_type 表示大部分張量的類型,以及 [llm].context_length 表示模型訓練時使用的 context 長度。Ollama 的匯入頁面指出,Ollama 在匯入時不會量化 GGUF 模型,所以你在這裡看到的量化就是你將執行的量化;若要更改,請先用 llama.cpp 的 llama-quantize 之類的工具量化該檔案。
大型模型可能會分割成多個檔案。規格以從 00001 開始的編號為分割檔命名,例如 00001-of-00003。請下載全部的檔案,並保留它們的名稱。
下方的檢視器會在瀏覽器中讀取 GGUF 檔案的標頭:架構、量化、context 長度、聊天範本,以及每一筆中繼資料。
試用看看: GGUF Metadata Viewer
或把檔案拖放到這裡。只會讀取檔案的開頭,檔案不會離開你的電腦。在我們的測試中,Ollama 存放在 models/blobs 資料夾中的權重檔案也是 GGUF 檔案。
KV cache = 2 × 層數 × KV heads × head 大小 × 2 位元組 (f16) × 上下文長度。以 qwen2.5:7b 來說,4096 個 token 時為 224 MiB,這是我們的測試中 Ollama 記錄裡的大小。
權重是儲存在檔案中的張量資料;與標頭合起來就是整個檔案。執行模型還需要 KV cache 與工作記憶體,記憶體估算工具會把它們加上去。
已於 2026-11-08 對照 GGUF 規格與 llama.cpp 的讀取器檢查,並以 qwen2.5:7b 的真實檔案驗證:其中繼資料與 339 個張量與 Ollama 0.40.11 回報的內容相符。 GGUF 規格 · Ollama:匯入模型
檔案在你的瀏覽器中讀取,不會上傳。只會讀取它的開頭。
撰寫 Modelfile 並建立模型
單一檔案時,Modelfile 只需要一行:FROM /path/to/file.gguf。Modelfile 參考文件指出,路徑可以是絕對路徑,也可以是相對於 Modelfile 的路徑。分割的模型請保留原本的檔名,並使用符合所有分割檔的萬用字元,例如 FROM /path/to/model-*.gguf,或是用多行 FROM 分別列出各個分割檔。
接著在放有 Modelfile 的資料夾中執行 ollama create my-model,或用 -f 指向該檔案,再以 ollama run my-model 試用模型。PARAMETER、SYSTEM 與 LICENSE 這幾行也可以寫在同一個 Modelfile 中。
下方的檢查工具會用 ollama create 的方式讀取 Modelfile,並顯示 Ollama 會印出的錯誤與對應的行。Ollama Modelfile Generator 可以用表單產生 Modelfile。
試用看看: Ollama Modelfile Checker
請貼上整個檔案。ollama show --modelfile <model> 會印出你已有模型的 Modelfile。
ollama create 可以讀取這個 Modelfile
本工具能檢查的步驟中沒有錯誤。FROM 與檔案是否存在,只有你電腦上的 ollama create 會檢查。
注意事項
- FROM qwen2.5:7b 是模型名稱:該模型必須能被 Ollama 使用。
instruction 會變成什麼
- FROM
- qwen2.5:7b
- SYSTEM
- You are a concise assistant. Answer in one or two sentences.
- TEMPLATE
- 未設定:沿用 FROM 模型的
PARAMETER
| 名稱 | Ollama 中的型別 | 儲存的值 |
|---|---|---|
| num_ctx | int | 8192 |
| stop | string[] | ["\u003c|im_end|\u003e"] |
| temperature | float32 | 0.7 |
新模型的 parameter 層(JSON):
{"num_ctx":8192,"stop":["\u003c|im_end|\u003e"],"temperature":0.7}FROM 模型中你沒有在這裡設定的 parameter 也會保留。
MESSAGE(範例對話)
- userIs Toronto in Canada?
- assistantyes
Ollama 寫回的 Modelfile
每個 instruction 一行,不含註解,格式為 Ollama 原始碼中的 Command.String。
FROM qwen2.5:7b PARAMETER num_ctx 8192 PARAMETER temperature 0.7 PARAMETER stop <|im_end|> SYSTEM " You are a concise assistant. Answer in one or two sentences. " MESSAGE user Is Toronto in Canada? MESSAGE assistant yes
於 2026-11-09 對照 Ollama 的原始碼(v0.40.15)、其文件,以及 Ollama 0.40.11 中的 ollama create 檢查: Ollama: Modelfile Reference · parser.go
所有處理都在你的瀏覽器中執行,不會傳送到任何地方。
匯入 Safetensors 資料夾
對 Safetensors 而言,FROM 指向存放權重的資料夾;若 Modelfile 就在該資料夾中,則用 FROM .。Modelfile 參考文件指出,資料夾中應包含受支援架構的權重。文件沒有列出那些架構。
在 Ollama v0.40.16 的原始碼中,資料夾只要有 config.json 以及至少一個 .safetensors 檔案,就算是 Safetensors 模型,並會匯入給 Ollama 的 MLX 引擎使用。當 config.json 中指名的架構不是 MLX 引擎所認得的,匯入會以 unsupported MLX architecture 錯誤中止;當該引擎無法載入時,則以 MLX runtime is not available 中止。Ollama 的開發指南指出,MLX 引擎執行的是 Safetensors 模型,並且在搭載 Apple silicon 的 macOS 上預設啟用。
該版本中登錄的架構包括 LlamaForCausalLM、Qwen3ForCausalLM 與 Gemma4ForCausalLM,但並非 Hugging Face 上的每一種架構。資料夾被拒絕時,請找找同一個模型的 GGUF。我們沒有在沒有 GPU 的伺服器上嘗試過 Safetensors 匯入。
下方的檢視器會在瀏覽器中讀取 .safetensors 檔案的標頭。加入 model.safetensors.index.json 可找出缺少的分片,加入 config.json 則可看到架構。
試用看看: Safetensors Metadata Viewer
或把檔案拖放到這裡:.safetensors 檔案、model.safetensors.index.json 與 config.json。只會讀取每個 .safetensors 檔案的開頭,任何內容都不會離開你的電腦。
curl -L -r 0-1048575 -o model.safetensors https://huggingface.co/OWNER/REPO/resolve/main/model.safetensors參數數量是將檔案中每個張量的元素數(其形狀的乘積)加總而得,與 Hugging Face Hub 計算模型所顯示參數數量的方式相同。權重大小是檔案所含的資料,以檔案中的資料型別計算。
KV cache = 2 × 層數 × KV 頭數 × head 大小 × 2 位元組(f16)× 上下文長度。數值來自 config.json(num_hidden_layers、num_key_value_heads、head_dim,或 hidden_size ÷ num_attention_heads)。對於文字部分在 text_config 中的模型,工具會從那裡讀取。
已於 2026-11-09 對照下列資料與官方 safetensors 0.8.0 檢查: safetensors 格式 · Hugging Face:讀取中繼資料 · Ollama:匯入模型
一切都在你的瀏覽器中執行。只會讀取每個 .safetensors 檔案的開頭,不會傳送任何內容到任何地方。
實際執行的是哪個聊天範本
聊天模型需要把訊息包在它訓練時使用的特殊 token 中。在 Ollama 裡,這就是模型的 TEMPLATE,以 Go 的範本語法撰寫。
當 GGUF 匯入的 Modelfile 沒有 TEMPLATE 時,Ollama v0.40.16 的原始碼會把 GGUF 中的 tokenizer.chat_template 與 Ollama 內建的範本比較。若有符合的,ollama create 會印出 using autodetected template 並接著顯示其名稱,同時把它存為 TEMPLATE。若都不符合,Ollama 就執行 GGUF 本身的聊天範本。
ollama show my-model 會印出一行 template source,說明執行的是哪一個:go_template 代表 TEMPLATE,gguf_chat_template 代表 GGUF 中的範本。我們在 Ollama 0.40.11 上只用一行 FROM 匯入 qwen2.5:7b 的 GGUF 時,ollama create 沒有印出 autodetected template,而 ollama show 列出的是 gguf_chat_template,capability 為 tools 與 completion。
如果你自己撰寫 TEMPLATE,下方的測試工具會顯示它針對一段對話產生的確切文字。若要與模型原本的 Jinja 範本比較(transformers 把它放在 chat_template.jinja 或 tokenizer_config.json 中),請使用 Hugging Face Chat Template Tester。
即使有 TEMPLATE,同一段原始碼仍可能偏好使用 GGUF 中的範本,例如 GGUF 範本支援更多 capability(像是工具呼叫)時。請查看 template source 那一行,不要自行假設。
試用看看: Ollama Template Tester
ollama show --template <model> 會印出你已有模型的範本。Modelfile 中 TEMPLATE 後面三重引號之間的文字也可以使用。
與 /api/chat 相同的格式:role、content,以及可選的 thinking、tool_calls、tool_name 與 images。
與 /api/chat 中 tools 相同的格式。留空表示不使用 tools。
進階:模型的 SYSTEM 與其 GGUF 聊天範本
Modelfile 中的 SYSTEM 那一行。當第一則訊息不是 system 訊息時(在 /api/generate 中,則是 system 為空時),Ollama 會加入它。
貼上後,就能看到 Ollama 使用 TEMPLATE 還是這一個。GGUF Metadata Viewer 可以顯示它。
模型收到的內容
<|im_start|>system
You are a helpful assistant.
# 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>
{"type": "function", "function": {get_weather Get the current weather for a city {object <nil> <nil> [city] {"city":{"type":"string","description":"City name"}}}}}
</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><|im_end|>
<|im_start|>user
What is the weather in Tokyo?<|im_end|>
<|im_start|>assistant
<tool_call>
{"name": "get_weather", "arguments": {"city":"Tokyo"}}
</tool_call><|im_end|>
<|im_start|>user
<tool_response>
{"temperature": 20}
</tool_response><|im_end|>
<|im_start|>assistant
It is 20°C in Tokyo.<|im_end|>
<|im_start|>user
Thanks!<|im_end|>
<|im_start|>assistant
關於這個範本
- Ollama 的執行方式
- 整段對話放入 .Messages,執行一次
- 使用的名稱
- $, $i, $last, arguments, content, function, messages, name, prompt, response, role, system, toolcalls, tools
- 支援的功能(就 Ollama 而言)
- tools
用你自己的 Ollama 驗證
請將 MODEL 換成你的模型名稱。_debug_render_only 是 Ollama 原始碼中的除錯選項,不在 API 文件中,因此可能會改變。提示詞會在回應的 _debug_info.rendered_template 中。
curl -s http://localhost:11434/api/chat -d '{"model":"MODEL","messages":[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"What is the weather in Tokyo?"},{"role":"assistant","content":"","tool_calls":[{"function":{"name":"get_weather","arguments":{"city":"Tokyo"}}}]},{"role":"tool","content":"{\"temperature\": 20}","tool_name":"get_weather"},{"role":"assistant","content":"It is 20°C in Tokyo."},{"role":"user","content":"Thanks!"}],"tools":[{"type":"function","function":{"name":"get_weather","description":"Get the current weather for a city","parameters":{"type":"object","properties":{"city":{"type":"string","description":"City name"}},"required":["city"]}}}],"stream":false,"_debug_render_only":true}'Modelfile 中有 RENDERER 或 PARSER 那一行的模型,以及 gpt-oss,使用的是 Ollama 內建的 renderer,而不是 TEMPLATE。
當對話比上下文(num_ctx)還長時,Ollama 會捨棄最舊的訊息,但保留 system 訊息與最後一則訊息。本工具不計算 token,因此一律顯示完整對話。
對於 qwen3 模型與 deepseek-r1,Ollama 會從最後一則 user 訊息之前的 assistant 訊息中移除 <think>…</think>。本工具不會。
印出整則訊息或 {{ .Messages }} 時,Ollama 會顯示記憶體位址(0xc…),而且每次都不同。本工具則顯示佔位符。
currentDate 與 yesterdayDate 使用你電腦上今天的日期。Ollama 使用伺服器上的日期。
於 2026-11-09 對照 Ollama 的原始碼(v0.40.15)、Go 的 text/template 與 Ollama 0.40.11 檢查: Ollama: Template · template.go
一切都在你的瀏覽器中執行,不會傳送到任何地方。
使用模型之前
- 執行 ollama show my-model,檢查架構、量化、context 長度、capability 與 template source。
- 請在 Hugging Face 的模型頁面檢查授權。Modelfile 中的 LICENSE 那一行會把授權與模型一起記錄下來。
- 匯入的支援取決於 Ollama 的版本。請查看你所執行版本的匯入頁面與 Modelfile 參考文件。
來源
- Ollama documentation: Importing a Model
- Ollama documentation: Modelfile Reference
- Hugging Face Hub: Use Ollama with any GGUF Model on Hugging Face Hub
- Hugging Face Hub: Command Line Interface (CLI)
- Hugging Face Hub: GGUF
- ggml: GGUF specification (docs/gguf.md)
- Transformers: Chat templates
- Ollama v0.40.16 source: server/model.go (template detection)
- Ollama v0.40.16 source: server/images.go (template selection)
- Ollama v0.40.16 source: create/create.go (Safetensors folders)
- Ollama v0.40.16 source: create/verify.go (MLX architecture check)
- Ollama v0.40.16: docs/development.md (MLX engine)
接著閱讀
- 用 pgvector 在 PostgreSQL 中儲存並搜尋 Ollama 的 embedding用 pgvector 把 Ollama 的 embedding 存進 PostgreSQL,以 HNSW 或 IVFFlat 搜尋,再把結果傳給 /api/chat,讓回答引用它的來源。
- 用 Ollama 產生 embedding:搜尋與 RAG 的基礎用 Ollama 的 /api/embed 產生 embedding:模型與大小、truncate 與 dimensions、單位長度向量與餘弦相似度,依官方文件說明。
- 在小型 VPS 上監看 Ollama 的記憶體使用查看 Ollama 在記憶體中載入了什麼、保留多久,以及哪些設定會改變記憶體用量:ollama ps、keep_alive 與各項上限,內容出自官方 FAQ。
- 在自己的 VPS 上將 Dify 連接到 Ollama在自己的 VPS 上執行 Dify,並以 Ollama 作為模型:依官方文件說明安裝、外掛欄位、Docker 位址問題與 embedding。