Messages API
透過 Anthropic 原生協議建立 Claude 對話。OfoxAI 完全相容 Anthropic Messages API,你可以直接使用官方 SDK。
端點
POST https://api.ofox.ai/anthropic/v1/messages認證
Anthropic 協議使用 x-api-key Header:
x-api-key: <你的 OFOXAI_API_KEY>
anthropic-version: 2023-06-01請求參數
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
model | string | ✅ | 模型識別碼,如 anthropic/claude-sonnet-4.5 |
max_tokens | number | ✅ | 最大生成 token 數 |
messages | array | ✅ | 訊息陣列 |
system | string | — | 系統提示詞 |
temperature | number | — | 取樣溫度 0-1 |
top_p | number | — | 核取樣參數 |
top_k | number | — | Top-K 取樣 |
stream | boolean | — | 是否啟用串流回應 |
tools | array | — | 工具定義 |
tool_choice | object | — | 工具選擇策略 |
Message 格式
interface Message {
role: 'user' | 'assistant'
content: string | ContentBlock[]
}
type ContentBlock =
| { type: 'text'; text: string }
| { type: 'image'; source: { type: 'base64'; media_type: string; data: string } }
| { type: 'tool_use'; id: string; name: string; input: object }
| { type: 'tool_result'; tool_use_id: string; content: string }請求範例
cURL
Terminal
curl https://api.ofox.ai/anthropic/v1/messages \
-H "x-api-key: $OFOX_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-4.5",
"max_tokens": 1024,
"system": "你是一個專業的程式設計助手。",
"messages": [
{"role": "user", "content": "用 Python 寫一個快速排序"}
]
}'回應格式
{
"id": "msg_abc123",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "以下是 Python 快速排序的實作..."
}
],
"model": "anthropic/claude-sonnet-4.5",
"stop_reason": "end_turn",
"usage": {
"input_tokens": 25,
"output_tokens": 200
}
}串流回應
Python
anthropic_stream.py
with client.messages.stream(
model="anthropic/claude-sonnet-4.5",
max_tokens=1024,
messages=[{"role": "user", "content": "講一個故事"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)支援的模型
| 模型 | 說明 |
|---|---|
anthropic/claude-opus-4.6 | Claude Opus 4 — 最強能力 |
anthropic/claude-sonnet-4.5 | Claude Sonnet 4 — 均衡效能 |
anthropic/claude-haiku-4.5 | Claude Haiku 4.5 — 快速回應 |
OfoxAI 的 Anthropic 協議支援全部原生功能,包括 Vision、Tool Use、Prompt Caching、Extended Thinking 等。
Last updated on