Grok Imagine 图像生成
xAI 推出的图像生成模型,以独特的创意风格和高质量的图像输出著称。
在线体验
快速开始
python
from openai import OpenAI
client = OpenAI(
base_url="https://open.dieyuyun.com/v1",
api_key="YOUR_API_KEY"
)
response = client.images.generate(
model="grok-imagine",
prompt="An astronaut riding a giant koi fish through a nebula, surreal art style",
size="1024x1024",
n=1
)
print(response.data[0].url)请求端点
| 项目 | 值 |
|---|---|
| 方法 | POST |
| 路径 | /v1/images/generations |
| Base URL | https://open.dieyuyun.com |
| 兼容协议 | OpenAI Images API |
认证
Bearer Token: Authorization: Bearer YOUR_API_KEY
标准请求字段
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| model | string | 是 | — | 模型标识: grok-imagine |
| prompt | string | 是 | — | 图像描述文本 |
| size | string | 否 | 1024x1024 | 图像尺寸 |
| quality | string | 否 | standard | 生成质量: standard、hd |
| n | integer | 否 | 1 | 生成数量 |
| response_format | string | 否 | url | 返回格式: url 或 b64_json |
| async | boolean | 否 | false | 启用异步模式,返回 taskId 用于查询结果,详见 异步任务 |
支持的尺寸
| 尺寸 | 比例 | 说明 |
|---|---|---|
1024x1024 | 1:1 | 方形 |
1536x1024 | 3:2 | 横版 |
1024x1536 | 2:3 | 竖版 |
请求示例
bash
curl https://open.dieyuyun.com/v1/images/generations \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine",
"prompt": "An astronaut riding a giant koi fish through a nebula, surreal art style",
"size": "1024x1024",
"n": 1
}'python
from openai import OpenAI
client = OpenAI(
base_url="https://open.dieyuyun.com/v1",
api_key="YOUR_API_KEY"
)
response = client.images.generate(
model="grok-imagine",
prompt="An astronaut riding a giant koi fish through a nebula, surreal art style",
size="1024x1024",
n=1
)
print(response.data[0].url)javascript
import OpenAI from 'openai'
const client = new OpenAI({
baseURL: 'https://open.dieyuyun.com/v1',
apiKey: 'YOUR_API_KEY',
})
const response = await client.images.generate({
model: 'grok-imagine',
prompt: 'An astronaut riding a giant koi fish through a nebula, surreal art style',
size: '1024x1024',
n: 1,
})
console.log(response.data[0].url)异步模式
在请求中添加 "async": true 即可启用异步模式。异步请求不会直接返回生成结果,而是返回一个 taskId,用于后续查询任务进度和获取结果。
json
{
"id": "task_abc123",
"status": "queued",
"progress": 0
}| 字段 | 类型 | 说明 |
|---|---|---|
| id | string | 任务 ID,用于查询任务状态 |
| status | string | 当前状态: queued / processing |
| progress | integer | 进度百分比 (0-100) |
任务完成后,通过 GET /v1/tasks/{taskId} 获取结果,返回格式与同步响应一致。
详见 异步任务 了解完整的轮询流程和最佳实践。
响应格式
成功响应
json
{
"created": 1717000000,
"data": [
{
"url": "https://...",
"revised_prompt": "A surreal artwork of an astronaut in a white spacesuit riding a magnificent golden koi fish, swimming through a colorful nebula filled with purple and blue cosmic dust, stars twinkling in the background"
}
]
}| 字段 | 类型 | 说明 |
|---|---|---|
| created | integer | Unix 时间戳 |
| data | array | 生成结果数组 |
| data[].url | string | 图像 URL |
| data[].revised_prompt | string | 优化后的提示词 |
错误响应
详见 错误码。
最佳实践
- 创意场景: Grok Imagine 在超现实、创意和幻想类场景上表现尤为出色,适合创作艺术性强的图像。
- 风格描述: 在提示词中明确指定艺术风格(如"surreal art style"、"oil painting"、"digital art"等)可获得更精准的效果。
- 英文提示词: Grok Imagine 对英文提示词的理解更好,建议使用英文描述以获得最佳效果。
- 高分辨率: 使用
hd质量档位可获得更精细的图像细节。
速率限制
详见 速率限制。
相关文档
- GPT Image 2 - OpenAI 图像生成模型
- Gemini Flash Image - Google 图像生成模型
- 模型列表与定价 - 浏览可用模型和定价信息
- 使用 Playground 测试 - 交互式测试图像生成