

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Anthropic Claude Text Completions API
<a name="model-parameters-anthropic-claude-text-completion"></a>

本部分介绍结合使用 Anthropic Claude 模型和 Text Completions API 所需的推理参数和代码示例。

**Topics**
+ [Anthropic Claude Text Completions API 概述](#model-parameters-anthropic-claude-text-completion-overview)
+ [支持的模型](#claude-messages-supported-models)
+ [请求和响应](#model-parameters-anthropic-claude-text-completion-request-response)
+ [代码示例](#api-inference-examples-claude-text-completion)

## Anthropic Claude Text Completions API 概述
<a name="model-parameters-anthropic-claude-text-completion-overview"></a>

使用 Text Completions API 根据用户提供的提示生成单回合文本。例如，您可以使用 Text Completions API 为博客文章生成文本或汇总用户输入的文本。

有关为 Anthropic Claude 模型创建提示的信息，请参阅[提示设计简介](https://docs.anthropic.com/claude/docs/introduction-to-prompt-design)。如果要在 [Anthropic Claude Messages API](model-parameters-anthropic-claude-messages.md) 中使用现有的文本补全提示，请参阅[从文本补全迁移](https://docs.anthropic.com/claude/reference/migrating-from-text-completions-to-messages)。

## 支持的模型
<a name="claude-messages-supported-models"></a>

您可以将 Text Completions API 与以下 Anthropic Claude 模型配合使用。
+ Anthropic Claude Instant v1.2
+ Anthropic Claude v2
+ Anthropic Claude v2.1 

## 请求和响应
<a name="model-parameters-anthropic-claude-text-completion-request-response"></a>

请求正文在请求的 `body` 字段中传递给 [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) 或 [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html)。

有关更多信息，请参阅 Anthropic Claude 文档中的 [https://docs.anthropic.com/claude/reference/complete\$1post](https://docs.anthropic.com/claude/reference/complete_post)。

------
#### [ Request ]

Anthropic Claude 具有以下推理参数，用于进行文本补全推理调用。

```
{
    "prompt": "\n\nHuman:<prompt>\n\nAssistant:",
    "temperature": float,
    "top_p": float,
    "top_k": int,
    "max_tokens_to_sample": int,
    "stop_sequences": [string]
}
```

以下是必要参数。
+  **prompt** –（必要）您希望 Claude 补全的提示。为了正确生成响应，您需要使用交替的 `\n\nHuman:` 和 `\n\nAssistant:` 对话回合来格式化提示。例如：

  ```
  "\n\nHuman: {userQuestion}\n\nAssistant:"
  ```

  有关更多信息，请参阅 Anthropic Claude 文档中的[提示验证](https://docs.anthropic.com/claude/reference/prompt-validation)。
+  **max\$1tokens\$1to\$1sample** –（必要）停止前要生成的最大词元数量。为了获得最佳性能，我们建议限制为 4000 个令牌。

  请注意，Anthropic Claude 模型可能会在达到值 `max_tokens_to_sample` 之前停止生成词元。不同的 Anthropic Claude 模型对此参数有不同的最大值要求。有关更多信息，请参阅 Anthropic Claude 文档中的[模型比较](https://docs.anthropic.com/claude/docs/models-overview#model-comparison)。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html)

以下是可选参数。
+  **stop\$1sequences** –（可选）将导致模型停止生成的序列。

  Anthropic Claude 模型将停止 `"\n\nHuman:"`，并且将来可能会包括其他内置的停止序列。使用 `stop_sequences` 推理参数来包括其他字符串，这些字符串将向模型发出停止生成文本的信号。
+  **temperature** –（可选）注入响应的随机性数量。对于分析/多项选择，使用接近 0 的值，对于创造性和生成性任务，使用接近 1 的值。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html)
+  **top\$1p** –（可选）使用 nucleus 采样。

  在 nucleus 采样中，Anthropic Claude 按概率递减顺序计算每个后续词元的所有选项的累积分布，并在达到 `top_p` 指定的特定概率后将其切断。您应该更改 `temperature` 或 `top_p`，但不能同时更改这两个。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html)
+  **top\$1k** –（可选）仅从每个后续词元的前 K 个选项中取样。

  使用 `top_k` 移除长尾低概率响应。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html)

------
#### [ Response ]

Anthropic Claude 模型将返回以下字段，用于进行文本补全推理调用。

```
{
    "completion": string,
    "stop_reason": string,
    "stop": string
}
```
+ **completion** – 生成的补全，不包括停止序列。
+ **stop\$1reason** – 模型停止生成响应的原因。
  + **“stop\$1sequence”**– 模型达到了停止序列，要么由您使用 `stop_sequences` 推理参数提供，要么是模型中内置的停止序列。
  + **“max\$1tokens”**– 模型超出了 `max_tokens_to_sample` 或模型的最大词元数量。
+ **stop** – 如果指定 `stop_sequences` 推理参数，则 `stop` 包含指示模型停止生成文本的停止序列。例如，以下响应中的 `holes`。

  ```
  {
      "completion": " Here is a simple explanation of black ",
      "stop_reason": "stop_sequence",
      "stop": "holes"
  }
  ```

  如果您未指定 `stop_sequences`，则 `stop` 的值为空。

------

## 代码示例
<a name="api-inference-examples-claude-text-completion"></a>

这些示例展示了如何使用按需吞吐量调用 *Anthropic Claude V2* 模型。要使用 Anthropic Claude 版本 2.1，请将 `modelId` 的值更改为 `anthropic.claude-v2:1`。

```
import boto3
import json
brt = boto3.client(service_name='bedrock-runtime')

body = json.dumps({
    "prompt": "\n\nHuman: explain black holes to 8th graders\n\nAssistant:",
    "max_tokens_to_sample": 300,
    "temperature": 0.1,
    "top_p": 0.9,
})

modelId = 'anthropic.claude-v2'
accept = 'application/json'
contentType = 'application/json'

response = brt.invoke_model(body=body, modelId=modelId, accept=accept, contentType=contentType)

response_body = json.loads(response.get('body').read())

# text
print(response_body.get('completion'))
```

以下示例展示如何在 Python 中使用提示 *write an essay for living on mars in 1000 words* 和 Anthropic Claude V2 模型生成流式传输文本：

```
import boto3
import json

brt = boto3.client(service_name='bedrock-runtime')

body = json.dumps({
    'prompt': '\n\nHuman: write an essay for living on mars in 1000 words\n\nAssistant:',
    'max_tokens_to_sample': 4000
})
                   
response = brt.invoke_model_with_response_stream(
    modelId='anthropic.claude-v2', 
    body=body
)
    
stream = response.get('body')
if stream:
    for event in stream:
        chunk = event.get('chunk')
        if chunk:
            print(json.loads(chunk.get('bytes').decode()))
```