Generation
图片生成示例
curl https://www.unikeyx.com/v1/images/generations \
-H "Authorization: Bearer YOUR_UNIKEYX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "MAI-Image-2",
"prompt": "一张高端咖啡豆礼盒的中文电商主图,白色背景,柔和棚拍光,真实产品摄影风格",
"width": 1024,
"height": 1024
}'
import base64
from openai import OpenAI
client = OpenAI(
api_key="YOUR_UNIKEYX_API_KEY",
base_url="https://www.unikeyx.com/v1",
)
result = client.images.generate(
model="gpt-image-2",
prompt="A clean editorial illustration of a small AI API gateway, with modern product UI details.",
size="1024x1024",
quality="high",
n=1,
)
with open("gpt-image-2-output.png", "wb") as file:
file.write(base64.b64decode(result.data[0].b64_json))
Editing
参考图改图示例
curl -sS https://www.unikeyx.com/v1/images/edits \
-H "Authorization: Bearer YOUR_UNIKEYX_API_KEY" \
-F "model=MAI-Image-2.5" \
-F "prompt=保留商品主体,替换为夏季户外露营场景,加入中文标题:清爽上新,真实商业摄影风格" \
-F "image=@./product-reference.png" \
| jq -r '.data[0].b64_json' \
| base64 --decode > mai-image-2-5-edit.png
import base64
from openai import OpenAI
client = OpenAI(
api_key="YOUR_UNIKEYX_API_KEY",
base_url="https://www.unikeyx.com/v1",
)
with open("product-reference.png", "rb") as image_file:
result = client.images.edit(
model="gpt-image-2",
image=image_file,
prompt="Keep the product shape and logo readable. Rebuild the scene as a premium e-commerce hero image.",
size="1024x1024",
quality="high",
)
with open("gpt-image-2-edit.png", "wb") as file:
file.write(base64.b64decode(result.data[0].b64_json))
Parameters
常用参数
MAI-Image-2、MAI-Image-2.5、gpt-image-2。
768,总像素不超过 1,048,576。
宽x高 字符串:16 像素步进,单边小于等于 3840,总像素 655,360~8,294,400,比例限制在 1:3~3:1。
auto、low、medium、high。MAI 模型不建议传。
multipart/form-data 上传 PNG 或 JPEG 等有效图片文件。
FAQ
图片生成 FAQ
图片返回 base64 而不是 URL
不同上游和通道配置可能返回不同图片字段。客户端建议同时兼容 url 和 b64_json。
gpt-image-2 的 size 应该怎么填?
使用 宽x高 字符串,例如 1024x1024、1024x1536、
1536x1024、2048x2048、3840x2160、
2160x3840、1024x3072。
宽高都应能被 16 整除;单边小于等于 3840;总像素需在
655,360~8,294,400 之间;宽高比限制在 1:3~3:1。
gpt-image-2 的 quality 应该怎么填?
常用 auto、low、medium、high。
不确定时用 auto;最终出图可用 high。
MAI-Image-2 的尺寸应该怎么填?
MAI-Image-2 使用 width 和 height 整数字段。
宽高各不低于 768,总像素不超过 1,048,576。
常用方图 1024x1024,横图 1024x768,
竖图 768x1024。不要把 GPT 的 size 字段直接复制到 MAI 示例里。
MAI 图片模型要传 quality 吗?
不建议传 quality。想提升效果,优先优化提示词和参考图质量。
400: Unknown parameter response_format
使用 gpt-image-2 做图片编辑时,不要传 response_format。
删除旧版 DALL-E 示例中的 response_format 后重试。
400: Invalid image file or mode
先只保留一张图调用,定位是哪张文件触发错误。建议重新导出为普通 RGB PNG 或 JPEG, 并确认文件能正常打开。
生图报 The requested operation is unsupported
通常是把图片模型发到了 /chat/completions。纯生图请用
/images/generations,图片编辑请用 /images/edits。