跳轉到主要內容

概覽

LemonData 支援原生 Anthropic Messages API 路徑,因此你可以直接使用官方 Anthropic SDK 來存取 Claude 模型。
對於 Anthropic SDK,請使用 https://api.lemondata.cc 作為 base URL,且不要自行附加 /v1
類型: 原生 SDK主要路徑: Anthropic-native支援級別: 強原生路徑
在已文件化的 SDK 路徑中,這是對 Claude 原生功能支援最強的一條。

安裝

pip install anthropic

設定 Client

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,而不是手動加入自訂 header
  • 請精確確認 Claude 模型名稱
  • 請在 LemonData 模型目錄中檢查目前是否可用