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.

Phi-4 (#1523)

Daniel Han6cbfca8c63da
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index da10f7e0036..d8dc385223a 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -890,6 +890,46 @@ CHAT_TEMPLATES["qwen2.5"] = (qwen25_template, qwen25_template_eos_token, False,
DEFAULT_SYSTEM_MESSAGE["qwen2.5"] = qwen25_default_system_message # No system message in Qwen 2.5
pass
+# =========================================== Phi-4
+# "{{ bos_token }}"\ # Phi-4 removes BOS?
+phi4_template = \
+ "{% for message in messages %}"\
+ "{% if (message['role'] == 'system') %}"\
+ "{{'<|im_start|>system<|im_sep|>' + message['content'] + '<|im_end|>'}}"\
+ "{% elif (message['role'] == 'user') %}"\
+ "{{'<|im_start|>user<|im_sep|>' + message['content'] + '<|im_end|>'}}"\
+ "{% elif (message['role'] == 'assistant') %}"\
+ "{{'<|im_start|>assistant<|im_sep|>' + message['content'] + '<|im_end|>'}}"\
+ "{% endif %}"\
+ "{% endfor %}"\
+ "{% if add_generation_prompt %}"\
+ "{{ '<|im_start|>assistant<|im_sep|>' }}"\
+ "{% endif %}"
+pass
+
+_phi4_ollama_template = \
+ "{{ if .System }}<|im_start|><|system|><|im_sep|>{{ .System }}<|im_end|>{{ end }}"\
+ "{{ if .Prompt }}<|im_start|><|user|><|im_sep|>{{ .Prompt }}<|im_end|>{{ end }}"\
+ "<|im_start|><|assistant|><|im_sep|>{{ .Response }}<|im_end|>"
+
+# Ollama from https://www.ollama.com/library/phi4 is different
+phi4_ollama = \
+f'''
+FROM {{__FILE_LOCATION__}}
+TEMPLATE """{_phi4_ollama_template}"""
+PARAMETER stop "<|im_end|>"
+PARAMETER stop "<|im_start|>"
+PARAMETER stop "<|im_sep|>"
+PARAMETER temperature 1.5
+PARAMETER min_p 0.1
+'''
+
+phi4_template_eos_token = "<|im_end|>"
+CHAT_TEMPLATES["phi-4"] = (phi4_template, phi4_template_eos_token, False, phi4_ollama,)
+DEFAULT_SYSTEM_MESSAGE["phi-4"] = None # No system message in Phi-4
+pass
+
+
def _change_system_message(template: str, type_chat_template: str, system_message: str = None):
system_message_pattern = r"\{system_message\}"
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 86adc0e6344..a93f18cd412 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-__version__ = "2025.1.1"
+__version__ = "2025.1.2"
__all__ = [
"SUPPORTS_BFLOAT16",
diff --git a/unsloth/models/mapper.py b/unsloth/models/mapper.py
index 41f7444643d..b7b24b5ccfd 100644
--- a/unsloth/models/mapper.py
+++ b/unsloth/models/mapper.py
@@ -520,6 +520,11 @@ __INT_TO_FLOAT_MAPPER = \
"unsloth/Llama-3.3-70B-Instruct",
"meta-llama/Llama-3.3-70B-Instruct",
),
+ "unsloth/phi-4-unsloth-bnb-4bit" : (
+ "unsloth/phi-4",
+ "microsoft/phi-4",
+ "unsloth/phi-4-bnb-4bit",
+ ),
}
INT_TO_FLOAT_MAPPER = {}