常用接口
多数图片分析模型仍使用文本对话接口:POST /v1/chat/completions
messages.content 不再只是字符串,而是文本和图片组成的数组。
支持的模型来源
| 来源 | 适合场景 | 说明 |
|---|---|---|
| GPT 视觉模型 | 通用图片问答、截图理解、图表分析 | 通常兼容 OpenAI 图文输入格式 |
| Claude 视觉模型 | 长上下文、文档截图、复杂图片描述 | 可能支持 OpenAI 兼容方式或 Anthropic 原生格式 |
| Gemini 视觉模型 | 多模态理解、图片/文件综合分析 | 特殊文件参数以模型广场说明为准 |
| 其他视觉模型 | 垂直识别、低价批量分析 | 以模型广场能力标签为准 |
curl 调用
图片可通过 HTTPS URL 或 base64 data URL 传入。本地图片可先转 base64:# 将本地图片转为 base64(去掉换行后填入下方 JSON)
base64 -w 0 /path/to/local.png
curl https://www.focusapi.cn/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "请对比这两张图片的主要内容" },
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,<your-base64-data>"
}
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.png"
}
}
]
}
]
}'
Python SDK 调用
pip install openai anthropic google-genai litellm requests
import base64
from openai import OpenAI
client = OpenAI(
base_url="https://www.focusapi.cn/v1",
api_key="sk-your-api-key",
)
# 本地图片转 base64
with open("/path/to/local.png", "rb") as f:
local_b64 = base64.b64encode(f.read()).decode("utf-8")
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "请对比这两张图片的主要内容"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{local_b64}",
},
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.png",
},
},
],
}
],
)
print(response.choices[0].message.content)
import base64
from anthropic import Anthropic
client = Anthropic(
api_key="sk-your-api-key",
base_url="https://www.focusapi.cn",
)
# 本地图片转 base64
with open("/path/to/local.png", "rb") as f:
local_b64 = base64.b64encode(f.read()).decode("utf-8")
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "请对比这两张图片的主要内容"},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": local_b64,
},
},
{
"type": "image",
"source": {
"type": "url",
"url": "https://example.com/image.png",
},
},
],
}
],
)
print(message.content[0].text)
import requests
from google import genai
from google.genai import types
client = genai.Client(
api_key="sk-your-api-key",
http_options=types.HttpOptions(base_url="https://www.focusapi.cn"),
)
# 本地图片
with open("/path/to/local.png", "rb") as f:
local_bytes = f.read()
# 远程图片 URL
remote_bytes = requests.get("https://example.com/image.png").content
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=[
"请对比这两张图片的主要内容",
types.Part.from_bytes(data=local_bytes, mime_type="image/png"),
types.Part.from_bytes(data=remote_bytes, mime_type="image/png"),
],
)
print(response.text)
import base64
import litellm
# 本地图片转 base64
with open("/path/to/local.png", "rb") as f:
local_b64 = base64.b64encode(f.read()).decode("utf-8")
response = litellm.completion(
model="openai/gpt-4o-mini",
api_key="sk-your-api-key",
api_base="https://www.focusapi.cn/v1",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "请对比这两张图片的主要内容"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{local_b64}",
},
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.png",
},
},
],
}
],
)
print(response.choices[0].message.content)
OpenAI SDK / LiteLLM 的 base64 使用
data:image/png;base64,{base64} 格式;Claude SDK 使用 source.type: "base64";Gemini SDK 直接传字节。每个示例同时演示本地 base64 和远程 URL 两种传图方式。若特殊格式报错,先用 curl 或 OpenAI SDK 验证模型是否支持视觉能力。图片输入建议
- 优先使用可公网访问的 HTTPS 图片 URL。
- base64 图片会显著增加请求体大小,超大图片容易触发 524 超时。
- 截图、票据、表格类图片要保证文字清晰。
- 批量分析时控制并发,避免 429 限流。
常见问题
| 问题 | 处理 |
|---|---|
| 模型说看不到图片 | 确认模型广场标记支持视觉能力 |
| 图片 URL 读取失败 | 确认 URL 可公网访问,且没有登录、Referer 或防盗链限制 |
| 返回内容不稳定 | 提示词写清输出格式和关注点 |
| 请求体太大 | 压缩图片或改用 URL,不要传超大 base64 |
相关能力
文本对话
不带图片的普通聊天、总结和问答。
图像生成
根据提示词生成新图片。
多模态理解
图片以外的文档、文件和混合输入。
错误排查
处理模型不支持、限流、超时和鉴权问题。
