Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Write the Ollama Modelfile from the trained chat template (#12311)

Nilay31f8b04f4f0a
diff --git a/tests/saving/test_ollama_modelfile_trained_template.py b/tests/saving/test_ollama_modelfile_trained_template.py
new file mode 100644
index 00000000000..1f50ccddef2
--- /dev/null
+++ b/tests/saving/test_ollama_modelfile_trained_template.py
@@ -0,0 +1,92 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+from datasets import Dataset
+from tokenizers import Tokenizer, models
+from transformers import CLIPImageProcessor, LlavaProcessor, PreTrainedTokenizerFast
+
+from unsloth.chat_templates import apply_chat_template, get_chat_template
+from unsloth.save import create_ollama_modelfile
+
+ALPACA = """Below are some instructions that describe some tasks. Write responses that appropriately complete each request.
+
+### Instruction:
+{INPUT}
+
+### Response:
+{OUTPUT}"""
+
+CONVERSATION = [{"role": "user", "content": "Hi"}, {"role": "assistant", "content": "Hello"}]
+CONVERSATIONS = Dataset.from_dict({"conversations": [CONVERSATION]})
+
+
+def _tokenizer(name):
+ vocab = {"<|begin_of_text|>": 0, "<|end_of_text|>": 1, "[UNK]": 2}
+ tokenizer = PreTrainedTokenizerFast(
+ tokenizer_object = Tokenizer(models.WordLevel(vocab, unk_token = "[UNK]")),
+ bos_token = "<|begin_of_text|>",
+ eos_token = "<|end_of_text|>",
+ unk_token = "[UNK]",
+ )
+ tokenizer.name_or_path = name
+ return tokenizer
+
+
+def test_base_model_gets_the_trained_template():
+ tokenizer = _tokenizer("unsloth/llama-3-8b-bnb-4bit")
+ apply_chat_template(CONVERSATIONS, tokenizer = tokenizer, chat_template = ALPACA)
+
+ modelfile = create_ollama_modelfile(tokenizer, "unsloth/llama-3-8b", "x.gguf")
+
+ assert modelfile is not None
+ assert modelfile.startswith("FROM x.gguf\n")
+ assert "### Instruction:\n{{ .Prompt }}" in modelfile
+ assert "### Response:\n{{ .Response }}<|end_of_text|>" in modelfile
+ assert "__FILE_LOCATION__" not in modelfile
+
+
+def test_instruct_model_trained_on_a_custom_template_keeps_it():
+ tokenizer = _tokenizer("unsloth/llama-3-8b-Instruct")
+ apply_chat_template(CONVERSATIONS, tokenizer = tokenizer, chat_template = ALPACA)
+
+ modelfile = create_ollama_modelfile(tokenizer, "unsloth/llama-3-8b-Instruct", "x.gguf")
+
+ assert "### Instruction:" in modelfile
+ assert "<|start_header_id|>" not in modelfile
+
+
+def test_stock_template_picked_after_training_leaves_the_mapped_template():
+ tokenizer = _tokenizer("unsloth/llama-3-8b-Instruct")
+ apply_chat_template(CONVERSATIONS, tokenizer = tokenizer, chat_template = ALPACA)
+ tokenizer = get_chat_template(tokenizer, chat_template = "alpaca")
+
+ modelfile = create_ollama_modelfile(tokenizer, "unsloth/llama-3-8b-Instruct", "x.gguf")
+
+ assert "<|start_header_id|>" in modelfile
+ assert "### Instruction:" not in modelfile
+
+
+def test_instruct_model_without_a_custom_template_uses_its_own():
+ tokenizer = _tokenizer("unsloth/llama-3-8b-Instruct")
+
+ modelfile = create_ollama_modelfile(tokenizer, "unsloth/llama-3-8b-Instruct", "x.gguf")
+
+ assert "FROM x.gguf\n" in modelfile
+ assert "<|start_header_id|>" in modelfile
+ assert getattr(tokenizer, "_ollama_modelfile", None) is None
+
+
+def test_unmapped_model_without_a_template_has_no_modelfile():
+ assert create_ollama_modelfile(_tokenizer("some/model"), "some/model", "x.gguf") is None
+
+
+def test_processor_template_survives_the_gguf_unwrap():
+ processor = LlavaProcessor(
+ image_processor = CLIPImageProcessor(), tokenizer = _tokenizer("some/vlm"), patch_size = 14
+ )
+ processor = get_chat_template(processor, chat_template = "alpaca")
+
+ modelfile = create_ollama_modelfile(processor.tokenizer, "some/vlm", "x.gguf")
+
+ assert modelfile is not None
+ assert "### Instruction:" in modelfile
diff --git a/tests/test_get_chat_template_processor.py b/tests/test_get_chat_template_processor.py
index 7e0e3447356..cbfcafa59ae 100644
--- a/tests/test_get_chat_template_processor.py
+++ b/tests/test_get_chat_template_processor.py
@@ -140,6 +140,7 @@ def _reattach(processor, tokenizer):
_processor = processor,
tokenizer = tokenizer,
chat_template = "TEMPLATE",
+ ollama_modelfile = "MODELFILE",
)
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index c297f89237c..33d794184d6 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -2144,6 +2144,8 @@ def get_chat_template(
# tokenizer and remapped eos. The loader mirrors these onto the processor
# (models/vision.py), so refresh them here or that copy goes stale.
if _processor is not None:
+ # GGUF export unwraps the processor before it builds the Ollama Modelfile.
+ tokenizer._ollama_modelfile = ollama_modelfile
_processor.tokenizer = tokenizer
_processor.chat_template = chat_template
for _token in ("bos_token", "eos_token", "pad_token",):
diff --git a/unsloth/save.py b/unsloth/save.py
index 9b147872e21..f8fc914810a 100644
--- a/unsloth/save.py
+++ b/unsloth/save.py
@@ -2780,19 +2780,15 @@ def create_ollama_modelfile(tokenizer, base_model_name, model_location):
Creates an Ollama Modelfile.
Use ollama.create(model = "new_ollama_model", modelfile = modelfile)
"""
- ollama_template_name = MODEL_TO_OLLAMA_TEMPLATE_MAPPER.get(base_model_name)
- if not ollama_template_name:
- print(
- f"Unsloth: No Ollama template mapping found for model '{base_model_name}'. Skipping Ollama Modelfile"
- )
- return None
- ollama_modelfile = OLLAMA_TEMPLATES.get(ollama_template_name)
+ ollama_modelfile = getattr(tokenizer, "_ollama_modelfile", None)
+ if not ollama_modelfile or ollama_modelfile in OLLAMA_TEMPLATES.values():
+ ollama_template_name = MODEL_TO_OLLAMA_TEMPLATE_MAPPER.get(base_model_name)
+ ollama_modelfile = OLLAMA_TEMPLATES.get(ollama_template_name) or ollama_modelfile
if not ollama_modelfile:
print(
f"Unsloth: No Ollama template mapping found for model '{base_model_name}'. Skipping Ollama Modelfile"
)
return None
- tokenizer._ollama_modelfile = ollama_modelfile
modelfile = ollama_modelfile
FILE_LOCATION_REPLACER = "⚫@✅#🦥__FILE_LOCATION__⚡@🦥#⛵"