> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/zh/mo-xing/tutorials/phi-4-reasoning-how-to-run-and-fine-tune.md).

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

学习如何使用 Unsloth + 我们的 Dynamic 2.0 量化版本在本地运行和微调 Phi-4 推理模型

Microsoft 的新 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-zhi-shi/dynamic-3.0-ggufs.md) 方法论。

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

| Dynamic 2.0 GGUF（用于运行）                                                                                                                                                                                                                                                             | Dynamic 4-bit Safetensor（用于微调/部署）                                                                                                                                                                                                                                                                   |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <ul><li><a href="https://huggingface.co/unsloth/Phi-4-reasoning-plus-GGUF/">推理增强版</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">推理增强版</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: 官方推荐设置

根据 Microsoft 的说法，以下是推荐的推理设置：

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

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

请确保使用正确的聊天模板，因为 'mini' 变体使用的是不同的模板。

#### **Phi-4-mini:**

{% code overflow="wrap" %}

```
<|system|>你的名字是 Phi，一个由 Microsoft 开发的 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，一个由 Microsoft 训练的语言模型，旨在帮助用户。作为助手，你的职责是在给出最终精确且准确的答案之前，通过系统性的思考过程彻底探索问题。这需要参与一个全面的分析、总结、探索、重新评估、反思、回溯和迭代循环，以形成经过深思熟虑的思考过程。请将你的回复分为两个主要部分：Thought 和 Solution，并使用指定格式：<think> {Thought section} </think> {Solution section}。在 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 上传中。

```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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
