

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

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

このセクションでは、Text Completions API で Anthropic Claude モデルを使用した推論パラメータとコード例について説明します。

**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) で既存の Text Completions プロンプトを使用する場合は、[「Text Completions から移行](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>

リクエストボディは、[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) へのリクエストの `body` フィールドに渡されます。

詳細については、「Anthropic Claude ドキュメント」の [https://docs.anthropic.com/claude/reference/complete\$1post](https://docs.anthropic.com/claude/reference/complete_post) を参照してください。

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

Anthropic Claude には、Text Completion 推論呼び出しの次の推論パラメータがあります。

```
{
    "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** – (必須) 停止する前に生成するトークンの最大数。最適なパフォーマンスを得るには、トークンを 4,000 個に制限することをお勧めします。

  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/ja_jp/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/ja_jp/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/ja_jp/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html)
+  **top\$1k** – (オプション) 後続の各トークンの top K オプションのサンプルのみ。

  `top_k` を使用して、ロングテールの低確率レスポンスを削除します。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html)

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

この Anthropic Claude モデルは、Text Completion 推論呼び出しに対して次のフィールドを返します。

```
{
    "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'))
```

次の例は、「*write an essay for living on mars in 1000 words (火星での生活に関する小論文を 1,000 語で書いてください)*」というプロンプトと、Anthropic Claude V2 モデルを使用して Python でストリーミングテキストを生成する方法を示しています。

```
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()))
```