图像理解
- Python
- JavaScript
- curl
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)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
const response = await 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: "请描述这张图片" },
],
},
],
});
console.log(response.content[0].text);
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"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": "请描述这张图片"}
]
}]
}'
Base64 图片
- Python
- JavaScript
- curl
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 from "@anthropic-ai/sdk";
import fs from "fs";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
const imageData = fs.readFileSync("image.jpg").toString("base64");
const response = await 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: imageData },
},
{ type: "text", text: "这张图片里有什么?" },
],
},
],
});
console.log(response.content[0].text);
IMAGE_DATA=$(base64 -w 0 image.jpg)
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d "{
\"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\": \"这张图片里有什么?\"}
]
}]
}"
多图片对比
- Python
- JavaScript
- curl
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)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
const response = await 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: "对比这两张图片的区别" },
],
},
],
});
console.log(response.content[0].text);
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"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": "对比这两张图片的区别"}
]
}]
}'
支持格式:JPEG, PNG, GIF, WEBP,单张最大 5MB。