レガシーAPI

テキスト補完APIはレガシーAPIです。今後のモデルと機能にはメッセージAPIの使用が必要となるため、できるだけ早く移行することをお勧めします。

テキスト補完を作成する際、"stream": trueを設定することで、サーバー送信イベント(SSE)を使用して応答を段階的にストリーミングできます。クライアントライブラリを使用している場合、これらのイベントの解析は自動的に処理されます。ただし、直接APIを統合する場合は、これらのイベントを自分で処理する必要があります。

Shell
curl https://api.anthropic.com/v1/complete \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --data '
{
  "model": "claude-2",
  "prompt": "\n\nHuman: Hello, world!\n\nAssistant:",
  "max_tokens_to_sample": 256,
  "stream": true
}
'
Response
event: completion
data: {"type": "completion", "completion": " Hello", "stop_reason": null, "model": "claude-2.0"}

event: completion
data: {"type": "completion", "completion": "!", "stop_reason": null, "model": "claude-2.0"}

event: ping
data: {"type": "ping"}

event: completion
data: {"type": "completion", "completion": " My", "stop_reason": null, "model": "claude-2.0"}

event: completion
data: {"type": "completion", "completion": " name", "stop_reason": null, "model": "claude-2.0"}

event: completion
data: {"type": "completion", "completion": " is", "stop_reason": null, "model": "claude-2.0"}

event: completion
data: {"type": "completion", "completion": " Claude", "stop_reason": null, "model": "claude-2.0"}

event: completion
data: {"type": "completion", "completion": ".", "stop_reason": null, "model": "claude-2.0"}

event: completion
data: {"type": "completion", "completion": "", "stop_reason": "stop_sequence", "model": "claude-2.0"}

イベント

各イベントには名前付きイベントタイプと関連するJSONデータが含まれています。

イベントタイプ: completionpingerror

エラーイベントタイプ

イベントストリームでエラーが送信されることがあります。例えば、使用量が多い期間中にoverloaded_errorを受け取ることがあります。これは通常、非ストリーミングのコンテキストではHTTP 529に相当します:

Example error
event: completion
data: {"completion": " Hello", "stop_reason": null, "model": "claude-2.0"}

event: error
data: {"error": {"type": "overloaded_error", "message": "Overloaded"}}

古いAPIバージョン

2023-06-01より前のAPIバージョンを使用している場合、レスポンスの形式は異なります。詳細についてはバージョニングをご覧ください。