File size: 6,032 Bytes
9853396 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | # 图像生成 API
## 概述
AxonHub 通过 OpenAI 兼容的 `/v1/images/generations` 端点支持图像生成功能。
**注意**:图像生成目前不支持流式传输。
## API 使用
要生成图像,请向 `/v1/images/generations` 端点发送请求。
### 示例
```python
import requests
import json
url = "https://your-axonhub-instance/v1/images/generations"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": "gpt-image-1",
"prompt": "Generate a beautiful sunset over mountains",
"size": "1024x1024",
"quality": "high",
"n": 1
}
response = requests.post(url, headers=headers, json=payload)
result = response.json()
# 访问生成的图像
for image in result.get("data", []):
if "b64_json" in image:
print(f"图像 (base64): {image['b64_json'][:50]}...")
if "url" in image:
print(f"图像 URL: {image['url']}")
if "revised_prompt" in image:
print(f"优化后的提示词: {image['revised_prompt']}")
```
```typescript
const response = await fetch("https://your-axonhub-instance/v1/images/generations", {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-image-1",
prompt: "Generate a beautiful sunset over mountains",
size: "1024x1024",
quality: "high",
n: 1,
}),
});
const result = await response.json();
// 访问生成的图像
if (result.data) {
result.data.forEach((image, index) => {
if (image.b64_json) {
console.log(`图像 ${index + 1} (base64): ${image.b64_json.substring(0, 50)}...`);
}
if (image.url) {
console.log(`图像 ${index + 1} URL: ${image.url}`);
}
if (image.revised_prompt) {
console.log(`优化后的提示词: ${image.revised_prompt}`);
}
});
}
```
## 响应格式
```json
{
"created": 1699000000,
"data": [
{
"b64_json": "iVBORw0KGgoAAAANSUhEUgAA...",
"url": "https://...",
"revised_prompt": "A beautiful sunset over mountains with orange and purple sky"
}
]
}
```
## 请求参数
| 参数 | 类型 | 描述 | 默认值 |
|-----------|------|-------------|---------|
| `prompt` | string | **必填。** 所需图像的文本描述。 | - |
| `model` | string | 用于图像生成的模型。 | `dall-e-2` |
| `n` | integer | 要生成的图像数量。 | 1 |
| `quality` | string | 图像质量:`"standard"`、`"hd"`、`"high"`、`"medium"`、`"low"` 或 `"auto"`。 | `"auto"` |
| `response_format` | string | 返回图像的格式:`"url"` 或 `"b64_json"`。 | `"b64_json"` |
| `size` | string | 生成图像的尺寸:`"256x256"`、`"512x512"` 或 `"1024x1024"`。 | `"1024x1024"` |
| `style` | string | 生成图像的风格(仅 DALL-E 3):`"vivid"` 或 `"natural"`。 | - |
| `user` | string | 代表最终用户的唯一标识符。 | - |
| `background` | string | 背景样式:`"opaque"` 或 `"transparent"`。 | - |
| `output_format` | string | 图像格式:`"png"`、`"webp"` 或 `"jpeg"`。 | `"png"` |
| `output_compression` | number | 压缩级别 (0-100%)。 | 100 |
| `moderation` | string | 内容审核级别:`"low"` 或 `"auto"`。 | - |
| `partial_images` | number | 要生成的部分图像数量。 | 1 |
## 图像编辑(局部重绘)
要编辑图像,请使用 `/v1/images/edits` 端点,使用 multipart/form-data 格式:
```python
import requests
url = "https://your-axonhub-instance/v1/images/edits"
headers = {
"Authorization": f"Bearer {API_KEY}"
}
with open("image.png", "rb") as image_file, open("mask.png", "rb") as mask_file:
files = {
"image": image_file,
"mask": mask_file
}
data = {
"model": "gpt-image-1",
"prompt": "将颜色改为白色",
"size": "1024x1024",
"n": 1
}
response = requests.post(url, headers=headers, files=files, data=data)
result = response.json()
```
### 图像编辑参数
| 参数 | 类型 | 描述 | 默认值 |
|-----------|------|-------------|---------|
| `image` | file | **必填。** 要编辑的图像。 | - |
| `prompt` | string | **必填。** 所需编辑的文本描述。 | - |
| `mask` | file | 可选的蒙版图像。透明区域表示要编辑的位置。 | - |
| `model` | string | 要使用的模型。 | `dall-e-2` |
| `n` | integer | 要生成的图像数量。 | 1 |
| `size` | string | 生成图像的尺寸。 | `"1024x1024"` |
| `response_format` | string | 格式:`"url"` 或 `"b64_json"`。 | `"b64_json"` |
| `user` | string | 最终用户的唯一标识符。 | - |
| `background` | string | 背景样式:`"opaque"` 或 `"transparent"`。 | - |
| `output_format` | string | 图像格式:`"png"`、`"webp"` 或 `"jpeg"`。 | `"png"` |
| `output_compression` | number | 压缩级别 (0-100%)。 | 100 |
| `input_fidelity` | string | 输入保真度级别。 | - |
| `partial_images` | number | 部分图像数量。 | 1 |
## 支持的提供商
| 提供商 | 状态 | 支持的模型 | 备注 |
| -------------------- | ------- | ------------------------------------------------------------- | --------------------- |
| **OpenAI** | ✅ 完成 | gpt-image-1、dall-e-2、dall-e-3 等 | 不支持流式传输 |
| **字节跳动豆包** | ✅ 完成 | doubao-seed-dream-4-0 等 | 不支持流式传输 |
| **OpenRouter** | ✅ 完成 | gpt-image-1、gemini-2.5-flash-image-preview 等 | 不支持流式传输 |
| **Gemini** | ✅ 完成 | gemini-2.5-flash-image、gemini-2.0-flash-preview-image-generation 等 | 不支持流式传输 |
| **ZAI** | ✅ 完成 | - | 仅支持生成,不支持编辑 |
## 相关资源
- [OpenAI API](openai-api.md)
- [Anthropic API](anthropic-api.md)
- [Gemini API](gemini-api.md)
- [Claude Code 集成](../guides/claude-code-integration.md)
|