跳到主要内容

图片生成

Knox Chat 支持通过 output_modalities 中包含 "image" 的模型进行图片生成。当你在请求中指定相应的模态时,这些模型可以根据文本提示创建图片。

模型发现

你可以通过以下几种方式找到图片生成模型:

在模型页面上

访问模型列表,按输出模态筛选即可找到具备图片生成能力的模型。查找 output_modalities 中列出 "image" 的模型。

在聊天室中

使用聊天时,点击图片按钮即可自动筛选并选择具有图片生成能力的模型。如果当前没有启用支持图片生成的模型,系统会提示你添加一个。

API 使用

要生成图片,请向 /v1/chat/completions 端点发送请求,并将 modalities 参数设置为同时包含 "image""text"

基本图片生成

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]}...")

流式图片生成

图片生成同样支持流式响应:

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

响应格式

生成图片时,助手消息中会包含一个 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,
  • 多张图片:部分模型可以在单次响应中生成多张图片
  • 尺寸:图片尺寸因模型能力而异

模型兼容性

并非所有模型都支持图片生成。要使用此功能:

  1. 检查输出模态:确保模型的 output_modalities 中包含 "image"
  2. 设置 modalities 参数:在请求中包含 "modalities": ["image", "text"]
  3. 使用兼容的模型:示例包括:
    • google/gemini-2.5-flash-image
    • google/gemini-2.5-flash-image-preview
    • google/gemini-3-pro-image-preview
    • 其他具有图片生成能力的模型

最佳实践

  • 清晰的提示:提供详细的描述以获得更好的图片质量
  • 模型选择:选择专为图片生成设计的模型
  • 错误处理:在处理之前检查响应中是否包含 images 字段
  • 速率限制:图片生成可能与文本生成有不同的速率限制
  • 存储:考虑如何处理和存储 base64 图片数据

故障排除

响应中没有图片?

  • 验证模型是否支持图片生成(output_modalities 包含 "image"
  • 确保请求中包含了 "modalities": ["image", "text"]
  • 检查你的提示是否要求生成图片

找不到模型?

  • 使用模型列表查找可用的图片生成模型
  • 按输出模态筛选以查看兼容的模型