curl --request POST \
--url https://api.getinfinityblue.com/v1/messages \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--header 'anthropic-version: <anthropic-version>' \
--data '
{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "用一句话介绍你自己。"
}
]
}
'{
"id": "<string>",
"type": "message",
"role": "assistant",
"content": [
{
"type": "<string>",
"text": "<string>"
}
],
"model": "<string>",
"stop_reason": "end_turn",
"usage": {
"input_tokens": 123,
"output_tokens": 123,
"cache_creation_input_tokens": 123,
"cache_read_input_tokens": 123
}
}{
"error": {
"message": "无效的时长。支持的范围为 4 到 15 秒。",
"type": "invalid_request_error",
"param": "seconds",
"code": "invalid_duration"
}
}{
"error": {
"message": "无效的时长。支持的范围为 4 到 15 秒。",
"type": "invalid_request_error",
"param": "seconds",
"code": "invalid_duration"
}
}{
"error": {
"message": "无效的时长。支持的范围为 4 到 15 秒。",
"type": "invalid_request_error",
"param": "seconds",
"code": "invalid_duration"
}
}{
"error": {
"message": "无效的时长。支持的范围为 4 到 15 秒。",
"type": "invalid_request_error",
"param": "seconds",
"code": "invalid_duration"
}
}创建 Claude 消息
以 Anthropic Claude Messages 原生格式创建消息响应,支持工具调用与扩展思考
curl --request POST \
--url https://api.getinfinityblue.com/v1/messages \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--header 'anthropic-version: <anthropic-version>' \
--data '
{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "用一句话介绍你自己。"
}
]
}
'{
"id": "<string>",
"type": "message",
"role": "assistant",
"content": [
{
"type": "<string>",
"text": "<string>"
}
],
"model": "<string>",
"stop_reason": "end_turn",
"usage": {
"input_tokens": 123,
"output_tokens": 123,
"cache_creation_input_tokens": 123,
"cache_read_input_tokens": 123
}
}{
"error": {
"message": "无效的时长。支持的范围为 4 到 15 秒。",
"type": "invalid_request_error",
"param": "seconds",
"code": "invalid_duration"
}
}{
"error": {
"message": "无效的时长。支持的范围为 4 到 15 秒。",
"type": "invalid_request_error",
"param": "seconds",
"code": "invalid_duration"
}
}{
"error": {
"message": "无效的时长。支持的范围为 4 到 15 秒。",
"type": "invalid_request_error",
"param": "seconds",
"code": "invalid_duration"
}
}{
"error": {
"message": "无效的时长。支持的范围为 4 到 15 秒。",
"type": "invalid_request_error",
"param": "seconds",
"code": "invalid_duration"
}
}可填写的模型
| 模型 ID | 说明 |
|---|---|
claude-3-opus-20240229 | Claude 3 旗舰,顶级推理与理解能力 |
claude-3-5-sonnet-20241022 | Claude 3.5 Sonnet,均衡性能 |
claude-3-5-haiku-20241022 | Claude 3.5 Haiku,快速轻量 |
GET /v1/models。
版本头
每次请求必须携带anthropic-version: 2023-06-01(或更新版本)。
系统提示
system 字段可以是纯文本字符串,也可以是包含缓存控制块的对象数组,
用于精细控制提示缓存行为。
工具调用
在tools 中定义函数,使用 JSON Schema 描述 input_schema。
模型会在 content 中返回 tool_use 类型的块;
将工具执行结果以 tool_result 类型回填到下一轮请求中。
扩展思考(Extended Thinking)
设置thinking.type: enabled 并指定 thinking.budget_tokens
可启用扩展思考模式,模型会在响应中返回 thinking 类型的内容块。
流式输出
设置stream: true 后,响应以 Server-Sent Events 形式返回,
事件类型包括 message_start、content_block_delta、message_stop 等。授权
请求头
Anthropic API 版本号,当前推荐值为 2023-06-01。
"2023-06-01"
Anthropic API Key(可选)。也可使用标准的
Authorization: Bearer YOUR_KEY 头,二者任选其一。
请求体
Anthropic Claude Messages API 请求体。
模型 ID,例如 claude-3-opus-20240229。完整列表见 GET /v1/models。
"claude-3-opus-20240229"
对话消息列表,role 必须在 user 和 assistant 之间交替。
Show child attributes
Show child attributes
模型在本次响应中可生成的最大 token 数,必须指定。
x >= 1系统提示,可以是纯文本字符串,也可以是包含缓存控制块的对象数组。
采样温度,取值 0–1,控制输出随机性。
0 <= x <= 1核采样概率阈值,与 temperature 二选一调整。
Top-K 采样参数,仅考虑概率最高的 K 个 token。
是否以 Server-Sent Events 流式返回。
停止序列列表,模型生成到其中任一序列时停止。
模型可调用的工具列表,每个工具需提供 name、description 和 input_schema。
Show child attributes
Show child attributes
工具调用策略。auto 由模型自行决定,any 强制至少调用一个工具,
tool 强制调用指定工具(需同时提供 name)。
Show child attributes
Show child attributes
扩展思考(Extended Thinking)配置,仅对支持该功能的模型生效。
Show child attributes
Show child attributes
请求附加元数据。
Show child attributes
Show child attributes
响应
成功创建消息
Anthropic Claude Messages API 响应体。
本次消息的唯一标识。
对象类型,值为 message。
"message"
响应角色,值为 assistant。
"assistant"
响应内容块列表,可包含文本、工具调用或思考块。
Show child attributes
Show child attributes
实际处理请求的模型。
生成结束原因。
end_turn, max_tokens, stop_sequence, tool_use Token 用量统计。
Show child attributes
Show child attributes