跳转到正文

生成图像 ​

根据文字提示生成图像。

POST/v1/images/generations

JSON 请求体。未在 schema 中定义的字段会作为扩展参数转发给提供商,见 厂商扩展字段。

请求
bash
curl http://localhost:8080/v1/images/generations \
  -H "Authorization: Bearer $LLM_PROXY_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-image-1", "prompt": "A lighthouse at dusk", "size": "1024x1024"}'
请求 — OpenAI SDK
python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8080/v1", api_key="sk-your-proxy-key")
img = client.images.generate(model="gpt-image-1", prompt="A lighthouse at dusk")
print(img.data[0].url)
响应
json
{
  "created": 1718047200,
  "data": [
    {
      "url": "https://…/lighthouse.png",
      "revised_prompt": "A lighthouse at dusk, warm light"
    }
  ],
  "usage": {
    "input_tokens": 50,
    "output_tokens": 1240,
    "input_tokens_details": {"text_tokens": 50, "image_tokens": 0}
  }
}

请求字段 ​

prompt(必填)、model、n(1–12)、quality、size、style、 response_format(默认 url,或 b64_json)、user、background、moderation、 output_format、output_compression、partial_images、stream。

  • size:auto,或 宽x高:两条边都 ≥ 16 且能被 16 整除,较长边 ≤ 3840,宽高比 在 1:3–3:1 之间。
  • n:schema 接受 1–12(各提供商公开的最宽范围)。实际生效的上限由提供商自己决定, 见厂商扩展字段。
  • gpt-image-* 系列的模型还会收到 background、moderation、output_compression、 output_format、complexity、frame_count、frame_duration_ms、 partial_images;对它们不下发 response_format 和 style。

GIF 与 SVG 输出 ​

GPT Image 2.6(gpt-image-2.6-flare、gpt-image-2.6-sunburst)在 /v1/images/generations 的 output_format 上增加了 gif 和 svg。

  • gif:frame_count 为 2–64(默认 9),frame_duration_ms 为 20–2000(默认 200, 提供商按 10 ms 取整)。size 取 auto,或两条边都是 16 的倍数的 宽x高。
  • svg:n 必须为 1。
  • 两种格式都不接受 stream、partial_images 和 output_compression,带上会返回 400 invalid_request_error;它们也不在 /v1/images/edits 上提供,该端点仍是 png、jpeg、webp。
  • frame_count 和 frame_duration_ms 只对 GIF 有效:与其它 output_format 一起发送 返回 400。

complexity(low、medium、high,上游默认 high)控制模型渲染前的规划量,对任何 gpt-image-* 模型都会下发。

代理原样透传媒体字节:GIF 或 SVG 就是提供商产出的内容,放在 b64_json 还是 url 里 取决于请求本身。

厂商扩展字段 ​

OpenAI Images schema 之外的字段会直接进入提供商的请求体,不借助 extra_body 也能使用 特定上游的选项(OpenAI SDK 只在 chat 上提供 extra_body,图像接口没有)。

qwen / qwen-intl(DashScope)接受:

  • image:公网图片 URL、base64 data URL,或二者的数组。带上它就是图生图,不带就是 文生图。
  • negative_prompt、seed、prompt_extend、prompt_extend_mode、enable_thinking、 watermark:放进 DashScope 的 parameters 块。
  • 在 /v1/images/edits 上:color_palette、bbox_list、enable_sequential、 thinking_mode。

qwen/qwen-intl 按模型规定上限(超出返回 400 invalid_request_error): qwen-image-* 的 n 为 1–6,wan* 为 1–4(带 enable_sequential 时为 1–12)。

设置 server_params.unknown_fields_policy: error 后,代理会拒收未知字段,但各提供商 已文档化的上游字段仍可正常使用。

响应 ​

{"created": <int>, "data":[{"url"|"b64_json", "revised_prompt"?}], …},可能附带 usage(input_tokens、output_tokens、 input_tokens_details.{text_tokens,image_tokens})。

  • 图像响应里没有 model 字段,代理也不会在 url 和 b64_json 之间转换,提供商返回 什么就是什么。
  • stream: true 会发出 SSE 事件 image_generation.partial_image(含 b64_json、 partial_image_index)和 image_generation.completed,最后是 [DONE]。
  • 没有异步或后台图像模式。

提供商支持 ​

openai、openai-compatible 家族、gemini 和 qwen 类型的提供商都能承接图像请求,见 能力矩阵和厂商扩展字段。

相关 ​

请求
bash
curl http://localhost:8080/v1/images/generations \
  -H "Authorization: Bearer $LLM_PROXY_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-image-1", "prompt": "A lighthouse at dusk", "size": "1024x1024"}'
请求 — OpenAI SDK
python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8080/v1", api_key="sk-your-proxy-key")
img = client.images.generate(model="gpt-image-1", prompt="A lighthouse at dusk")
print(img.data[0].url)
响应
json
{
  "created": 1718047200,
  "data": [
    {
      "url": "https://…/lighthouse.png",
      "revised_prompt": "A lighthouse at dusk, warm light"
    }
  ],
  "usage": {
    "input_tokens": 50,
    "output_tokens": 1240,
    "input_tokens_details": {"text_tokens": 50, "image_tokens": 0}
  }
}

MIT 许可