Langsung ke konten utama

Ringkasan

LemonData mendukung format native Anthropic Messages API. Gunakan Anthropic SDK resmi dengan LemonData untuk mengakses model Claude.

Instalasi

pip install anthropic

Konfigurasi

from anthropic import Anthropic

client = Anthropic(
    api_key="sk-your-lemondata-key",
    base_url="https://api.lemondata.cc"
)
Catatan: Base URL adalah https://api.lemondata.cc (tanpa /v1) untuk Anthropic SDK.

Penggunaan Dasar

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

print(message.content[0].text)

Dengan 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"}
    ]
)

Streaming

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

# Dari 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"
                }
            }
        ]
    }]
)

# Dari 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
                }
            }
        ]
    }]
)

Penggunaan Tool

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?"}]
)

# Periksa penggunaan tool
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)

Untuk model yang mendukung extended thinking:
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..."}]
)

# Akses blok thinking
for block in message.content:
    if block.type == "thinking":
        print(f"Thinking: {block.thinking}")
    elif block.type == "text":
        print(f"Response: {block.text}")

Model Claude yang Tersedia

ModelTerbaik Untuk
claude-opus-4-5Penalaran kompleks, extended thinking
claude-sonnet-4-5Tujuan umum, coding
claude-haiku-4-5Respons cepat

Penanganan Error

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}")

Perbandingan: OpenAI SDK vs Anthropic SDK

Keduanya berfungsi dengan LemonData untuk model Claude:
FiturOpenAI SDKAnthropic SDK
Base URLhttps://api.lemondata.cc/v1https://api.lemondata.cc
Endpoint/chat/completions/v1/messages
System promptDalam array messagesParameter system terpisah
Extended thinkingTidak didukungDidukung
Pilih berdasarkan preferensi Anda atau codebase yang sudah ada.