> 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/ji-ben/inference-and-deployment/lm-studio.md).

# モデルを LM Studio にデプロイ

微調整したLLMをLM Studioで直接実行・デプロイできます。 [LM Studio](https://lmstudio.ai/) での簡単な実行とデプロイを可能にします **GGUF** モデル（llama.cpp形式）。

私たちの [LM Studioノートブック](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/FunctionGemma_\(270M\)-LMStudio.ipynb) または以下の手順に従ってください：

1. **Unslothで微調整したモデルを `.gguf`**
2. **LM StudioにGGUFをインポート／ダウンロードする**
3. **Chatで読み込む** （またはOpenAI互換のローカルAPIの背後で実行する）

<div><figure><img src="/files/165e5d5d448e21d2222c189b5a651ca02ed1ef8d" alt="" width="375"><figcaption><p>LM Studioでファインチューニングする前</p></figcaption></figure> <figure><img src="/files/c9d2947af07c85fb9c6a64a9d83d7cfef30b8718" alt="" width="375"><figcaption><p>LM Studioでファインチューニングした後</p></figcaption></figure></div>

### 1) GGUFにエクスポートする（Unslothから）

すでに `.gguf`をエクスポート済みなら、次へスキップしてください **LM Studioへのインポート**.

```python
# ローカルに保存（フォルダ内にGGUFアーティファクトを作成します）
model.save_pretrained_gguf("my_model_gguf", tokenizer, quantization_method = "q4_k_m")
# model.save_pretrained_gguf("my_model_gguf", tokenizer, quantization_method = "q8_0")
# model.save_pretrained_gguf("my_model_gguf", tokenizer, quantization_method = "f16")

# またはGGUFをHugging Face Hubにプッシュする
model.push_to_hub_gguf("hf_username/my_model_gguf", tokenizer, quantization_method = "q4_k_m")
```

{% hint style="info" %}
`q4_k_m` は通常、ローカル実行のデフォルトです。

`q8_0` はほぼフル精度に近い品質の最適解です。

`f16` は最大／最遅ですが、量子化されていない元の精度です。
{% endhint %}

### 2) GGUFをLM Studioにインポートする

{% tabs %}
{% tab title="CLIインポート（lms import）" %}
LM Studioには `lms` というCLIがあり、ローカルの `.gguf` をLM Studioのmodelsフォルダにインポートできます。

**GGUFファイルをインポートする：**

```bash
lms import /path/to/model.gguf
```

**元のファイルを保持する（移動ではなくコピー）：**

```bash
lms import /path/to/model.gguf --copy
```

<details>

<summary><strong>より細かく設定をカスタマイズするにはクリック</strong></summary>

**モデルをその場所に保持する（シンボリックリンク）：**

専用ドライブに保存された大きなモデルに便利です。

```bash
lms import /path/to/model.gguf --symbolic-link
```

**プロンプトを省略し、ターゲットの名前空間を自分で選ぶ：**

```bash
lms import /path/to/model.gguf --user-repo my-user/my-finetuned-models
```

**ドライラン（何が起こるかを表示します）：**

```bash
lms import /path/to/model.gguf --dry-run
```

</details>

インポート後、モデルはLM Studioの以下の場所に表示されるはずです **My Models**.

<figure><img src="/files/ec85e55fef73a52626e8f4b4fa46df00d8d0554c" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="Hugging Faceから" %}
GGUFリポジトリをHugging Faceにプッシュした場合、LM Studio内から直接ダウンロードできます。

**方法A：LM Studioのアプリ内ダウンローダーを使う**

1. LM Studioを開く
2. 次の **Discover** タブ
3. を検索する `hf_username/repo_name` （またはHugging FaceのURLを貼り付ける）
4. 欲しい量子化モデルをダウンロードする（例： `Q4_K_M`)

**方法B：CLIダウンローダーを使う**

```bash
# リポジトリ名でHFからダウンロード
lms get hf_username/my_model_gguf

# @で量子化を指定
lms get hf_username/my_model_gguf@Q4_K_M
```

{% endtab %}

{% tab title="手動インポート（フォルダ構造）" %}
CLIを使いたくない場合は、 `.gguf` ファイルをLM Studioが期待するモデルディレクトリ構造に配置できます。

LM Studioはモデルを次のような構造で想定しています：

```
~/.lmstudio/models/
└── publisher/
    └── model/
        └── model-file.gguf
```

例：

```
~/.lmstudio/models/
└── my-name/
    └── my-finetune/
        └── my-finetune-Q4_K_M.gguf
```

その後、LM Studioを開いて確認してください **My Models**.

**ヒント：** モデルディレクトリはLM Studioの **My Models** タブから管理／確認できます。
{% endtab %}
{% endtabs %}

