多模态
图像输入
通过 /api/v1/chat/completions API 使用多部分 messages 参数,可以向多模态模型发送图像请求。image_url 可以是 URL 或 base64 编码的图像。请注意,可以在单独的内容数组条目中发送多个图像。您可以在单个请求中发送的图像数量因提供商和模型而异。由于内容解析方式,我们建议先发送文本提示,然后发送图像。如果图像必须放在前面,我们建议将其放在系统提示中。
OpenRoute 支持图像的 直接 URL 和 base64 编码数据:
- URL:对于公开可访问的图像更高效,因为它们不需要本地编码
- Base64:对于本地文件或非公开可访问的私有图像是必需的
使用图像 URL
以下是如何使用 URL 发送图像:
import requests
import json
url = "https://www.openroute.cn/api/v1/chat/completions"
headers = {
"Authorization": f"Bearer <OPENROUTE_API_KEY>",
"Content-Type": "application/json"
}
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "这张图片里有什么?"
},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
}
}
]
}
]
payload = {
"model": "google/gemini-2.0-flash-001",
"messages": messages
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())使用 Base64 编码图像
对于本地存储的图像,您可以使用 base64 编码发送它们。以下是如何操作:
import requests
import json
import base64
from pathlib import Path
def encode_image_to_base64(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
url = "https://www.openroute.cn/api/v1/chat/completions"
headers = {
"Authorization": f"Bearer <OPENROUTE_API_KEY>",
"Content-Type": "application/json"
}
# Read and encode the image
image_path = "path/to/your/image.jpg"
base64_image = encode_image_to_base64(image_path)
data_url = f"data:image/jpeg;base64,{base64_image}"
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": data_url
}
}
]
}
]
payload = {
"model": "google/gemini-2.0-flash-001",
"messages": messages
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())支持的图像内容类型有:
image/pngimage/jpegimage/webpimage/gif