图片生成
Knox Chat 支持通过 output_modalities 中包含 "image" 的模型进行图片生成。当你在请求中指定相应的模态时,这些模型可以根据文本提示创建图片。
模型发现
你可以通过以下几种方式找到图片生成模型:
在模型页面上
访问模型列表,按输出模态筛选即可找到具备图片生成能力的模型。查找 output_modalities 中列出 "image" 的模型。
在聊天室中
使用聊天时,点击图片按钮即可自动筛选并选择具有图片生成能力的模型。如果当前没有启用支持图片生成的模型,系统会提示你添加一个。
API 使用
要生成图片,请向 /v1/chat/completions 端点发送请求,并将 modalities 参数设置为同时包含 "image" 和 "text"。
基本图片生成
- Python
- TypeScript
import requests
import json
url = "https://api.knox.chat/v1/chat/completions"
headers = {
"Authorization": f"Bearer {API_KEY_REF}",
"Content-Type": "application/json"
}
payload = {
"model": "google/gemini-2.5-flash-image",
"messages": [
{
"role": "user",
"content": "Generate a beautiful sunset over mountains"
}
],
"modalities": ["image", "text"]
}
response = requests.post(url, headers=headers, json=payload)
result = response.json()
# The generated image will be in the assistant message
if result.get("choices"):
message = result["choices"][0]["message"]
if message.get("images"):
for image in message["images"]:
image_url = image["image_url"]["url"] # Base64 data URL
print(f"Generated image: {image_url[:50]}...")
const response = await fetch('https://api.knox.chat/v1/chat/completions', {
method: 'POST',
headers: {
Authorization: `Bearer ${API_KEY_REF}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: '{{MODEL}}',
messages: [
{
role: 'user',
content: 'Generate a beautiful sunset over mountains',
},
],
modalities: ['image', 'text'],
}),
});
const result = await response.json();
// The generated image will be in the assistant message
if (result.choices) {
const message = result.choices[0].message;
if (message.images) {
message.images.forEach((image, index) => {
const imageUrl = image.image_url.url; // Base64 data URL
console.log(`Generated image ${index + 1}: ${imageUrl.substring(0, 50)}...`);
});
}
}
流式图片生成
图片生成同样支持流式响应:
- Python
- TypeScript
import requests
import json
url = "https://api.knox.chat/v1/chat/completions"
headers = {
"Authorization": f"Bearer {API_KEY_REF}",
"Content-Type": "application/json"
}
payload = {
"model": "google/gemini-2.5-flash-image",
"messages": [
{
"role": "user",
"content": "Create an image of a futuristic city"
}
],
"modalities": ["image", "text"],
"stream": True
}
response = requests.post(url, headers=headers, json=payload, stream=True)
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data = line[6:]
if data != '[DONE]':
try:
chunk = json.loads(data)
if chunk.get("choices"):
delta = chunk["choices"][0].get("delta", {})
if delta.get("images"):
for image in delta["images"]:
print(f"Generated image: {image['image_url']['url'][:50]}...")
except json.JSONDecodeError:
continue
const response = await fetch('https://api.knox.chat/v1/chat/completions', {
method: 'POST',
headers: {
Authorization: `Bearer ${API_KEY_REF}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: '{{MODEL}}',
messages: [
{
role: 'user',
content: 'Create an image of a futuristic city',
},
],
modalities: ['image', 'text'],
stream: true,
}),
});
const reader = response.body?.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data !== '[DONE]') {
try {
const parsed = JSON.parse(data);
if (parsed.choices) {
const delta = parsed.choices[0].delta;
if (delta?.images) {
delta.images.forEach((image, index) => {
console.log(`Generated image ${index + 1}: ${image.image_url.url.substring(0, 50)}...`);
});
}
}
} catch (e) {
// Skip invalid JSON
}
}
}
}
}
响应格式
生成图片时,助手消息中会包含一个 images 字段,其中包含生成的图片:
{
"choices": [
{
"message": {
"role": "assistant",
"content": "I've generated a beautiful sunset image for you.",
"images": [
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
}
]
}
}
]
}
图片格式
- 格式:图片以 base64 编码的数据 URL 形式返回
- 类型:通常为 PNG 格式(
data:image/png;base64,) - 多张图片:部分模型可以在单次响应中生成多张图片
- 尺寸:图片尺寸因模型能力而异
模型兼容性
并非所有模型都支持图片生成。要使用此功能:
- 检查输出模态:确保模型的
output_modalities中包含"image" - 设置 modalities 参数:在请求中包含
"modalities": ["image", "text"] - 使用兼容的模型:示例包括:
google/gemini-2.5-flash-imagegoogle/gemini-2.5-flash-image-previewgoogle/gemini-3-pro-image-preview- 其他具有图片生成能力的模型
最佳实践
- 清晰的提示:提供详细的描述以获得更好的图片质量
- 模型选择:选择专为图片生成设计的模型
- 错误处理:在处理之前检查响应中是否包含
images字段 - 速率限制:图片生成可能与文本生成有不同的速率限制
- 存储:考虑如何处理和存储 base64 图片数据
故障排除
响应中没有图片?
- 验证模型是否支持图片生成(
output_modalities包含"image") - 确保请求中包含了
"modalities": ["image", "text"] - 检查你的提示是否要求生成图片
找不到模型?
- 使用模型列表查找可用的图片生成模型
- 按输出模态筛选以查看兼容的模型