# Grok 4.6 API xAI 提供的 Grok 4.6 LLM 模型完整 API 参考。 ## 模型变体 此 API 支持两种 Grok 4.6 模型变体: | 模型 | ID | 描述 | |-------|----|-------------| | **Grok 4.6 Text to Text** | `grok-4.6-text-to-text` | 最新的 Grok 文本推理与生成 | | **Grok 4.6 Image to Text** | `grok-4.6-image-to-text` | 最新的 Grok 图像理解与分析 | ## 快速对比 | 功能 | 文本转文本 | 图像转文本 | |---------|--------------|---------------| | 文本输入 | 必填 ✅ | 可选 | | 图像输入 | 不支持 ❌ | 可选 | | 文件输入 | 不支持 ❌ | 不支持 ❌ | | 流式响应 | SSE | SSE | | `max_tokens` | 可选 | 可选 | ## Grok 4.6 Text to Text ### 接口端点 ```http POST /api/v1/chat/completions ``` ### 请求参数 #### 必填参数 | 参数 | 类型 | 描述 | |-----------|------|-------------| | `model` | string | 必须为 `"grok-4.6-text-to-text"`。 | | `messages` | array | 按顺序排列的消息。消息结构请参阅 LLM API。 | | `stream` | boolean | `true` 表示 SSE,`false` 表示单个 JSON 响应。 | #### 消息支持 | 字段 | 文本 | 文件 | 图像 | |-------|------|------|-------| | `messages` | 必填 ✅ | 不支持 ❌ | 不支持 ❌ | #### 可选参数 | 参数 | 类型 | 范围 | 描述 | |-----------|------|-------|-------------| | `max_tokens` | integer | `> 1` | 最大输出 token 数。 | ### 请求示例 ```javascript 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.6-text-to-text', messages: [ { role: 'user', content: 'Explain the key differences between REST and GraphQL APIs.' } ], stream: true, max_tokens: 2048 }) }); ``` ## Grok 4.6 Image to Text ### 接口端点 ```http POST /api/v1/chat/completions ``` ### 请求参数 #### 必填参数 | 参数 | 类型 | 描述 | |-----------|------|-------------| | `model` | string | 必须为 `"grok-4.6-image-to-text"`。 | | `messages` | array | 按顺序排列的消息。请参阅 LLM API 和消息附件文档。 | | `stream` | boolean | `true` 表示 SSE,`false` 表示单个 JSON 响应。 | #### 消息支持 | 字段 | 文本 | 文件 | 图像 | |-------|------|------|-------| | `messages` | 可选 | 不支持 ❌ | 可选 | #### 可选参数 | 参数 | 类型 | 范围 | 描述 | |-----------|------|-------|-------------| | `max_tokens` | integer | `> 1` | 最大输出 token 数。 | ### 请求示例 ```javascript 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.6-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.6 LLM 模型返回兼容 OpenAI 的补全响应。使用 `stream: true` 时,响应采用 Server-Sent Events;使用 `stream: false` 时,响应为单个 JSON 对象。 ### 成功响应 ```text data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.6-text-to-text","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]} data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.6-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.6-text-to-text","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]} data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.6-text-to-text","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":8,"total_tokens":20}} data: [DONE] ``` ### 错误响应 ```text event: error data: {"error":{"message":"API requests too frequent, exceeding rate limit","type":"rate_limit_error","code":"1302","param":null}} ``` ## 最佳实践 1. **使用结构化消息:** 通过 `messages[]` 发送对话历史,而不是将上下文合并到一个提示词中。 2. **选择合适的模型变体:** 纯文本推理使用 Text to Text,需要理解图像时使用 Image to Text。 3. **处理 SSE 事件:** 追加 `choices[0].delta.content` 以实现流式显示,并将 `data: [DONE]` 视为成功完成。 4. **仅发送支持的输入:** Text to Text 不支持图像或文件附件。