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)请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | Claude 模型名称 |
max_tokens | integer | 是 | 最大生成 token 数 |
messages | array | 是 | 消息数组,支持 user/assistant 角色 |
system | string/array | 否 | 系统 prompt(独立于 messages) |
temperature | number | 否 | 0-1,采样温度(默认 1) |
top_p | number | 否 | 核采样参数(默认 1) |
top_k | integer | 否 | Top-K 采样 |
stream | boolean | 否 | 是否启用流式输出 |
tools | array | 否 | 工具定义(Anthropic 格式) |
stop_sequences | array | 否 | 自定义停止序列 |
响应格式
响应示例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"