Anthropic 風格
使用 Anthropic Messages API 格式
SilentAPI 在 /v1/messages 提供與 Anthropic Messages API 相容的端點,將 Anthropic SDK 指向 SilentAPI 並傳送您的 API 金鑰。
端點
| 方法 | 路徑 | 描述 |
|---|---|---|
| POST | /v1/messages | 建立訊息(Anthropic Messages API) |
基本請求
curl https://api.silentapi.com/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-5.5",
"max_tokens": 1024,
"messages": [{"role":"user","content":"What is zero data retention (ZDR)?"}]
}'
Response:
{
"id": "msg_1719584000",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-5.5",
"content": [{
"type": "text",
"text": "Zero data retention means no prompts or outputs are kept."
}],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 7,
"output_tokens": 89
},
"silentapi": {
"cost": 0.000084,
"fallback": null
}
}
使用 Anthropic SDK
Python
import anthropic
client = anthropic.Anthropic(
api_key="YOUR_API_KEY",
base_url="https://api.silentapi.com"
)
message = client.messages.create(
model="claude-sonnet-5.5",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain quantum computing"}
]
)
print(message.content[0].text)
TypeScript
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.silentapi.com"
});
const message = await client.messages.create({
model: "claude-sonnet-5.5",
max_tokens: 1024,
messages: [{ role: "user", content: "Explain quantum computing" }]
});
const responseText = message.content[0].text;
支援的參數
| 參數 | 類型 | 描述 |
|---|---|---|
| model | string | 支援的文字或多模態模型 ID |
| messages | array | 多輪對話訊息 |
| system | string | 系統指示 |
| max_tokens | integer | 最大輸出 詞元(Anthropic API 要求) |
| temperature | number | 取樣溫度 |
| top_p | number | 核心取樣參數 |
| stop_sequences | array | 停止生成的序列 |
| tools | array | 包含名稱與 input_schema 的函式定義 |
| tool_choice | object | 選擇 auto、any、none 或具名工具 |
| thinking | object | Thinking 設定,請參閱 推理。 |
| stream | boolean | 啟用 SSE 串流(參閱 串流) |
推理
傳送帶有 type: "enabled" 的 thinking,SilentAPI 會將 budget_tokens 轉換為推理等級:
| budget_tokens | 推理等級 |
|---|---|
| 無 | medium |
| 1 至 2,048 | low |
| 2,049 至 8,192 | medium |
| 8,193 至 32,768 | high |
| 32,769 至 65,536 | xhigh |
| 超過 65,536 | max |
各模型支援各自的等級組合,SilentAPI 會將不支援的等級轉換為最接近的支援等級,規則請參閱 推理。
參數處理
| 請求 | 處理方式 |
|---|---|
thinking.type: "disabled" | 等級 none,可在支援的模型上關閉推理,其他模型使用其最低等級。 |
thinking.type: "adaptive" without budget_tokens | 等級 medium |
output_config.effort | 用作推理等級:low、medium、high、xhigh 或 max。它優先於 budget_tokens,而 thinking.type: "disabled" 優先於它。 |
任何模型上的 budget_tokens | 先按上表轉換為等級,再轉為模型支援的最接近等級 |
高於 2 的 temperature | 限制為 2,請參閱 參數處理。 |
curl https://api.silentapi.com/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_API_KEY" \
-d '{
"model": "claude-sonnet-5.5",
"max_tokens": 4096,
"thinking": {"type": "enabled", "budget_tokens": 4096},
"messages": [{"role":"user","content":"Compare two sorting algorithms."}]
}'
跨風格使用
將任何支援的文字或多模態模型與 Anthropic Messages API 搭配使用,回應採用 Anthropic Messages 格式。
串流
設定 stream: true 即可接收 Anthropic Messages API 格式的伺服器傳送事件(SSE),事件順序請參閱 串流。