# Grok Imagine API xAI 提供的 Grok Imagine 圖像生成模型的完整 API 參考文件。 ## 模型變體 此 API 支援兩種模型變體: | 模型 | ID | 說明 | |-------|-----|-------------| | **Grok Imagine** | `grok-imagine` | 文字生成圖像 | | **Grok Imagine Edit** | `grok-imagine-edit` | 基於指令的圖像編輯 | ## 快速對比 | 功能 | Grok Imagine | Grok Imagine Edit | |---------|--------------|-------------------| | 輸入 | 文字提示詞 | 圖像 + 文字提示詞 | | 圖像輸入 | 否 | 必需(`image_url_list`,1-5 張) | ## Grok Imagine(文字生成圖像) ### 端點 ``` POST /api/v1/image/task ``` ### 請求參數 #### 必填參數 | 參數 | 類型 | 說明 | |-----------|------|-------------| | `model_name` | string | 必須為 `"grok-imagine"` | | `prompt` | string | 圖像生成提示詞 | | `width` | number | 寬度比例值(例如 16:9 中的 16) | | `height` | number | 高度比例值(例如 16:9 中的 9) | ### 請求示例 ```javascript const response = await fetch('https://api.flaq.ai/api/v1/image/task', { method: 'POST', headers: { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' }, body: JSON.stringify({ model_name: 'grok-imagine', prompt: 'A surreal landscape with floating islands and waterfalls', width: 16, height: 9 }) }); const { data } = await response.json(); const taskId = data.task_id; // Poll for results const pollResponse = await fetch( `https://api.flaq.ai/api/v1/image/${taskId}`, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } } ); const result = await pollResponse.json(); console.log(result.data.task_result.images[0].url); ``` ## Grok Imagine Edit(圖像編輯) ### 請求參數 #### 必填參數 | 參數 | 類型 | 說明 | |-----------|------|-------------| | `model_name` | string | 必須為 `"grok-imagine-edit"` | | `prompt` | string | 期望編輯的說明 | | `width` | number | 寬高比中的寬度值(預設 `1:1` 時為 `1`) | | `height` | number | 寬高比中的高度值(預設 `1:1` 時為 `1`) | | `image_url_list` | string[] | 要編輯的輸入圖像 URL 陣列(1-5 張) | ### 請求示例 ```javascript { model_name: 'grok-imagine-edit', width: 1, height: 1, image_url_list: ['https://example.com/photo.jpg'], prompt: 'Change the sky to a dramatic sunset with orange and purple hues' } ``` ## 回應格式 ### 初始回應 ```json { "code": 0, "data": { "task_id": "{task_id}", "task_status": "submitted", "response_url": "https://api.flaq.ai/api/v1/image/{task_id}" }, "message": "success" } ``` ### 輪詢回應 輪詢 `GET /api/v1/image/{task_id}` 以檢查狀態: ```json { "code": 0, "data": { "task_id": "{task_id}", "task_status": "succeed", "task_status_msg": null, "response_url": "https://api.flaq.ai/api/v1/image/{task_id}", "task_result": { "credit": 0.0997, "images": [ { "url": "https://example.com/generated-image.jpeg" } ] } }, "message": "success" } ``` ### 狀態值 - `submitted`:任務已接受 - `processing`:圖像生成进行中 - `succeed`:圖像生成完成 - `failed`:圖像生成失敗 ## 處理時間 典型處理時間:15-60 秒。 ## 最佳實踐 1. **創意提示詞**:Grok Imagine 对富有想象力、详细的說明回應良好 2. **編輯特异性**:对于編輯,清楚地說明要更改的內容和要保留的內容