Claude 图片输入 (Vision) 使用指南基础
·
基础地址:
https://www.moyu.info接口:
POST /v1/messages认证:
x-api-key: sk-xxx必须请求头:
anthropic-version: 2023-06-01
---
1. 支持的模型
以下模型均已通过实际测试,全部支持图片/视觉输入:
|
模型 ID |
系列 |
测试结果 |
|
|
Claude 4.6 Opus |
通过 |
|
|
Claude 4.6 Sonnet |
通过 |
|
|
Claude 4.5 Opus |
通过 |
|
|
Claude 4.5 Sonnet |
通过 |
---
2. 图片输入格式
在 messages[].content 中,图片作为 type: "image" 的内容块传入,支持两种 source 类型:
2.1 Base64 编码(推荐)
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "<base64编码的图片数据>"
}
}
2.2 URL 引用
{
"type": "image",
"source": {
"type": "url",
"url": "https://example.com/image.png"
}
}
注意:URL 方式要求图片地址可被服务器直接访问(无需登录、无防盗链),否则会报 403 错误。推荐使用 base64 方式确保稳定性。
2.3 支持的图片格式
|
media_type |
格式 |
|
|
PNG |
|
|
JPEG |
|
|
GIF |
|
|
WebP |
---
3. 完整请求示例
3.1 Base64 图片输入(单张图片)
curl -X POST "https://www.moyu.info/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: sk-你的密钥" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "iVBORw0KGgoAAAANSUhEUgAAAAo...(base64数据)"
}
},
{
"type": "text",
"text": "请描述这张图片的内容。"
}
]
}
]
}'
3.2 URL 图片输入
curl -X POST "https://www.moyu.info/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: sk-你的密钥" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://httpbin.org/image/png"
}
},
{
"type": "text",
"text": "请描述这张图片的内容。"
}
]
}
]
}'
3.3 多张图片输入
一次请求中可以传入多张图片,支持 base64 和 URL 混合使用:
curl -X POST "https://www.moyu.info/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: sk-你的密钥" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "iVBORw0KGgoAAAANSUhEUg...(第一张图base64)"
}
},
{
"type": "image",
"source": {
"type": "url",
"url": "https://httpbin.org/image/png"
}
},
{
"type": "text",
"text": "请分别描述这两张图片的内容。"
}
]
}
]
}'
3.4 Python 示例(本地图片文件)
import base64
import requests
# 读取本地图片并转为 base64
with open("my_image.png", "rb") as f:
img_base64 = base64.b64encode(f.read()).decode()
response = requests.post(
"https://www.moyu.info/v1/messages",
headers={
"Content-Type": "application/json",
"x-api-key": "sk-你的密钥",
"anthropic-version": "2023-06-01",
},
json={
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": img_base64,
},
},
{
"type": "text",
"text": "请详细描述这张图片的内容。",
},
],
}
],
},
timeout=120,
)
result = response.json()
print(result["content"][0]["text"])
3.5 Python 示例(使用 Anthropic SDK)
import anthropic
import base64
client = anthropic.Anthropic(
api_key="sk-你的密钥",
base_url="https://www.moyu.info",
)
# 方式一:base64
with open("my_image.png", "rb") as f:
img_base64 = base64.b64encode(f.read()).decode()
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": img_base64,
},
},
{
"type": "text",
"text": "请描述这张图片。",
},
],
}
],
)
print(message.content[0].text)
更多推荐

所有评论(0)