Messages

POST /anthropic/v1/messages — Anthropic 原生 Messages API,用于调用 Claude 系列模型进行对话。

请求示例

cURLBash
curl https://www.ulovegpt.com/anthropic/v1/messages \
  -H "x-api-key: <你的 API_KEY>" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4.6",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "解释一下什么是 CAP 定理"}
    ]
  }'
PythonPython
import anthropic

client = anthropic.Anthropic(
    base_url="https://www.ulovegpt.com/anthropic",
    api_key="<你的 API_KEY>"
)

message = client.messages.create(
    model="anthropic/claude-sonnet-4.6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "解释一下什么是 CAP 定理"}
    ]
)

print(message.content[0].text)

请求参数

参数类型必填说明
modelstringClaude 模型名称
max_tokensinteger最大生成 token 数
messagesarray消息数组,支持 user/assistant 角色
systemstring/array系统 prompt(独立于 messages)
temperaturenumber0-1,采样温度(默认 1)
top_pnumber核采样参数(默认 1)
top_kintegerTop-K 采样
streamboolean是否启用流式输出
toolsarray工具定义(Anthropic 格式)
stop_sequencesarray自定义停止序列

响应格式

响应示例Json
{
  "id": "msg_abc123",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "CAP 定理(也称为 Brewer 定理)指出,在分布式系统中..."
    }
  ],
  "model": "anthropic/claude-sonnet-4.6",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 15,
    "output_tokens": 128,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0
  }
}

认证

Anthropic 协议使用 x-api-key Header 进行认证:

认证 HeaderBash
curl https://www.ulovegpt.com/anthropic/v1/messages \
  -H "x-api-key: sk-of-your-api-key" \
  -H "anthropic-version: 2023-06-01"