help.khb.com 图像系列 API: 创建图片生成请求 (Images Generations)

API: 创建图片生成请求 (Images Generations)

Image Generation

Creates an image response for the given prompt. The URL for the generated image is valid for one hour. Please make sure to download and store it promptly to avoid any issues due to URL expiration.

端点:POST /v1/images/generations

Request Body

model (string, required)

对应模型名称。完整可用模型列表请查看 模型广场

示例:"Qwen/Qwen-Image-Edit-2509"

prompt (string, required)

图像生成的提示词,描述您希望生成的图像内容。

示例:"an island near sea, with seagulls, moon shining over the sea, light house, boats in the background, fish flying over the sea"

negative_prompt (string)

负向提示词,描述您不希望图像中出现的内容。

image_size (string, required)

图像分辨率,格式为 [width]x[height]。为保证最佳质量,强烈建议使用模型推荐值。

Qwen/Qwen-Image-Edit-2509 和 Qwen/Qwen-Image-Edit 不支持此字段。

推荐值:

  • Kolor 模型:
    • “1024×1024” (1:1)
    • “960×1280” (3:4)
    • “768×1024” (3:4)
    • “720×1440” (1:2)
    • “720×1280” (9:16)
  • Qwen-Image 模型:
    • “1328×1328” (1:1)
    • “1664×928” (16:9)
    • “928×1664” (9:16)
    • “1472×1140” (4:3)
    • “1140×1472” (3:4)
    • “1584×1056” (3:2)
    • “1056×1584” (2:3)

batch_size (integer)

输出图像数量,仅 Kwai-Kolors/Kolors 模型适用。默认 1。

seed (integer)

随机数生成器种子,设置固定值可复现结果。

num_inference_steps (integer)

推理步数,影响生成质量与速度的平衡。常见值 20~50。

代码示例

cURL

curl --request POST \
  --url https://khb.net.cn/v1/images/generations \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "Kwai-Kolors/Kolors",
    "prompt": "a cat, soft light, high quality",
    "image_size": "1024x1024",
    "batch_size": 1,
    "num_inference_steps": 30
  }'

Python (OpenAI 库)

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://khb.net.cn/v1"
)

response = client.images.generate(
    model="Kwai-Kolors/Kolors",
    prompt="a cat, soft light, high quality",
    size="1024x1024",
    n=1,
    extra_body={"step": 20}
)

for img in response.data:
    print(img.url)

Response Body

{
  "created": 1741685396,
  "data": [
    {
      "url": "https://xxx.khb.com.cn/imgs/xxx.png"
    }
  ]
}

注意事项

生成的图像 URL 有效期为 1 小时,请及时下载并转存到自己的存储服务中,避免因 URL 过期导致无法访问。

错误码

状态码 说明
200 成功
400 参数错误(模型不支持该尺寸等)
401 API Key 无效
403 权限不足
429 触发 Rate Limits(IPM/IPD)
500/503/504 服务端错误