跳到主要内容

Messages API

Anthropic Claude 原生 Messages 接口,支持 Content Block 结构和工具使用。

POST /v1/messages

请求示例

curl https://gw.opentoken.io/v1/messages \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPEN_TOKEN_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-4-6",
"system": "你是一位资深 Python 编程导师。",
"messages": [
{"role": "user", "content": [{"type": "text", "text": "请解释 Python 装饰器的工作原理。"}]}
],
"max_tokens": 500,
"temperature": 0.7
}'

请求参数

参数类型必填描述
modelstring模型 ID
messagesarray消息列表,Content Block 格式
systemstring / array系统提示词
max_tokensinteger限制本次响应可生成的最大 token 数;实际输出长度可能因停止条件、上下文长度或模型限制而更短。
temperaturenumber采样温度,用于控制生成过程中的随机性。较低的值通常会使输出更稳定、可预测;较高的值通常会增加输出的多样性。
toolsarray工具定义
streamboolean流式输出

多模态(图片 + 文本)

{
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "描述这张图片中的内容。"},
{"type": "image", "source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "/9j/4AAQSkZJRg..."
}}
]
}]
}

Content Block 结构

Claude 使用 Content Block 表示消息内容,支持文本、图片和工具结果:

{
"role": "user",
"content": [
{"type": "text", "text": "描述这张图片"},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "/9j/4AAQSkZJRg..."
}
}
]
}

Tool Use 示例

{
"model": "claude-sonnet-4-6",
"max_tokens": 500,
"tools": [{
"name": "get_weather",
"description": "获取指定城市的天气",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}],
"messages": [{"role": "user", "content": "北京今天天气如何?"}]
}

响应结构

{
"id": "msg_abc123",
"type": "message",
"role": "assistant",
"content": [
{"type": "text", "text": "装饰器是 Python 中的一种设计模式..."}
],
"model": "claude-sonnet-4-6",
"stop_reason": "end_turn",
"usage": {
"input_tokens": 25,
"output_tokens": 120
}
}
Copyright © 2026 OpenToken.