Claude 原生协议(Anthropic)
POST https://ai-api.mandao.com/v1/messages
使用 Anthropic Claude Messages API 原生格式进行内容生成。
请求参数
Header 参数
| 参数名 | 类型 | 必需 | 说明 |
|---|---|---|---|
| Authorization | string | 是 | Bearer Token 认证。或使用 x-api-key 替代 |
| anthropic-version | string | 否 | API 版本,如 2023-06-01 |
| anthropic-beta | string | 否 | Beta 功能标识,如 tools-2024-04-04 |
| Content-Type | string | 是 | application/json |
Body 参数 (application/json)
| 参数名 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
| model | string | 是 | - | 模型 ID,如 claude-sonnet-4-5、claude-opus-4-7 等 |
| messages | array[object] | 是 | - | 对话消息列表 |
| messages[].role | string | 是 | - | 角色:user、assistant |
| messages[].content | string 或 array[object] | 是 | - | 消息内容,支持文本和图片 |
| system | string 或 array[object] | 否 | - | 系统提示词 |
| max_tokens | integer | 是 | - | 最大生成 token 数 |
| temperature | number | 否 | - | 采样温度,范围 0~1 |
| top_p | number | 否 | - | 核采样参数 |
| top_k | integer | 否 | - | Top-K 采样 |
| stream | boolean | 否 | false | 是否流式输出 |
| stop_sequences | array[string] | 否 | - | 停止序列 |
| tools | array[object] | 否 | - | 工具定义列表 |
| tool_choice | object | 否 | - | 工具选择策略 |
| thinking | object | 否 | - | 思考配置:{"type": "enabled", "budget_tokens": 1024} |
| metadata | object | 否 | - | 元数据,如 {"user_id": "xxx"} |
| web_search_options | object | 否 | - | 网络搜索配置 |
图片消息格式
{
"role": "user",
"content": [
{"type": "text", "text": "描述这张图片"},
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "<base64>"}}
]
}
请求示例
curl --location 'https://ai-api.mandao.com/v1/messages' \
--header 'x-api-key: <your-api-key>' \
--header 'anthropic-version: 2023-06-01' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-opus-4-6",
"messages": [
{
"role": "user",
"content": "What should I search for to find the latest developments in renewable energy?"
}
],
"max_tokens": 1000
}'
返回响应
200 成功
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"model": "claude-opus-4-6",
"content": [
{
"type": "text",
"text": "Here are some search suggestions..."
}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 20,
"output_tokens": 100
}
}
错误响应(Claude 格式)
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "错误描述信息"
}
}
注意事项
- Claude 原生协议使用
x-api-keyHeader 认证,平台会自动转换为Bearer Token。 - 支持
anthropic-versionHeader 指定 API 版本。 - Claude 支持
system参数作为系统提示词(支持字符串和数组格式)。 - 图片需要以 Base64 编码传输。