中国站
帮助中心 > 人工智能 > 大模型服务 > API参考 > Image > GPT-5.4 Image-2 API文档

GPT-5.4 Image-2 API文档

GPT-5.4 Image-2 API文档

概述

GPT-5.4 Image-2 是一款多模态图像模型,支持:

  • 文生图(Text-to-Image)
  • 图生图(Image-to-Image)
  • 图片编辑(Edits / Inpainting)
  • 多图融合(Multi-image Composition)
  • 图片理解(Vision)
  • Base64 图像输出

完全兼容 OpenAI API 风格。


Base URL

  1. https://api.yisu.com

认证

  1. Authorization: Bearer YOUR_API_KEY
  2. Content-Type: application/json

统一模型

  1. gpt-5.4-image-2

1. Images Generations

Endpoint

  1. POST /v1/images/generations

请求参数

参数 类型 必填 说明
model string gpt-5.4-image-2
prompt string 文生图提示词
n integer 生成数量
size string 图片尺寸
quality string 图片质量
response_format string b64_json

size

  1. 1024x1024
  2. 1024x1536
  3. 1536x1024
  4. auto

quality

  1. low
  2. medium
  3. high
  4. auto

示例

  1. curl https://api.yisu.com/v1/images/generations \
  2. -H "Authorization: Bearer YOUR_API_KEY" \
  3. -H "Content-Type: application/json" \
  4. -d '{
  5. "model": "gpt-5.4-image-2",
  6. "prompt": "赛博朋克城市夜景",
  7. "size": "1024x1024",
  8. "n": 1,
  9. "quality": "high",
  10. "response_format": "b64_json"
  11. }'

响应

  1. {
  2. "created": 1779778659,
  3. "data": [
  4. {
  5. "index": 0,
  6. "b64_json": "iVBORw0KGgoAAAANSUhEUgAA..."
  7. }
  8. ]
  9. }

2. Images Edits

Endpoint

  1. POST /v1/images/edits

请求方式

  1. multipart/form-data

参数

参数 类型 必填 说明
model string gpt-5.4-image-2
prompt string 编辑指令
image file 输入图片
mask file 局部编辑区域
n integer 输出数量
size string 输出尺寸
quality string 图像质量
response_format string b64_json

mask 规则

颜色 作用
白色 修改区域
黑色 保留区域

示例

  1. curl https://api.yisu.com/v1/images/edits \
  2. -H "Authorization: Bearer YOUR_API_KEY" \
  3. -F "model=gpt-5.4-image-2" \
  4. -F "prompt=给人物添加墨镜并增强光影" \
  5. -F "image=@input.png" \
  6. -F "response_format=b64_json"

响应

  1. {
  2. "created": 1779778659,
  3. "data": [
  4. {
  5. "index": 0,
  6. "b64_json": "iVBORw0KGgoAAAANSUhEUgAA..."
  7. }
  8. ]
  9. }

3. 图生图 / 多图融合(Chat Completions)

Endpoint

  1. POST /v1/chat/completions

请求示例

  1. {
  2. "model": "gpt-5.4-image-2",
  3. "messages": [
  4. {
  5. "role": "user",
  6. "content": [
  7. {
  8. "type": "text",
  9. "text": "把人物背景改成雪山"
  10. },
  11. {
  12. "type": "image",
  13. "image": "data:image/png;base64,IMAGE"
  14. }
  15. ]
  16. }
  17. ]
  18. }

响应

  1. {
  2. "id": "chatcmpl_xxx",
  3. "object": "chat.completion",
  4. "created": 1779778659,
  5. "model": "gpt-5.4-image-2",
  6. "choices": [
  7. {
  8. "message": {
  9. "role": "assistant",
  10. "content": "",
  11. "images": [
  12. {
  13. "type": "image_url",
  14. "image_url": {
  15. "url": "data:image/png;base64,..."
  16. }
  17. }
  18. ]
  19. },
  20. "finish_reason": "stop"
  21. }
  22. ]
  23. }

4. 图片理解(Vision)

请求

  1. {
  2. "model": "gpt-5.4-image-2",
  3. "messages": [
  4. {
  5. "role": "user",
  6. "content": [
  7. {
  8. "type": "text",
  9. "text": "描述这张图片"
  10. },
  11. {
  12. "type": "image",
  13. "image": "data:image/jpeg;base64,..."
  14. }
  15. ]
  16. }
  17. ]
  18. }

响应

  1. {
  2. "choices": [
  3. {
  4. "message": {
  5. "role": "assistant",
  6. "content": "图片中是一只猫坐在沙发上。",
  7. "images": []
  8. }
  9. }
  10. ]
  11. }

5. 图片返回规则(统一)

生成图片位置

  1. Chat API
  2. choices[0].message.images[]
  3. Images API
  4. data[].b64_json

图片格式

  1. data:image/png;base64,...
  2. data:image/jpeg;base64,...

特点

  • 不返回公网 URL
  • 仅返回 Base64
  • 客户端需自行存储

6. Base64 解码示例

Python

  1. import base64
  2. img = response["data"][0]["b64_json"]
  3. with open("out.png", "wb") as f:
  4. f.write(base64.b64decode(img))

PHP

  1. $img = $response['data'][0]['b64_json'];
  2. file_put_contents("out.png", base64_decode($img));

7. 流式输出(Chat)

SSE 示例

  1. data: {
  2. "choices": [
  3. {
  4. "delta": {
  5. "content": "正在生成..."
  6. }
  7. }
  8. ]
  9. }
  10. data: {
  11. "choices": [
  12. {
  13. "delta": {
  14. "images": [
  15. {
  16. "image_url": {
  17. "url": "data:image/png;base64,..."
  18. }
  19. }
  20. ]
  21. }
  22. }
  23. ]
  24. }
  25. data: [DONE]

8. 错误响应

API Key 无效

  1. {
  2. "error": {
  3. "message": "Invalid API key.",
  4. "type": "authentication_error",
  5. "code": "invalid_api_key"
  6. }
  7. }

参数错误

  1. {
  2. "error": {
  3. "message": "Invalid request.",
  4. "type": "invalid_request_error",
  5. "code": "invalid_request"
  6. }
  7. }

图片错误

  1. {
  2. "error": {
  3. "message": "Invalid image format.",
  4. "type": "invalid_request_error",
  5. "code": "invalid_image"
  6. }
  7. }

9. 支持能力总结

  • 文生图
  • 图生图
  • 图片编辑
  • 局部重绘
  • 多图融合
  • 图片理解
  • Base64 输出