### 3) LM Studioで読み込んでチャットする

1. LM Studioを開く → **Chat**
2. を開く **モデルローダー**
3. インポートしたモデルを選択する
4. （任意）読み込み設定を調整する（GPUオフロード、コンテキスト長など）
5. UIで通常どおりチャットする

### 4) 微調整済みモデルをローカルAPIとして提供する（OpenAI互換）

LM Studioは、読み込んだモデルをOpenAI互換APIの背後で提供できます（Open WebUI、カスタムエージェント、スクリプトなどに便利です）。

{% tabs %}
{% tab title="GUI（Developerタブ）" %}

1. LM Studioでモデルを読み込む
2. 次の **Developer** タブ
3. ローカルサーバーを起動する
4. 表示されたベースURLを使用する（通常のデフォルトは `http://localhost:1234/v1`)
   {% endtab %}

{% tab title="CLI（lms load + lms server start）" %}

#### 1) 利用可能なモデルを一覧表示

```bash
lms ls
```

#### 2) モデルを読み込む（任意のフラグ）

```bash
lms load <model-identifier> --gpu=auto --context-length=8192
```

注：

* `--gpu=1.0` は「GPUに100%オフロードすることを試みる」を意味します
* 安定した識別子を設定できます：

```bash
lms load <model-identifier> --identifier="my-finetuned-model"
```

#### 3) サーバーを起動

```bash
lms server start --port 1234
```

{% endtab %}
{% endtabs %}

**クイックテスト：モデル一覧を表示**

```bash
curl http://localhost:1234/v1/models
```

**Pythonの例（OpenAI SDK）：**

{% code expandable="true" %}

```python
from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:1234/v1",
    api_key="lm-studio",  # LM Studioでは実際のキーが不要な場合があります。これは一般的なプレースホルダーです
)

resp = client.chat.completions.create(
    model="model-identifier-from-lm-studio",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello! What did I fine-tune you to do?"},
    ],
    temperature=0.7, # モデルのニーズに応じてtemperatureを調整してください
)

print(resp.choices[0].message.content)
```

{% endcode %}

**cURLの例（chat completions）：**

{% code expandable="true" %}

```bash
curl http://localhost:1234/v1/chat/completions \\
  -H "Content-Type: application/json" \\
  -d '{
    "model": "model-identifier-from-lm-studio",
    "messages": [
      {"role": "user", "content": "Say this is a test!"}
    ],
    "temperature": 0.7 # モデルのニーズに応じてtemperatureを調整してください
  }'
```

{% endcode %}

{% hint style="info" %}
**デバッグのヒント：** フォーマット／テンプレートを調査したい場合は、 *raw* LM Studioがモデルへ送るプロンプトを次のコマンドで確認できます： `lms log stream`
{% endhint %}

### トラブルシューティング

#### **モデルはUnslothでは動くが、LM Studioの出力が文字化けする／繰り返す**

これはほぼ常に **プロンプトテンプレート／チャットテンプレートの不一致**.

LM Studioは可能な場合、GGUFメタデータからプロンプトテンプレートを **自動検出** しますが、カスタムモデルや誤ってタグ付けされたモデルでは手動での上書きが必要なことがあります。

**修正方法：**

1. に移動します **My Models** → モデルの横にある歯車⚙️をクリック
2. 次を探す **Prompt Template** を、学習に使ったテンプレートに合わせて設定する
3. または、Chatサイドバーで次を有効にする： **Prompt Template** ボックス（常に表示するように強制できます）

#### LM Studioの“My Models”にモデルが表示されない

* 次を優先してください `lms import /path/to/model.gguf`
* または、ファイルが正しいフォルダ構造にあることを確認してください： `~/.lmstudio/models/publisher/model/model-file.gguf`

#### OOM／パフォーマンス低下

* より小さい量子化を使う（例： `Q4_K_M`)
* コンテキスト長を短くする
* GPUオフロードを調整する（LM Studioの「Per-model defaults」／読み込み設定）

***

### 追加リソース

* [LM Studio + Unslothのブログ記事](https://lmstudio.ai/blog/functiongemma-unsloth) （FunctionGemmaの手順）：&#x20;
* LM Studio [Import Modelsのドキュメント](https://lmstudio.ai/docs/app/advanced/import-model)
* LM Studio [Prompt Templateのドキュメント](https://lmstudio.ai/docs/app/advanced/prompt-template)
* LM Studio [OpenAI互換APIのドキュメント](https://lmstudio.ai/docs/developer/openai-compat)


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://unsloth.ai/docs/jp/ji-ben/inference-and-deployment/lm-studio.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
