# Phi-4 推理：如何运行和微调

微软新的 Phi-4 推理模型现在已在 Unsloth 中支持。‘plus’ 版本的表现可与 OpenAI 的 o1-mini、o3-mini 和 Sonnet 3.7 相媲美。‘plus’ 和标准推理模型参数量均为 14B，而 ‘mini’ 的参数量为 4B。\
\
所有 Phi-4 推理上传都使用我们的 [Unsloth Dynamic 2.0](/docs/zh/ji-chu/unsloth-dynamic-2.0-ggufs.md) 方法。

#### **Phi-4 推理 - Unsloth Dynamic 2.0 上传：**

| Dynamic 2.0 GGUF（用于运行）                                                                                                                                                                                                                                                               | 动态 4 位 Safetensor（用于微调/部署）                                                                                                                                                                                                                                                                            |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <ul><li><a href="https://huggingface.co/unsloth/Phi-4-reasoning-plus-GGUF/">推理-plus</a> (14B)</li><li><a href="https://huggingface.co/unsloth/Phi-4-reasoning-GGUF">推理</a> (14B)</li><li><a href="https://huggingface.co/unsloth/Phi-4-mini-reasoning-GGUF/">迷你推理</a> (4B)</li></ul> | <ul><li><a href="https://huggingface.co/unsloth/Phi-4-reasoning-plus-unsloth-bnb-4bit">推理-plus</a></li><li><a href="https://huggingface.co/unsloth/phi-4-reasoning-unsloth-bnb-4bit">推理</a></li><li><a href="https://huggingface.co/unsloth/Phi-4-mini-reasoning-unsloth-bnb-4bit">迷你推理</a></li></ul> |

## 🖥️ **运行 Phi-4 推理**

### :gear: 官方推荐设置

根据微软的说法，这些是推理时推荐的设置：

* <mark style="background-color:blue;">**温度 = 0.8**</mark>
* Top\_P = 0.95

### **Phi-4 推理聊天模板**

请确保使用正确的聊天模板，因为 ‘mini’ 版本使用的是不同的模板。

#### **Phi-4-mini：**

{% code overflow="wrap" %}

```
<|system|>你的名字是 Phi，一款由微软开发的 AI 数学专家。<|end|><|user|>如何求解 3*x^2+4*x+5=1？<|end|><|assistant|>
```

{% endcode %}

#### **Phi-4-reasoning 和 Phi-4-reasoning-plus：**

此格式用于一般对话和指令：

{% code overflow="wrap" %}

```
<|im_start|>system<|im_sep|>你是 Phi，一个由微软训练的大语言模型，旨在通过系统化思考过程帮助用户。作为助手，你的职责是在提供最终精确且准确的解决方案之前，彻底探索问题。这需要参与一个全面的分析、总结、探索、重新评估、反思、回溯和迭代的循环，以形成经过深思熟虑的思考过程。请将你的回答分为两个主要部分：Thought 和 Solution，并使用指定格式：<think> {Thought 部分} </think> {Solution 部分}。在 Thought 部分中，分步骤详细说明你的推理过程。每一步都应包括诸如分析问题、总结相关发现、集思广益提出新想法、验证当前步骤的准确性、修正任何错误以及重新审视前面步骤等详细考虑。在 Solution 部分中，基于 Thought 部分中的各种尝试、探索和反思，系统地 प्रस्तुत出你认为正确的最终解答。Solution 部分应当逻辑清晰、准确且简洁，并详细说明得出结论所需的步骤。现在，请按照以上指南尝试解决以下问题：<|im_end|><|im_start|>user<|im_sep|>1+1 等于多少？<|im_end|><|im_start|>assistant<|im_sep|>
```

{% endcode %}

{% hint style="info" %}
是的，聊天模板/提示格式就是这么长！
{% endhint %}

### 🦙 Ollama：运行 Phi-4 推理教程

1. 安装 `ollama` 如果你还没有的话！

```bash
apt-get update
apt-get install pciutils -y
curl -fsSL https://ollama.com/install.sh | sh
```

2. 运行模型！注意，如果失败，你可以在另一个终端中调用 `ollama serve`如果失败，请在另一个终端中运行。我们在 `params` 我们的 Hugging Face 上传中包含了所有修复和建议参数（如 temperature 等）。

```bash
ollama run hf.co/unsloth/Phi-4-mini-reasoning-GGUF:Q4_K_XL
```

### 📖 Llama.cpp：运行 Phi-4 推理教程

{% hint style="warning" %}
你必须使用 `--jinja` 在 llama.cpp 中启用模型推理，‘mini’ 版本除外。否则不会提供 token。
{% endhint %}

1. 获取最新的 `llama.cpp` 在 [GitHub 这里](https://github.com/ggml-org/llama.cpp)。你也可以按照下面的构建说明操作。将 `-DGGML_CUDA=ON` 改为 `-DGGML_CUDA=OFF` 如果你没有 GPU，或者只想进行 CPU 推理。 **对于 Apple Mac / Metal 设备**，设置 `-DGGML_CUDA=OFF` 然后照常继续——Metal 支持默认开启。

```bash
apt-get update
apt-get install pciutils build-essential cmake curl libcurl4-openssl-dev -y
git clone https://github.com/ggml-org/llama.cpp
cmake llama.cpp -B llama.cpp/build \
    -DBUILD_SHARED_LIBS=OFF -DGGML_CUDA=ON -DLLAMA_CURL=ON
cmake --build llama.cpp/build --config Release -j --clean-first --target llama-cli llama-gguf-split
cp llama.cpp/build/bin/llama-* llama.cpp
```

2. 通过以下方式下载模型（在安装 `pip install huggingface_hub hf_transfer` ）。你可以选择 Q4\_K\_M，或其他量化版本。

```python
# !pip install huggingface_hub hf_transfer
import os
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
from huggingface_hub import snapshot_download
snapshot_download(
    repo_id = "unsloth/Phi-4-mini-reasoning-GGUF",
    local_dir = "unsloth/Phi-4-mini-reasoning-GGUF",
    allow_patterns = ["*UD-Q4_K_XL*"],
)
```

3. 在 llama.cpp 中以对话模式运行模型。你必须使用 `--jinja` 在 llama.cpp 中启用模型推理。不过如果你使用的是 ‘mini’ 版本，则不需要这样做。

```bash
./llama.cpp/llama-cli \
    --model unsloth/Phi-4-mini-reasoning-GGUF/Phi-4-mini-reasoning-UD-Q4_K_XL.gguf \
    --threads -1 \\
    --n-gpu-layers 99 \\
    --prio 3 \
    --temp 0.8 \
    --top-p 0.95 \
    --jinja \
    --min-p 0.00 \\
    --ctx-size 32768 \\
    --seed 3407
```

## 🦥 使用 Unsloth 对 Phi-4 进行微调

[Phi-4 微调](https://unsloth.ai/blog/phi4) 这些模型现在也已在 Unsloth 中支持。若要在 Google Colab 上免费微调，只需将 `model_name` 从 'unsloth/Phi-4' 改为 'unsloth/Phi-4-mini-reasoning' 等。

* [Phi-4（14B）微调笔记本](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Phi_4-Conversational.ipynb)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://unsloth.ai/docs/zh/mo-xing/tutorials/phi-4-reasoning-how-to-run-and-fine-tune.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
