控制台

图像生成

根据文本描述生成图像,支持长文本和文字渲染,适合海报设计、插画创作和视觉内容生成。

Endpoint
POST/v1/images/generations
模型名称
moss-image-sft-1.0
  1. 创建 API Key

    进入 API Key 管理平台,创建 API Key,并保存为环境变量。不要把 API Key 写进客户端代码或提交到仓库。

    环境变量
    export MOSS_API_KEY='YOUR_API_KEY'
  2. 准备请求输入

    填入文本描述、图片尺寸和模型 ID。目前仅支持一次生成一张图片,不支持流式输出。

    字段类型要求说明
    model: string必填

    模型 ID。推荐使用稳定 model ID;需要复现固定版本时,可填写 snapshot model ID。

    prompt: string必填

    待生成图像的文本描述,非空字符串,最多 4000 字符。

    size: string可选

    图片尺寸,格式为宽x高。每边 256–2048,且为 16 的倍数;总像素不超过 2097152。

    n: integer可选

    生成图片数量,仅支持 1。

    response_format: string可选

    仅支持 url,不支持 b64_json。

    请求体
    {
      "model": "moss-image-sft-1.0",
      "prompt": "一只橘猫坐在窗边,窗外是雨后的花园,柔和的自然光,温暖细腻的摄影风格。",
      "negative_prompt": "nsfw, worst quality, bad anatomy, watermark, jpeg artifact",
      "size": "1024x1024",
      "n": 1,
      "response_format": "url"
    }
  3. 发起图像生成请求

    向 /v1/images/generations 发起请求。生成完成后,接口返回 JSON。

    请求示例
    curl --max-time 900 -X POST "https://api.mosi.cn/v1/images/generations" \
      -H "Authorization: Bearer $MOSS_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{
      "model": "moss-image-sft-1.0",
      "prompt": "一只橘猫坐在窗边,窗外是雨后的花园,柔和的自然光,温暖细腻的摄影风格。",
      "negative_prompt": "nsfw, worst quality, bad anatomy, watermark, jpeg artifact",
      "size": "1024x1024",
      "n": 1,
      "response_format": "url"
    }'

    示例超时为 900 秒,不代表固定服务端超时。

  4. 获取图片结果

    成功后,从响应的 data[0].url 读取图片地址,可直接打开或下载 PNG。usage.generated_images、usage.output_width、usage.output_height 分别表示实际生成张数、图片宽度和高度。

    请求示例
    # 将示例 URL 替换为 data[0].url
    curl --fail --location "https://example.com/generated-image.png" \
      --output image.png
  5. 验证成功

    成功标志:拿到结果图片 URL,可查看或下载生成的 PNG 图片。

下一步