基于湖库一体架构,统一管理结构化、半结构化与非结构化等多模态数据,一个系统承载事务处理、实时分析与 AI 工作负载。
Create a Message - Anthropic 消息创建
更新时间:2026-08-13 10:47:55
功能说明
此接口用于与 Anthropic 模型进行消息交互,遵循 Anthropic Messages API 规范。使用专用前缀 /api/anthropic/v1/messages,支持多轮对话、流式响应、工具调用等功能。
调用说明
接口约束
- 调用者需要拥有 API Key,详情请参见 管理 AI API Key。
请求路径
POST https://ai-api-cn.oceanbase.com/api/anthropic/v1/messages
请求头
| 名称 | 是否必选 | 示例值 | 描述 |
|---|---|---|---|
| x-api-key | 是 | YOUR_API_KEY | 鉴权信息 |
| anthropic-version | 否 | 2023-06-01 | Anthropic API 版本 |
| Content-Type | 是 | application/json | 请求体格式 |
请求参数
Path 参数
此接口无 Path 参数。
Query 参数
此接口无 Query 参数。
Body 参数
| 名称 | 类型 | 是否必填 | 示例值 | 描述 |
|---|---|---|---|---|
| model | string | 是 | kimi-k2.6 | 模型名称,支持的模型请参见 查询 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 -sS -X POST "https://ai-api-cn.oceanbase.com/api/anthropic/v1/messages" \
-H "x-api-key: YOUR_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "kimi-k2.6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "你好"
}
]
}'
多轮对话
curl -sS -X POST "https://ai-api-cn.oceanbase.com/api/anthropic/v1/messages" \
-H "x-api-key: YOUR_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "kimi-k2.6",
"max_tokens": 1024,
"system": "You are a helpful assistant",
"messages": [
{
"role": "user",
"content": "Hello there."
},
{
"role": "assistant",
"content": "Hi! How can I help you?"
},
{
"role": "user",
"content": "Can you explain LLMs in plain English?"
}
]
}'
流式响应
curl -sS -X POST "https://ai-api-cn.oceanbase.com/api/anthropic/v1/messages" \
-H "x-api-key: YOUR_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "kimi-k2.6",
"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": "kimi-k2.6",
"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": "kimi-k2.6", "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"
}
}