跳到主要内容

图像生成

MoziaVerse 提供 OpenAI Images 兼容接口,用统一的请求方式调用图像生成与图像编辑模型。

快速开始

基础地址:

https://mzsjai.com

所有请求都必须携带 API Key:

Authorization: Bearer <YOUR_API_KEY>

请只在服务端保存 API Key,不要将其写入浏览器代码、公开仓库或日志。

生成图像

curl --fail-with-body \
'https://mzsjai.com/v1/images/generations' \
-H "Authorization: Bearer ${MOZIA_API_KEY}" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen/qwen-image",
"prompt": "A blue ceramic teapot on a white table, soft studio lighting",
"size": "1328x1328",
"n": 1
}'

成功响应中的 data[].b64_json 是 Base64 编码的图片内容。

接口一览

方法路径说明
POST/v1/images/generations根据文本生成图像
POST/v1/images/edits根据图片和提示词编辑图像

选择模型

当前支持:

模型 ID能力接口
qwen/qwen-image文生图/v1/images/generations
qwen/qwen-image-edit图像编辑、多图参考/v1/images/edits

模型列表、可用状态、支持尺寸和计费可能调整,请以控制台模型目录为准。单次生成多张图片时,通常按实际生成数量计费;当前建议使用 n=1

Qwen 模型的完整参数、尺寸与编辑示例请参阅 Qwen Image

文本生成图像

POST /v1/images/generations
Content-Type: application/json

请求参数

字段类型必填默认/建议值说明
modelstring-控制台模型目录中的完整模型 ID
promptstring-图像描述
sizestring1328x1328输出尺寸
ninteger1生成数量,必须大于 0
seedinteger随机固定后有助于复现
negative_promptstring不希望出现的内容
response_formatstringb64_json当前统一返回 Base64 图片
num_inference_stepsinteger模型默认值模型扩展参数;推理步数
guidance_scalenumber模型默认值模型扩展参数;提示词引导强度
true_cfg_scalenumber模型默认值模型扩展参数;True CFG 强度

num_inference_stepsguidance_scaletrue_cfg_scale 并非所有图像模型都支持。跨模型接入时,优先使用通用参数,并按模型目录补充扩展参数。

编辑图像

图像编辑使用 multipart/form-data。输入图片可以通过文件上传或 HTTP(S) URL 提供;至少选择一种方式。

POST /v1/images/edits
Content-Type: multipart/form-data

请求参数

字段类型必填说明
modelstring支持图像编辑的完整模型 ID
promptstring编辑要求
imagefile二选一PNG 或 JPEG 文件;可重复传递
urlstring二选一HTTP(S) 图片地址;可重复传递
sizestring输出尺寸
ninteger生成数量,默认 1
seedinteger随机种子
negative_promptstring不希望出现的内容
response_formatstring当前统一使用 b64_json
num_inference_stepsinteger模型扩展参数;推理步数
guidance_scalenumber模型扩展参数;提示词引导强度
true_cfg_scalenumber模型扩展参数;True CFG 强度

当前 qwen/qwen-image-edit 最多支持 8 张参考图片。

上传文件

curl --fail-with-body \
'https://mzsjai.com/v1/images/edits' \
-H "Authorization: Bearer ${MOZIA_API_KEY}" \
-F 'model=qwen/qwen-image-edit' \
-F 'prompt=Change the blue teapot to red while keeping the composition unchanged' \
-F 'image=@input.jpg;type=image/jpeg' \
-F 'size=1328x1328' \
-F 'n=1'

多图输入时重复传递 image

curl --fail-with-body \
'https://mzsjai.com/v1/images/edits' \
-H "Authorization: Bearer ${MOZIA_API_KEY}" \
-F 'model=qwen/qwen-image-edit' \
-F 'prompt=Combine the subjects from both references into one scene' \
-F 'image=@reference-1.jpg;type=image/jpeg' \
-F 'image=@reference-2.png;type=image/png' \
-F 'size=1328x1328' \
-F 'n=1'

使用远程 URL

curl --fail-with-body \
'https://mzsjai.com/v1/images/edits' \
-H "Authorization: Bearer ${MOZIA_API_KEY}" \
-F 'model=qwen/qwen-image-edit' \
-F 'prompt=Replace the background with snowy mountains' \
-F 'url=https://example.com/input.jpg' \
-F 'size=1328x1328'

多图输入时可以重复传递 url。URL 必须是模型服务可访问的绝对 HTTP(S) 地址。

当前 Qwen 模型参数

以下是当前 Qwen 图像模型的推荐值,并不代表其他图像模型使用相同配置:

参数qwen/qwen-imageqwen/qwen-image-edit
num_inference_steps5040
guidance_scale1.01.0
true_cfg_scale4.04.0

支持的标准尺寸:

1328x1328
1664x928
928x1664
1472x1104
1104x1472
1584x1056
1056x1584

正式推理的耗时会明显高于低步数测试,建议将客户端超时设置为至少 20 分钟。

响应格式

成功响应采用 OpenAI Images 兼容结构:

{
"created": 1787738693,
"data": [
{
"url": null,
"b64_json": "<BASE64_IMAGE_DATA>",
"revised_prompt": ""
}
]
}

当前平台统一返回 b64_json。即使传入 response_format=url,客户端也应从 data[].b64_json 读取并解码图片。

错误处理

错误响应采用统一结构:

{
"error": {
"message": "Error description",
"type": "new_api_error",
"code": "error_code"
}
}
状态码说明建议
400参数、尺寸或图片无效修正请求后重试
401API Key 缺失或无效检查鉴权配置
422Multipart 字段校验失败检查字段类型和必填参数
429额度不足或触发限流检查余额并降低请求频率
500模型推理失败保存请求 ID 后联系平台
502/503/504服务暂不可用或超时使用指数退避进行有限重试

不要无限重试参数错误。对临时服务错误进行重试时,应设置最大次数并加入随机抖动。