跳转到主要内容

概述

LemonData 支持原生 Anthropic Messages API 路径,因此你可以直接将官方 Anthropic SDK 用于 Claude 模型。
对于 Anthropic SDK,请使用 https://api.lemondata.cc 作为 base URL,不要自行追加 /v1
类型: 原生 SDK主要路径: Anthropic-native支持级别: 强原生路径
在已文档化的 SDK 路径中,这是 Claude 原生功能支持性最强的一条。

安装

pip install anthropic

配置客户端

from anthropic import Anthropic

client = Anthropic(
    api_key="sk-your-lemondata-key",
    base_url="https://api.lemondata.cc",
)

基本用法

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain LemonData in one sentence."}
    ]
)

print(message.content[0].text)

流式传输

with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a short poem about coding."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

视觉

import base64

message = client.messages.create(
    model="claude-sonnet-4-6",
    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"
                }
            }
        ]
    }]
)

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

message = client.messages.create(
    model="claude-sonnet-4-6",
    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
                }
            }
        ]
    }]
)

工具使用

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

for block in message.content:
    if block.type == "tool_use":
        print(block.name)
        print(block.input)

扩展思考

message = client.messages.create(
    model="claude-opus-4-6",
    max_tokens=16000,
    thinking={
        "type": "enabled",
        "budget_tokens": 10000
    },
    messages=[{"role": "user", "content": "Solve this complex problem step by step."}]
)

for block in message.content:
    if block.type == "thinking":
        print(block.thinking)
    elif block.type == "text":
        print(block.text)

推荐的 Claude 模型

Model最适合
claude-opus-4-6深度推理、长篇分析
claude-sonnet-4-6编码、通用助手任务
claude-haiku-4-5快速、轻量级响应

故障排查

  • 使用 https://api.lemondata.cc
  • 配置 Anthropic SDK 时,不要手动追加 /v1
  • 检查你的 LemonData API key 是否以 sk- 开头
  • 确认该 key 已在 LemonData dashboard 中激活
  • 让 Anthropic SDK 管理 auth header,而不是手动添加自定义 headers
  • 精确核对 Claude 模型名称
  • 在 LemonData 模型目录中检查当前可用性