亿速API文档中心

Chat Completions 图片生成

功能概览

本页用于说明 Chat Completions 图片生成 的核心能力、调用入口和接入要点,帮助开发者快速判断适用场景并完成集成。

适用场景

  • 产品原型验证:快速接入模型能力,验证内容生成、理解或编辑流程。
  • 生产业务接入:用于批量任务、自动化工作流和多模型组合调用。
  • 能力迁移适配:适合从原有模型或 SDK 平滑切换到亿速API统一接口。

接入建议

  • 优先确认模型名称、请求路径和响应字段,再接入具体业务流程。
  • 对异步任务、媒体生成和长耗时请求,建议在业务侧加入重试与状态轮询。
  • 上线前建议准备日志追踪、错误兜底和内容安全校验,便于稳定运行。

当前行为

网关会在发送给 worker 前给最后一条 user 消息追加英文生图兜底指令,避免 worker 只返回文字 prompt。

CHAT IMAGE GENERATION REQUIREMENTS:

固定兜底指令一定追加;参数转换段只有实际传入图片相关参数时才追加。后台请求详情中可以查看“原始 Prompt”和“发送给 Worker 的 Prompt”。

文本生图

curl "/v1/chat/completions" \
  -H "Authorization: Bearer <API_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-image-2",
    "messages": [
      {
        "role": "user",
        "content": "生成一张未来城市夜景图,霓虹灯,电影感"
      }
    ],
    "stream": false
  }'

响应示例:

{
  "id": "chatcmpl_xxx",
  "object": "chat.completion",
  "created": 1710000000,
  "model": "gpt-5.4-image-2",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "/7ed82c6454482a26808c3170a36/req_xxx_0_random.png"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 0,
    "completion_tokens": 0,
    "total_tokens": 0
  }
}

参考图生图

图片输入使用 OpenAI 多模态 image_url 格式。

curl "/v1/chat/completions" \
  -H "Authorization: Bearer <API_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-image-2",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "参考这张图片,生成一张相同主体但换成雪山背景的图片"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://example.com/input.png"
            }
          }
        ]
      }
    ],
    "stream": false
  }'

data URL 图片输入

curl "/v1/chat/completions" \
  -H "Authorization: Bearer <API_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-image-2",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "根据参考图生成一张新图"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "data:image/png;base64,..."
            }
          }
        ]
      }
    ]
  }'

传图片参数

图片生成参数可以放在顶层,也可以放在 extra_body 中。extra_body.image_config 是推荐的图片配置容器。

curl "/v1/chat/completions" \
  -H "Authorization: Bearer <API_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-image-2",
    "messages": [
      {
        "role": "user",
        "content": "生成一张淘宝电商主图,突出产品质感和卖点"
      }
    ],
    "extra_body": {
      "modalities": ["image"],
      "image_config": {
        "aspect_ratio": "1:1",
        "image_size": "2K"
      },
      "quality": "high",
      "style": "vivid",
      "output_format": "png",
      "background": "transparent"
    },
    "stream": false
  }'

实际发给 worker 的提示词会追加类似内容:

Request parameters converted into image requirements:
Requested output modalities: ["image"].
Image aspect ratio requirement: 1:1.
Image size tier requirement: 2K.
Quality requirement: high. Create a clear, detailed, sharp, high-quality image.
Style requirement: vivid.
Output format requirement: png.
Background requirement: transparent.

如果没有传这些参数,不会追加对应参数行,也不会出现 Request parameters converted into image requirements: 段。

流式响应

stream: true 支持 SSE 文本分片。worker 仍返回完整结果,网关再切分成 OpenAI 风格 SSE chunk。

curl -N "/v1/chat/completions" \
  -H "Authorization: Bearer <API_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-image-2",
    "messages": [
      {
        "role": "user",
        "content": "生成一张蓝色科幻飞船概念图"
      }
    ],
    "stream": true
  }'

参数支持状态

参数位置必填当前支持说明
model顶层支持模型名称
messages顶层支持OpenAI Chat Completions 消息数组
messages[].rolemessages支持常用 userassistantsystem
messages[].contentmessages支持字符串或多模态数组
stream顶层支持SSE 文本分片
modalities顶层或 extra_body转提示词输出模态要求,例如 ["image"]
image_config.aspect_ratio顶层或 extra_body转提示词图片比例,例如 1:116:92:3
image_config.image_size顶层或 extra_body转提示词图片大小档位,例如 1K2K
aspect_ratio顶层或 extra_body转提示词图片比例要求
image_size顶层或 extra_body转提示词图片大小档位要求
size顶层或 extra_body转提示词目标图片尺寸或规格
n顶层或 extra_body转提示词生成数量要求
quality顶层或 extra_body转提示词质量要求
style顶层或 extra_body转提示词风格要求
output_format / outputFormat顶层或 extra_body转提示词输出格式要求
output_compression / outputCompression顶层或 extra_body转提示词输出压缩要求
background顶层或 extra_body转提示词背景要求
moderation顶层或 extra_body转提示词审核偏好
其它自定义参数顶层或 extra_body转提示词会作为附加参数传给 worker

以下控制字段不会作为自定义参数重复追加到提示词:

model
messages
stream
extra_body
extraBody
image_config

图片输入格式

支持的图片输入:

https://example.com/image.png
http://example.com/image.jpg

推荐对象格式:

{
  "type": "image_url",
  "image_url": {
    "url": "https://example.com/image.png"
  }
}

常见错误

code说明
invalid_api_keytoken 无效或缺失
model_not_found模型不存在或未开放
worker_timeoutworker 处理超时
worker_errorworker 返回错误
worker_disconnectedworker 处理过程中断开
output_image_download_failedworker 返回的图片 URL 下载失败或不是图片