POST /v1/chat/completions
POST
/v1/chat/completionsチャット補完を作成します。テキスト、マルチモーダル入力、ストリーミングに対応しています。
リクエスト
ヘッダー
| ヘッダー | 値 |
|---|---|
Authorization | Bearer {api_key} |
Content-Type | application/json |
ボディ
{
"model": "gemini-3-flash-preview",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "Hello, world!" }
],
"stream": false,
"temperature": 0.7,
"max_tokens": 1024
}
パラメーター
| パラメーター | 型 | 必須 | 説明 |
|---|---|---|---|
model | string | はい | モデル ID。対応モデルを参照してください。 |
messages | array | はい | メッセージオブジェクトの配列。 |
stream | boolean | いいえ | JSON レスポンスは false(デフォルト)。SSE ストリーミングは true。 |
temperature | number | いいえ | サンプリング温度。0 から 2。 |
top_p | number | いいえ | nucleus sampling。0 から 1。 |
max_tokens | integer | いいえ | 生成する最大トークン数。 |
stop | string | string[] | いいえ | 最大 4 個の停止シーケンス。 |
n | integer | いいえ | 生成する補完数。 |
presence_penalty | number | いいえ | -2.0 から 2.0。 |
frequency_penalty | number | いいえ | -2.0 から 2.0。 |
user | string | いいえ | 不正利用検知のためのエンドユーザー識別子。 |
メッセージオブジェクト
| フィールド | 型 | 必須 | 説明 |
|---|---|---|---|
role | string | はい | "system"、"user"、または "assistant"。 |
content | string | array | はい | テキスト文字列、またはマルチモーダル用の content part 配列。 |
name | string | いいえ | 参加者名。 |
Content parts(マルチモーダル)
content が配列の場合、各要素は content part です。
Text: { "type": "text", "text": "Describe this image." }
Image: { "type": "image_url", "image_url": { "url": "https://..." } }
Audio: { "type": "input_audio", "input_audio": { "data": "<base64>", "format": "webm" } }
レスポンス(非ストリーミング)
{
"id": "chatcmpl-abc123",
"object": "text_completion",
"created": 1709123456,
"model": "gemini-3-flash-preview",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 9,
"total_tokens": 21
}
}
レスポンスフィールド
| フィールド | 型 | 説明 |
|---|---|---|
id | string | 一意の補完 ID。 |
object | string | 常に "text_completion"。 |
created | integer | Unix タイムスタンプ。 |
model | string | 使用されたモデル。 |
choices[].message | object | { role, content }。 |
choices[].finish_reason | string | "stop"、"length"、または "content_filter"。 |
usage.prompt_tokens | integer | 入力トークン数。 |
usage.completion_tokens | integer | 出力トークン数。 |
usage.total_tokens | integer | 入力 + 出力の合計。 |
レスポンス(ストリーミング)
stream: true の場合、レスポンスは Server-Sent Events のストリームになります。
Content-Type: text/event-stream
data: {"id":"chatcmpl-abc123","object":"text_completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"text_completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"text_completion.chunk","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":"stop"}],"usage":{"prompt_tokens":12,"completion_tokens":2,"total_tokens":14}}
data: [DONE]
最後の chunk には、累積トークン数を含む usage が入ります。
ステータスコード
| ステータス | 説明 |
|---|---|
200 | 成功 |
400 | 不正なリクエスト(model/messages の不足、モデルタイプの不一致など) |
401 | 認証なし |
402 | 残高不足 |
502 | 上流プロバイダーエラー |
例
基本的なチャット
curl -X POST https://api.modelmax.io/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MODELMAX_API_KEY" \
-d '{
"model": "deepseek-v3.2",
"messages": [
{ "role": "user", "content": "What is 2+2?" }
]
}'
from openai import OpenAI
client = OpenAI(api_key="your-key", base_url="https://api.modelmax.io/v1")
response = client.chat.completions.create(
model="deepseek-v3.2",
messages=[{"role": "user", "content": "What is 2+2?"}],
)
print(response.choices[0].message.content)
import OpenAI from "openai";
const client = new OpenAI({ apiKey: "your-key", baseURL: "https://api.modelmax.io/v1" });
const response = await client.chat.completions.create({
model: "deepseek-v3.2",
messages: [{ role: "user", content: "What is 2+2?" }],
});
console.log(response.choices[0].message.content);
画像入力つき
curl -X POST https://api.modelmax.io/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MODELMAX_API_KEY" \
-d '{
"model": "gemini-3-flash-preview",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "What is in this image?" },
{ "type": "image_url", "image_url": { "url": "https://example.com/cat.jpg" } }
]
}
]
}'
response = client.chat.completions.create(
model="gemini-3-flash-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/cat.jpg"}},
],
}
],
)
const response = await client.chat.completions.create({
model: "gemini-3-flash-preview",
messages: [
{
role: "user",
content: [
{ type: "text", text: "What is in this image?" },
{ type: "image_url", image_url: { url: "https://example.com/cat.jpg" } },
],
},
],
});
