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
Add gemma-3n chat template to chat_templates.py (#3051)
Etherllb14841facfd8
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.pyindex e7849092cb0..66d4b1c9aa4 100644--- a/unsloth/chat_templates.py+++ b/unsloth/chat_templates.py@@ -1190,6 +1190,78 @@ CHAT_TEMPLATES["qwen3"] = (qwen3_template, qwen3_template_eos_token, False, qwenDEFAULT_SYSTEM_MESSAGE["qwen3"] = None # No default system message for Qwen-3pass+# =========================================== Gemma-3n+# Obtained via+# print(tokenizer.chat_template.replace("}\n", "####").replace("\n", "\\n").replace("####", "}\n"))+gemma3n_template = \+"""{{ bos_token }}+{%- if messages[0]['role'] == 'system' -%}+ {%- if messages[0]['content'] is string -%}+ {%- set first_user_prefix = messages[0]['content'] + '\n\n' -%}+ {%- else -%}+ {%- set first_user_prefix = messages[0]['content'][0]['text'] + '\n\n' -%}+ {%- endif -%}+ {%- set loop_messages = messages[1:] -%}+{%- else -%}+ {%- set first_user_prefix = "" -%}+ {%- set loop_messages = messages -%}+{%- endif -%}+{%- for message in loop_messages -%}+ {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}+ {{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}+ {%- endif -%}+ {%- if (message['role'] == 'assistant') -%}+ {%- set role = "model" -%}+ {%- else -%}+ {%- set role = message['role'] -%}+ {%- endif -%}+ {{ '<start_of_turn>' + role + '\n' + (first_user_prefix if loop.first else "") }}+ {%- if message['content'] is string -%}+ {{ message['content'] | trim }}+ {%- elif message['content'] is iterable -%}+ {%- for item in message['content'] -%}+ {%- if item['type'] == 'audio' -%}+ {{ '<audio_soft_token>' }}+ {%- elif item['type'] == 'image' -%}+ {{ '<image_soft_token>' }}+ {%- elif item['type'] == 'text' -%}+ {{ item['text'] | trim }}+ {%- endif -%}+ {%- endfor -%}+ {%- else -%}+ {{ raise_exception("Invalid content type") }}+ {%- endif -%}+ {{ '<end_of_turn>\n' }}+{%- endfor -%}+{%- if add_generation_prompt -%}+ {{'<start_of_turn>model\n'}}+{%- endif -%}+"""++# Ollama from https://ollama.com/library/gemma3n/blobs/e0a42594d802+gemma3n_ollama = \+'''+{{- range $i, $_ := .Messages }}+{{- $last := eq (len (slice $.Messages $i)) 1 }}+{{- if or (eq .Role "user") (eq .Role "system") }}<start_of_turn>user+{{ .Content }}<end_of_turn>+{{ if $last }}<start_of_turn>model+{{ end }}+{{- else if eq .Role "assistant" }}<start_of_turn>model+{{ .Content }}{{ if not $last }}<end_of_turn>+{{ end }}+{{- end }}+{{- end }}+'''++gemma3n_template_eos_token = "<end_of_turn>"+CHAT_TEMPLATES["gemma-3n"] = (gemma3n_template, gemma3n_template_eos_token, False, gemma3n_ollama,)+DEFAULT_SYSTEM_MESSAGE["gemma-3n"] = None # No system message in Gemma-3n++CHAT_TEMPLATES["gemma3n"] = (gemma3n_template, gemma3n_template_eos_token, False, gemma3n_ollama,)+DEFAULT_SYSTEM_MESSAGE["gemma3n"] = None # No system message in Gemma-3n+pass+def _change_system_message(template: str, type_chat_template: str, system_message: str = None):system_message_pattern = r"\{system_message\}"