Claude Sonnet 5 API
由 Anthropic 提供支援的 Claude Sonnet 5 LLM 模型完整 API 參考。
模型變體
此 API 支援兩種 Claude Sonnet 5 模型變體:
快速比較
Claude Sonnet 5 文字轉文字
端點
POST /api/v1/chat/completions
請求參數
必填參數
訊息支援
選填參數
請求範例
const response = await fetch('https://api.flaq.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Accept': 'text/event-stream',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'claude-sonnet-5-text-to-text',
messages: [
{
role: 'user',
content: 'Write a four-line modern poem about a programmer drinking coffee.'
}
],
stream: true,
max_tokens: 500,
top_p: 0.5,
top_k: 50
})
});
Claude Sonnet 5 檔案分析
端點
POST /api/v1/chat/completions
請求參數
必填參數
訊息支援
選填參數
請求範例
const response = await fetch('https://api.flaq.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Accept': 'text/event-stream',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'claude-sonnet-5-file-analysis',
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Summarize the key points of this PDF.' },
{
type: 'file',
file: {
filename: 'gptproto.pdf',
file_data: 'https://tos.gptproto.com/resource/gptproto.pdf'
}
}
]
}
],
stream: true,
max_tokens: 1000,
top_p: 0.5,
top_k: 50
})
});
回應格式
Claude Sonnet 5 LLM 模型會傳回與 OpenAI 相容的補全回應。使用 stream: true 時,回應為伺服器傳送事件;使用 stream: false 時,回應為單一 JSON 物件。
成功回應
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"claude-sonnet-5-file-analysis","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"claude-sonnet-5-file-analysis","choices":[{"index":0,"delta":{"content":"Here is a concise summary"},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"claude-sonnet-5-file-analysis","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"claude-sonnet-5-file-analysis","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":8,"total_tokens":20}}
data: [DONE]
錯誤回應
event: error
data: {"error":{"message":"Authorization is empty","type":"invalid_request_error","code":"1001","param":null}}
最佳實務
- 使用結構化訊息: 透過
messages[] 傳送對話歷程,而不是將上下文扁平化為一個提示詞。
- 選擇正確的模型變體: 純文字推理與寫作使用 Text to Text,需要檔案或圖像時使用 File Analysis。
- 謹慎調整 Claude 參數: 支援
top_p 和 top_k,它們會改變輸出多樣性。
- 處理 SSE 事件: 追加
choices[0].delta.content 以進行串流顯示,並將 data: [DONE] 視為成功完成。
- 僅傳送支援的輸入: 目前用戶端中的文字模型會忽略不支援的檔案和圖像部分。