旧版 API

文本补全 API 是一个旧版 API。未来的模型和功能将需要使用 消息 API,我们建议尽快迁移

在创建文本补全时,你可以设置 "stream": true 来使用 服务器发送事件 (SSE) 以增量方式流式传输响应。如果你使用我们的客户端库,解析这些事件将自动为你处理。但是,如果你正在构建直接的 API 集成,你需要自己处理这些事件。

示例

Request
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: 你好,世界!\n\nAssistant:",
  "max_tokens_to_sample": 256,
  "stream": true
}
'
Response
event: completion
data: {"type": "completion", "completion": " 你好", "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": " 我的", "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": 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": " 你好", "stop_reason": null, "model": "claude-2.0"}

event: error
data: {"error": {"type": "overloaded_error", "message": "过载"}}  

旧版 API

如果你使用的是 2023-06-01 之前的 API 版本,响应格式会有所不同。有关详细信息,请参阅版本控制