Claude Sonnet 5 API
Tài liệu tham khảo API đầy đủ cho các mô hình LLM Claude Sonnet 5 do Anthropic cung cấp.
Các biến thể mô hình
API này hỗ trợ hai biến thể mô hình Claude Sonnet 5:
So sánh nhanh
Claude Sonnet 5 Text to Text
Điểm cuối
POST /api/v1/chat/completions
Tham số yêu cầu
Bắt buộc
Hỗ trợ tin nhắn
Tùy chọn
Yêu cầu mẫu
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 File Analysis
Điểm cuối
POST /api/v1/chat/completions
Tham số yêu cầu
Bắt buộc
Hỗ trợ tin nhắn
Tùy chọn
Yêu cầu mẫu
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
})
});
Định dạng phản hồi
Các mô hình LLM Claude Sonnet 5 trả về phản hồi completion tương thích với OpenAI. Với stream: true, phản hồi là Server-Sent Events; với stream: false, phản hồi là một đối tượng JSON duy nhất.
Phản hồi thành công
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]
Phản hồi lỗi
event: error
data: {"error":{"message":"Authorization is empty","type":"invalid_request_error","code":"1001","param":null}}
Các phương pháp hay nhất
- Sử dụng tin nhắn có cấu trúc: Gửi lịch sử hội thoại qua
messages[] thay vì gộp toàn bộ ngữ cảnh vào một prompt.
- Chọn đúng biến thể mô hình: Sử dụng Text to Text để lập luận và viết chỉ với văn bản, đồng thời sử dụng File Analysis khi cần tệp hoặc hình ảnh.
- Điều chỉnh tham số Claude cẩn thận:
top_p và top_k được hỗ trợ và có thể thay đổi độ đa dạng của đầu ra.
- Xử lý sự kiện SSE: Nối thêm
choices[0].delta.content để hiển thị theo luồng và coi data: [DONE] là hoàn tất thành công.
- Chỉ gửi đầu vào được hỗ trợ: Các mô hình văn bản bỏ qua phần tệp và hình ảnh không được hỗ trợ trong ứng dụng khách hiện tại.