# Claude Sonnet 5 API Anthropic が提供する Claude Sonnet 5 LLM モデルの完全な API リファレンスです。 ## モデルバリエーション この API は、Claude Sonnet 5 の 2 つのモデルバリエーションをサポートしています。 | モデル | ID | 説明 | |-------|----|-------------| | **Claude Sonnet 5 Text to Text** | `claude-sonnet-5-text-to-text` | 高速なテキスト推論と文章作成 | | **Claude Sonnet 5 File Analysis** | `claude-sonnet-5-file-analysis` | Claude Sonnet によるファイルと画像の分析 | ## クイック比較 | 機能 | Text to Text | File Analysis | |---------|--------------|---------------| | テキスト入力 | 必須 ✅ | 任意 | | 画像入力 | 非対応 ❌ | 任意 | | ファイル入力 | 非対応 ❌ | 任意 | | ストリーミング応答 | SSE | SSE | | `max_tokens` | 任意 | 任意 | | `top_p` | 任意 | 任意 | | `top_k` | 任意 | 任意 | ## Claude Sonnet 5 Text to Text ### エンドポイント ```http POST /api/v1/chat/completions ``` ### リクエストパラメータ #### 必須 | パラメータ | 型 | 説明 | |-----------|------|-------------| | `model` | string | `"claude-sonnet-5-text-to-text"` を指定する必要があります | | `messages` | array | 順序付けされたメッセージ。メッセージ構造については LLM API を参照してください。 | | `stream` | boolean | SSE の場合は `true`、単一の JSON 応答の場合は `false`。 | #### メッセージ対応 | フィールド | テキスト | ファイル | 画像 | |-------|------|------|-------| | `messages` | 必須 ✅ | 非対応 ❌ | 非対応 ❌ | #### 任意 | パラメータ | 型 | 範囲 | 説明 | |-----------|------|-------|-------------| | `max_tokens` | integer | `1` - `64000` | 出力トークンの最大数。 | | `top_p` | number | `0.0` - `1.0` | 候補トークンの累積確率質量を制限します。 | | `top_k` | integer | `> 0` | 候補トークンの数を制限します。 | ### リクエスト例 ```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 ### エンドポイント ```http POST /api/v1/chat/completions ``` ### リクエストパラメータ #### 必須 | パラメータ | 型 | 説明 | |-----------|------|-------------| | `model` | string | `"claude-sonnet-5-file-analysis"` を指定する必要があります | | `messages` | array | 順序付けされたメッセージ。LLM API と Message Attachments を参照してください。 | | `stream` | boolean | SSE の場合は `true`、単一の JSON 応答の場合は `false`。 | #### メッセージ対応 | フィールド | テキスト | ファイル | 画像 | |-------|------|------|-------| | `messages` | 任意 | 任意 | 任意 | #### 任意 | パラメータ | 型 | 範囲 | 説明 | |-----------|------|-------|-------------| | `max_tokens` | integer | `1` - `64000` | 出力トークンの最大数。 | | `top_p` | number | `0.0` - `1.0` | 候補トークンの累積確率質量を制限します。 | | `top_k` | integer | `> 0` | 候補トークンの数を制限します。 | ### リクエスト例 ```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 }) }); ``` ## 応答形式 Claude Sonnet 5 LLM モデルは OpenAI 互換の completion 応答を返します。`stream: true` の場合は Server-Sent Events、`stream: false` の場合は単一の JSON オブジェクトになります。 ### 成功時の応答 ```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] ``` ### エラー応答 ```text event: error data: {"error":{"message":"Authorization is empty","type":"invalid_request_error","code":"1001","param":null}} ``` ## ベストプラクティス 1. **構造化メッセージを使用する:** コンテキストを 1 つのプロンプトにまとめず、`messages[]` を通して会話履歴を送信します。 2. **適切なモデルバリエーションを選ぶ:** テキストのみの推論と文章作成には Text to Text、ファイルや画像が必要な場合は File Analysis を使用します。 3. **Claude のパラメータを慎重に調整する:** `top_p` と `top_k` がサポートされており、出力の多様性を変更できます。 4. **SSE イベントを処理する:** ストリーミング表示では `choices[0].delta.content` を追加し、`data: [DONE]` を正常完了として扱います。 5. **対応している入力のみを送信する:** 現在のクライアントでは、テキストモデルは対応していないファイルと画像のパートを無視します。