# 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 ### API 端點 ```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 ### API 端點 ```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 不支援圖像或檔案附件。