Anthropic 兼容
标准 Anthropic Messages 兼容路径,适合 Anthropic SDK 与已有 Anthropic 代码迁移。
POST/api/v1/messages
Anthropic 兼容
标准 Anthropic Messages 兼容路径;历史路径 /api/anthropic/v1/messages 继续兼容。
🔑Bearer API Key请求头使用 Authorization: Bearer sk_live_...,也支持 x-api-key。
🧩先确认模型调用名请先在 模型广场 筛选支持当前接口格式的模型,打开模型详情页复制调用名。当前账号的最终可用范围还受组织权限影响。 请求结构
下方内容用于确认方法、地址和鉴权方式,属于 HTTP 结构片段,不是可独立执行示例。
POST https://www.silvamux.com/api/v1/messages
Authorization: Bearer $SILVAMUX_API_KEY
Content-Type: application/json
请求体
application/json · AnthropicRequest · 必填
| 字段 | 类型 | 必填 | 说明 |
|---|
max_tokens | integer | 是 | 单次响应允许生成的最大 token 数。 |
messages | array<AnthropicMessage> | 是 | 对话消息列表(按时间顺序)。 |
messages.content | string | array<AnthropicContentBlock> | 是 | 消息内容:字符串或内容块数组。 |
messages.role | string (user | assistant) | 是 | 消息角色(system 是顶层参数,不是消息角色)。 |
metadata | object | 否 | 请求元数据。 |
metadata.user_id | string | 否 | 终端用户标识符。 |
model | string | 是 | SilvaMux 模型调用名(id 或 alias)。从模型广场模型详情页复制。 |
stop_sequences | array<string> | 否 | 停止序列列表。 |
stream | boolean | 否 | 是否启用流式输出(SSE)。;默认值:false |
system | string | array<AnthropicTextBlock> | 否 | 系统提示(顶层参数)。 |
temperature | number | 否 | 采样温度,控制输出随机性。 |
tool_choice | AnthropicToolChoice | 否 | — |
tool_choice.name | string | 否 | type=tool 时指定的工具名。 |
tool_choice.type | string (auto | any | tool) | 是 | auto 模型自行决定;any 必须调用其中之一;tool 指定工具。 |
tools | array<AnthropicTool> | 否 | 可用工具列表。 |
tools.description | string | 否 | 工具用途描述。 |
tools.input_schema | object | 是 | 入参的 JSON Schema。 |
tools.name | string | 是 | 工具名。 |
top_k | integer | 否 | 仅从概率前 k 的候选中采样。 |
top_p | number | 否 | 核采样参数,temperature 的替代方案。 |
响应
200Anthropic Messages JSON 或 SSE 响应。
application/json · AnthropicMessageResponse
| 字段 | 类型 | 说明 |
|---|
content | array<AnthropicTextBlock | AnthropicToolUseBlock> | 响应内容块列表。 |
id | string | 消息 ID。 |
model | string | 实际使用的模型名。 |
role | string (assistant) | — |
stop_reason | string (end_turn | max_tokens | stop_sequence | tool_use) | 结束原因。 |
stop_sequence | string | 命中的停止序列。 |
type | string (message) | — |
usage | object | — |
usage.input_tokens | integer | 输入 token 数。 |
usage.output_tokens | integer | 输出 token 数。 |
defaultAnthropic 兼容错误。
application/json · AnthropicError
| 字段 | 类型 | 说明 |
|---|
error | object | — |
error.message | string | — |
error.type | string | — |
type | string (error) | — |
ℹ️历史路径 /api/anthropic/v1/messages 继续兼容,仅供已接入项目使用,新项目应使用 /api/v1/messages。
curl https://www.silvamux.com/api/v1/messages \
-H "Authorization: Bearer $SILVAMUX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "minimax-m2.5",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "你好"}]
}'
# pip install anthropic
# base_url 指到 /api:SDK 会在其后拼接 /v1/messages
import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["SILVAMUX_API_KEY"],
base_url="https://www.silvamux.com/api",
)
message = client.messages.create(
model="minimax-m2.5",
max_tokens=1024,
messages=[{"role": "user", "content": "你好"}],
)
print(message.content[0].text)
// npm install @anthropic-ai/sdk
// baseURL 指到 /api:SDK 会在其后拼接 /v1/messages
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: process.env.SILVAMUX_API_KEY,
baseURL: "https://www.silvamux.com/api",
});
const message = await client.messages.create({
model: "minimax-m2.5",
max_tokens: 1024,
messages: [{ role: "user", content: "你好" }],
});
console.log(message.content[0].text);
// 仅依赖标准库
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
)
func main() {
payload := []byte(`{
"model": "minimax-m2.5",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "你好"}]
}`)
req, err := http.NewRequest("POST", "https://www.silvamux.com/api/v1/messages", bytes.NewReader(payload))
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("SILVAMUX_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}
响应(Anthropic 格式):
{
"id": "msg_xxxx",
"type": "message",
"role": "assistant",
"content": [{"type": "text", "text": "你好!有什么可以帮你的吗?"}],
"usage": {"input_tokens": 10, "output_tokens": 15}
}
流式遵循 Anthropic SSE 规范。