Grok 4.5 API
由 xAI 提供支持的 Grok 4.5 LLM 模型完整 API 参考。
模型变体
此 API 支持两种 Grok 4.5 模型变体:
快速比较
Grok 4.5 文本转文本
端点
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.5-text-to-text',
messages: [
{
role: 'user',
content: 'Explain the key differences between REST and GraphQL APIs.'
}
],
stream: true,
max_tokens: 2048
})
});
Grok 4.5 图片转文本
端点
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.5-image-to-text',
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe the image and extract any visible text.' },
{
type: 'image_url',
image_url: {
url: 'https://example.com/sample-image.jpg'
}
}
]
}
],
stream: true,
max_tokens: 2048
})
});
响应格式
Grok 4.5 LLM 模型返回与 OpenAI 兼容的补全响应。使用 stream: true 时,响应为服务器发送事件;使用 stream: false 时,响应为单个 JSON 对象。
成功响应
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.5-text-to-text","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.5-text-to-text","choices":[{"index":0,"delta":{"content":"Here is a concise explanation"},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.5-text-to-text","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.5-text-to-text","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":8,"total_tokens":20}}
data: [DONE]
错误响应
event: error
data: {"error":{"message":"API requests too frequent, exceeding rate limit","type":"rate_limit_error","code":"1302","param":null}}
最佳实践
- 使用结构化消息: 通过
messages[] 发送对话历史记录,而不是将上下文合并到一个提示词中。
- 选择正确的模型变体: 纯文本推理使用文本转文本,需要理解图片时使用图片转文本。
- 接收 SSE 事件: 将
choices[0].delta.content 追加用于流式显示,并将 data: [DONE] 视为成功完成。
- 仅发送受支持的输入: 文本转文本不支持图片或文件附件。