API 参考OpenAI 兼容
视觉
GPT-4 Vision API - 通过 UniterRouter 使用 GPT-4o、GPT-4-turbo 分析图像。图像理解、OCR、图表分析等多模态 AI 应用。
视觉
使用多模态模型(如 GPT-4o、Claude 3.5 Sonnet 或 Gemini)通过标准 Chat Completions API 分析图像。
支持的模型: GPT-4o, GPT-4 Turbo, Claude 3.5 Sonnet/Haiku, Gemini 2.0/1.5 Pro 等视觉模型。
图像输入方式
URL 引用
{
"role": "user",
"content": [
{"type": "text", "text": "这张图片里有什么?"},
{"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
]
}Base64 编码
{
"role": "user",
"content": [
{"type": "text", "text": "描述这张图片"},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,/9j/4AAQSkZJRg..."
}
}
]
}示例请求
from openai import OpenAI
import base64
client = OpenAI(
base_url="https://api.uniterouter.ai/v1",
api_key="your_api_key",
)
# 使用 URL
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "这张图片里有什么?"},
{
"type": "image_url",
"image_url": {"url": "https://example.com/photo.jpg"}
}
]
}
],
max_tokens=300
)
# 使用 Base64
with open("image.jpg", "rb") as f:
image_data = base64.b64encode(f.read()).decode()
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "描述这张图片"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_data}"
}
}
]
}
]
)import OpenAI from "openai";
import * as fs from "fs";
const client = new OpenAI({
baseURL: "https://api.uniterouter.ai/v1",
apiKey: process.env.UNITEROUTER_API_KEY,
});
// 使用 URL
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "user",
content: [
{ type: "text", text: "这张图片里有什么?" },
{
type: "image_url",
image_url: { url: "https://example.com/photo.jpg" },
},
],
},
],
max_tokens: 300,
});
// 使用 Base64
const imageData = fs.readFileSync("image.jpg").toString("base64");
const response2 = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "user",
content: [
{ type: "text", text: "描述这张图片" },
{
type: "image_url",
image_url: { url: `data:image/jpeg;base64,${imageData}` },
},
],
},
],
});curl https://api.uniterouter.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $UNITEROUTER_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "这张图片里有什么?"},
{"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
]
}
],
"max_tokens": 300
}'多图像分析
在单个请求中分析多张图像:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "比较这两张图片"},
{"type": "image_url", "image_url": {"url": "https://example.com/image1.jpg"}},
{"type": "image_url", "image_url": {"url": "https://example.com/image2.jpg"}}
]
}
]
)图像细节级别
控制图像处理精度:
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg",
"detail": "high" // "low", "high", 或 "auto"
}
}| 级别 | 描述 | 使用场景 |
|---|---|---|
low | 512px,更快,更少 token | 快速分类 |
high | 完整分辨率,更多细节 | OCR,详细分析 |
auto | 模型自动决定 | 通用(默认) |
支持的格式
| 格式 | MIME 类型 |
|---|---|
| JPEG | image/jpeg |
| PNG | image/png |
| GIF | image/gif |
| WebP | image/webp |
使用场景
| 场景 | 示例提示 |
|---|---|
| OCR | "提取图片中的所有文字" |
| 物体检测 | "列出你能看到的所有物体" |
| 图像描述 | "详细描述这张图片" |
| 图像对比 | "这两张图片有什么区别?" |
| 图表分析 | "分析这个图表并总结数据" |
| 代码截图 | "将这个代码截图转换为文字" |
限制
- 最大图像尺寸因模型而异
- 部分模型限制每次请求的图像数量
- 动态 GIF 仅使用第一帧
相关文档
- Chat Completions - 基础 API 参考
- 流式响应 - 流式传输
How is this guide?