首批通过分布式安全可靠测评,为关键业务系统打造
Create a Message - Anthropic 消息创建
更新时间:2026-07-02 17:33:29
功能说明
此接口用于与 Anthropic 模型进行消息交互,遵循 Anthropic Messages API 规范。使用专用前缀 /api/anthropic/v1/messages,支持多轮对话、流式响应、工具调用等功能。
调用说明
接口约束
- 调用者需要拥有 API Key,详情请参见 管理 AI API Key。
请求路径
POST {BASE_URL}/api/anthropic/v1/messages
请求头
| 名称 | 是否必选 | 示例值 | 描述 |
|---|---|---|---|
| Authorization | 是 | Bearer {token} | 鉴权信息 |
| Content-Type | 否 | application/json | 请求体格式 |
请求参数
Path 参数
此接口无 Path 参数。
Query 参数
此接口无 Query 参数。
Body 参数
| 名称 | 类型 | 是否必填 | 示例值 | 描述 |
|---|---|---|---|---|
| model | string | 是 | claude-3-opus-20240229 | 模型名称,支持的模型请参见 查询 Anthropic 模型列表 |
| max_tokens | integer | 是 | 1024 | Anthropic 官方要求必填,建议始终显式传入。不同模型有不同的最大值限制 |
| messages | array[object] | 是 | [{"role": "user", "content": "Hello"}] | 消息列表。支持多轮对话,每条消息包含 role 和 content |
| system | string 或 array[object] | 否 | "You are a helpful assistant" | 系统提示词,用于提供上下文和指令 |
messages 字段说明
每条消息包含以下字段:
| 名称 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| role | string | 是 | 角色,可选值:user(用户)、assistant(助手) |
| content | string 或 array[object] | 是 | 消息内容。可以是字符串,也可以是内容块数组 |
content 内容块类型:
- 文本内容:
{"type": "text", "text": "消息文本"} - 图片内容:
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": "..."}} - 工具使用:
{"type": "tool_use", "id": "...", "name": "...", "input": {...}} - 工具结果:
{"type": "tool_result", "tool_use_id": "...", "content": "..."}
返回结果
返回参数
| 名称 | 类型 | 描述 |
|---|---|---|
| success | boolean | 请求是否成功 |
| code | string | 返回码 |
| message | string | 返回信息 |
| data | object | 业务返回数据 |
请求示例
基础文本对话
curl --request POST '{BASE_URL}/api/anthropic/v1/messages' \
--header 'Authorization: Bearer {token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Hello, Claude"
}
]
}'
多轮对话
curl --request POST '{BASE_URL}/api/anthropic/v1/messages' \
--header 'Authorization: Bearer {token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"system": "You are a helpful assistant",
"messages": [
{
"role": "user",
"content": "Hello there."
},
{
"role": "assistant",
"content": "Hi, I'\''m Claude. How can I help you?"
},
{
"role": "user",
"content": "Can you explain LLMs in plain English?"
}
]
}'
流式响应
curl --request POST '{BASE_URL}/api/anthropic/v1/messages' \
--header 'Authorization: Bearer {token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Write a short poem"
}
],
"stream": true
}'
返回示例
成功响应
{
"id": "msg_01XYZ",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello! How can I help you today?"
}
],
"model": "claude-3-opus-20240229",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 10,
"output_tokens": 8,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0
}
}
流式响应事件
event: message_start
data: {"type": "message_start", "message": {"id": "msg_01XYZ", "type": "message", "role": "assistant", "content": [], "model": "claude-3-opus-20240229", "stop_reason": null, "usage": {"input_tokens": 10, "output_tokens": 0}}}
event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}
event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello"}}
event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "!"}}
event: content_block_stop
data: {"type": "content_block_stop", "index": 0}
event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn"}, "usage": {"output_tokens": 8}}
event: message_stop
data: {"type": "message_stop"}
错误响应
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "messages: field required"
}
}