# 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. **编辑特异性**:对于编辑,清楚地描述要更改的内容和要保留的内容