> 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/tong-he/curltohttpwounslothni.md).

# Curl & HTTPをUnslothに接続

Unsloth は、Unsloth が起動したポート上の同じベース URL で、OpenAI/Anthropic 互換のワイヤ形式を 3 つ公開します。これらはすべて、 `Authorization: Bearer sk-unsloth-…` ヘッダーを受け取り、設定した `stream`に応じて、JSON または SSE を返します。\
\
このページでは、エンドポイントごとにレシピをまとめ（`/v1/chat/completions`, `/v1/messages`, `/v1/responses`, `/v1/models`）、最後に Unsloth 組み込みの共通セクションで締めくくります。 **サーバーサイドツール**。これはすべてのチャットエンドポイントで動作します。

{% hint style="info" %}
どの URL / キー / モデル名を使えばよいかわからない場合は、まず API 概要を読んでください。Unsloth の起動、モデルの読み込み、そして `sk-unsloth-…` キーの作成までを案内します。
{% endhint %}

### 🔑 認証

すべてのリクエストには `Authorization` ヘッダーが必要です:

```
Authorization: Bearer sk-unsloth-xxxxxxxxxxxx
```

キーをシェル履歴に残さないために、一度キーを export して環境変数を参照してください:

```bash
export UNSLOTH_STUDIO_AUTH_TOKEN=sk-unsloth-xxxxxxxxxxxx
```

以下のスニペットでは、わかりやすさのためにキーを `sk-unsloth-xxxxxxxxxxxx` として直接書いています。実際には、 `$UNSLOTH_STUDIO_AUTH_TOKEN`.

### 📋 読み込まれているモデルを一覧表示

```bash
curl http://localhost:8888/v1/models \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxxx"
```

レスポンス:

```json
{
  "object": "list",
  "data": [
    {"id": "unsloth/gemma-3-27b-it-GGUF", "object": "model", "owned_by": "local"}
  ]
}
```

<figure><img src="/files/b42b361f2bc271b628e0484e7b43cb7afe782af3" alt=""><figcaption></figcaption></figure>

リクエストで `id` フィールドは、 `"model"` 値が必要なとき（または opencode のようなクライアントが **Model ID**).

### 💬 Chat Completions（`/v1/chat/completions`)

OpenAI Chat Completions 方言です。最も広い互換性のあるインターフェースです。OpenAI SDK、opencode、Cursor、Continue、Cline、Open WebUI、SillyTavern、およびほとんどの OpenAI 互換ツールで動作します。

#### 基本的なリクエスト

```bash
curl http://localhost:8888/v1/chat/completions \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "default",
    "messages": [{"role": "user", "content": "こんにちは"}]
  }'
```

<figure><img src="/files/418734e6ab34f2e5512fa7f98fd44c7327b0719a" alt=""><figcaption></figcaption></figure>

#### ストリーミング

追加します `"stream": true` すると、レスポンスは Server-Sent Events（`text/event-stream`）に切り替わります。 `curl` に、バイトが到着したらすぐに出力するよう `--no-buffer` (`-N`):

```bash
curl -N http://localhost:8888/v1/chat/completions \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen-local",
    "messages": [{"role": "user", "content": "ローカルで動かす LLM について俳句を書いてください。"}],
    "stream": true
  }'
```

レスポンスの各行は次のようになります `data: {"choices":[{"delta":{"content":"..."}}]}`、最後は `data: [DONE]`.

<figure><img src="/files/b44828078e25a81b1e2f5986819bd42ac8870e52" alt=""><figcaption></figcaption></figure>

#### 画像（ビジョン）

画像を `image_url` コンテンツパートとしてユーザーメッセージに追加します。URL には HTTPS または base64 の `data:` URI:

```bash
# ローカルファイルを base64 として埋め込む（簡潔さのため省略）
IMG=$(base64 -w 0 test.jpg)

cat > /tmp/request.json <<EOF
{
  "model":"default",
  "messages":[{"role":"user","content":[
    {"type":"text","text":"画像を説明してください。"},
    {"type":"image_url","image_url":{"url":"data:image/jpeg;base64,$IMG"}}
  ]}],
  "max_tokens":200,
  "stream":false
}
EOF

curl http://localhost:8888/v1/chat/completions \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d @/tmp/request.json
```

{% hint style="info" %}
読み込まれているモデルはマルチモーダルである必要があります。テキスト専用モデルを読み込んだ場合、リクエスト自体の構造は成功しますが、モデルは画像を処理しません。
{% endhint %}

<figure><img src="/files/5fb512a832980ec06bd71b3c8c7ceb35f67ea4e7" alt=""><figcaption></figcaption></figure>

#### 関数呼び出し（OpenAI tools）

OpenAI スタイルの `tools` と（任意で） `tool_choice`を渡します。クライアントは各ツール呼び出しを実行し、次のターンで結果を返します。

