Gateway API 文档

OpenAI 兼容
POST/openai/v1/images/generations

Images Generations

根据文本 prompt 生成图像。model 必须是已发布且绑定 LiteLLM model_name 的生图模型;具体 Provider、协议适配和可用参数由对应 LiteLLM Deployment 决定。

需要认证

请求参数

参数类型必填说明
modelstring必填模型 ID,必须为已注册的 OpenAI 生图模型(如 "gpt-image-1"、"gpt-image-1-mini"、"dall-e-3"、"dall-e-2")。OpenAI 官方允许此字段缺省,但 Gateway 依赖它做路由与计费,所以强制必填。
promptstring必填图像生成提示词,非空字符串。
nnumber可选生成图片数量,默认 1。gpt-image-* 系列仅支持 n=1;dall-e-2 最多 10;dall-e-3 仅 1。
sizestring可选图像尺寸。gpt-image-* 支持 "1024x1024" / "1024x1536" / "1536x1024" / "auto";dall-e-3 支持 "1024x1024" / "1792x1024" / "1024x1792";dall-e-2 支持 "256x256" / "512x512" / "1024x1024"。
qualitystring可选图像质量。gpt-image-* 支持 "low" / "medium" / "high" / "auto";dall-e-3 支持 "standard" / "hd"。
stylestring可选仅 dall-e-3 支持,"vivid"(默认)或 "natural"。
response_formatstring可选返回格式,"url"(默认,仅 dall-e 系列)或 "b64_json"。gpt-image-* 始终返回 b64_json,忽略此字段。
output_formatstring可选仅 gpt-image-* 支持,"png"(默认)/ "jpeg" / "webp"。
output_compressionnumber可选仅 gpt-image-* + jpeg/webp 时生效,压缩级别 0~100,默认 100。
backgroundstring可选仅 gpt-image-* 支持,"transparent" / "opaque" / "auto"(默认)。设为 transparent 时建议 output_format 用 png 或 webp。
moderationstring可选仅 gpt-image-* 支持,"low" 或 "auto"(默认)。
userstring可选终端用户标识,便于上游做滥用监控。

调用示例

typescript
import OpenAI from "openai"

const client = new OpenAI({
  baseURL: "https://llmapi.memene.cn/openai/v1",
  apiKey: "gw_memene_<keyId>_<secret>",
})

const res = await client.images.generate({
  "model": "gpt-image-1",
  "prompt": "A serene Japanese garden with cherry blossoms at sunset, watercolor style.",
  "size": "1024x1024",
  "quality": "high",
  "n": 1
})
console.log(res.data[0].b64_json)

响应示例

json
{
  "created": 1700000000,
  "data": [
    {
      "b64_json": "iVBORw0KGgoAAAANSUhEUgAA... (base64-encoded image)"
    }
  ],
  "usage": {
    "input_tokens": 23,
    "output_tokens": 1056,
    "total_tokens": 1079
  }
}

注意:⚠️ 本端点仅对 OpenAI 模型生效。 计费说明:gpt-image-1 / gpt-image-1-mini 返回 usage.{input,output,total}_tokens,按 token 计费;dall-e-2 / dall-e-3 上游不返回 usage,单价按张换算(由前端 / 调用方根据 size+quality 自行核算,Gateway 默认计入 0)。 生图请求耗时较长(典型 5~30s),且响应体可达数 MB(b64_json),请预留足够的客户端超时与带宽。本端点不支持流式。