PROVIDER · US

Anthropic

使用 Messages API;部分模型支持 OpenAI SDK 兼容层,但原生 SDK 的推理参数更完整。

Anthropic SDK通常需代理官方文档 ↗

三步接入

以下示例使用环境变量保存密钥;不要把密钥写进代码或提交到仓库。

  1. 设置环境变量
    shell
    export ANTHROPIC_API_KEY="你的密钥"
  2. 安装 SDK(Anthropic 官方包)
    install
    pip install anthropic
    # 或 npm install @anthropic-ai/sdk
  3. 发起请求

    base URL https://api.anthropic.com;示例模型 ID your-model-id(占位,请替换为厂商文档中的真实 ID)。

cURL
cURL
curl https://api.anthropic.com/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "your-model-id",
    "max_tokens": 512,
    "messages": [{"role": "user", "content": "你好,请用三句话介绍你自己。"}]
  }'
Python
Python
import os
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.anthropic.com",
    api_key=os.environ["ANTHROPIC_API_KEY"],
)

message = client.messages.create(
    model="your-model-id",
    max_tokens=512,
    messages=[{"role": "user", "content": "你好,请用三句话介绍你自己。"}],
)
print(message.content[0].text)
Node.js
Node.js
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  baseURL: 'https://api.anthropic.com',
  apiKey: process.env.ANTHROPIC_API_KEY,
});

const message = await client.messages.create({
  model: 'your-model-id',
  max_tokens: 512,
  messages: [{ role: 'user', content: '你好,请用三句话介绍你自己。' }],
});
console.log(message.content[0].text);

本站收录的相关模型

按文本综合排名排序,最多展示 20 个;价格为默认路由价。

0 个
目前没有模型记录绑定到该 Provider。国内模型的官方价格与调用信息仍在人工整理中; 可先参考上方通用示例和官方文档。