Claude Sonnet 5.5 API
Tài liệu tham khảo API đầy đủ cho các mô hình LLM Claude Sonnet 5.5 của Anthropic.
Các biến thể mô hình
API này hỗ trợ hai biến thể mô hình Claude Sonnet 5.5:
So sánh nhanh
Claude Sonnet 5.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
Ví dụ yêu cầ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.5-text-to-text',
messages: [
{
role: 'user',
content: 'Write a four-line modern poem about a programmer drinking coffee.'
}
],
stream: true,
max_tokens: 500,
})
});
Claude Sonnet 5.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
Ví dụ yêu cầ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.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,
})
});
Định dạng phản hồi
Các mô hình LLM Claude Sonnet 5.5 trả về phản hồi hoàn thành tương thích với OpenAI. Với stream: true, phản hồi là các sự kiện do máy chủ gửi (SSE); 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.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.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.5-file-analysis","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"claude-sonnet-5.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 ngữ cảnh thành một câu lệnh duy nhất.
- Chọn biến thể mô hình phù hợp: Sử dụng Text to Text để suy luận và viết chỉ với văn bản, và File Analysis khi cần tệp hoặc hình ảnh.
- Đặt
max_tokens theo nhu cầu: Sử dụng tham số này để kiểm soát độ dài đầu ra tối đa.
- Xử lý sự kiện SSE: Nối thêm nội dung
choices[0].delta.content để hiển thị dạng luồng và xem data: [DONE] là dấu hiệu hoàn thành thành công.
- Chỉ gửi đầu vào được hỗ trợ: Trong ứng dụng khách hiện tại, các mô hình văn bản bỏ qua những phần tệp và hình ảnh không được hỗ trợ.