> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sunra.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a chat completion

指定されたチャット会話に対するモデルレスポンスのリクエストを送信します。ストリーミングモードと非ストリーミングモードの両方に対応しています。OpenAI Chat Completions APIフォーマットと互換性があります。

## 認証

<ParamField header="Authorization" type="string" required>
  Bearerトークン。APIキーをAuthorizationヘッダーのBearerトークンとして使用してください。

  Format: `Bearer <SUNRA_KEY>`
</ParamField>

## リクエスト

このエンドポイントはJSONオブジェクトを受け付けます。

<ParamField body="messages" type="object[]" required>
  会話のメッセージリスト。

  <Expandable title="プロパティ">
    <ParamField body="role" type="string" required>
      メッセージ作成者のロール。サポートされている値: `system`, `user`, `assistant`, `tool`。
    </ParamField>

    <ParamField body="content" type="string | object[]" required>
      メッセージの内容。文字列またはマルチモーダル入力用のコンテンツパーツの配列を指定できます。
    </ParamField>

    <ParamField body="name" type="string">
      参加者のオプション名。同じロールの参加者を区別するための情報をモデルに提供します。
    </ParamField>

    <ParamField body="tool_calls" type="object[]">
      関数呼び出しなど、モデルによって生成されたツール呼び出し。`assistant`メッセージにのみ存在します。
    </ParamField>

    <ParamField body="tool_call_id" type="string">
      このメッセージが応答するツール呼び出し。`tool`メッセージにのみ存在します。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="model" type="string" required>
  補完に使用するモデル。利用可能なモデルは [sunra.ai/models](https://sunra.ai/models) で確認できます。
</ParamField>

<ParamField body="stream" type="boolean" default={false}>
  `true`に設定すると、部分的なメッセージデルタがサーバー送信イベント（SSE）として送信されます。
</ParamField>

<ParamField body="max_tokens" type="integer">
  補完で生成するトークンの最大数。入力トークンと生成トークンの合計はモデルのコンテキスト長によって制限されます。
</ParamField>

<ParamField body="temperature" type="number" default={1}>
  0から2の間のサンプリング温度。0.8のような高い値は出力をよりランダムにし、0.2のような低い値はより集中的で決定論的にします。
</ParamField>

<ParamField body="top_p" type="number" default={1}>
  核サンプリングパラメータ（0-1）。temperatureサンプリングの代替として、モデルがtop\_pの確率質量を持つトークンを考慮します。
</ParamField>

<ParamField body="frequency_penalty" type="number" default={0}>
  -2.0から2.0の間の数値。正の値は、テキスト中の既存の頻度に基づいて新しいトークンにペナルティを課し、モデルが同じ行をそのまま繰り返す可能性を低下させます。
</ParamField>

<ParamField body="presence_penalty" type="number" default={0}>
  -2.0から2.0の間の数値。正の値は、テキスト中に既に出現しているかどうかに基づいて新しいトークンにペナルティを課し、モデルが新しいトピックについて話す可能性を高めます。
</ParamField>

<ParamField body="stop" type="string | string[]">
  APIがそれ以上トークンを生成するのを停止する最大4つのシーケンス。
</ParamField>

<ParamField body="n" type="integer" default={1}>
  各入力メッセージに対して生成するチャット補完の選択肢の数。
</ParamField>

<ParamField body="logprobs" type="boolean" default={false}>
  出力トークンの対数確率を返すかどうか。trueの場合、メッセージのコンテンツに返される各出力トークンの対数確率を返します。
</ParamField>

<ParamField body="top_logprobs" type="integer">
  各トークン位置で返す最も可能性の高いトークンの数を指定する0から20の整数。このパラメータを使用する場合は、`logprobs`を`true`に設定する必要があります。
</ParamField>

<ParamField body="response_format" type="object">
  モデルが出力する必要があるフォーマットを指定するオブジェクト。

  <Expandable title="プロパティ">
    <ParamField body="type" type="string" required>
      レスポンスフォーマットのタイプ。サポートされている値: `text`, `json_object`, `json_schema`。
    </ParamField>

    <ParamField body="json_schema" type="object">
      JSONスキーマオブジェクト。typeが`json_schema`の場合に必須です。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="seed" type="integer">
  指定した場合、システムは決定論的にサンプリングするよう最善を尽くします。同じseedとパラメータを使用した繰り返しのリクエストは同じ結果を返すはずです。
</ParamField>

<ParamField body="tools" type="object[]">
  モデルが呼び出す可能性のあるツールのリスト。現在、ツールとしてサポートされているのは関数のみです。

  <Expandable title="プロパティ">
    <ParamField body="type" type="string" required>
      ツールのタイプ。現在、`function`のみサポートされています。
    </ParamField>

    <ParamField body="function" type="object" required>
      関数の定義。

      <Expandable title="プロパティ">
        <ParamField body="name" type="string" required>
          呼び出す関数の名前。
        </ParamField>

        <ParamField body="description" type="string">
          関数が何をするかの説明。
        </ParamField>

        <ParamField body="parameters" type="object">
          関数が受け入れるパラメータ。JSONスキーマオブジェクトとして記述されます。
        </ParamField>

        <ParamField body="strict" type="boolean" default={false}>
          厳密なスキーマ準拠を有効にするかどうか。
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="tool_choice" type="string | object">
  モデルがどのツールを呼び出すかを制御します。`none`はモデルがツールを呼び出さないことを意味します。`auto`はモデルがメッセージ生成とツール呼び出しのいずれかを選択できることを意味します。`required`はモデルが1つ以上のツールを呼び出す必要があることを意味します。`{"type": "function", "function": {"name": "my_function"}}`のように特定の関数を指定することもできます。
</ParamField>

<ParamField body="parallel_tool_calls" type="boolean" default={true}>
  ツール使用時に並列関数呼び出しを有効にするかどうか。
</ParamField>

<ParamField body="user" type="string">
  エンドユーザーを表す一意の識別子。不正使用の監視と検出に役立ちます。
</ParamField>

## レスポンス

チャット補完の成功レスポンス。

<ResponseField name="id" type="string">
  チャット補完の一意の識別子。
</ResponseField>

<ResponseField name="object" type="string">
  オブジェクトタイプ。常に`chat.completion`。
</ResponseField>

<ResponseField name="created" type="integer">
  チャット補完が作成された時のUnixタイムスタンプ（秒単位）。
</ResponseField>

<ResponseField name="model" type="string">
  チャット補完に使用されたモデル。
</ResponseField>

<ResponseField name="choices" type="object[]">
  チャット補完の選択肢リスト。`n`が1より大きい場合、複数になることがあります。

  <Expandable title="プロパティ">
    <ResponseField name="index" type="integer">
      選択肢リスト内のインデックス。
    </ResponseField>

    <ResponseField name="message" type="object">
      モデルによって生成されたチャット補完メッセージ。

      <Expandable title="プロパティ">
        <ResponseField name="role" type="string">
          このメッセージの作成者のロール。常に`assistant`。
        </ResponseField>

        <ResponseField name="content" type="string | null">
          メッセージの内容。
        </ResponseField>

        <ResponseField name="tool_calls" type="object[]">
          関数呼び出しなど、モデルによって生成されたツール呼び出し。

          <Expandable title="プロパティ">
            <ResponseField name="id" type="string">
              ツール呼び出しのID。
            </ResponseField>

            <ResponseField name="type" type="string">
              ツールのタイプ。現在、`function`のみサポートされています。
            </ResponseField>

            <ResponseField name="function" type="object">
              モデルが呼び出した関数。

              <Expandable title="プロパティ">
                <ResponseField name="name" type="string">
                  呼び出す関数の名前。
                </ResponseField>

                <ResponseField name="arguments" type="string">
                  モデルによってJSON形式で生成された、関数を呼び出すための引数。
                </ResponseField>
              </Expandable>
            </ResponseField>
          </Expandable>
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="finish_reason" type="string">
      モデルがトークン生成を停止した理由。`stop`、`length`、`tool_calls`、または`content_filter`。
    </ResponseField>

    <ResponseField name="logprobs" type="object | null">
      選択肢の対数確率情報。
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="usage" type="object">
  補完リクエストの使用統計。

  <Expandable title="プロパティ">
    <ResponseField name="prompt_tokens" type="integer">
      プロンプトのトークン数。
    </ResponseField>

    <ResponseField name="completion_tokens" type="integer">
      生成された補完のトークン数。
    </ResponseField>

    <ResponseField name="total_tokens" type="integer">
      使用されたトークンの合計数（プロンプト + 補完）。
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="system_fingerprint" type="string | null">
  モデルが実行されるバックエンド構成を表すフィンガープリント。`seed`パラメータと共に使用して、バックエンドの変更がいつ行われたかを把握できます。
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api-llm.sunra.ai/v1/chat/completions \
    -H "Authorization: Bearer <SUNRA_KEY>" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "messages": [
        {
          "role": "system",
          "content": "You are a helpful assistant."
        },
        {
          "role": "user",
          "content": "What is the capital of France?"
        }
      ]
    }'
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      "https://api-llm.sunra.ai/v1/chat/completions",
      headers={
          "Authorization": "Bearer <SUNRA_KEY>",
          "Content-Type": "application/json"
      },
      json={
          "model": "openai/gpt-4o",
          "messages": [
              {"role": "system", "content": "You are a helpful assistant."},
              {"role": "user", "content": "What is the capital of France?"}
          ]
      }
  )
  print(response.json())
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch("https://api-llm.sunra.ai/v1/chat/completions", {
    method: "POST",
    headers: {
      "Authorization": "Bearer <SUNRA_KEY>",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      model: "openai/gpt-4o",
      messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "What is the capital of France?" }
      ]
    })
  });
  const data = await response.json();
  console.log(data);
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "chatcmpl-abc123",
    "object": "chat.completion",
    "created": 1677652288,
    "model": "openai/gpt-4o",
    "choices": [
      {
        "index": 0,
        "message": {
          "role": "assistant",
          "content": "The capital of France is Paris."
        },
        "finish_reason": "stop",
        "logprobs": null
      }
    ],
    "system_fingerprint": "fp_44709d6fcb",
    "usage": {
      "prompt_tokens": 25,
      "completion_tokens": 8,
      "total_tokens": 33
    }
  }
  ```
</ResponseExample>
