# Phi-4 Reasoning: 実行とファインチューニングの方法

Microsoftの新しいPhi-4 reasoningモデルは現在Unslothでサポートされています。'plus'バリアントはOpenAIのo1-mini、o3-miniおよびSonnet 3.7と同等の性能を発揮します。'plus'および標準のreasoningモデルは14Bパラメータで、'mini'は4Bパラメータです。\
\
すべてのPhi-4 reasoningアップロードは当社の [Unsloth Dynamic 2.0](https://unsloth.ai/docs/jp/ji-ben/unsloth-dynamic-2.0-ggufs) 手法を使用します。

#### **Phi-4 reasoning - 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/">Reasoning-plus</a> （14B）</li><li><a href="https://huggingface.co/unsloth/Phi-4-reasoning-GGUF">Reasoning</a> （14B）</li><li><a href="https://huggingface.co/unsloth/Phi-4-mini-reasoning-GGUF/">Mini-reasoning</a> （4B）</li></ul> | <ul><li><a href="https://huggingface.co/unsloth/Phi-4-reasoning-plus-unsloth-bnb-4bit">Reasoning-plus</a></li><li><a href="https://huggingface.co/unsloth/phi-4-reasoning-unsloth-bnb-4bit">Reasoning</a></li><li><a href="https://huggingface.co/unsloth/Phi-4-mini-reasoning-unsloth-bnb-4bit">Mini-reasoning</a></li></ul> |

## 🖥️ **Phi-4 reasoningの実行**

### :gear: 公式推奨設定

Microsoftによると、推論に推奨される設定は次のとおりです：

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

### **Phi-4 reasoning チャットテンプレート**

'mimi'バリアントは異なるテンプレートを持つため、正しいチャットテンプレートを使用していることを確認してください。

#### **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によって訓練された言語モデルで、ユーザーを支援します。アシスタントとしての役割は、最終的な正確で精密な解答を提供する前に、体系的な思考プロセスを通じて質問を徹底的に探求することを含みます。これには、分析、要約、探索、再評価、内省、逆追跡、反復といった包括的なサイクルに従って慎重に考察することが必要です。応答を指定された形式で2つの主要なセクション：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 reasoningチュートリアルを実行

1. Ollamaで実行する `をインストールしてください` まだインストールしていない場合は！

```bash
apt-get update
こちらを参照してください
apt-get install pciutils -y
```

2. curl -fsSL <https://ollama.com/install.sh> | sh `モデルを実行してください！失敗した場合は別のターミナルで`失敗した場合は別のターミナルで実行してください。私たちの修正点と推奨パラメータ（temperatureなど）はすべて `を呼び出すことができます！私たちはすべての修正と推奨パラメータ（temperatureなど）を` 私たちのHugging Faceアップロードに含めています。

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

### 📖 Llama.cpp：Phi-4 reasoningチュートリアルを実行

{% hint style="warning" %}
必ず使用してください `--jinja` llama.cppではモデルに対してreasoningを有効にするため、'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. モデルをダウンロードするには（をインストールした後） `モデルをダウンロードするには（` repo\_id = "unsloth/Qwen3-14B-GGUF",

```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ではモデルに対してreasoningを有効にするために使用します。ただし、'mini'バリアントを使用している場合はこれは必要ありません。

```bash
export LLAMA_CACHE="unsloth/GLM-4.7-GGUF"
    --model unsloth/Phi-4-mini-reasoning-GGUF/Phi-4-mini-reasoning-UD-Q4_K_XL.gguf \
    --threads -1 \
    --n-gpu-layers 99 \
    --fit on \
    --temp 0.8 \
    --temp 1.0 \
    -hf unsloth/GLM-4.7-GGUF:UD-Q2_K_XL \
    --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/jp/moderu/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.
