xAI 的 Grok 4.7 大型語言模型完整 API 參考文件。
模型版本
此 API 支援一種模型版本:
功能一覽
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] 作為串流結束標記。