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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
get_chat_template: write the chosen system message into the Ollama Modelfile (#16863)
Mohammad Hijjawi5249097dc765
diff --git a/tests/saving/test_ollama_modelfile_trained_template.py b/tests/saving/test_ollama_modelfile_trained_template.pyindex 1f50ccddef2..1bc97757ae6 100644--- a/tests/saving/test_ollama_modelfile_trained_template.py+++ b/tests/saving/test_ollama_modelfile_trained_template.py@@ -1,11 +1,13 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.+import pytestfrom datasets import Datasetfrom tokenizers import Tokenizer, modelsfrom transformers import CLIPImageProcessor, LlavaProcessor, PreTrainedTokenizerFastfrom unsloth.chat_templates import apply_chat_template, get_chat_template+from unsloth.ollama_template_mappers import OLLAMA_TEMPLATESfrom unsloth.save import create_ollama_modelfileALPACA = """Below are some instructions that describe some tasks. Write responses that appropriately complete each request.@@ -90,3 +92,149 @@ def test_processor_template_survives_the_gguf_unwrap():assert modelfile is not Noneassert "### Instruction:" in modelfile+++def test_system_message_passed_to_get_chat_template_reaches_the_modelfile():+ tokenizer = get_chat_template(+ _tokenizer("some/model"), chat_template = "alpaca", system_message = "You are a pirate."+ )++ modelfile = create_ollama_modelfile(tokenizer, "some/model", "x.gguf")++ assert modelfile.endswith('SYSTEM """You are a pirate."""')+ assert modelfile.count("SYSTEM ") == 1+ assert "Below are some instructions" not in modelfile+++def test_system_message_replaces_the_default_of_a_mapped_model():+ name = "unsloth/Qwen2.5-0.5B-Instruct"+ tokenizer = get_chat_template(+ _tokenizer(name), chat_template = "qwen-2.5", system_message = "Be brief."+ )++ modelfile = create_ollama_modelfile(tokenizer, name, "x.gguf")++ assert modelfile.endswith('SYSTEM """Be brief."""')+ assert "You are Qwen" not in modelfile+ assert "<|im_start|>system" in modelfile+++def test_qwen_tool_prompt_uses_the_same_system_message_as_the_modelfile():+ name = "unsloth/Qwen2.5-0.5B-Instruct"+ tools = [{"type": "function", "function": {"name": "f", "parameters": {}}}]+ tokenizer = get_chat_template(+ _tokenizer(name), chat_template = "qwen-2.5", system_message = "Be brief."+ )+ stock = get_chat_template(_tokenizer(name), chat_template = "qwen-2.5")++ prompt = tokenizer.apply_chat_template(CONVERSATION, tools = tools, tokenize = False)++ assert prompt.startswith("<|im_start|>system\nBe brief.\n\n# Tools")+ assert create_ollama_modelfile(tokenizer, name, "x.gguf").endswith('SYSTEM """Be brief."""')+ assert stock.apply_chat_template(CONVERSATION, tools = tools, tokenize = False).startswith(+ "<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.\n\n"+ )+++def test_system_message_is_added_when_the_modelfile_has_none():+ tokenizer = get_chat_template(+ _tokenizer("some/model"), chat_template = "llama-3.1", system_message = "Be brief."+ )++ modelfile = create_ollama_modelfile(tokenizer, "some/model", "x.gguf")++ assert modelfile.endswith('PARAMETER min_p 0.1\nSYSTEM """Be brief."""')+ assert modelfile.count("SYSTEM ") == 1+++def test_empty_system_message_drops_the_default_from_the_modelfile():+ tokenizer = get_chat_template(+ _tokenizer("some/model"), chat_template = "unsloth", system_message = ""+ )++ modelfile = create_ollama_modelfile(tokenizer, "some/model", "x.gguf")++ assert "SYSTEM " not in modelfile+ assert ">>> Assistant: {{ .Response }}" in modelfile+++def test_default_system_message_leaves_the_stock_modelfile():+ tokenizer = get_chat_template(_tokenizer("some/model"), chat_template = "alpaca")++ modelfile = create_ollama_modelfile(tokenizer, "some/model", "x.gguf")++ assert tokenizer._ollama_modelfile == OLLAMA_TEMPLATES["alpaca"]+ assert tokenizer._ollama_system_message is None+ assert modelfile.endswith(+ 'SYSTEM """Below are some instructions that describe some tasks. '+ 'Write responses that appropriately complete each request."""'+ )+++def test_system_message_without_a_template_default_leaves_the_modelfile():+ # chatml has no default, so get_chat_template only warns and the Jinja template ignores it.+ tokenizer = get_chat_template(+ _tokenizer("some/model"), chat_template = "chatml", system_message = "Be brief."+ )+ stock = get_chat_template(_tokenizer("some/model"), chat_template = "chatml")++ modelfile = create_ollama_modelfile(tokenizer, "some/model", "x.gguf")++ assert modelfile == create_ollama_modelfile(stock, "some/model", "x.gguf")+++def test_system_message_keeps_the_mapped_template_choice():+ # qwen-2.5 and qwen-2.5-coder train the same; the mapper picks the coder Modelfile, which adds FIM.+ name = "unsloth/Qwen2.5-Coder-7B-Instruct"+ tokenizer = get_chat_template(+ _tokenizer(name), chat_template = "qwen-2.5", system_message = "Be brief."+ )++ modelfile = create_ollama_modelfile(tokenizer, name, "x.gguf")++ assert "<|fim_prefix|>" in modelfile+ assert modelfile.endswith('SYSTEM """Be brief."""')+ assert modelfile.count("SYSTEM ") == 1+++def test_system_message_reaches_the_modelfile_of_a_processor():+ processor = LlavaProcessor(+ image_processor = CLIPImageProcessor(), tokenizer = _tokenizer("some/vlm"), patch_size = 14+ )+ processor = get_chat_template(processor, chat_template = "alpaca", system_message = "Be brief.")++ modelfile = create_ollama_modelfile(processor.tokenizer, "some/vlm", "x.gguf")++ assert modelfile.endswith('SYSTEM """Be brief."""')+++def test_later_get_chat_template_without_system_message_restores_the_default():+ tokenizer = get_chat_template(+ _tokenizer("some/model"), chat_template = "alpaca", system_message = "Be brief."+ )+ tokenizer = get_chat_template(tokenizer, chat_template = "alpaca")++ modelfile = create_ollama_modelfile(tokenizer, "some/model", "x.gguf")++ assert "Be brief." not in modelfile+ assert 'SYSTEM """Below are some instructions' in modelfile+++def test_system_message_is_written_verbatim():+ message = 'Say "hi" and use {braces}, {__EOS_TOKEN__}, \\1 and """ freely.'+ tokenizer = get_chat_template(+ _tokenizer("some/model"), chat_template = "alpaca", system_message = message+ )++ modelfile = create_ollama_modelfile(tokenizer, "some/model", "x.gguf")++ assert modelfile.endswith(f'SYSTEM """{message}"""')+++def test_system_message_that_ollama_would_cut_short_is_rejected():+ tokenizer = get_chat_template(+ _tokenizer("some/model"), chat_template = "alpaca", system_message = 'Use """ \nfor code.'+ )++ with pytest.raises(ValueError, match = "Ollama Modelfile"):+ create_ollama_modelfile(tokenizer, "some/model", "x.gguf")diff --git a/tests/test_get_chat_template_processor.py b/tests/test_get_chat_template_processor.pyindex 5d8a9677b81..6fe2bcf7ee9 100644--- a/tests/test_get_chat_template_processor.py+++ b/tests/test_get_chat_template_processor.py@@ -122,6 +122,7 @@ def _reattach(processor, tokenizer):tokenizer = tokenizer,chat_template = "TEMPLATE",ollama_modelfile = "MODELFILE",+ ollama_system_message = None,)diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.pyindex 5de363f73c2..56b379d0f8e 100644--- a/unsloth/chat_templates.py+++ b/unsloth/chat_templates.py@@ -2894,6 +2894,8 @@ def get_chat_template("{%- endif %}" + \chat_template+ # Only templates that bake the message in pass it on; applied after create_ollama_modelfile picks the template.+ ollama_system_message = system_message if "{system_message}" in chat_template else Nonechat_template, system_message = _change_system_message(chat_template, type_chat_template, system_message)tokenizer.chat_template = chat_template@@ -2917,6 +2919,7 @@ def get_chat_template(if _processor is not None:# GGUF export unwraps the processor before it builds the Ollama Modelfile.tokenizer._ollama_modelfile = ollama_modelfile+ tokenizer._ollama_system_message = ollama_system_message_processor.tokenizer = tokenizer_processor.chat_template = chat_templatefor _token in ("bos_token", "eos_token", "pad_token",):@@ -2930,6 +2933,7 @@ def get_chat_template(tokenizer = patch_saving_functions(tokenizer, vision = _processor is not None)tokenizer._ollama_modelfile = ollama_modelfile+ tokenizer._ollama_system_message = ollama_system_messagetokenizer._system_message = system_messagetokenizer._unsloth_chat_template_name = ((template_name, chat_template) if template_name is not None else None@@ -3645,11 +3649,14 @@ extra_eos_tokens = None,tokenizer.chat_template = jinja_templatetokenizer._ollama_modelfile = modelfile+ # This Modelfile already holds default_system_message; drop one left by an earlier get_chat_template.+ tokenizer._ollama_system_message = Nonetokenizer._unsloth_input_part = input_parttokenizer._unsloth_output_part = output_partif hasattr(tokenizer, "tokenizer"):tokenizer.tokenizer.chat_template = jinja_templatetokenizer.tokenizer._ollama_modelfile = modelfile+ tokenizer.tokenizer._ollama_system_message = Nonetokenizer.tokenizer._unsloth_input_part = input_parttokenizer.tokenizer._unsloth_output_part = output_partdiff --git a/unsloth/save.py b/unsloth/save.pyindex ea3addd93ec..3cb5c3a48f8 100644--- a/unsloth/save.py+++ b/unsloth/save.py@@ -3608,6 +3608,23 @@ def fix_tokenizer_bos_token(tokenizer):return fix_bos_token, chat_template+_OLLAMA_SYSTEM_RE = re.compile(r'^SYSTEM """.*?"""[ \t]*\n?', flags = re.DOTALL | re.MULTILINE)+# Ollama ends a """ value at the first line break preceded by """, so such a message would be cut short.+_OLLAMA_SYSTEM_CUT_RE = re.compile(r'"""\s*\n')+++def _set_ollama_system_message(modelfile, system_message):+ if _OLLAMA_SYSTEM_CUT_RE.search(system_message):+ raise ValueError(+ 'Unsloth: The system message has a line ending in """, which an Ollama Modelfile cannot hold.'+ )+ system_line = f'SYSTEM """{system_message}"""\n' if system_message != "" else ""+ modelfile, n_found = _OLLAMA_SYSTEM_RE.subn(lambda _: system_line, modelfile, count = 1)+ if n_found == 0 and system_line:+ modelfile = modelfile.rstrip("\n") + "\n" + system_line+ return modelfile++def create_ollama_modelfile(tokenizer, base_model_name, model_location):"""Creates an Ollama Modelfile.@@ -3651,9 +3668,14 @@ def create_ollama_modelfile(tokenizer, base_model_name, model_location):__FILE_LOCATION__ = model_location,)- modelfile = modelfile.replace("⚫@✅#🦥", "{").replace("⚡@🦥#⛵", "}").rstrip()+ modelfile = modelfile.replace("⚫@✅#🦥", "{").replace("⚡@🦥#⛵", "}")- return modelfile+ # Applied after formatting, so the message is written verbatim.+ system_message = getattr(tokenizer, "_ollama_system_message", None)+ if system_message is not None:+ modelfile = _set_ollama_system_message(modelfile, system_message)++ return modelfile.rstrip()def create_ollama_model(username: str, model_name: str, tag: str, modelfile_path: str):