Skip to content

Claude (Anthropic协议)

概述

Claude是Anthropic开发的大语言模型,具有强大的对话和写作能力。它能理解上下文、生成连贯文本、编写代码,并擅长长逻辑推理和分析。注重安全性和道德准则,会明确表明自己是AI助手的身份。它支持多语言交流,能够处理复杂任务和长对话。

符合Anthropic API规范

本 API 符合 Anthropic Claude 接口格式规范,支持官方所有参数。

文档参考

本文档只列举了一部分参数,详细参数列表可参考 官方文档

支持的模型 ID

  • claude-opus-4-7
  • claude-opus-4-6
  • claude-opus-4-5
  • claude-opus-4
  • claude-sonnet-4-6
  • claude-sonnet-4-5
  • claude-sonnet-4
  • claude-haiku-4-5

基础信息 & 认证

API 请求地址:

https://api.exchangetoken.ai/v1/messages

请求方法:

POST

请求参数

Header 参数

参数名称类型必须说明示例值
Content-Typestring设置请求头类型,必须为 application/jsonapplication/json
Acceptstring设置响应类型,建议统一为 application/jsonapplication/json
x-api-keystring身份验证所需的 API_KEY,格式为 $YOUR_API_KEY$YOUR_API_KEY

Body 参数 (application/json)

参数名称类型必须说明示例
modelstring要使用的模型 ID。详见概述列出的可用版本,如 claude-opus-4-7claude-opus-4-7
messagesarray聊天消息列表,格式与 Anthropic 兼容。数组中的每个对象包含 rolecontent[{"role": "user", "content": "你好"}]
  message.rolestring消息角色,可选值:userassistantuser
  message.contentstring消息的具体内容。你好,请给我讲个笑话。
systemstringSystem 提示词。你是一个友善的人工智能助手。
temperaturenumber采样温度,取值 0~2。数值越大,输出越随机;数值越小,输出越集中和确定。0.7
top_pnumber另一种调节采样分布的方式,取值 0~1。和 temperature 通常二选一设置。0.9
streamboolean是否开启流式输出。设置为 true 时,返回类似 ChatGPT 的流式数据。false
max_tokensinteger单次回复可生成的最大 token 数量,受模型上下文长度限制。8192

完整代码示例

1. 基础对话 (Basic Chat)

bash
curl -X POST "https://api.exchangetoken.ai/v1/messages" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-7",
    "system": [
      {
        "type": "text",
        "text": "你是一个友善的人工智能助手"
      }
    ],
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "你好,给我科普一下量子力学吧"
          }
        ]
      }
    ],
    "temperature": 0.7,
    "max_tokens": 1000
  }'
python
import requests
import json

url = "https://api.exchangetoken.ai/v1/messages"
headers = {
    "x-api-key": "YOUR_API_KEY",
    "Content-Type": "application/json"
}

data = {
    "model": "claude-opus-4-7",
    "system": [
        {
            "type": "text",
            "text": "你是一个友善的人工智能助手"
        }
    ],
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "你好,给我科普一下量子力学吧"
                }
            ]
        }
    ],
    "temperature": 0.7,
    "max_tokens": 1000
}

response = requests.post(url, headers=headers, json=data)
print(response.json())

2. 思考模型 (Thinking Mode)

bash
curl -X POST "https://api.exchangetoken.ai/v1/messages" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-6",
    "max_tokens": 2048,
    "thinking": {
      "type": "enabled",
      "budget_tokens": 1024
    },
    "messages": [
      {
        "role": "user",
        "content": "Explain how quantum entanglement works."
      }
    ]
  }'
python
import requests
import json

url = "https://api.exchangetoken.ai/v1/messages"
headers = {
    "x-api-key": "YOUR_API_KEY",
    "Content-Type": "application/json"
}

data = {
    "model": "claude-sonnet-4-6",
    "max_tokens": 2048,
    "thinking": {
        "type": "enabled",
        "budget_tokens": 1024
    },
    "messages": [
        {
            "role": "user",
            "content": "Explain how quantum entanglement works."
        }
    ]
}

response = requests.post(url, headers=headers, json=data)
print(response.json())

3. 流式响应 (Streaming)

bash
curl -X POST "https://api.exchangetoken.ai/v1/messages" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-7",
    "messages": [
      {
        "role": "user",
        "content": "给我讲一个简短的故事"
      }
    ],
    "stream": true,
    "max_tokens": 1024
  }'
python
import requests
import json

url = "https://api.exchangetoken.ai/v1/messages"
headers = {
    "x-api-key": "YOUR_API_KEY",
    "Content-Type": "application/json"
}

data = {
    "model": "claude-opus-4-7",
    "messages": [
        {
            "role": "user",
            "content": "给我讲一个简短的故事"
        }
    ],
    "stream": True,
    "max_tokens": 1024
}

response = requests.post(url, headers=headers, json=data, stream=True)

for line in response.iter_lines():
    if line:
        print(line.decode('utf-8'))