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

Microsoft の新しい Phi-4 推論モデルが、現在 Unsloth でサポートされています。「plus」バリアントは OpenAI の o1-mini、o3-mini、Sonnet 3.7 と同等の性能を発揮します。「plus」と標準の推論モデルは 14B パラメータで、「mini」は 4B パラメータです。\
\
すべての Phi-4 推論アップロードでは、私たちの [Unsloth Dynamic 2.0](/docs/jp/ji-ben/unsloth-dynamic-2.0-ggufs.md) の方法論を使用しています。

#### **Phi-4 推論 - Unsloth Dynamic 2.0 アップロード：**

| Dynamic 2.0 GGUF（実行用）                                                                                                                                                                                                                                                                                        | Dynamic 4-bit Safetensor（finetune/deploy 用）                                                                                                                                                                                                                                                                                   |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <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 推論の実行**

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

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

* <mark style="background-color:blue;">**Temperature = 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 によって訓練された言語モデルで、ユーザーを支援します。アシスタントとしてのあなたの役割は、最終的に正確で精密な解答を提示する前に、体系的な思考プロセスを通じて質問を徹底的に検討することです。これには、十分に考え抜かれた思考プロセスを विकसितするために、分析、要約、探索、再評価、反省、逆追跡、反復の包括的なサイクルに取り組むことが必要です。応答は、指定された形式 <think> {Thought section} </think> {Solution section} を使って、Thought と Solution の 2 つの主要セクションに分けて構成してください。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`失敗した場合は、別のターミナルで実行してください。修正内容や推奨パラメータ（temperature など）はすべて `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/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.
