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の場合、ベース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)
システムプロンプトの使用
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)
ビジョン(画像認識)
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)
思考の拡張をサポートしているモデルの場合:
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
どちらもClaudeモデルでLemonDataを使用できます:
| 機能 | OpenAI SDK | Anthropic SDK |
|---|
| ベースURL | https://api.lemondata.cc/v1 | https://api.lemondata.cc |
| エンドポイント | /chat/completions | /v1/messages |
| システムプロンプト | messages配列内 | 個別のsystemパラメータ |
| 思考の拡張(Extended Thinking) | 非対応 | 対応 |
好みや既存のコードベースに基づいて選択してください。