Appearance
Images 图像生成
图像生成与编辑接口。
参考文档: https://platform.openai.com/docs/api-reference/images
创建图像
POST /v1/images/generations
请求体 (application/json)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| prompt | string | 是 | 图像生成提示词 |
| model | string | 否 | 模型 ID(如 dall-e-3) |
| n | integer | 否 | 生成图像数量,1-10,默认 1 |
| size | string | 否 | 256x256、512x512、1024x1024、1792x1024、1024x1792,默认 1024x1024 |
| quality | string | 否 | standard、hd,默认 standard |
| style | string | 否 | vivid、natural,默认 vivid |
| response_format | string | 否 | url、b64_json,默认 url |
| user | string | 否 | 用户标识 |
请求示例
bash
curl -X POST "https://your-api-base/v1/images/generations" \
-H "Authorization: Bearer sk-xxxxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "dall-e-3",
"prompt": "A white siamese cat",
"n": 1,
"size": "1024x1024"
}'python
from openai import OpenAI
client = OpenAI(
api_key="sk-xxxxxx",
base_url="https://your-api-base/v1"
)
response = client.images.generate(
model="dall-e-3",
prompt="A white siamese cat",
n=1,
size="1024x1024"
)
print(response.data[0].url)200 成功响应
json
{
"created": 1677858242,
"data": [
{
"url": "https://example.com/image.png",
"revised_prompt": "A white siamese cat with blue eyes sitting on a cushion"
}
]
}编辑图像
POST /v1/images/edits
请求体 (multipart/form-data)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| image | binary | 是 | 源图像文件 |
| prompt | string | 是 | 编辑提示词 |
| mask | binary | 否 | 遮罩图像 |
| model | string | 否 | 模型 ID |
| n | integer | 否 | 生成图像数量 |
| size | string | 否 | 图像尺寸 |
| response_format | string | 否 | 响应格式 |
请求示例
bash
curl -X POST "https://your-api-base/v1/images/edits" \
-H "Authorization: Bearer sk-xxxxxx" \
-F image="@original.png" \
-F mask="@mask.png" \
-F prompt="A sunlit indoor room with a pool" \
-F model="dall-e-2" \
-F n=1