# Phi-4 Reasoning：如何运行与微调

微软新的 Phi-4 推理模型现在在 Unsloth 中受支持。“plus” 变体的表现可与 OpenAI 的 o1-mini、o3-mini 和 Sonnet 3.7 相媲美。“plus”和标准推理模型为 14B 参数，而“mini”为 4B 参数。\
\
所有 Phi-4 推理上传均使用我们的 [Unsloth 动态 2.0](https://unsloth.ai/docs/zh/ji-chu/unsloth-dynamic-2.0-ggufs) 方法。

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

| Dynamic 2.0 GGUF（用于运行）                                                                                                                                                                                                                                                               | Dynamic 4-bit 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;">**Temperature = 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> {思考部分} </think> {解决方案部分}。在思考部分，按步骤详细说明你的推理过程。每一步应包括详细的考虑，例如分析问题、总结相关发现、头脑风暴新想法、验证当前步骤的准确性、修正任何错误以及重访先前步骤。在解决方案部分，基于思考部分的各种尝试、探索和反思，系统地呈现你认为正确的最终解答。解决方案部分应具有逻辑性、准确且简明，并详细说明达到结论所需的必要步骤。现在，尝试按照上述指南解决以下问题：<|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 上传中包含了我们所有的修复和建议的参数（温度等）。

```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' 变体除外。否则不会提供任何令牌。
{% 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.
