通过合作伙伴平台使用Anthropic的客户端SDK需要额外的配置。如果您正在使用Amazon Bedrock,请参阅此指南;如果您正在使用Google Cloud Vertex AI,请参阅此指南

Python

Python库GitHub仓库

示例:

Python
import anthropic

client = anthropic.Anthropic(
    # 默认为os.environ.get("ANTHROPIC_API_KEY")
    api_key="my_api_key",
)
message = client.messages.create(
    model="claude-3-5-sonnet-20240620",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "你好,Claude"}
    ]
)
print(message.content)

Typescript

Typescript库GitHub仓库

示例:

Typescript
import Anthropic from '@anthropic-ai/sdk';

const anthropic = new Anthropic({
  apiKey: 'my_api_key', // 默认为process.env["ANTHROPIC_API_KEY"]
});

const msg = await anthropic.messages.create({
  model: "claude-3-5-sonnet-20240620",
  max_tokens: 1024,
  messages: [{ role: "user", content: "你好,Claude" }],
});
console.log(msg);