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] 作为流结束标记。