Skip to content

图像生成 ​

根据文本提示生成图像,兼容 OpenAI 的 POST /v1/images/generations 接口。同时支持传入 image 进行图生图 / 参考图编辑(网关扩展,JSON 透传)。

  • 方法:POST /v1/images/generations
  • 鉴权:必须使用 API Key(agent = "api")
  • 来源:manager/router.go → ImagesRelayAPI(manager/images.go)

请求体 ​

字段类型必填说明
modelstring✅图像模型标识,如 dall-e-3、gpt-image-2。带 -official 后缀会被自动剥离
promptstring✅图像描述,去空格后不能为空
imagestring | string[]❌输入图片(图生图)。每项为图片 URL(http/https)或 base64 data-URL;原样透传
sizestring❌尺寸,如 1024x1024、2K、auto 或任意 WIDTHxHEIGHT
response_formatstring❌url 或 b64_json
output_formatstring❌png / jpeg / webp
backgroundstring❌transparent / opaque / auto
watermarkany❌水印开关/内容(渠道专属),原样透传
qualitystring❌low / medium / high / auto 等

每种模型可能支持的参数不一致。除上述字段外的其他 JSON 字段(如 style、moderation、output_compression、input_fidelity 等) 不会被丢弃,会被捕获到内部并透传给上游,可根据模型官方文档调整。

请求示例(文生图) ​

bash
curl https://app.1in2.top/api/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-your-api-key-here" \
  -d '{
    "model": "dall-e-3",
    "prompt": "一只戴帽子的小猫,水彩风格",
    "size": "1024x1024",
    "quality": "hd",
    "seed": 0,
    "steps": 50
  }'
python
from openai import OpenAI

client = OpenAI(
    api_key="sk-your-api-key-here",
    base_url="https://app.1in2.top/api/v1",
)

image = client.images.generate(
    model="dall-e-3",
    prompt="一只戴帽子的小猫,水彩风格",
    size="1024x1024",
    quality="hd",
    extra_body={
        "seed": 0,      # 随机种子
        "steps": 50     # 采样步数
    }
)
print(image.data[0].url)

请求示例(图生图) ​

图生图通过 extra_body 透传 image(与火山方舟/豆包 Seedream 等 OpenAI 兼容供应商一致):

python
from openai import OpenAI

client = OpenAI(
    api_key="sk-your-api-key-here",
    base_url="https://app.1in2.top/api/v1",
)

image = client.images.generate(
    model="doubao-seedream-5-0-pro",
    prompt="根据手绘草图对图像进行编辑,移除草图线条。",
    size="2K",
    extra_body={
        "image": "https://example.com/input.png",
        "watermark": False,
    },
)
print(image.data[0].url)

响应体 ​

json
{
  "created": 1700000000,
  "data": [
    {
      "url": "https://example.com/generated/xxx.png",
      "b64_json": ""
    }
  ]
}

响应字段 ​

字段类型说明
createdint64时间戳
dataRelayImageData[]生成结果数组(长度对应 n)
data[].urlstring图片 URL(若上游返回图片链接则透出此字段)
data[].b64_jsonstring图片 Base64(若上游返回 Base64 数据则透出此字段)

url 与 b64_json 二选一

url 与 b64_json 至少其一非空,否则返回 image_generation_error。

错误 ​

错误类型触发场景
invalid_request_errormodel 或 prompt 为空
authentication_error未携带 Key 或非 api 身份
quota_exceeded_error额度不足
image_generation_error上游未返回可用图片