跳转到主要内容

概览

LemonData 支持原生的 Anthropic Messages API 格式。通过 LemonData 使用官方 Anthropic SDK 即可访问 Claude 模型。

安装

pip install anthropic

配置

from anthropic import Anthropic

client = Anthropic(
    api_key="sk-your-lemondata-key",
    base_url="https://api.lemondata.cc"
)
注意:对于 Anthropic SDK,base URL 为 https://api.lemondata.cc(不包含 /v1)。

基础用法

message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, Claude!"}
    ]
)

print(message.content[0].text)

使用 System Prompt

message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    system="You are a helpful coding assistant.",
    messages=[
        {"role": "user", "content": "Write a Python function to reverse a string"}
    ]
)

流式传输

with client.messages.stream(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Tell me a story"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

视觉 (Vision)

import base64

# 来自 URL
message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What's in this image?"},
            {
                "type": "image",
                "source": {
                    "type": "url",
                    "url": "https://example.com/image.jpg"
                }
            }
        ]
    }]
)

# 来自 base64
with open("image.png", "rb") as f:
    image_data = base64.b64encode(f.read()).decode()

message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "Describe this image"},
            {
                "type": "image",
                "source": {
                    "type": "base64",
                    "media_type": "image/png",
                    "data": image_data
                }
            }
        ]
    }]
)

工具调用 (Tool Use)

message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    tools=[{
        "name": "get_weather",
        "description": "Get the weather for a location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {"type": "string", "description": "City name"}
            },
            "required": ["location"]
        }
    }],
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)

# 检查工具调用
for block in message.content:
    if block.type == "tool_use":
        print(f"Tool: {block.name}")
        print(f"Input: {block.input}")

深度思考 (Extended Thinking) (Claude Opus 4.5)

对于支持深度思考的模型:
message = client.messages.create(
    model="claude-opus-4-5",
    max_tokens=16000,
    thinking={
        "type": "enabled",
        "budget_tokens": 10000
    },
    messages=[{"role": "user", "content": "Solve this complex math problem..."}]
)

# 访问思考区块
for block in message.content:
    if block.type == "thinking":
        print(f"Thinking: {block.thinking}")
    elif block.type == "text":
        print(f"Response: {block.text}")

可用的 Claude 模型

模型最适用于
claude-opus-4-5复杂推理,深度思考
claude-sonnet-4-5通用场景,编程
claude-haiku-4-5快速响应

错误处理

from anthropic import APIError, APIStatusError, APIConnectionError

try:
    message = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello"}]
    )
except APIStatusError as e:
    if e.status_code == 401:
        print("Invalid API key")
    elif e.status_code == 429:
        print("Rate limit exceeded")
    else:
        print(f"API error: {e.status_code}")
except APIConnectionError:
    print("Connection error")
except APIError as e:
    print(f"Unexpected error: {e}")

对比:OpenAI SDK vs Anthropic SDK

两者均可配合 LemonData 用于 Claude 模型:
特性OpenAI SDKAnthropic SDK
Base URLhttps://api.lemondata.cc/v1https://api.lemondata.cc
Endpoint/chat/completions/v1/messages
System promptmessages 数组中独立的 system 参数
深度思考 (Extended thinking)不支持支持
请根据您的偏好或现有代码库进行选择。