跳到主要内容

图像理解

import anthropic

client = anthropic.Anthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")

# 使用图片 URL
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {"type": "url", "url": "https://example.com/image.jpg"},
},
{"type": "text", "text": "请描述这张图片"},
],
}
],
)
print(response.content[0].text)

Base64 图片

import anthropic
import base64

client = anthropic.Anthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")

with open("image.jpg", "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")

response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": image_data
},
},
{"type": "text", "text": "这张图片里有什么?"},
],
}
],
)
print(response.content[0].text)

多图片对比

import anthropic

client = anthropic.Anthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")

response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{"type": "image", "source": {"type": "url", "url": "https://example.com/img1.jpg"}},
{"type": "image", "source": {"type": "url", "url": "https://example.com/img2.jpg"}},
{"type": "text", "text": "对比这两张图片的区别"},
],
}
],
)
print(response.content[0].text)

支持格式:JPEG, PNG, GIF, WEBP,单张最大 5MB。