外观
图像生成
根据文本提示生成图像,兼容 OpenAI 的 POST /v1/images/generations 接口。同时支持传入 image 进行图生图 / 参考图编辑(网关扩展,JSON 透传)。
- 方法:
POST /v1/images/generations - 鉴权:必须使用 API Key(
agent = "api") - 来源:
manager/router.go→ImagesRelayAPI(manager/images.go)
请求体
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | ✅ | 图像模型标识,如 dall-e-3、gpt-image-2。带 -official 后缀会被自动剥离 |
prompt | string | ✅ | 图像描述,去空格后不能为空 |
image | string | string[] | ❌ | 输入图片(图生图)。每项为图片 URL(http/https)或 base64 data-URL;原样透传 |
size | string | ❌ | 尺寸,如 1024x1024、2K、auto 或任意 WIDTHxHEIGHT |
response_format | string | ❌ | url 或 b64_json |
output_format | string | ❌ | png / jpeg / webp |
background | string | ❌ | transparent / opaque / auto |
watermark | any | ❌ | 水印开关/内容(渠道专属),原样透传 |
quality | string | ❌ | 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": ""
}
]
}响应字段
| 字段 | 类型 | 说明 |
|---|---|---|
created | int64 | 时间戳 |
data | RelayImageData[] | 生成结果数组(长度对应 n) |
data[].url | string | 图片 URL(若上游返回图片链接则透出此字段) |
data[].b64_json | string | 图片 Base64(若上游返回 Base64 数据则透出此字段) |
url 与 b64_json 二选一
url 与 b64_json 至少其一非空,否则返回 image_generation_error。
错误
| 错误类型 | 触发场景 |
|---|---|
invalid_request_error | model 或 prompt 为空 |
authentication_error | 未携带 Key 或非 api 身份 |
quota_exceeded_error | 额度不足 |
image_generation_error | 上游未返回可用图片 |
