# API Claude Sonnet 5 Referensi API lengkap untuk model LLM Claude Sonnet 5 yang didukung Anthropic. ## Varian Model API ini mendukung dua varian model Claude Sonnet 5: | Model | ID | Deskripsi | |-------|----|-------------| | **Claude Sonnet 5 Text to Text** | `claude-sonnet-5-text-to-text` | Penalaran dan penulisan teks yang cepat | | **Claude Sonnet 5 File Analysis** | `claude-sonnet-5-file-analysis` | Analisis file dan gambar dengan Claude Sonnet | ## Perbandingan Singkat | Fitur | Text to Text | File Analysis | |---------|--------------|---------------| | Input teks | Wajib ✅ | Opsional | | Input gambar | Tidak didukung ❌ | Opsional | | Input file | Tidak didukung ❌ | Opsional | | Respons streaming | SSE | SSE | | `max_tokens` | Opsional | Opsional | | `top_p` | Opsional | Opsional | | `top_k` | Opsional | Opsional | ## Claude Sonnet 5 Text to Text ### Endpoint ```http POST /api/v1/chat/completions ``` ### Parameter Permintaan #### Wajib | Parameter | Tipe | Deskripsi | |-----------|------|-------------| | `model` | string | Harus berupa `"claude-sonnet-5-text-to-text"` | | `messages` | array | Pesan berurutan. Lihat API LLM untuk struktur pesan. | | `stream` | boolean | `true` untuk SSE, `false` untuk satu respons JSON. | #### Dukungan Pesan | Bidang | Teks | File | Gambar | |-------|------|------|-------| | `messages` | Wajib ✅ | Tidak didukung ❌ | Tidak didukung ❌ | #### Opsional | Parameter | Tipe | Rentang | Deskripsi | |-----------|------|-------|-------------| | `max_tokens` | integer | `1` - `64000` | Jumlah maksimum token keluaran. | | `top_p` | number | `0.0` - `1.0` | Membatasi massa probabilitas kumulatif untuk token kandidat. | | `top_k` | integer | `> 0` | Membatasi jumlah token kandidat. | ### Contoh Permintaan ```javascript 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 ### Endpoint ```http POST /api/v1/chat/completions ``` ### Parameter Permintaan #### Wajib | Parameter | Tipe | Deskripsi | |-----------|------|-------------| | `model` | string | Harus berupa `"claude-sonnet-5-file-analysis"` | | `messages` | array | Pesan berurutan. Lihat API LLM dan Lampiran Pesan. | | `stream` | boolean | `true` untuk SSE, `false` untuk satu respons JSON. | #### Dukungan Pesan | Bidang | Teks | File | Gambar | |-------|------|------|-------| | `messages` | Opsional | Opsional | Opsional | #### Opsional | Parameter | Tipe | Rentang | Deskripsi | |-----------|------|-------|-------------| | `max_tokens` | integer | `1` - `64000` | Jumlah maksimum token keluaran. | | `top_p` | number | `0.0` - `1.0` | Membatasi massa probabilitas kumulatif untuk token kandidat. | | `top_k` | integer | `> 0` | Membatasi jumlah token kandidat. | ### Contoh Permintaan ```javascript 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 }) }); ``` ## Format Respons Model LLM Claude Sonnet 5 mengembalikan respons completion yang kompatibel dengan OpenAI. Dengan `stream: true`, respons berupa Server-Sent Events; dengan `stream: false`, respons berupa satu objek JSON. ### Respons Berhasil ```text 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] ``` ### Respons Error ```text event: error data: {"error":{"message":"Authorization is empty","type":"invalid_request_error","code":"1001","param":null}} ``` ## Praktik Terbaik 1. **Gunakan pesan terstruktur:** Kirim riwayat percakapan melalui `messages[]`, bukan meratakan konteks menjadi satu prompt. 2. **Pilih varian model yang tepat:** Gunakan Text to Text untuk penalaran dan penulisan khusus teks, serta File Analysis saat membutuhkan file atau gambar. 3. **Sesuaikan parameter Claude dengan cermat:** `top_p` dan `top_k` didukung dan dapat mengubah keragaman keluaran. 4. **Konsumsi event SSE:** Tambahkan `choices[0].delta.content` untuk tampilan streaming dan perlakukan `data: [DONE]` sebagai penyelesaian yang berhasil. 5. **Kirim hanya input yang didukung:** Model teks mengabaikan bagian file dan gambar yang tidak didukung dalam klien saat ini.