外观
Chat Messages
创建对话消息,是 Anthropic Messages API 的兼容实现, 意味着本平台支持Claude智能体的最佳消息格式。请求体遵循 Claude 格式
- 方法:
POST /v1/messages - 鉴权:必须使用 API Key(
agent = "api",即sk-开头的 Key)
请求体
请求体遵循 Anthropic Messages API 格式,转换为内部 RelayForm(manager/types.go):
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | ✅ | 模型标识,如 claude-sonnet-4 |
messages | Message[] | ✅ | 对话消息数组(Anthropic 格式) |
system | string | ❌ | 系统提示词,会转换为 system 消息 |
max_tokens | int | ❌ | 最大生成 token 数 |
temperature | float32 | ❌ | 采样温度 |
top_p | float32 | ❌ | 核采样 |
top_k | int | ❌ | Top-K 采样 |
stream | bool | ❌ | 是否流式返回,默认 false |
tools | FunctionTools | ❌ | 工具定义(见 公共类型) |
tool_choice | any | ❌ | 工具选择策略 |
消息结构(Anthropic Message)
| 字段 | 类型 | 说明 |
|---|---|---|
role | string | user / assistant |
content | string | ContentBlock[] | 文本内容,或内容块数组 |
内容块(ContentBlock)支持 text 类型:
json
{ "type": "text", "text": "Hello" }非流式响应
响应示例
json
{
"id": "msg_9a1b2c3d",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-4",
"content": [
{ "type": "text", "text": "你好!我是壹通提供的 AI 助手。" }
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 12,
"output_tokens": 18
}
}响应字段
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 本次消息 ID,前缀 msg_ |
type | string | 固定为 "message" |
role | string | 固定为 "assistant" |
model | string | 实际使用的模型 |
content | ContentBlock[] | 内容块数组,text 或 tool_use |
stop_reason | string | "end_turn"(正常结束)或 "tool_use"(工具调用) |
usage | ClaudeMessageUsage | token 用量(input_tokens / output_tokens) |
非流式请求示例
bash
curl https://app.1in2.top/api/v1/messages \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-api-key-here" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 200,
"system": "你是一个乐于助人的助手。",
"messages": [
{ "role": "user", "content": "用一句话介绍你自己" }
]
}'流式响应(SSE)
设置 stream: true 后,服务端以 text/event-stream 返回,每个事件为一行 event: <type> + data: <json>。
event: message_start
data: {"type":"message_start","message":{...}}
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":"你好"}}
event: content_block_stop
data: {"type":"content_block_stop","index":0}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":18}}
event: message_stop
data: {"type":"message_stop"}流式事件序列
| 事件 | 说明 |
|---|---|
message_start | 消息开始,携带消息信封与 input_tokens |
content_block_start | 内容块开始(text 或 tool_use) |
content_block_delta | 内容增量(text_delta 或 input_json_delta) |
content_block_stop | 内容块结束 |
message_delta | 消息增量,携带 stop_reason 与 output_tokens |
message_stop | 消息结束 |
error | 发生错误时返回,error 含 api_error 信息 |
流式请求示例
bash
curl -N https://app.1in2.top/api/v1/messages \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-api-key-here" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 200,
"messages": [ { "role": "user", "content": "讲一个笑话" } ],
"stream": true
}'工具调用
使用 tools 声明工具,模型返回的 content 会携带 tool_use 内容块,stop_reason 为 "tool_use"。
请求示例(工具调用)
bash
curl https://app.1in2.top/api/v1/messages \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-api-key-here" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 200,
"messages": [
{ "role": "user", "content": "北京今天天气怎么样?" }
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "城市名" }
},
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}'响应(工具调用被触发)
此时 stop_reason 为 "tool_use",content 中包含 tool_use 内容块:
json
{
"id": "msg_9a1b2c3d",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-4",
"content": [
{
"type": "tool_use",
"id": "call_abc123",
"name": "get_weather",
"input": { "city": "北京" }
}
],
"stop_reason": "tool_use",
"usage": { "input_tokens": 15, "output_tokens": 10 }
}将工具执行结果以 user 消息携带 tool_result 内容块回传,即可继续多轮对话:
json
{
"model": "claude-sonnet-4",
"max_tokens": 200,
"messages": [
{ "role": "user", "content": "北京今天天气怎么样?" },
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "call_abc123",
"name": "get_weather",
"input": { "city": "北京" }
}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "call_abc123",
"content": "{\"temperature\": \"18°C\", \"condition\": \"晴\"}"
}
]
}
]
}常见错误
| 错误类型 | 触发场景 |
|---|---|
access_denied_error | 中继被关闭(CloseRelay) |
authentication_error | 未携带 Key 或非 api 身份 |
invalid_request_error | 请求体非法或无法转换 |
access_denied_error | 模型不在 API Key 白名单 |
quota_exceeded_error | 额度不足或套餐不可用 |
