> 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/jp/moderu/tutorials/phi-4-reasoning-how-to-run-and-fine-tune.md).

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

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/jp/ji-chu/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/">Reasoning-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">Reasoning-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: 公式推奨設定

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 によって訓練された言語モデルです。アシスタントとしてのあなたの役割は、最終的に正確で適切な解答を提示する前に、体系的な思考プロセスを通じて質問を徹底的に検討することです。これには、十分に考え抜かれた思考プロセスを形成するために、分析、要約、探索、再評価、反省、遡及、反復の包括的なサイクルに取り組むことが必要です。回答は、指定された形式 <think> {思考セクション} </think> {解答セクション} を使用し、思考と解答の 2 つの主要セクションに構成してください。思考セクションでは、あなたの推論過程を手順ごとに詳述してください。各手順には、質問の分析、関連する発見の要約、新しいアイデアのブレインストーミング、現在の手順の正確性の確認、誤りの修正、前の手順の再確認などの詳細な考察を含めてください。解答セクションでは、思考セクションでのさまざまな試行、探索、反省に基づいて、正しいと判断した最終解答を体系的に提示してください。解答セクションは論理的、正確、簡潔であり、結論に至るために必要な手順を詳述する必要があります。では、上記のガイドラインに従って次の質問を解いてください：<|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
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/jp/moderu/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.
