Skip to content

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 URLhttps://open.dieyuyun.com
兼容协议OpenAI Images API

认证

Bearer Token: Authorization: Bearer YOUR_API_KEY

标准请求字段

字段类型必填默认值说明
modelstring模型标识: grok-imagine
promptstring图像描述文本
sizestring1024x1024图像尺寸
qualitystringstandard生成质量: standardhd
ninteger1生成数量
response_formatstringurl返回格式: urlb64_json
asyncbooleanfalse启用异步模式,返回 taskId 用于查询结果,详见 异步任务

支持的尺寸

尺寸比例说明
1024x10241:1方形
1536x10243:2横版
1024x15362: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
}
字段类型说明
idstring任务 ID,用于查询任务状态
statusstring当前状态: queued / processing
progressinteger进度百分比 (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"
    }
  ]
}
字段类型说明
createdintegerUnix 时间戳
dataarray生成结果数组
data[].urlstring图像 URL
data[].revised_promptstring优化后的提示词

错误响应

详见 错误码

最佳实践

  • 创意场景: Grok Imagine 在超现实、创意和幻想类场景上表现尤为出色,适合创作艺术性强的图像。
  • 风格描述: 在提示词中明确指定艺术风格(如"surreal art style"、"oil painting"、"digital art"等)可获得更精准的效果。
  • 英文提示词: Grok Imagine 对英文提示词的理解更好,建议使用英文描述以获得最佳效果。
  • 高分辨率: 使用 hd 质量档位可获得更精细的图像细节。

速率限制

详见 速率限制

相关文档