多模态

图像生成

OpenRoute 支持通过在其 output_modalities 中具有 "image" 的模型进行图像生成。当您在请求中指定适当的模态时,这些模型可以从文本提示创建图像。

模型发现

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

在模型页面

访问模型页面并按输出模态过滤,找到能够进行图像生成的模型。寻找在其输出模态中列出 "image" 的模型。

在聊天室中

使用聊天室时,点击图像按钮自动过滤并选择具有图像生成功能的模型。如果没有激活的图像模型,系统会提示您添加一个。

API 使用

要生成图像,请向 /api/v1/chat/completions 端点发送请求,将 modalities 参数设置为包含 "image" 和 "text"。

基本图像生成

import requests
import json

url = "https://openroute.cn/api/v1/chat/completions"
headers = {
    "Authorization": f"Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

payload = {
    "model": "google/gemini-2.5-flash-image-preview",
    "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://openroute.cn/api/v1/chat/completions"
headers = {
    "Authorization": f"Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

payload = {
    "model": "google/gemini-2.5-flash-image-preview",
    "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": ["image", "text"]
  3. 使用兼容模型:示例包括:
    • google/gemini-2.5-flash-image-preview
    • 其他具有图像生成功能的模型

最佳实践

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

故障排除

响应中没有图像?

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

找不到模型?

  • 使用模型页面查找可用的图像生成模型
  • 按输出模态过滤以查看兼容模型

On this page