Messages 接口(Anthropic 兼容)
接口说明
- 方法:
POST - 路径:
/v1/messages - 完整地址:
https://anideaai.com/v1/messages - 用途:使用 Anthropic Messages 请求格式调用模型。
请求头
| 请求头 | 必填 | 说明 |
|---|---|---|
Authorization | 是 | Bearer sk-xxxxx |
anthropic-version | 是 | 例如 2023-06-01 |
Content-Type | 是 | application/json |
x-api-key | 否 | Anthropic 兼容头,可选 |
主要请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名称 |
messages | array | 是 | 消息数组(user/assistant) |
max_tokens | integer | 是 | 最大输出长度 |
system | string/array | 否 | 系统提示 |
temperature | number | 否 | 随机性(0~1) |
top_p | number | 否 | 核采样 |
top_k | integer | 否 | 候选采样 |
stream | boolean | 否 | 是否流式 |
stop_sequences | array | 否 | 停止序列 |
tools | array | 否 | 工具定义(含 input_schema) |
tool_choice | object | 否 | 工具策略(auto/any/tool) |
thinking | object | 否 | 推理开关与预算 |
cURL 示例
curl https://anideaai.com/v1/messages \
-H "Authorization: Bearer sk-xxxxx" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-opus-4-6",
"max_tokens": 512,
"messages": [
{"role": "user", "content": "请给一份一周健身计划"}
]
}'
响应示例
{
"id": "msg_01abc",
"type": "message",
"role": "assistant",
"model": "claude-opus-4-6",
"content": [
{"type": "text", "text": "下面是一份可执行的一周健身计划..."}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 120,
"output_tokens": 260,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0
}
}
流式说明
stream=true 时返回事件流,事件类型可能包含 message_start、content_block_delta、message_stop。不同模型事件细节会略有差异。
与 Chat Completions 的主要区别
| 维度 | Messages API | Chat Completions API |
|---|---|---|
| 端点 | /v1/messages | /v1/chat/completions |
| 版本头 | 需要 anthropic-version | 不需要 |
| 消息结构 | 更接近 Anthropic 风格 | OpenAI 风格 |
| 工具字段 | input_schema | parameters |
| 返回内容 | content 数组块 | choices[].message |