GPT Image 2 API 接入
本页说明如何通过本平台的 OpenAI Images 兼容代理调用 gpt-image-2。适合需要直接做文生图、参考图生成或蒙版编辑的外部开发者。
本页只覆盖 /v1/images/* 代理接口,不覆盖需要 Portal 登录态的 /api/image-gen/* 产品接口,也不覆盖 /v1/responses 的 image-generation tool。
准备条件
请求需要使用本平台的 Local API Key:
export BASE_URL="https://<your-host>"
export JQKAI_API_KEY="sk-abcredai-..."
发起请求前,请确认:
- Local API Key 已启用,并绑定到正确的 Route Group。
- Route Group 已选择可用的 OpenAI Images supply。
- 该 supply 当前可路由,并支持
gpt-image-2。
JQKAI_API_KEY 只用于向本平台认证。不要把 upstream API key 放进客户端、日志、截图或工单。
Endpoint 与 Path
| 用途 | 方法 | Path | Content-Type |
|---|---|---|---|
| 文生图 | POST |
/v1/images/generations |
application/json |
| 图生图 / 蒙版编辑 | POST |
/v1/images/edits |
multipart/form-data |
请求地址由 ${BASE_URL} 和 path 组成,例如 ${BASE_URL}/v1/images/generations。
文生图
POST /v1/images/generations 接收 JSON body。model 和 prompt 必填;size、quality、output_format 等图片参数会按当前 OpenAI Images supply 的能力转发。
curl -sS -X POST "${BASE_URL}/v1/images/generations" \
-H "Authorization: Bearer ${JQKAI_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A clean editorial illustration of a blue gateway on a white desk, soft daylight, minimal composition.",
"size": "1024x1024",
"quality": "medium",
"output_format": "png"
}' \
-o response.json
成功响应按上游 OpenAI Images JSON shape 返回。gpt-image-2 的常见响应会在 data[0].b64_json 中包含图片内容:
jq -r '.data[0].b64_json' response.json | base64 --decode > gateway.png
macOS 的 BSD base64 可改为:
jq -r '.data[0].b64_json' response.json | base64 -D > gateway.png
图生图与蒙版编辑
POST /v1/images/edits 使用 multipart/form-data。model、prompt 和至少一个 image 文件必填。
本平台以字段名 image 接收参考图。提交多张参考图时,重复传递 image 字段;不要使用 image[]。mask 为可选文件,用于指定希望修改的区域。
curl -sS -X POST "${BASE_URL}/v1/images/edits" \
-H "Authorization: Bearer ${JQKAI_API_KEY}" \
-F "model=gpt-image-2" \
-F "prompt=Turn the scene into a warm sunset illustration while keeping the building layout." \
-F "image=@source.png" \
-F "mask=@mask.png" \
-F "size=1024x1024" \
-F "quality=medium" \
-F "output_format=png" \
-o response.json
如果不需要局部编辑,去掉 -F "mask=@mask.png" 即可。需要多张参考图时,重复 image:
curl -sS -X POST "${BASE_URL}/v1/images/edits" \
-H "Authorization: Bearer ${JQKAI_API_KEY}" \
-F "model=gpt-image-2" \
-F "prompt=Create a cohesive product scene that uses the colors and materials in both references." \
-F "image=@product.png" \
-F "image=@palette.png" \
-F "size=1024x1024" \
-F "quality=medium" \
-F "output_format=png" \
-o response.json
与文生图相同,可从 data[0].b64_json 解码输出:
jq -r '.data[0].b64_json' response.json | base64 --decode > edited.png
支持范围
- 仅支持
POST /v1/images/generations和POST /v1/images/edits。 - 不提供
/v1/images/variations。 - 不把
/v1/responses的 image-generation tool 作为本页的兼容范围。 - 成功时保持上游 OpenAI Images 响应 shape;参数是否可用仍取决于当前命中的
gpt-image-2supply。
常见问题
| 现象 | 优先检查 |
|---|---|
401 或认证失败 |
Authorization 是否为 Bearer sk-abcredai-...,Local API Key 是否仍启用。 |
| 当前 Route Group 没有可用图片供给 | 为 Local API Key 绑定正确的 Route Group,并在该组中选择可路由、支持 gpt-image-2 的 OpenAI Images supply。系统不会组外 fallback。 |
supply_model_not_supported |
当前命中的 supply 没有配置或不支持 gpt-image-2;在 supply 的模型目录和 Route Group 选择中修正配置。 |
| 上游返回错误 | 检查请求参数、图片文件和当前 supply 的能力;上游失败会按实际 HTTP 状态返回,不应通过重试改用组外 supply。 |
| edit 请求提示缺少图片 | 至少传一个 -F "image=@<file>";字段名必须是 image。 |