```bash
curl http://localhost:8888/v1/chat/completions \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model":"default",
    "messages":[{"role":"user","content":"パリの天気は？"}],
    "tools":[{
      "type":"function",
      "function":{
        "name":"get_weather",
        "description":"都市の現在の天気を取得します。",
        "parameters":{
          "type":"object",
          "properties":{"city":{"type":"string"}},
          "required":["city"]
        }
      }
    }],
    "tool_choice":"required"
  }' | jq '.choices[0].message, .usage'
```

<figure><img src="/files/d5074fb4ca36311ce10f28b93906466d486023c8" alt=""><figcaption></figcaption></figure>

### 📨 Anthropic Messages（`/v1/messages`)

Claude Code、Anthropic SDK、OpenClaw、および Messages API を話せるあらゆるクライアントで使われる、Unsloth の Anthropic 互換方言です。

#### 基本的なリクエスト

```bash
curl http://localhost:8888/v1/messages \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "default",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "こんにちは"}]
  }'
```

{% hint style="warning" %}
`max_tokens` は `/v1/messages` では必須です（ `/v1/chat/completions`).
{% endhint %}

<figure><img src="/files/511f5d478b13045ad0cf791d036d572a77d25e7e" alt=""><figcaption></figcaption></figure>

#### ストリーミング

```bash
curl -N http://localhost:8888/v1/messages \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen-local",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "LoRA を 2 文で説明してください。"}],
    "stream": true
  }'
```

イベントは Anthropic の SSE 形式に従います: `message_start`, `content_block_start`, `content_block_delta`, `content_block_stop`, `message_delta`, `message_stop`、さらに Unsloth 独自の `tool_result` イベントがあり、これはサーバーサイドツールの出力用です。

#### 画像（ビジョン）

Anthropic スタイルの画像コンテンツは、 `source` ブロックに base64 データを入れて使います:

```bash
IMG=$(base64 -w 0 test.jpg)

cat > /tmp/request.json <<EOF
{
  "messages":[{"role":"user","content":[
    {"type":"text","text":"画像を説明してください。"},
    {"type":"image","source":{"type":"base64","media_type":"image/jpeg","data":"$IMG"}}
  ]}],
  "max_tokens":200,
  "stream":true
}
EOF

curl -s http://localhost/v1/messages \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxx" \
  -H "Content-Type: application/json" \
  -d @/tmp/request.json
```

<figure><img src="/files/e7a10607c2dfb9258663d54111657edfa6f48629" alt=""><figcaption></figcaption></figure>

#### ツール呼び出し（Anthropic tools）

```bash
curl http://localhost:8888/v1/messages \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen-local",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "東京の天気は？"}],
    "tools": [
      {
        "name": "get_weather",
        "description": "都市の現在の天気を取得します",
        "input_schema": {
          "type": "object",
          "properties": {
            "city": {"type": "string"}
          },
          "required": ["city"]
        }
      }
    ],
    "tool_choice": {"type": "auto"}
  }'
```

`tool_choice` 値は OpenAI 方言では次のように対応します: Anthropic `auto` → OpenAI `auto`、Anthropic `any` → OpenAI `required`、Anthropic `{type: "tool", name: "x"}` → OpenAI `{type: "function", function: {name: "x"}}`、Anthropic `none` → OpenAI `none`.

<figure><img src="/files/05d465dfc1a1eae24a650712c8fc803379434078" alt=""><figcaption></figcaption></figure>

### 🧬 Responses（`/v1/responses`)

Unsloth は、より新しい **OpenAI Responses API**, も話します。これは Codex や他の最近の OpenAI クライアントが移行したプロトコルです。

```bash
curl http://localhost:8888/v1/responses \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen-local",
    "input": "1 文の挨拶を書いてください。"
  }'
```

<figure><img src="/files/ff7d6c7893c163da26e4e871094cf4cff4f4a696" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/a93b3af4d0fd8e8d69c40f5e47c361c8c634d89a" alt=""><figcaption></figcaption></figure>

ストリーミングは Chat Completions と同じように動作します。追加するのは `"stream": true` そしてパイプするのは `-N`.

### 🧰 Unsloth サーバーサイドツール（短縮記法）

クライアントサイドの関数呼び出しに加えて、Unsloth は **Python**, **bash**、および **Web 検索** をサーバーサイドで実行し、その結果をカスタム `tool_result` イベントとしてストリームで返せます。これは、ツール呼び出しをクライアント経由で往復させなくても、Unsloth が最初から「本物の」エージェントのように感じられる機能です。

これらの追加フィールドを **いずれかの** `/v1/chat/completions` または `/v1/messages`:

| フィールド             | 型               | メモ                                                    |
| ----------------- | --------------- | ----------------------------------------------------- |
| `enable_thinking` | `boolean`       | `false` で思考を無効にします。 `true` デフォルトでは                    |
| `enable_tools`    | `boolean`       | `true` でサーバーサイドツール実行を有効にします。                          |
| `enabled_tools`   | `array<string>` | モデルが呼び出せるツール。対応しているのは `python`, `bash`, `web_search`. |
| `session_id`      | `string`        | 任意。呼び出し間でツール状態（例: Python カーネル）を保持します。                 |

#### 思考モード

思考モードはデフォルトで有効です。&#x20;

