xAI の大規模言語モデル Grok 4.7 の API リファレンスです。
モデルの種類
この API は次の 1 種類のモデルに対応しています。
機能一覧
Grok 4.7
エンドポイント {#}
POST /api/v1/chat/completions
リクエストパラメータ
必須パラメータ
対応するメッセージ内容
任意パラメータ
リクエスト例
const response = await fetch('https://api.flaq.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Accept': 'text/event-stream',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'grok-4.7',
messages: [
{
role: 'user',
content: 'Explain how a browser caches an image in two sentences.'
}
],
stream: true,
max_tokens: 2048,
reasoning_effort: 'medium'
})
});
レスポンス形式
Grok 4.7 は OpenAI 互換のチャット補完レスポンスを返します。stream: true の場合はサーバー送信イベント(SSE)で応答します。stream: false を指定するか stream を省略すると、単一の JSON オブジェクトを返します。
成功時のレスポンス
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.7","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.7","choices":[{"index":0,"delta":{"reasoning_content":"Explain cache reuse."},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.7","choices":[{"index":0,"delta":{"content":"The browser saves a copy of the image. It reuses the copy while it is fresh."},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.7","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
エラーレスポンス
data: {"error":{"message":"API requests too frequent, exceeding rate limit","type":"rate_limit_error","code":"1302","param":null}}
ベストプラクティス
- 構造化メッセージを使用する:
messages[] で会話履歴を送信します。
- メッセージ内容に画像を添付する:
messages[].content に image_url ブロックを追加します。
- SSE イベントを処理する:
choices[0].delta.content の値を順に連結し、data: [DONE] をストリームの終了として扱います。