LemonData는 네이티브 Anthropic Messages API 형식을 지원합니다. Claude 모델에 액세스하려면 LemonData와 함께 공식 Anthropic SDK를 사용하세요.
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
}
}
]
}]
)
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)
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..."}]
)
# thinking 블록 액세스
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 | 복잡한 추론, extended thinking |
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
두 SDK 모두 Claude 모델을 위해 LemonData와 함께 작동합니다:
| 기능 | OpenAI SDK | Anthropic SDK |
|---|
| Base URL | https://api.lemondata.cc/v1 | https://api.lemondata.cc |
| Endpoint | /chat/completions | /v1/messages |
| System prompt | messages 배열 내 | 별도의 system 파라미터 |
| Extended thinking | 지원되지 않음 | 지원됨 |
선호도나 기존 코드베이스에 따라 선택하세요.