```bash
curl http://localhost:8888/v1/chat/completions \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "default",
    "messages": [{"role": "user", "content": "量子トンネル効果を 1 文で要約してください"}],
    "stream": false
  }'
```

モデルは回答を出す前に思考します。

<figure><img src="/files/9a6c58d9b2f23c7df17b83c6ac44a1718ad1e673" alt=""><figcaption></figcaption></figure>

思考を無効にするには、 `enable_thinking: false` をリクエストに渡します。モデルは最初に思考せずに回答を返します。

<figure><img src="/files/8de107aa76d7e8ebad3faf15662b74dfe20a7848" alt=""><figcaption></figcaption></figure>

#### Python 実行

```bash
curl http://localhost:8888/v1/chat/completions \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen-local",
    "messages": [{"role": "user", "content": "123 * 456 はいくつですか？ コードを使って計算してください。"}],
    "stream": false,
    "enable_tools": true,
    "enabled_tools": ["python"],
    "session_id": "my-session"
  }'
```

<figure><img src="/files/e8d805b5c1edf0f38044484d32ba18d06f2daac0" alt=""><figcaption></figcaption></figure>

#### Web 検索 + Python（ストリーミング）

```bash
curl http://localhost:8888/v1/chat/completions \
  -H "Authorization: Bearer sk-unsloth-xxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen-local",
    "messages": [{"role": "user", "content": "Python 3.13 の機能を検索してください"}],
    "stream": true,
    "enable_tools": true,
    "enabled_tools": ["web_search", "python"],
    "session_id": "my-session"
  }'
```

<figure><img src="/files/b868f933df65b504b7af7c938e0c5a710c42bcb9" alt=""><figcaption></figcaption></figure>

#### オン `/v1/messages`

同じ短縮記法は Anthropic Messages エンドポイントでも使えます:

```bash
curl http://localhost:8888/v1/messages \
  -H "Authorization: Bearer sk-unsloth-xxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen-local",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Python 3.13 の機能を検索してください"}],
    "stream": true,
    "enable_tools": true,
    "enabled_tools": ["web_search", "python"],
    "session_id": "my-session"
  }'
```

<figure><img src="/files/d4cd5845f1477f98dc1ebcb2e328d620db8d515f" alt=""><figcaption></figcaption></figure>

Unsloth は独自の `tool_result` SSE イベントを、標準の Anthropic / OpenAI イベント型に加えてストリームします。モデルは次のターンで各ツールの出力を確認します。

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

**`401 Unauthorized`** **-** この `Authorization` ヘッダーがないか、キーが間違っています。次を再確認してください: `Authorization: Bearer sk-unsloth-…`.

**`curl` ストリーミングリクエストで停止する -** 追加します `-N` （ `--no-buffer`と同じです）。これがないと、 `curl` が SSE ストリームをバッファリングし、最後まで何も表示されません。

**Base64 エンコードは OS によって異なります** **-** Linux の `base64` はデフォルトで行を折り返しますが、macOS / BSD はそうしません。Linux では `base64 -w 0` を使い、macOS では `base64` を使うか、出力を `tr -d '\n'`.

**シェルでの JSON エスケープ** **-** Heredoc（`-d @file.json`）は、本文が複雑になったらインライン文字列よりもきれいです。例: `curl ... -d @body.json`.

**`max_tokens` でのエラー `/v1/messages`** **-** Anthropic 方言ではこれが必須です。 `"max_tokens": 1024` （または必要な上限）を追加してください。

エンドポイントレベルの問題（モデルが読み込まれない、接続が切れる、ポートが違う）については、API 概要ページを参照してください。

### 任意: サーバーのデフォルトを調整する

サーバー起動時に、デフォルトの動作を `unsloth run`.

```bash
# カスタムデフォルトでサーバーを起動
unsloth run \
  --model unsloth/Qwen3-1.7B-GGUF \
  --reasoning off \
  --temp 0.6 \
  -p 8888
```

使用します `--reasoning off` で思考をオフにするか、 `--reasoning on` で、推論をサポートするモデルの思考をオンにします。

```bash
# ローカルネットワークで API を公開
unsloth run \
  --model unsloth/Qwen3-1.7B-GGUF \
  -H 0.0.0.0 \
  -p 8888
```

これによりサーバーは `0.0.0.0:8888`で起動し、ローカルネットワーク上の他のデバイスが接続できるようになります。

#### リクエストごとに設定を上書きする

各 API リクエストで生成設定を直接上書きすることもできます。

```bash
curl http://localhost:8888/v1/chat/completions \
  -H "Authorization: Bearer $UNSLOTH_STUDIO_AUTH_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "default",
    "messages": [
      {
        "role": "user",
        "content": "ローカル AI について短い詩を書いてください。"
      }
    ],
    "temperature": 0.8,
    "top_p": 0.9,
    "max_tokens": 512
  }'
```

次のようなリクエストレベルの値 `temperature`, `top_p`, `max_tokens`、および `stream` は、そのリクエストについてサーバーのデフォルトを上書きします。


---

# 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:

```
GET https://unsloth.ai/docs/jp/tong-he/curltohttpwounslothni.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.
