

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

# API を使用してプロンプトを送信し、レスポンスを生成する
<a name="inference-api"></a>

Amazon Bedrock には、モデル推論を実行するための followingAPI オペレーションが用意されています。
+ [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) を使用します。
+ [Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) – プロンプトを送信し、すべてのモデルにわたって統合された構造でレスポンスを生成します。モデル固有のリクエストフィールドを `additionalModelRequestFields` フィールドで指定できます。コンテキストがわかるように、システムプロンプトと以前の会話を含めることもできます。ストリーミングレスポンスを生成するには、[ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html) を使用します。
+ [StartAsyncInvoke](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_StartAsyncInvoke.html) – プロンプトを送信し、後で取得できるレスポンスを非同期的に生成します。動画の生成に使用されます。
+ [InvokeModelWithBidirectionalStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithBidirectionalStream.html) – 
+ OpenAI Chat completions API – Amazon Bedrock でサポートされているモデルで [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat/create) を使用してレスポンスを生成します。

**注記**  
制限は、`InvokeModel`、、`InvokeModelWithResponseStream``Converse`、および のオペレーションに適用されます`ConverseStream`。詳細については、[「API の制限](inference-api-restrictions.md)」を参照してください。

モデル推論では、次のパラメータを決定する必要があります。
+ モデル ID – モデルまたは推論プロファイルの ID または Amazon リソースネーム (ARN)。推論の `modelId` フィールドで使用します。次の表は、各種リソースの ID の調べ方をまとめたものです。  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/bedrock/latest/userguide/inference-api.html)
+ リクエスト本文 – モデルの推論パラメータおよびその他の設定が含まれます。各ベースモデルには、独自の推論パラメータがあります。カスタムモデルまたはプロビジョニングモデルの推論パラメータは、作成元のベースモデルによって異なります。詳細については、「[Inference request parameters and response fields for foundation models](model-parameters.md)」を参照してください。

トピックを選択して、モデル呼び出し API の使い方を確認してください。

**Topics**
+ [InvokeModel で 1 つのプロンプトを送信する](inference-invoke.md)
+ [OpenAI Chat Completions API を使用してモデルを呼び出す](inference-chat-completions.md)
+ [Converse API オペレーションを使用して会話を実行する](conversation-inference.md)
+ [API の制限](inference-api-restrictions.md)

# InvokeModel で 1 つのプロンプトを送信する
<a name="inference-invoke"></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) API オペレーションを使用してモデルを指定し、単一のプロンプトで推論を実行します。Amazon Bedrock モデルは、テキスト、画像、または動画入力を受け入れるかどうか、およびテキスト、イメージ、または埋め込みの出力を生成できるかどうかによって異なります。一部のモデルはストリームでレスポンスを返すことができます。入力、出力、ストリーミングのサポートについてモデルのサポートを確認するには、次のいずれかを実行します。
+ [Amazon Bedrock でサポートされている基盤モデル](models-supported.md) でモデルの**入力モダリティ**、**出力モダリティ**、または**ストリーミングでサポートされている**列の値を確認します。
+ モデル ID を使用して [GetFoundationModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_GetFoundationModel.html) リクエストを送信し、`inputModalities`、`outputModalities`、`responseStreamingSupported` フィールドの値を確認します。

[Amazon Bedrock ランタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt)で [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) リクエストを送信して、プロンプトでモデル推論を実行します。

**注記**  
制限は、`InvokeModel`、、`InvokeModelWithResponseStream`、`Converse`および のオペレーションに適用されます`ConverseStream`。詳細については、[「API の制限](inference-api-restrictions.md)」を参照してください。

以下のフィールドが必要です。


****  

| フィールド | ユースケース | 
| --- | --- | 
| modelId | 使用するプロンプト管理からモデル、推論プロファイル、またはプロンプトを指定します。この値の調べ方については、「[API を使用してプロンプトを送信し、レスポンスを生成する](inference-api.md)」を参照してください。 | 
| body | モデルの推論パラメータを指定します。さまざまなモデルの推論パラメータを確認するには、「[Inference request parameters and response fields for foundation models](model-parameters.md)」を参照してください。modelId フィールドでプロンプト管理からプロンプトを指定する場合は、このフィールドを省略します (含めると無視されます)。 | 

次のフィールドはオプションです。


****  

| フィールド | ユースケース | 
| --- | --- | 
| accept | リクエスト本文のメディアタイプを指定します。詳細については、[Swagger ウェブサイト](https://swagger.io/specification/)の「Media Types」を参照してください。 | 
| contentType | レスポンス本文のメディアタイプを指定します。詳細については、[Swagger ウェブサイト](https://swagger.io/specification/)の「Media Types」を参照してください。 | 
| performanceConfigLatency | レイテンシーに合わせてモデルを最適化するかどうかを指定します。詳細については、「[レイテンシーのモデル推論を最適化する](latency-optimized-inference.md)」を参照してください。 | 
| guardrailIdentifier | プロンプトとレスポンスに適用するガードレールを指定します。詳細については、「[ガードレールをテストする](guardrails-test.md)」を参照してください。 | 
| guardrailVersion | プロンプトとレスポンスに適用するガードレールを指定します。詳細については、「[ガードレールをテストする](guardrails-test.md)」を参照してください。 | 
| trace | 指定したガードレールのトレースを返すかどうかを指定します。詳細については、「[ガードレールをテストする](guardrails-test.md)」を参照してください。 | 
| serviceTier | リクエストのサービス層を指定するには。詳細については、「[パフォーマンスとコストを最適化するためのサービス階層](service-tiers-inference.md)」を参照してください。 | 

## モデルコードの呼び出しの例
<a name="inference-example-invoke"></a>

このトピックでは、[InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) API で単一のプロンプトを使用して推論を実行するための基本的な例をいくつか紹介します。さまざまなモデルを使用したその他の例については、次のリソースを参照してください。
+ [AWS SDKsコード例](service_code_examples_bedrock-runtime.md) トピックで例を選択します。
+ 目的のモデルの推論パラメータリファレンスについては、「[Inference request parameters and response fields for foundation models](model-parameters.md)」を参照してください。

次の例では、これらの例を実行する AWS リージョン ときに、デフォルトで AWS CLI と SDK for Python (Boto3) に対して自動的に認証されるようにプログラムによるアクセスを設定していることを前提としています。プログラムによるアクセスの設定については、「[API の使用を開始する](getting-started-api.md)」を参照してください。

**注記**  
例を試す前に、次の点を確認してください。  
これらの例は、例で使用されているすべてのモデルをサポートする米国東部 (バージニア北部) (us-east-1) でテストする必要があります。
`body` パラメータは大きい場合があるため、一部の CLI 例では、コマンドラインで指定するのではなく、JSON ファイルを作成し、そのファイルを `--body` 引数に提供するように求められます。
画像と動画の例については、独自のイメージと動画を使用するように求められます。この例では、画像ファイルの名前が *image.png* で、動画ファイルの名前が *video.mp4* であることを前提としています。
画像や動画を base64 でエンコードされた文字列に変換するか、Amazon S3 の場所にアップロードする必要がある場合があります。この例では、プレースホルダーを実際の base64 でエンコードされた文字列または S3 の場所に置き換える必要があります。

セクションを展開して、いくつかの基本的なコード例を試してください。

### テキストプロンプトを使用してテキストを生成する
<a name="w2aac13c32c33c17c19c13b1"></a>

次の例では、Amazon Titan Text Premier モデルを使用してテキストプロンプトへのテキストレスポンスを生成します。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

ターミナルで次のコマンドを実行し、*invoke-model-output.txt* というファイルで生成されたレスポンスを見つけます。

```
aws bedrock-runtime invoke-model \
    --model-id amazon.titan-text-premier-v1:0 \
    --body '{
        "inputText": "Describe the purpose of a 'hello world' program in one line.",
        "textGenerationConfig": {
            "maxTokenCount": 512,
            "temperature": 0.5
        }
    }' \
    --cli-binary-format raw-in-base64-out \
    invoke-model-output.txt
```

------
#### [ Python ]

次の Python コード例を実行して、テキストレスポンスを生成します。

```
# Use the native inference API to send a text message to Amazon Titan Text.

import boto3
import json

from botocore.exceptions import ClientError

# Create a Bedrock Runtime client in the AWS Region of your choice.
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Set the model ID, e.g., Titan Text Premier.
model_id = "amazon.titan-text-premier-v1:0"

# Define the prompt for the model.
prompt = "Describe the purpose of a 'hello world' program in one line."

# Format the request payload using the model's native structure.
native_request = {
    "inputText": prompt,
    "textGenerationConfig": {
        "maxTokenCount": 512,
        "temperature": 0.5,
    },
}

# Convert the native request to JSON.
request = json.dumps(native_request)

try:
    # Invoke the model with the request.
    response = client.invoke_model(modelId=model_id, body=request)

except (ClientError, Exception) as e:
    print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
    exit(1)

# Decode the response body.
model_response = json.loads(response["body"].read())

# Extract and print the response text.
response_text = model_response["results"][0]["outputText"]
print(response_text)
```

------

### サービス層を使用してテキストプロンプトでテキストを生成する
<a name="w2aac13c32c33c17c19c13b3"></a>

次の例では、OpenAIGPT モデルとサービス層を使用してテキストプロンプトへのテキストレスポンスを生成し、リクエストに優先順位を付けます。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

ターミナルで次のコマンドを実行し、レスポンスでサービス層を検証します。

```
aws bedrock-runtime invoke-model \
    --model-id openai.gpt-oss-120b-1:0 \
    --body '{
        "messages": [
            {
                "role": "user",
                "content": "Describe the purpose of a '\''hello world'\'' program in one line."
            }
        ],
        "max_tokens": 512,
        "temperature": 0.7
    }' \
    --content-type application/json \
    --accept application/json \
    --service-tier priority \
    --cli-binary-format raw-in-base64-out
```

------
#### [ Python ]

次の Python コード例を実行して、サービス層を含むテキストレスポンスを生成します。

```
import boto3
import json

# Create a Bedrock Runtime client
bedrock_runtime = boto3.client(
    service_name="bedrock-runtime",
    region_name="us-east-1"
)

# Define the model ID and request body
model_id = "openai.gpt-oss-120b-1:0"
body = json.dumps({
    "messages": [
        {
            "role": "user",
            "content": "Describe the purpose of a 'hello world' program in one line."
        }
    ],
    "max_tokens": 512,
    "temperature": 0.7
})

# Make the request with service tier
response = bedrock_runtime.invoke_model(
    modelId=model_id,
    body=body,
    contentType="application/json",
    accept="application/json",
    serviceTier="priority"
)

# Parse and print the response
response_body = json.loads(response["body"])
print(response_body)
```

------

### テキストプロンプトを使用して画像を生成します。
<a name="w2aac13c32c33c17c19c13b5"></a>

次のコード例では、Stable Diffusion XL 1.0 モデルでテキストプロンプトを使用して画像を生成します。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

ターミナルで次のコマンドを実行し、*invoke-model-output.txt* というファイルで生成されたレスポンスを見つけます。画像を表すバイトは、レスポンスの `base64` フィールドにあります。

```
aws bedrock-runtime invoke-model \
    --model-id stability.stable-diffusion-xl-v1 \
    --body '{
        "text_prompts": [{"text": "A stylized picture of a cute old steampunk robot."}],
        "style_preset": "photographic",
        "seed": 0,
        "cfg_scale": 10,
        "steps": 30
    }' \
    --cli-binary-format raw-in-base64-out \
    invoke-model-output.txt
```

------
#### [ Python ]

次の Python コード例を実行して画像を生成し、*output* というフォルダで *stability\$11.png* イメージファイルを見つけます。

```
# Use the native inference API to create an image with Amazon Titan Image Generator

import base64
import boto3
import json
import os
import random

# Create a Bedrock Runtime client in the AWS Region of your choice.
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Set the model ID, e.g., Titan Image Generator G1.
model_id = "amazon.titan-image-generator-v2:0"

# Define the image generation prompt for the model.
prompt = "A stylized picture of a cute old steampunk robot."

# Generate a random seed.
seed = random.randint(0, 2147483647)

# Format the request payload using the model's native structure.
native_request = {
    "taskType": "TEXT_IMAGE",
    "textToImageParams": {"text": prompt},
    "imageGenerationConfig": {
        "numberOfImages": 1,
        "quality": "standard",
        "cfgScale": 8.0,
        "height": 512,
        "width": 512,
        "seed": seed,
    },
}

# Convert the native request to JSON.
request = json.dumps(native_request)

# Invoke the model with the request.
response = client.invoke_model(modelId=model_id, body=request)

# Decode the response body.
model_response = json.loads(response["body"].read())

# Extract the image data.
base64_image_data = model_response["images"][0]

# Save the generated image to a local folder.
i, output_dir = 1, "output"
if not os.path.exists(output_dir):
    os.makedirs(output_dir)
while os.path.exists(os.path.join(output_dir, f"titan_{i}.png")):
    i += 1

image_data = base64.b64decode(base64_image_data)

image_path = os.path.join(output_dir, f"titan_{i}.png")
with open(image_path, "wb") as file:
    file.write(image_data)

print(f"The generated image has been saved to {image_path}")
```

------

### テキストから埋め込みを生成する
<a name="w2aac13c32c33c17c19c13b9"></a>

次の例では、Amazon Titan Text Embeddings V2 モデルを使用して、テキスト入力のバイナリ埋め込みを生成します。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

ターミナルで次のコマンドを実行し、*invoke-model-output.txt* というファイルで生成されたレスポンスを見つけます。結果として生じる埋め込みは `binary` フィールドにあります。

```
aws bedrock-runtime invoke-model \
    --model-id amazon.titan-embed-text-v2:0 \
    --body '{
        "inputText": "What are the different services that you offer?",
        "embeddingTypes": ["binary"]
    }' \
    --cli-binary-format raw-in-base64-out \
    invoke-model-output.txt
```

------
#### [ Python ]

次の Python コード例を実行して、指定されたテキストの埋め込みを生成します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate an embedding with the Amazon Titan Text Embeddings V2 Model
"""

import json
import logging
import boto3


from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_embedding(model_id, body):
    """
    Generate an embedding with the vector representation of a text input using Amazon Titan Text Embeddings G1 on demand.
    Args:
        model_id (str): The model ID to use.
        body (str) : The request body to use.
    Returns:
        response (JSON): The embedding created by the model and the number of input tokens.
    """

    logger.info("Generating an embedding with Amazon Titan Text Embeddings V2 model %s", model_id)

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

    accept = "application/json"
    content_type = "application/json"

    response = bedrock.invoke_model(
        body=body, modelId=model_id, accept=accept, contentType=content_type
    )

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

    return response_body


def main():
    """
    Entrypoint for Amazon Titan Embeddings V2 - Text example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "amazon.titan-embed-text-v2:0"
    input_text = "What are the different services that you offer?"


    # Create request body.
    body = json.dumps({
        "inputText": input_text,
        "embeddingTypes": ["binary"]
    })


    try:

        response = generate_embedding(model_id, body)

        print(f"Generated an embedding: {response['embeddingsByType']['binary']}") # returns binary embedding
        print(f"Input text: {input_text}")
        print(f"Input Token count:  {response['inputTextTokenCount']}")

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))

    else:
        print(f"Finished generating an embedding with Amazon Titan Text Embeddings V2 model {model_id}.")


if __name__ == "__main__":
    main()
```

------

### 画像から埋め込みを生成する
<a name="w2aac13c32c33c17c19c13c11"></a>

次の例では、Amazon Titan Multimodal Embeddings G1 モデルを使用して画像入力の埋め込みを生成します。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

次の操作を行って、ターミナルを開きます。

1. 現在のフォルダの *image.png* というタイトルの画像を base64 でエンコードされた文字列に変換し、次のコマンドを実行して *image.txt* というタイトルのファイルに書き込みます。

   ```
   base64 -i image.png -o image.txt
   ```

1. *image-input-embeddings-output.json* という JSON ファイルを作成し、次の JSON を貼り付けます。*\$1\$1image-base64\$1* を *image.txt* ファイルの内容に置き換えます (文字列の末尾に新しい行がないことを確認します)。

   ```
   {
       "inputImage": "${image-base64}",
       "embeddingConfig": {
           "outputEmbeddingLength": 256
       }
   }
   ```

1. 本文として *image-input-embeddings-output.json* ファイルを指定して、次のコマンドを実行します。

   ```
   aws bedrock-runtime invoke-model \
       --model-id amazon.titan-embed-image-v1 \
       --body file://image-input-embeddings-output.json \
       --cli-binary-format raw-in-base64-out \
       invoke-model-output.txt
   ```

1. *invoke-model-output.txt* ファイルで結果の埋め込みを見つけます。

------
#### [ Python ]

次の Python スクリプトで、*/path/to/image* を実際の画像へのパスに置き換えます。次に、スクリプトを実行して埋め込みを生成します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate embeddings from an image with the Amazon Titan Multimodal Embeddings G1 model (on demand).
"""

import base64
import json
import logging
import boto3

from botocore.exceptions import ClientError

class EmbedError(Exception):
    "Custom exception for errors returned by Amazon Titan Multimodal Embeddings G1"

    def __init__(self, message):
        self.message = message

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_embeddings(model_id, body):
    """
    Generate a vector of embeddings for an image input using Amazon Titan Multimodal Embeddings G1 on demand.
    Args:
        model_id (str): The model ID to use.
        body (str) : The request body to use.
    Returns:
        response (JSON): The embeddings that the model generated, token information, and the
        reason the model stopped generating embeddings.
    """

    logger.info("Generating embeddings with Amazon Titan Multimodal Embeddings G1 model %s", model_id)

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

    accept = "application/json"
    content_type = "application/json"

    response = bedrock.invoke_model(
        body=body, modelId=model_id, accept=accept, contentType=content_type
    )

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

    finish_reason = response_body.get("message")

    if finish_reason is not None:
        raise EmbedError(f"Embeddings generation error: {finish_reason}")

    return response_body


def main():
    """
    Entrypoint for Amazon Titan Multimodal Embeddings G1 example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    # Read image from file and encode it as base64 string.
    with open("/path/to/image", "rb") as image_file:
        input_image = base64.b64encode(image_file.read()).decode('utf8')

    model_id = 'amazon.titan-embed-image-v1'
    output_embedding_length = 256

    # Create request body.
    body = json.dumps({
        "inputImage": input_image,
        "embeddingConfig": {
            "outputEmbeddingLength": output_embedding_length
        }
    })


    try:

        response = generate_embeddings(model_id, body)

        print(f"Generated image embeddings of length {output_embedding_length}: {response['embedding']}")

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))
        
    except EmbedError as err:
        logger.error(err.message)
        print(err.message)

    else:
        print(f"Finished generating image embeddings with Amazon Titan Multimodal Embeddings G1 model {model_id}.")


if __name__ == "__main__":
    main()
```

------

### 付随するテキストプロンプトを使用して画像へのテキストレスポンスを生成する
<a name="w2aac13c32c33c17c19c13c13"></a>

任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

次の例では、画像と画像の内容を尋ねるテキストプロンプトに基づいて、Anthropic Claude 3 Haiku モデルを使用してレスポンスを生成します。次の操作を行って、ターミナルを開きます。

1. 現在のフォルダの *image.png* というタイトルの画像を base64 でエンコードされた文字列に変換し、次のコマンドを実行して *image.txt* というタイトルのファイルに書き込みます。

   ```
   base64 -i image.png -o image.txt
   ```

1. *image-text-input.json* という JSON ファイルを作成し、次の JSON を貼り付けます。*\$1\$1image-base64\$1* を *image.txt* ファイルの内容に置き換えます (文字列の末尾に新しい行がないことを確認します)。

   ```
   {
       "anthropic_version": "bedrock-2023-05-31",
       "max_tokens": 1000,
       "messages": [
           {               
               "role": "user",
               "content": [
                   {
                       "type": "image",
                       "source": {
                           "type": "base64",
                           "media_type": "image/png", 
                           "data": "${image-base64}"
                       }
                   },
                   {
                       "type": "text",
                       "text": "What's in this image?"
                   }
               ]
           }
       ]
   }
   ```

1. 次のコマンドを実行して、画像とそれに付随するテキストプロンプトに基づいて *invoke-model-output.txt* というファイルにテキスト出力を生成します。

   ```
   aws bedrock-runtime invoke-model \
       --model-id anthropic.claude-3-haiku-20240307-v1:0 \
       --body file://image-text-input.json \
       --cli-binary-format raw-in-base64-out \
       invoke-model-output.txt
   ```

1. 現在のフォルダの *invoke-model-output.txt* ファイルで出力を見つけます。

------
#### [ Python ]

次の Python スクリプトでは、スクリプトを実行する前に */path/to/image.png* を画像への実際のパスに置き換えます。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to run a multimodal prompt with Anthropic Claude (on demand) and InvokeModel.
"""

import json
import logging
import base64
import boto3

from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens):
    """
    Invokes a model with a multimodal prompt.
    Args:
        bedrock_runtime: The Amazon Bedrock boto3 client.
        model_id (str): The model ID to use.
        messages (JSON) : The messages to send to the model.
        max_tokens (int) : The maximum  number of tokens to generate.
    Returns:
        None.
    """



    body = json.dumps(
        {
            "anthropic_version": "bedrock-2023-05-31",
            "max_tokens": max_tokens,
            "messages": messages
        }
    )

    response = bedrock_runtime.invoke_model(
        body=body, modelId=model_id)
    response_body = json.loads(response.get('body').read())

    return response_body


def main():
    """
    Entrypoint for Anthropic Claude multimodal prompt example.
    """

    try:

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

        model_id = 'anthropic.claude-3-sonnet-20240229-v1:0'
        max_tokens = 1000
        input_text = "What's in this image?"
        input_image = "/path/to/image" # Replace with actual path to image file
 
        # Read reference image from file and encode as base64 strings.
        image_ext = input_image.split(".")[-1]
        with open(input_image, "rb") as image_file:
            content_image = base64.b64encode(image_file.read()).decode('utf8')

        message = {
            "role": "user",
            "content": [
                {
                    "type": "image", 
                    "source": {
                        "type": "base64",
                        "media_type": f"image/{image_ext}", 
                        "data": content_image
                    }
                },
                {
                    "type": "text", 
                    "text": input_text
                }
            ]
        }

    
        messages = [message]

        response = run_multi_modal_prompt(
            bedrock_runtime, model_id, messages, max_tokens)
        print(json.dumps(response, indent=4))

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occurred: " +
              format(message))


if __name__ == "__main__":
    main()
```

------

### Amazon S3 にアップロードされたビデオに付随するテキストプロンプトでテキストレスポンスを生成する
<a name="w2aac13c32c33c17c19c13c15"></a>

次の例は、S3 バケットにアップロードした動画とそれに付随するテキストプロンプトに基づいて、Amazon Nova Lite モデルを使用してレスポンスを生成する方法を示しています。

**前提条件:** 「Amazon Simple Storage Service ユーザーガイド」の「[オブジェクトのアップロード](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html#upload-objects-procedure)」の手順に従って、*video.mp4* というタイトルの動画をアカウントの Amazon S3 バケットにアップロードします。動画の S3 URI を書き留めます。

任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

ターミナルを開き、次のコマンドを実行します。*s3://amzn-s3-demo-bucket/video.mp4* を動画の実際の S3 の場所に置き換えます。

```
aws bedrock-runtime invoke-model \
    --model-id amazon.nova-lite-v1:0 \
    --body '{
        "messages": [          
            {               
                "role": "user",
                "content": [      
                    {                       
                        "video": {     
                            "format": "mp4",   
                            "source": {
                                "s3Location": {
                                    "uri": "s3://amzn-s3-demo-bucket/video.mp4"
                                }
                            }
                        }                                    
                    },
                    {
                        "text": "What happens in this video?"
                    }
                ]
            }                              
        ]                  
    }' \
    --cli-binary-format raw-in-base64-out \
    invoke-model-output.txt
```

現在のフォルダの *invoke-model-output.txt* ファイルで出力を見つけます。

------
#### [ Python ]

次の Python スクリプトで、*s3://amzn-s3-demo-bucket/video.mp4* を動画の実際の S3 の場所に置き換えます。次に、スクリプトを実行します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to run a multimodal prompt with Nova Lite (on demand) and InvokeModel.
"""

import json
import logging
import base64
import boto3

from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens):
    """
    Invokes a model with a multimodal prompt.
    Args:
        bedrock_runtime: The Amazon Bedrock boto3 client.
        model_id (str): The model ID to use.
        messages (JSON) : The messages to send to the model.
        max_tokens (int) : The maximum  number of tokens to generate.
    Returns:
        None.
    """

    body = json.dumps(
        {
            "messages": messages,
            "inferenceConfig": {
                "maxTokens": max_tokens
            }
        }
    )

    response = bedrock_runtime.invoke_model(
        body=body, modelId=model_id)
    response_body = json.loads(response.get('body').read())

    return response_body


def main():
    """
    Entrypoint for Nova Lite video prompt example.
    """

    try:

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

        model_id = "amazon.nova-lite-v1:0"
        max_tokens = 1000
        input_video_s3_uri = "s3://amzn-s3-demo-bucket/video.mp4" # Replace with real S3 URI
        video_ext = input_video_s3_uri.split(".")[-1]
        input_text = "What happens in this video?"

        message = {
            "role": "user",
            "content": [
                {
                    "video": {
                        "format": video_ext,
                        "source": {
                            "s3Location": {
                                "uri": input_video_s3_uri
                            }
                        }
                    }
                },
                {
                    "text": input_text
                }
            ]
        }

    
        messages = [message]

        response = run_multi_modal_prompt(
            bedrock_runtime, model_id, messages, max_tokens)
        print(json.dumps(response, indent=4))

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))


if __name__ == "__main__":
    main()
```

------

### テキストプロンプトが付随する、base64 でエンコードされた文字列に変換された動画へのテキストレスポンスを生成する
<a name="w2aac13c32c33c17c19c13c17"></a>

次の例は、base64 でエンコードされた文字列に変換された動画とそれに付随するテキストプロンプトに基づいて、Amazon Nova Lite モデルを使用してレスポンスを生成する方法を示しています。任意の方法のタブを選択し、その手順に従います。

------
#### [ CLI ]

以下の操作を実行します。

1. 次のコマンドを実行して、現在のフォルダの *video.mp4* というタイトルの動画を base64 に変換します。

   ```
   base64 -i video.mp4 -o video.txt
   ```

1. *video-text-input.json* という JSON ファイルを作成し、次の JSON を貼り付けます。*\$1\$1video-base64\$1* を `video.txt` ファイルの内容に置き換えます (末尾に新しい行がないことを確認します)。

   ```
   {
       "messages": [          
           {               
               "role": "user",
               "content": [      
                   {                       
                       "video": {     
                           "format": "mp4",   
                           "source": {
                               "bytes": ${video-base64}
                           }
                       }                                    
                   },
                   {
                       "text": "What happens in this video?"
                   }
               ]
           }                              
       ]                  
   }
   ```

1. 次のコマンドを実行して、動画とそれに付随するテキストプロンプトに基づいて *invoke-model-output.txt* というファイルへのテキスト出力を生成します。

   ```
   aws bedrock-runtime invoke-model \
       --model-id amazon.nova-lite-v1:0 \
       --body file://video-text-input.json \
       --cli-binary-format raw-in-base64-out \
       invoke-model-output.txt
   ```

1. 現在のフォルダの *invoke-model-output.txt* ファイルで出力を見つけます。

------
#### [ Python ]

次の Python スクリプトで、*/path/to/video.mp4* を動画への実際のパスに置き換えます。次に、スクリプトを実行します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to run a multimodal prompt with Nova Lite (on demand) and InvokeModel.
"""

import json
import logging
import base64
import boto3

from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens):
    """
    Invokes a model with a multimodal prompt.
    Args:
        bedrock_runtime: The Amazon Bedrock boto3 client.
        model_id (str): The model ID to use.
        messages (JSON) : The messages to send to the model.
        max_tokens (int) : The maximum  number of tokens to generate.
    Returns:
        None.
    """

    body = json.dumps(
        {
            "messages": messages,
            "inferenceConfig": {
                "maxTokens": max_tokens
            }
        }
    )

    response = bedrock_runtime.invoke_model(
        body=body, modelId=model_id)
    response_body = json.loads(response.get('body').read())

    return response_body


def main():
    """
    Entrypoint for Nova Lite video prompt example.
    """

    try:

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

        model_id = "amazon.nova-lite-v1:0"
        max_tokens = 1000
        input_video = "/path/to/video.mp4" # Replace with real path to video
        video_ext = input_video.split(".")[-1]
        input_text = "What happens in this video?"

        # Read reference video from file and encode as base64 string.
        with open(input_video, "rb") as video_file:
            content_video = base64.b64encode(video_file.read()).decode('utf8')\

        message = {
            "role": "user",
            "content": [
                {
                    "video": {
                        "format": video_ext,
                        "source": {
                            "bytes": content_video
                        }
                    }
                },
                {
                    "text": input_text
                }
            ]
        }

    
        messages = [message]

        response = run_multi_modal_prompt(
            bedrock_runtime, model_id, messages, max_tokens)
        print(json.dumps(response, indent=4))

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))


if __name__ == "__main__":
    main()
```

------

## ストリーミングコードによるモデル呼び出しの例
<a name="inference-examples-stream"></a>

**注記**  
 AWS CLI はストリーミングをサポートしていません。

次の例は、「*火星での生活に関する小論文を 1,000 語で書いてください*」というプロンプトを使用して、[InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) API で 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()))
```

# OpenAI Chat Completions API を使用してモデルを呼び出す
<a name="inference-chat-completions"></a>

Amazon Bedrock モデルで[OpenAI Create chat completion API](https://platform.openai.com/docs/api-reference/chat/create) を使用してモデル推論を実行できます。

以下の方法で Create chat completion API を呼び出すことができます。
+ Amazon Bedrock ランタイムエンドポイントを使用して HTTP リクエストを行う。
+ Amazon Bedrock ランタイムエンドポイントで OpenAI SDK リクエストを使用する。

詳細情報については、トピックを選択してください。

**Topics**
+ [OpenAI Chat Completions API でサポートされているモデルとリージョン](#inference-chat-completions-supported)
+ [Chat Completions API を使用するための前提条件](#inference-chat-completions-prereq)
+ [チャット完了を作成する](#inference-chat-completions-create)
+ [チャット完了にガードレールを含める](#inference-chat-completions-guardrails)

## OpenAI Chat Completions API でサポートされているモデルとリージョン
<a name="inference-chat-completions-supported"></a>

Create chat completion API は、Amazon Bedrock およびこれらのOpenAIモデルをサポートする AWS リージョンでサポートされているすべてのモデルで使用できます。サポートされているモデルとリージョンの詳細については、「[Amazon Bedrock でサポートされている基盤モデル](models-supported.md)」を参照してください。

## Chat Completions API を使用するための前提条件
<a name="inference-chat-completions-prereq"></a>

Chat Completions API を使用するための前提条件を確認するには、任意の方法のタブを選択し、そのステップに従います。

------
#### [ OpenAI SDK ]
+ **認証** – OpenAI SDK は Amazon Bedrock API キーを使用した認証のみをサポートします。Amazon Bedrock API キーを生成してリクエストを認証します。Amazon Bedrock API キーとその生成方法については、「ビルド」章の「API キー」セクションを参照してください。
+ **エンドポイント** – Amazon Bedrock ランタイムエンドポイントとクォータで使用する AWS リージョンに対応するエンドポイントを見つけます。 [https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt) AWS SDK を使用する場合は、クライアントの設定時にエンドポイント全体ではなく、リージョンコードを指定するだけで済みます。
+ **OpenAI SDK のインストール** – 詳細については、「OpenAI ドキュメント」の「[ライブラリ](https://platform.openai.com/docs/libraries)」を参照してください。

------
#### [ HTTP request ]
+ **認証** – AWS 認証情報または Amazon Bedrock API キーを使用して認証できます。

   AWS 認証情報を設定するか、Amazon Bedrock API キーを生成してリクエストを認証します。
  +  AWS 認証情報の設定については、[AWS 「セキュリティ認証情報を使用したプログラムによるアクセス](https://docs.aws.amazon.com/IAM/latest/UserGuide/security-creds-programmatic-access.html)」を参照してください。
  + Amazon Bedrock API キーとその生成方法については、「ビルド」章の「API キー」セクションを参照してください。
+ **エンドポイント** – Amazon Bedrock ランタイムエンドポイントとクォータで使用する AWS リージョンに対応するエンドポイントを見つけます。 [https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt) AWS SDK を使用する場合は、クライアントの設定時にエンドポイント全体ではなく、リージョンコードを指定するだけで済みます。

------

## チャット完了を作成する
<a name="inference-chat-completions-create"></a>

Create chat completion API の詳細については、OpenAI ドキュメントの以下のリソースを参照してください。
+ [リクエストボディパラメータ](https://platform.openai.com/docs/api-reference/chat/create)
+ [レスポンス本文パラメータ](https://platform.openai.com/docs/api-reference/chat/object)

**注記**  
Amazon Bedrock では現在、他のOpenAI Chat completion API オペレーションがサポートされていません。

OpenAI Create chat completion API の使用方法を確認するには、任意の方法のタブを選択し、そのステップに従ってください。

------
#### [ OpenAI SDK (Python) ]

OpenAI SDK でチャット完了を作成するには、次の手順を実行します。

1. OpenAI SDK をインポートし、次のフィールドを使用してクライアントを設定します。
   + `base_url` – 次の形式のように、Amazon Bedrock ランタイムエンドポイントのプレフィックスを `/openai/v1` に付けます。

     ```
     https://${bedrock-runtime-endpoint}/openai/v1
     ```
   + `api_key` – Amazon Bedrock API キーを指定します。
   + `default_headers` – ヘッダーを含める必要がある場合は、キーと値のペアとしてこのオブジェクトに含めることができます。または、特定の API コールを行うときに `extra_headers` でヘッダーを指定することもできます。

1. クライアントで `chat.completions.create()` メソッドを使用し、リクエスト本文で少なくとも `model` と `messages` を指定します。

次の例では、`us-west-2` で Create chat completion API を呼び出します。*\$1AWS\$1BEARER\$1TOKEN\$1BEDROCK* を実際の API キーに置き換えます。

```
from openai import OpenAI

client = OpenAI(
    base_url="https://bedrock-runtime.us-west-2.amazonaws.com/openai/v1", 
    api_key="$AWS_BEARER_TOKEN_BEDROCK" # Replace with actual API key
)

completion = client.chat.completions.create(
    model="openai.gpt-oss-20b-1:0",
    messages=[
        {
            "role": "developer",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "Hello!"
        }
    ]
)

print(completion.choices[0].message)
```

------
#### [ HTTP request ]

直接 HTTP リクエストを使用してチャット完了を作成するには、次の手順を実行します。

1. 次の形式で、Amazon Bedrock ランタイムエンドポイントのプレフィックスを `/openai/v1/chat/completions` に付け加えて URL を指定します。

   ```
   https://${bedrock-runtime-endpoint}/openai/v1/chat/completions
   ```

1. `Authorization` ヘッダーで AWS 認証情報または Amazon Bedrock API キーを指定します。

1. リクエスト本文で、リクエスト本文に少なくとも `model` と `messages` を指定します。

次の例では、`us-west-2` で curl を使用して Create chat completion API を呼び出します。*\$1AWS\$1BEARER\$1TOKEN\$1BEDROCK* を実際の API キーに置き換えます。

```
curl -X POST https://bedrock-runtime.us-west-2.amazonaws.com/openai/v1/chat/completions \
   -H "Content-Type: application/json" \
   -H "Authorization: Bearer $AWS_BEARER_TOKEN_BEDROCK" \
   -d '{
    "model": "openai.gpt-oss-20b-1:0",
    "messages": [
        {
            "role": "developer",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "Hello!"
        }
    ]
}'
```

------

## チャット完了にガードレールを含める
<a name="inference-chat-completions-guardrails"></a>

モデルの入力とレスポンスに保護対策を追加するには、モデル呼び出しを実行するとき、次の[追加のパラメータ](https://github.com/openai/openai-python#undocumented-request-params)をリクエスト本文にフィールドとして含めることにより[ガードレール](guardrails.md)を適用します。
+ `extra_headers` – リクエストで追加のヘッダーを指定する次のフィールドを含むオブジェクトにマッピングします。
  + `X-Amzn-Bedrock-GuardrailIdentifier` (必須) – ガードレールの ID。
  + `X-Amzn-Bedrock-GuardrailVersion` (必須) – ガードレールのバージョン。
  + `X-Amzn-Bedrock-Trace` (オプション) – ガードレールトレースを有効にするかどうか。
+ `extra_body` – オブジェクトにマッピングします。そのオブジェクトには、次のフィールドを含むオブジェクトにマッピングされる `amazon-bedrock-guardrailConfig` フィールドを含めることができます。
  + `tagSuffix` (オプション) – [入力タグ付け](guardrails-tagging.md)にこのフィールドを含めます。

Amazon Bedrock ガードレールのこれらのパラメータの詳細については、「[ガードレールをテストする](guardrails-test.md)」を参照してください。

OpenAI chat completion を使用したガードレールの使用例を確認するには、任意の方法のタブを選択し、そのステップに従ってください。

------
#### [ OpenAI SDK (Python) ]

```
import openai
from openai import OpenAIError

# Endpoint for Amazon Bedrock Runtime
bedrock_endpoint = "https://bedrock-runtime.us-west-2.amazonaws.com/openai/v1"

# Model ID
model_id = "openai.gpt-oss-20b-1:0"

# Replace with actual values
bedrock_api_key = "$AWS_BEARER_TOKEN_BEDROCK"
guardrail_id = "GR12345"
guardrail_version = "DRAFT"

client = openai.OpenAI(
    api_key=bedrock_api_key,
    base_url=bedrock_endpoint,
)

try:
    response = client.chat.completions.create(
        model=model_id,
        # Specify guardrail information in the header
        extra_headers={
            "X-Amzn-Bedrock-GuardrailIdentifier": guardrail_id,
            "X-Amzn-Bedrock-GuardrailVersion": guardrail_version,
            "X-Amzn-Bedrock-Trace": "ENABLED",
        },
        # Additional guardrail information can be specified in the body
        extra_body={
            "amazon-bedrock-guardrailConfig": {
                "tagSuffix": "xyz"  # Used for input tagging
            }
        },
        messages=[
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "assistant", 
                "content": "Hello! How can I help you today?"
            },
            {
                "role": "user",
                "content": "What is the weather like today?"
            }
        ]
    )

    request_id = response._request_id
    print(f"Request ID: {request_id}")
    print(response)
    
except OpenAIError as e:
    print(f"An error occurred: {e}")
    if hasattr(e, 'response') and e.response is not None:
        request_id = e.response.headers.get("x-request-id")
        print(f"Request ID: {request_id}")
```

------
#### [ OpenAI SDK (Java) ]

```
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.HttpResponseFor;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

// Endpoint for Amazon Bedrock Runtime
String bedrockEndpoint = "http://bedrock-runtime.us-west-2.amazonaws.com/openai/v1"

// Model ID
String modelId = "openai.gpt-oss-20b-1:0"

// Replace with actual values
String bedrockApiKey = "$AWS_BEARER_TOKEN_BEDROCK"
String guardrailId = "GR12345"
String guardrailVersion = "DRAFT"

OpenAIClient client = OpenAIOkHttpClient.builder()
        .apiKey(bedrockApiKey)
        .baseUrl(bedrockEndpoint)
        .build()

ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
        .addUserMessage("What is the temperature in Seattle?")
        .model(modelId)
        // Specify additional headers for the guardrail
        .putAdditionalHeader("X-Amzn-Bedrock-GuardrailIdentifier", guardrailId)
        .putAdditionalHeader("X-Amzn-Bedrock-GuardrailVersion", guardrailVersion)
        // Specify additional body parameters for the guardrail
        .putAdditionalBodyProperty(
                "amazon-bedrock-guardrailConfig",
                JsonValue.from(Map.of("tagSuffix", JsonValue.of("xyz"))) // Allows input tagging
        )
        .build();
        
HttpResponseFor<ChatCompletion> rawChatCompletionResponse =
        client.chat().completions().withRawResponse().create(request);

final ChatCompletion chatCompletion = rawChatCompletionResponse.parse();

System.out.println(chatCompletion);
```

------

# Converse API オペレーションを使用して会話を実行する
<a name="conversation-inference"></a>

Amazon Bedrock Converse API を使用すると、Amazon Bedrock モデルとの間でメッセージを送受信する会話型アプリケーションを作成できます。例えば、多くのターンにわたって会話を維持し、役に立つテクニカルサポートアシスタントなど、ニーズに固有のペルソナまたはトーンをカスタマイズしたチャットボットを作成できます。

Converse API を使用するには、[Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) または [ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html) (ストリーミングレスポンス用) オペレーションを使用して、モデルにメッセージを送信します。会話アプリケーションには、既存のベース推論オペレーション ([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)) を使用できます。ただし、メッセージをサポートするすべての Amazon Bedrock モデルで動作する一貫した API を提供する Converse API を使用することをお勧めします。そうすることで、コードを 1 回だけ記述し、それをさまざまなモデルで使用できます。Converse API では、モデルに一意の推論パラメータがある場合、モデル固有の構造でこれらの一意のパラメータを渡すこともできます。

Converse API を使用すると、アプリケーションで[ツールの使用](tool-use.md)と[ガードレール](guardrails-use-converse-api.md)を実装できます。

**注記**  
Mistral AI モデルと Meta モデルでは、Converse API は入力内容をモデル固有のプロンプトテンプレートに埋め込み、会話ができるようにします。
制限は、`InvokeModel`、、`InvokeModelWithResponseStream``Converse`、および のオペレーションに適用されます`ConverseStream`。詳細については、[「API の制限](inference-api-restrictions.md)」を参照してください。

コード例については、以下を参照してください。
+ このトピックの Python の例 - [Converse API の例](conversation-inference-examples.md)
+ さまざまな言語とモデル – [AWS SDKsコード例](service_code_examples_bedrock-runtime.md)
+ Java チュートリアル – [Bedrock の新しい Converse API](https://community.aws/content/2hUiEkO83hpoGF5nm3FWrdfYvPt/amazon-bedrock-converse-api-java-developer-guide) に関する Java デベロッパーガイド
+ JavaScript チュートリアル – [Bedrock の新しい Converse API](https://community.aws/content/2dtauBCeDa703x7fDS9Q30MJoBA/amazon-bedrock-converse-api-developer-guide) に関するデベロッパーガイド

**Topics**
+ [サポートされているモデルとモデルの機能](conversation-inference-supported-models-features.md)
+ [Converse API を使用する場合](conversation-inference-call.md)
+ [Converse API の例](conversation-inference-examples.md)

# サポートされているモデルとモデルの機能
<a name="conversation-inference-supported-models-features"></a>

Converse API は、以下の Amazon Bedrock モデルとモデルの機能をサポートしています。Converse API は、いずれの埋め込みモデルやイメージ生成モデルもサポートしていません。


| モデル | Converse | ConverseStream | システムプロンプト | ドキュメントチャット | 視覚 | ツールの使用 | ストリーミングツールの使用 | ガードレール | マルチメディア用の Amazon S3 リンク | 
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | 
| AI21 Jamba-Instruct | はい  | はい | あり | なし | なし | なし | なし | なし | いいえ | 
| AI21 Labs Jurassic-2 (テキスト) | 制限あり。チャットはサポートされていません。 | いいえ | なし | なし | なし | なし | なし | あり | なし | 
| AI21 Labs Jamba 1.5 Large | はい | はい | はい | あり | なし | はい | はい | あり | なし | 
| AI21 Labs Jamba 1.5 Mini | はい | はい | はい | あり | なし | はい | はい | あり | なし | 
| Amazon Nova Premier | はい | はい | はい | はい | はい | はい | はい | はい | はい | 
| Amazon Nova Pro | はい | はい | はい | はい | はい | はい | はい | はい | はい | 
| Amazon Nova Lite | はい | はい | はい | はい | はい | はい | はい | はい | はい | 
| Amazon Nova Micro | はい | はい | あり | なし | なし | はい | はい | あり | なし | 
| Amazon Titan モデル | はい  | あり | いいえ | はい (Titan Text Premier を除く) | いいえ | なし | なし | あり | なし | 
| Anthropic Claude 2.x 以前のモデル | はい  | はい | はい | あり | なし | なし | なし | あり | なし | 
| Anthropic Claude 3 モデル | はい  | はい | はい | はい | はい | はい | はい | あり | なし | 
| Anthropic Claude 3.5 Sonnet | はい | はい | はい | はい | はい | はい | はい | あり | なし | 
| Anthropic Claude 3.5 Sonnet v2 | はい  | はい | はい | はい | はい | はい | はい | あり | なし | 
| Anthropic Claude 3.7 Sonnet | はい | はい | はい | はい | はい | はい | はい | あり | なし | 
| Anthropic Claude 3.5 Haiku | はい | はい | はい | あり | なし | はい | あり | なし | なし | 
| Anthropic Claude Sonnet 4 | はい | はい | はい | はい | はい | はい | あり | なし | いいえ | 
| Anthropic Claude Opus 4 | はい  | はい | はい | はい | はい | はい | あり | なし | なし | 
| Anthropic Claude Sonnet 4.5 | はい | はい | はい | はい | はい | はい | あり | なし | なし | 
| Anthropic Claude Haiku 4.5 | はい | はい | はい | はい | はい | はい | あり | なし | いいえ | 
| Anthropic Claude Opus 4.1 | はい  | はい | はい | はい | はい | はい | あり | なし | いいえ | 
| Claude Opus 4.5 | はい  | はい | はい | はい | はい | はい | あり | なし | いいえ | 
| Cohere Command | 制限あり。チャットはサポートされていません。 | 制限あり。チャットはサポートされていません。 | いいえ | あり | なし | なし | なし | あり | いいえ | 
| Cohere Command Light | 制限あり。チャットはサポートされていません。 | 制限あり。チャットはサポートされていません。 | いいえ | なし | なし | なし | なし | あり | いいえ | 
| Cohere および Command R Command R\$1 | はい  | はい | はい | あり | なし | はい | あり | なし | なし | 
| DeepSeek-R1 | はい | はい | はい | あり | なし | なし | なし | あり | いいえ | 
| Meta および Llama 2 Llama 3 | はい  | はい | はい | あり | なし | なし | なし | あり | なし | 
| Meta Llama 3.1 | はい | はい | はい | あり | なし | あり | なし | あり | なし | 
| Meta Llama 3.2 1b and Llama 3.2 3b | はい | はい | はい | あり | なし | なし | なし | あり | なし | 
| Meta Llama 3.2 11b and Llama 3.2 90b | はい | はい | はい | はい | はい | あり | なし | あり | なし | 
| Meta Llama 4 Maverick 17B and Llama 4.0 Scout 17B | はい | はい | はい | はい | はい | あり | なし | あり | なし | 
| Mistral AI Instruct | はい | あり | なし | あり | なし | なし | なし | あり | なし | 
| Mistral Large | はい | はい | はい | あり | なし | あり | なし | あり | なし | 
| Mistral Large 2 (24.07) | はい | はい | はい | あり | なし | あり | なし | あり | なし | 
| Mistral Small | はい | はい | あり | なし | なし | あり | なし | あり | なし | 
| Pixtral Large (25.02) | はい | はい | はい | あり | なし | あり | なし | あり | なし | 
| Writer Palmyra X4 | はい | はい | はい | あり | なし | あり | なし | あり | なし | 
| Writer Palmyra X5 | はい | はい | はい | あり | なし | あり | なし | あり | なし | 

各モデルをサポートするリージョンの表については、「[Amazon Bedrock AWS リージョン での によるモデルサポート](models-regions.md)」を参照してください。

**注記**  
Cohere Command (テキスト) と AI21 Labs Jurassic-2 (テキスト) は、Converse API とのチャットをサポートしていません。モデルは一度に 1 つのユーザーメッセージのみを処理でき、会話の履歴を維持することはできません。複数のメッセージを渡そうとすると、エラーが発生します。

# Converse API を使用する場合
<a name="conversation-inference-call"></a>

Converse API を使用するには、`Converse` または `ConverseStream` オペレーションを呼び出してモデルにメッセージを送信します。`Converse` を呼び出すには、`bedrock:InvokeModel` オペレーションを呼び出す許可も必要です。`ConverseStream` を呼び出すには、`bedrock:InvokeModelWithResponseStream` オペレーションを呼び出す許可も必要です。

**Topics**
+ [リクエスト](#conversation-inference-call-request)
+ [応答](#conversation-inference-call-response)

**注記**  
制限は、InvokeModel、InvokeModelWithResponseStream、Converse、ConverseStream の各オペレーションに適用されます。詳細については、[「API の制限](inference-api-restrictions.md)」を参照してください。

## リクエスト
<a name="conversation-inference-call-request"></a>

[Amazon Bedrock ランタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#br-rt)で [Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) リクエストを行うときは、次のフィールドを含めることができます。
+ **modelId** – 推論に使用するリソースを指定できるヘッダー内の必須パラメータ。
+ 次のフィールドでは、プロンプトをカスタマイズできます。
  + **messages** – プロンプトの内容とロールを指定するために使用します。
  + **system** – モデルの指示またはコンテキストを定義するシステムプロンプトを指定するために使用します。
  + **inferenceConfig** – すべてのモデルに共通の推論パラメータを指定するために使用します。推論パラメータはレスポンスの生成に影響を与えます。
  + **additionalModelRequestFields** – 推論を実行するモデルに固有の推論パラメータを指定するために使用します。
  + **promptVariables** – (プロンプト管理のプロンプトを使用する場合) このフィールドを使用して、入力するプロンプト内の変数と入力する値を定義します。
+ 次のフィールドでは、レスポンスが返される方法をカスタマイズできます。
  + **guardrailConfig** – このフィールドを使用して、プロンプト全体に適用するガードレールを含めます。
  + **toolConfig** – このフィールドを使用して、モデルがレスポンスを生成するのに役立つツールを含めます。
  + **additionalModelResponseFieldPaths** – このフィールドを使用して、JSON ポインタオブジェクトとして返すフィールドを指定します。
  + **serviceTier** – このフィールドを使用して、特定のリクエストのサービス層を指定します。
+ **requestMetadata** – このフィールドを使用して、呼び出しログを使用するときにフィルタリングできるメタデータを含めます。

**注記**  
`Converse` または `ConverseStream` でプロンプト管理プロンプトを使用する場合、次の制限が適用されます。  
`additionalModelRequestFields`、`inferenceConfig`、`system`、`toolConfig` フィールドを含めることはできません。
`messages` フィールドを含めると、プロンプトで定義されたメッセージの後にメッセージが追加されます。
`guardrailConfig` フィールドを含めると、プロンプト全体にガードレールが適用されます。[ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) フィールドに `guardContent` ブロックを含めると、ガードレールはそれらのブロックにのみ適用されます。

セクションを展開すると、`Converse` リクエスト本文のフィールドの詳細を確認できます。

### メッセージ
<a name="converse-messages"></a>

`messages` フィールドは [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトの配列であり、それぞれがユーザーとモデル間のメッセージを定義します。`Message` オブジェクトには、以下のフィールドが含まれています。
+ **role** – メッセージの送信元が `user` (モデルに送信されたプロンプト) か `assistant` (モデルレスポンス) かを定義します。
+ **content** – プロンプト内のコンテンツを定義します。
**注記**  
Amazon Bedrock は、ユーザーがコンテンツとして提供したテキスト、画像、ドキュメントを保存しません。データはレスポンスの生成にのみ使用されます。

会話コンテキストを維持するには、後続の `Converse` リクエストに会話のすべてのメッセージを含め、`role` フィールドを使用して、メッセージがユーザーからのものであるかモデルからのものであるかを指定します。

`content` フィールドは [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) オブジェクトの配列にマッピングされます。各 [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) 内で、次のいずれかのフィールドを指定できます (どのモデルがどのブロックをサポートしているかを確認するには、「[サポートされているモデルとモデルの機能](conversation-inference-supported-models-features.md)」を参照)。

------
#### [ text ]

`text` フィールドは、プロンプトを指定する文字列にマッピングされます。`text` フィールドは、同じ [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) で指定された他のフィールドとともに解釈されます。

以下は、テキスト [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) のみを含む `content` 配列を持つ [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "text": "string"
        }
    ]
}
```

------
#### [ image ]

`image` フィールドは [ImageBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ImageBlock.html) にマッピングされます。base64 でエンコードされた raw バイトを、`bytes` フィールド内の画像に渡します。 AWS SDK を使用する場合、base64 のバイトをエンコードする必要はありません。

`text` フィールドを除外すると、モデルは画像を記述します。

以下は、画像 [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) のみを含む `content` 配列を持つサンプル [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "image": {
                "format": "png",
                "source": {
                    "bytes": "image in bytes"
                }
            }
        }
    ]
}
```

リクエスト本文でバイトを直接渡す代わりに、Amazon S3 URI を指定することもできます。以下は、Amazon S3 URI を通過するソースを含むコンテンツ配列を持つサンプル `Message` オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "image": {
                "format": "png",
                "source": {
                    "s3Location": {
                        "uri": "s3://amzn-s3-demo-bucket/myImage",
                        "bucketOwner": "111122223333"
                    }
                }
            }
        }
    ]
}
```

------
#### [ document ]

`document` フィールドは [DocumentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_DocumentBlock.html) にマッピングされます。`DocumentBlock` を含める場合は、リクエストが次の制限に準拠していることを確認します。
+ [メッセージ](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html)オブジェクトの `content` フィールドに、ドキュメントに関連するプロンプトを含む `text` フィールドを含める必要があります。
+ base64 でエンコードされた raw バイトを、`bytes` フィールド内のドキュメントに渡します。 AWS SDK を使用する場合、base64 のドキュメントのバイトをエンコードする必要はありません。
+ `name` フィールドには以下の文字のみ含めることができます。
  + アルファベットの文字
  + 空白文字 (連続した空白文字は使用不可)
  + ハイフン
  + 括弧
  + 角括弧
**注記**  
モデルが誤って指示として解釈する可能性があるため、`name` フィールドはプロンプトインジェクションに対して脆弱です。したがって、中立的な名前を指定することが推奨されます。

ドキュメントを使用する場合、`citations` タグを有効にできます。これにより、API コールのレスポンスでドキュメント固有の引用が提供されます。詳細については、「[DocumentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_DocumentBlock.html)」 API を参照してください。

以下は、ドキュメント [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) と必須の付随テキスト [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) のみを含む `content` 配列を持つサンプル [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "text": "string"
        },
        {
            "document": {
                "format": "pdf",
                "name": "MyDocument",
                "source": {
                    "bytes": "document in bytes"
                }
            }
        }
    ]
}
```

リクエスト本文でバイトを直接渡す代わりに、Amazon S3 URI を指定することもできます。以下は、Amazon S3 URI を通過するソースを含むコンテンツ配列を持つサンプル `Message` オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "text": "string"
        },
        {
            "document": {
                "format": "pdf",
                "name": "MyDocument",
                "source": {
                    "s3Location": {
                      "uri": "s3://amzn-s3-demo-bucket/myDocument",
                      "bucketOwner": "111122223333"
                    }
                }
            }
        }
    ]
}
```

------
#### [ video ]

`video` フィールドは [VideoBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_VideoBlock.html) オブジェクトにマッピングされます。base64 でエンコードされた `bytes` フィールド内の raw バイトを渡します。 AWS SDK を使用する場合、base64 でバイトをエンコードする必要はありません。

`text` フィールドを含めない場合、モデルは動画を記述します。

以下は、画像 [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) のみを含む `content` 配列を持つサンプル [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "video": {
                "format": "mp4",
                "source": {
                    "bytes": "video in bytes"
                }
            }
        }
    ]
}
```

リクエスト本文でバイトを直接渡す代わりに、Amazon S3 URI を指定することもできます。以下は、Amazon S3 URI を通過するソースを含むコンテンツ配列を持つサンプル `Message` オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "video": {
                "format": "mp4",
                "source": {
                    "s3Location": {
                        "uri": "s3://amzn-s3-demo-bucket/myVideo",
                        "bucketOwner": "111122223333"
                    }
                }
            }
        }
    ]
}
```

**注記**  
引き受けたロールには、Amazon S3 URI に対する `s3:GetObject` アクセス許可が必要です。`bucketOwner` フィールドはオプションですが、リクエストを行うアカウントが Amazon S3 URI が見つかるバケットを所有していない場合は、指定する必要があります。詳細については、「[Amazon S3 バケットへのアクセスを設定する](s3-bucket-access.md)」を参照してください。

------
#### [ cachePoint ]

`cachePoint` フィールドを使用してプロンプトキャッシュを利用することで、キャッシュチェックポイントをメッセージ内のブロックとして付随するプロンプトとともに追加できます。プロンプトキャッシュは、会話のコンテキストのキャッシュを開始してコストとレイテンシーを節約できる機能です。詳細については、「[モデル推論を高速化するためのプロンプトキャッシュ](prompt-caching.md)」を参照してください。

以下は、ドキュメント [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) と付随する必須のテキスト [ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) を含む `content` 配列を持つ [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトの例と、ドキュメントとテキストの両方の内容をキャッシュに追加する **cachePoint** を示しています。

```
{
    "role": "user",
    "content": [
        {
            "text": "string"
        },
        {
            "document": {
                "format": "pdf",
                "name": "string",
                "source": {
                    "bytes": "document in bytes"
                }
            }
        },
        {
            "cachePoint": {
                "type": "default"
            }
        }
    ]
}
```

------
#### [ guardContent ]

`guardContent` フィールドは [GuardrailConverseContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailConverseContentBlock.html) オブジェクトにマッピングされます。このフィールドを使用して、`guardrailConfig` フィールドで定義されたガードレールによって評価される入力をターゲットにできます。このフィールドを指定しない場合、ガードレールはリクエスト本文内のすべてのメッセージを評価します。`GuardBlock` では、次のタイプのコンテンツを渡すことができます。
+ **text** – 以下は、テキスト [GuardrailConverseContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailConverseContentBlock.html) のみを含む `content` 配列を持つ [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトの例を示しています。

  ```
  {
      "role": "user",
      "content": [
          {
              "text": "Tell me what stocks to buy.",
              "qualifiers": [
                  "guard_content"
              ]
          }
      ]
  }
  ```

  評価するテキストを定義し、[コンテキストグラウンディング](guardrails-contextual-grounding-check.md)に使用する修飾子を含めます。
+ **image** – 以下は、画像 [GuardrailConverseContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailConverseContentBlock.html) のみを含む `content` 配列を持つ [Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html) オブジェクトの例を示しています。

  ```
  {
      "role": "user",
      "content": [
          {
              "format": "png",
              "source": {
                  "bytes": "image in bytes"
              }
          }
      ]
  }
  ```

  画像の形式を指定し、画像をバイト単位で定義します。

ガードレールの使用方法の詳細については、「[Amazon Bedrock ガードレールを使用して有害なコンテンツを検出してフィルタリングする](guardrails.md)」を参照してください。

------
#### [ reasoningContent ]

`reasoningContent` フィールドは [ReasoningContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ReasoningContentBlock.html) にマッピングされます。このブロックには、付随する `ContentBlock` でレスポンスを生成するためにモデルが実行した推論に関するコンテンツが含まれています。

以下は、`ReasoningContentBlock` とそれに付随するテキスト `ContentBlock` のみを含む `content` 配列を持つ `Message` オブジェクトを示しています。

```
{
    "role": "user",
    "content": [
        {
            "text": "string"
        },
        {
            "reasoningContent": {
                "reasoningText": {
                    "text": "string",
                    "signature": "string"
                }
                "redactedContent": "base64-encoded binary data object"
            }
        }
    ]
}
```

`ReasoningContentBlock` には、`reasoningText` フィールド内に付随するコンテンツを生成するために使用する推論と、`redactedContent` フィールドの信頼と安全の理由でモデルプロバイダーが暗号化した推論内のコンテンツが含まれます。

`reasoningText` フィールド内で、`text` フィールドは推論を記述します。`signature` フィールドは会話内のすべてのメッセージのハッシュであり、モデルで使用される推論の改ざんに対するセーフガードです。署名と以前のすべてのメッセージを後続の `Converse` リクエストに含める必要があります。メッセージのいずれかが変更されると、レスポンスはエラーをスローします。

------
#### [ toolUse ]

モデルが使用するツールに関する情報が含まれています。詳細については、「[ツールを使用して Amazon Bedrock のモデルレスポンスを完成させる](tool-use.md)」を参照してください。

------
#### [ toolResult ]

ツールを使用するモデルの結果に関する情報が含まれます。詳細については、「[ツールを使用して Amazon Bedrock のモデルレスポンスを完成させる](tool-use.md)」を参照してください。

------

次の `messages` 例では、ユーザーはポップの曲を 3 曲含むリストを要求し、モデルは曲のリストを生成します。

```
[
    {
        "role": "user",
        "content": [
            {
                "text": "Create a list of 3 pop songs."
            }
        ]
    },
    {
        "role": "assistant",
        "content": [
            {
                "text": "Here is a list of 3 pop songs by artists from the United Kingdom:\n\n1. \"As It Was\" by Harry Styles\n2. \"Easy On Me\" by Adele\n3. \"Unholy\" by Sam Smith and Kim Petras"
            }
        ]
    }
]
```

### アタッチ
<a name="converse-system"></a>

システムプロンプトは、会話中に実行するタスク、または採用するペルソナに関する指示やコンテキストをモデルに提供するプロンプトの一種です。次の例に示すように、`system` ([SystemContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_SystemContentBlock.html)) フィールドでリクエストのシステムプロンプトのリストを指定できます。

```
[
    {
        "text": "You are an app that creates play lists for a radio station that plays rock and pop music. Only return song names and the artist. "
    }
]
```

### inferenceConfig
<a name="converse-inference"></a>

Converse API は、`inferenceConfig` フィールド ([InferenceConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InferenceConfiguration.html)) で設定した推論パラメータの基本セットをサポートします。推論パラメータの基本セットは次のとおりです。
+ **maxTokens** – 生成されたレスポンスで許可するトークンの最大数。
+ **stopSequences** – ストップシーケンスのリスト。停止シーケンスは、モデルがレスポンスの生成を停止する一連の文字です。
+ **temperature** – モデルがレスポンスの生成中に確率の高いオプションを選択する可能性。
+ **topP** — モデルが次のトークンについて考慮する最も可能性の高い候補のパーセンテージ。

詳細については、「[推論パラメータでレスポンスの生成に影響を与える](inference-parameters.md)」を参照してください。

次の JSON の例では、`temperature` 推論パラメータを設定します。

```
{"temperature": 0.5}
```

### additionalModelRequestFields
<a name="converse-additional-model-request-fields"></a>

使用しているモデルに追加の推論パラメータがある場合は、`additionalModelRequestFields` フィールドに JSON として指定することで、これらのパラメータを設定できます。次の JSON の例は、`top_k` を設定する方法を示しています。これは Anthropic Claude モデルで使用できますが、メッセージ API のベース推論パラメータではありません。

```
{"top_k": 200}
```

### promptVariables
<a name="converse-prompt-variables"></a>

推論を実行するリソースとして `modelId` の[プロンプト管理](prompt-management.md)からプロンプトを指定する場合は、このフィールドを使用してプロンプト変数に実際の値を入力します。`promptVariables` フィールドは、プロンプトで定義された変数に対応するキーと、変数を置き換える値を持つ JSON オブジェクトにマップされます。

例えば **Make me a *\$1\$1genre\$1\$1* playlist consisting of the following number of songs: *\$1\$1number\$1\$1*.** というプロンプトがあるとします。プロンプトの ID は `PROMPT12345` で、バージョンは `1` です。変数を置き換えるには、次の `Converse` リクエストを送信できます。

```
POST /model/arn:aws:bedrock:us-east-1:111122223333:prompt/PROMPT12345:1/converse HTTP/1.1
Content-type: application/json

{
   "promptVariables": { 
      "genre" : "pop",
      "number": 3
   }
}
```

### guardrailConfig
<a name="converse-guardrail"></a>

このフィールドを含めることで、[Amazon Bedrock ガードレール](guardrails.md)で作成したガードレールを適用できます。会話内の特定のメッセージにガードレールを適用するには、[GuardrailConverseContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailConverseContentBlock.html) にメッセージを含めます。リクエスト本文に `GuardrailConverseContentBlock` を含めない場合、ガードレールは `messages` フィールド内のすべてのメッセージに適用されます。例については、[Converse API にガードレールを含める](guardrails-use-converse-api.md)を参照してください。

### toolConfig
<a name="converse-tool"></a>

このフィールドでは、モデルがレスポンスを生成するのに役立つツールを定義できます。詳細については、「[ツールを使用して Amazon Bedrock のモデルレスポンスを完成させる](tool-use.md)」を参照してください。

### additionalModelResponseFieldPaths
<a name="converse-additional-model-response-field-paths"></a>

次の例に示すように、`additionalModelResponseFieldPaths` フィールドで追加のモデルパラメータのパスを指定できます。

```
[ "/stop_sequence" ]
```

API は、`additionalModelResponseFields` フィールドでリクエストした追加のフィールドを返します。

### requestMetadata
<a name="converse-request-metadata"></a>

このフィールドは JSON オブジェクトにマッピングされます。このオブジェクト内でマッピングするメタデータキーと値を指定できます。リクエストメタデータを使用して、モデル呼び出しログをフィルタリングできます。

### serviceTier
<a name="inference-service-tiers"></a>

このフィールドは JSON オブジェクトにマッピングされます。特定のリクエストのサービス層を指定できます。

次の例は、 `serviceTier`構造を示しています。

```
"serviceTier": {
  "type": "reserved" | "priority" | "default" | "flex"
}
```

料金やパフォーマンス特性など、サービス階層の詳細については、「」を参照してください[パフォーマンスとコストを最適化するためのサービス階層](service-tiers-inference.md)。

オプションでキャッシュチェックポイントを `system` または `tools` フィールドに追加して、使用しているモデルに応じてプロンプトキャッシュを使用することもできます。詳細については、「[モデル推論を高速化するためのプロンプトキャッシュ](prompt-caching.md)」を参照してください。

## 応答
<a name="conversation-inference-call-response"></a>

Converse API から得られるレスポンスは、呼び出すオペレーション、`Converse`、または `ConverseStream` によって異なります。

**Topics**
+ [会話レスポンス](#conversation-inference-call-response-converse)
+ [ConverseStream レスポンス](#conversation-inference-call-response-converse-stream)

### 会話レスポンス
<a name="conversation-inference-call-response-converse"></a>

`Converse` からのレスポンスでは、`output` フィールド ([ConverseOutput](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseOutput.html)) には、モデルが生成するメッセージ ([Message](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html)) が含まれます。メッセージの内容は `content` ([ContentBlock](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html)) フィールドにあり、メッセージが対応するロール (`user` または `assistant`) は `role` フィールドにあります。

[プロンプトキャッシュ](prompt-caching.md)を使用した場合は、使用量フィールドの `cacheReadInputTokensCount` と`cacheWriteInputTokensCount` はキャッシュから読み取られたトークンの合計数とキャッシュに書き込まれたトークンの合計数をそれぞれ示します。

[サービス層](#inference-service-tiers)を使用した場合、レスポンスフィールドで、 `service tier`はリクエストに使用されたサービス層を示します。

`metrics` フィールド ([ConverseMetrics](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseMetrics.html)) には、呼び出しのメトリクスが含まれます。モデルがコンテンツの生成を停止した理由を確認するには、`stopReason` フィールドを確認します。`usage` フィールド ([TokenUsage](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_TokenUsage.html)) を確認することで、リクエストでモデルに渡されたトークンと、レスポンスで生成されたトークンに関する情報を取得できます。リクエストで追加のレスポンスフィールドを指定した場合、API は `additionalModelResponseFields` フィールドに JSON として返します。

次の例は、[リクエスト](#conversation-inference-call-request) で説明されているプロンプトを渡す際の `Converse` からのレスポンスを示しています。

```
{
    "output": {
        "message": {
            "role": "assistant",
            "content": [
                {
                    "text": "Here is a list of 3 pop songs by artists from the United Kingdom:\n\n1. \"Wannabe\" by Spice Girls\n2. \"Bitter Sweet Symphony\" by The Verve \n3. \"Don't Look Back in Anger\" by Oasis"
                }
            ]
        }
    },
    "stopReason": "end_turn",
    "usage": {
        "inputTokens": 125,
        "outputTokens": 60,
        "totalTokens": 185
    },
    "metrics": {
        "latencyMs": 1175
    }
}
```

### ConverseStream レスポンス
<a name="conversation-inference-call-response-converse-stream"></a>

モデルからレスポンスをストリーミングするために `ConverseStream` を呼び出すと、`stream` レスポンスフィールドにストリームが返されます。ストリームは、以下の順序で次のイベントを出力します。

1. `messageStart` ([MessageStartEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_MessageStartEvent.html))。メッセージの開始イベント。メッセージのロールが含まれます。

1. `contentBlockStart` ([ContentBlockStartEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlockStartEvent.html))。コンテンツブロックの開始イベント。ツールの使用のみ。

1. `contentBlockDelta` ([ContentBlockDeltaEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlockDeltaEvent.html))。コンテンツブロックのデルタイベント。次のいずれかが含まれます。
   + `text` – モデルが生成する部分的なテキスト。
   + `reasoningContent` – レスポンスを生成するためにモデルが実行する部分的な推論。後続の `Converse` リクエストで、以前のすべてのメッセージに加えて、返された `signature` を送信する必要があります。メッセージのいずれかが変更されると、レスポンスはエラーをスローします。
   + `toolUse` – ツール使用のための部分的な入力 JSON オブジェクト。

1. `contentBlockStop` ([ContentBlockStopEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlockStopEvent.html))。コンテンツブロックの停止イベント。

1. `messageStop` ([MessageStopEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_MessageStopEvent.html))。メッセージの停止イベント。モデルが出力の生成を停止した理由が含まれます。

1. `metadata` ([ConverseStreamMetadataEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStreamMetadataEvent.html))。リクエストのメタデータ。メタデータには、`usage` ([TokenUsage](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_TokenUsage.html)) のトークン使用量と `metrics` ([ConverseStreamMetadataEvent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStreamMetadataEvent.html)) の呼び出しのメトリクスが含まれます。

ConverseStream は、完全なコンテンツブロックを `ContentBlockStartEvent` イベント、1 つ以上の `ContentBlockDeltaEvent` イベント、および `ContentBlockStopEvent` イベントとしてストリーミングします。`contentBlockIndex` フィールドをインデックスとして使用して、コンテンツブロックを構成するイベントを関連付けます。

次の例は、`ConverseStream` からのレスポンスの一部です。

```
{'messageStart': {'role': 'assistant'}}
{'contentBlockDelta': {'delta': {'text': ''}, 'contentBlockIndex': 0}}
{'contentBlockDelta': {'delta': {'text': ' Title'}, 'contentBlockIndex': 0}}
{'contentBlockDelta': {'delta': {'text': ':'}, 'contentBlockIndex': 0}}
.
.
.
{'contentBlockDelta': {'delta': {'text': ' The'}, 'contentBlockIndex': 0}}
{'messageStop': {'stopReason': 'max_tokens'}}
{'metadata': {'usage': {'inputTokens': 47, 'outputTokens': 20, 'totalTokens': 67}, 'metrics': {'latencyMs': 100.0}}}
```

# Converse API の例
<a name="conversation-inference-examples"></a>

次の例では、`Converse` および `ConverseStream` オペレーションの使用方法を示します。

------
#### [ Text ]

この例では、*Anthropic Claude 3 Sonnet* モデルで `Converse` オペレーションを呼び出す方法を示しています。この例では、入力テキスト、推論パラメータ、およびモデルに固有の追加のパラメータを送信する方法を示しています。コードは、モデルに曲のリストを作成するように依頼することで会話を開始します。その後、英国のアーティストによる曲であることを尋ねることで会話を続けます。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to use the <noloc>Converse</noloc> API with Anthropic Claude 3 Sonnet (on demand).
"""

import logging
import boto3

from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_conversation(bedrock_client,
                          model_id,
                          system_prompts,
                          messages):
    """
    Sends messages to a model.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        system_prompts (JSON) : The system prompts for the model to use.
        messages (JSON) : The messages to send to the model.

    Returns:
        response (JSON): The conversation that the model generated.

    """

    logger.info("Generating message with model %s", model_id)

    # Inference parameters to use.
    temperature = 0.5
    top_k = 200

    # Base inference parameters to use.
    inference_config = {"temperature": temperature}
    # Additional inference parameters to use.
    additional_model_fields = {"top_k": top_k}

    # Send the message.
    response = bedrock_client.converse(
        modelId=model_id,
        messages=messages,
        system=system_prompts,
        inferenceConfig=inference_config,
        additionalModelRequestFields=additional_model_fields
    )

    # Log token usage.
    token_usage = response['usage']
    logger.info("Input tokens: %s", token_usage['inputTokens'])
    logger.info("Output tokens: %s", token_usage['outputTokens'])
    logger.info("Total tokens: %s", token_usage['totalTokens'])
    logger.info("Stop reason: %s", response['stopReason'])

    return response

def main():
    """
    Entrypoint for Anthropic Claude 3 Sonnet example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"

    # Setup the system prompts and messages to send to the model.
    system_prompts = [{"text": "You are an app that creates playlists for a radio station that plays rock and pop music. Only return song names and the artist."}]
    message_1 = {
        "role": "user",
        "content": [{"text": "Create a list of 3 pop songs."}]
    }
    message_2 = {
        "role": "user",
        "content": [{"text": "Make sure the songs are by artists from the United Kingdom."}]
    }
    messages = []

    try:

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

        # Start the conversation with the 1st message.
        messages.append(message_1)
        response = generate_conversation(
            bedrock_client, model_id, system_prompts, messages)

        # Add the response message to the conversation.
        output_message = response['output']['message']
        messages.append(output_message)

        # Continue the conversation with the 2nd message.
        messages.append(message_2)
        response = generate_conversation(
            bedrock_client, model_id, system_prompts, messages)

        output_message = response['output']['message']
        messages.append(output_message)

        # Show the complete conversation.
        for message in messages:
            print(f"Role: {message['role']}")
            for content in message['content']:
                print(f"Text: {content['text']}")
            print()

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print(f"A client error occured: {message}")

    else:
        print(
            f"Finished generating text with model {model_id}.")


if __name__ == "__main__":
    main()
```

------
#### [ Image ]

この例では、メッセージの一部として画像を送信する方法と、モデルが画像を記述するリクエストを示しています。この例では、`Converse` オペレーションと *Anthropic Claude 3 Sonnet* モデルを使用します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to send an image with the <noloc>Converse</noloc> API with an accompanying text prompt to Anthropic Claude 3 Sonnet (on demand).
"""

import logging
import boto3


from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_conversation(bedrock_client,
                          model_id,
                          input_text,
                          input_image):
    """
    Sends a message to a model.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        input text : The text prompt accompanying the image.
        input_image : The path to the input image.

    Returns:
        response (JSON): The conversation that the model generated.

    """

    logger.info("Generating message with model %s", model_id)

    # Get image extension and read in image as bytes
    image_ext = input_image.split(".")[-1]
    with open(input_image, "rb") as f:
        image = f.read()

    message = {
        "role": "user",
        "content": [
            {
                "text": input_text
            },
            {
                "image": {
                    "format": image_ext,
                    "source": {
                        "bytes": image
                    }
                }
            }
        ]
    }

    messages = [message]

    # Send the message.
    response = bedrock_client.converse(
        modelId=model_id,
        messages=messages
    )

    return response


def main():
    """
    Entrypoint for Anthropic Claude 3 Sonnet example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
    input_text = "What's in this image?"
    input_image = "path/to/image"

    try:

        bedrock_client = boto3.client(service_name="bedrock-runtime")

        response = generate_conversation(
            bedrock_client, model_id, input_text, input_image)

        output_message = response['output']['message']

        print(f"Role: {output_message['role']}")

        for content in output_message['content']:
            print(f"Text: {content['text']}")

        token_usage = response['usage']
        print(f"Input tokens:  {token_usage['inputTokens']}")
        print(f"Output tokens:  {token_usage['outputTokens']}")
        print(f"Total tokens:  {token_usage['totalTokens']}")
        print(f"Stop reason: {response['stopReason']}")

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print(f"A client error occured: {message}")

    else:
        print(
            f"Finished generating text with model {model_id}.")


if __name__ == "__main__":
    main()
```

------
#### [ Document ]

この例では、メッセージの一部としてドキュメントを送信する方法と、ドキュメントの内容を説明するようモデルにリクエストする方法を示しています。この例では、`Converse` オペレーションと *Anthropic Claude 3 Sonnet* モデルを使用します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to send an document as part of a message to Anthropic Claude 3 Sonnet (on demand).
"""

import logging
import boto3


from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_message(bedrock_client,
                     model_id,
                     input_text,
                     input_document_path):
    """
    Sends a message to a model.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        input text : The input message.
        input_document_path : The path to the input document.

    Returns:
        response (JSON): The conversation that the model generated.

    """

    logger.info("Generating message with model %s", model_id)

    # Get format from path and read the path
    input_document_format = input_document_path.split(".")[-1]
    with open(input_document_path, 'rb') as input_document_file:
        input_document = input_document_file.read()

    # Message to send.
    message = {
        "role": "user",
        "content": [
            {
                "text": input_text
            },
            {
                "document": {
                    "name": "MyDocument",
                    "format": input_document_format,
                    "source": {
                        "bytes": input_document
                    }
                }
            }
        ]
    }

    messages = [message]

    # Send the message.
    response = bedrock_client.converse(
        modelId=model_id,
        messages=messages
    )

    return response


def main():
    """
    Entrypoint for Anthropic Claude 3 Sonnet example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
    input_text = "What's in this document?"
    input_document_path = "path/to/document"

    try:

        bedrock_client = boto3.client(service_name="bedrock-runtime")


        response = generate_message(
            bedrock_client, model_id, input_text, input_document_path)

        output_message = response['output']['message']

        print(f"Role: {output_message['role']}")

        for content in output_message['content']:
            print(f"Text: {content['text']}")

        token_usage = response['usage']
        print(f"Input tokens:  {token_usage['inputTokens']}")
        print(f"Output tokens:  {token_usage['outputTokens']}")
        print(f"Total tokens:  {token_usage['totalTokens']}")
        print(f"Stop reason: {response['stopReason']}")

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print(f"A client error occured: {message}")

    else:
        print(
            f"Finished generating text with model {model_id}.")


if __name__ == "__main__":
    main()
```

------
#### [ Streaming ]

この例では、*Anthropic Claude 3 Sonnet* モデルで `ConverseStream` オペレーションを呼び出す方法を示しています。この例では、入力テキスト、推論パラメータ、およびモデルに固有の追加のパラメータを送信する方法を示しています。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to use the <noloc>Converse</noloc> API to stream a response from Anthropic Claude 3 Sonnet (on demand).
"""

import logging
import boto3


from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def stream_conversation(bedrock_client,
                    model_id,
                    messages,
                    system_prompts,
                    inference_config,
                    additional_model_fields):
    """
    Sends messages to a model and streams the response.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        messages (JSON) : The messages to send.
        system_prompts (JSON) : The system prompts to send.
        inference_config (JSON) : The inference configuration to use.
        additional_model_fields (JSON) : Additional model fields to use.

    Returns:
        Nothing.

    """

    logger.info("Streaming messages with model %s", model_id)

    response = bedrock_client.converse_stream(
        modelId=model_id,
        messages=messages,
        system=system_prompts,
        inferenceConfig=inference_config,
        additionalModelRequestFields=additional_model_fields
    )

    stream = response.get('stream')
    if stream:
        for event in stream:

            if 'messageStart' in event:
                print(f"\nRole: {event['messageStart']['role']}")

            if 'contentBlockDelta' in event:
                print(event['contentBlockDelta']['delta']['text'], end="")

            if 'messageStop' in event:
                print(f"\nStop reason: {event['messageStop']['stopReason']}")

            if 'metadata' in event:
                metadata = event['metadata']
                if 'usage' in metadata:
                    print("\nToken usage")
                    print(f"Input tokens: {metadata['usage']['inputTokens']}")
                    print(
                        f":Output tokens: {metadata['usage']['outputTokens']}")
                    print(f":Total tokens: {metadata['usage']['totalTokens']}")
                if 'metrics' in event['metadata']:
                    print(
                        f"Latency: {metadata['metrics']['latencyMs']} milliseconds")


def main():
    """
    Entrypoint for streaming message API response example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
    system_prompt = """You are an app that creates playlists for a radio station
      that plays rock and pop music. Only return song names and the artist."""

    # Message to send to the model.
    input_text = "Create a list of 3 pop songs."

    message = {
        "role": "user",
        "content": [{"text": input_text}]
    }
    messages = [message]
    
    # System prompts.
    system_prompts = [{"text" : system_prompt}]

    # inference parameters to use.
    temperature = 0.5
    top_k = 200
    # Base inference parameters.
    inference_config = {
        "temperature": temperature
    }
    # Additional model inference parameters.
    additional_model_fields = {"top_k": top_k}

    try:
        bedrock_client = boto3.client(service_name='bedrock-runtime')

        stream_conversation(bedrock_client, model_id, messages,
                        system_prompts, inference_config, additional_model_fields)

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))

    else:
        print(
            f"Finished streaming messages with model {model_id}.")


if __name__ == "__main__":
    main()
```

------
#### [ Video ]

この例は、メッセージの一部として動画を送信し、モデルに動画の説明をリクエストする方法を説明しています。この例では、`Converse` オペレーションと Amazon Nova Pro モデルを使用します。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to send a video with the <noloc>Converse</noloc> API to Amazon Nova Pro (on demand).
"""

import logging
import boto3


from botocore.exceptions import ClientError


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_conversation(bedrock_client,
                          model_id,
                          input_text,
                          input_video):
    """
    Sends a message to a model.
    Args:
        bedrock_client: The Boto3 Bedrock runtime client.
        model_id (str): The model ID to use.
        input text : The input message.
        input_video : The input video.

    Returns:
        response (JSON): The conversation that the model generated.

    """

    logger.info("Generating message with model %s", model_id)

    # Message to send.

    with open(input_video, "rb") as f:
        video = f.read()

    message = {
        "role": "user",
        "content": [
            {
                "text": input_text
            },
            {
                    "video": {
                        "format": 'mp4',
                        "source": {
                            "bytes": video
                        }
                    }
            }
        ]
    }

    messages = [message]

    # Send the message.
    response = bedrock_client.converse(
        modelId=model_id,
        messages=messages
    )

    return response


def main():
    """
    Entrypoint for Amazon Nova Pro example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = "amazon.nova-pro-v1:0"
    input_text = "What's in this video?"
    input_video = "path/to/video"

    try:

        bedrock_client = boto3.client(service_name="bedrock-runtime")

        response = generate_conversation(
            bedrock_client, model_id, input_text, input_video)

        output_message = response['output']['message']

        print(f"Role: {output_message['role']}")

        for content in output_message['content']:
            print(f"Text: {content['text']}")

        token_usage = response['usage']
        print(f"Input tokens:  {token_usage['inputTokens']}")
        print(f"Output tokens:  {token_usage['outputTokens']}")
        print(f"Total tokens:  {token_usage['totalTokens']}")
        print(f"Stop reason: {response['stopReason']}")

    except ClientError as err:
        message = err.response['Error']['Message']
        logger.error("A client error occurred: %s", message)
        print(f"A client error occured: {message}")

    else:
        print(
            f"Finished generating text with model {model_id}.")


if __name__ == "__main__":
    main()
```

------

# API の制限
<a name="inference-api-restrictions"></a>

、`InvokeModel`、`InvokeModelWithResponseStream`、および `ConverseStream`オペレーションには`Converse`、次の制限が適用されます。一部の制限は、以下に示すように、オペレーションまたはモデルによって異なります。
+ これらのオペレーションを使用する場合、 `role`が の場合にのみイメージとドキュメントを含めることができます`user`。
+ **動画生成:** 動画生成は、 `InvokeModel`および ではサポートされていません`InvokeModelWithResponseStream`。代わりに、[StartAsyncInvoke](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_StartAsyncInvoke.html) オペレーションを使用できます。例については、「[Amazon Nova Reel を使用してテキストプロンプトで動画を生成する](https://docs.aws.amazon.com/bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_Scenario_AmazonNova_TextToVideo_section.html)」を参照してください。
+ **リクエスト本文でのドキュメントのサポート:** `InvokeModel`および を使用する場合、リクエスト本文にドキュメントを含めることはサポートされていません`InvokeModelWithResponseStream`。推論中にドキュメントを含めるには、 で[チャット/テキストプレイグラウンド](playgrounds.md)を使用する AWS マネジメントコンソール か、 `Converse` または `ConverseStream`オペレーションを使用します。
+ **ドキュメント数とサイズ:** リクエストごとに最大 5 つのドキュメントを含めることができます。各ドキュメントのサイズは 4.5 MB 以下にする必要があります。Claude 4 以降のバージョンでは、4.5 MB のドキュメントサイズ制限は `PDF`形式には適用されません。Nova モデルの場合、4.5 MB のドキュメントサイズ制限は `PDF`および `DOCX`形式には適用されません。これらの制限は Bedrock コンソールで引き続き適用されます。個々のモデルには、Amazon Bedrock によって適用されるもの以外の追加のコンテンツ制限がある場合があります。詳細については、**「サードパーティーのモデルプロバイダーの要件**」を参照してください。
+ **イメージの数とサイズ**: Amazon Bedrock では、イメージの数とサイズに制限はありません。ただし、個々のモデルには特定のイメージ要件がある場合があります。詳細については、**「サードパーティーのモデルプロバイダーの要件**」を参照してください。
+ **サードパーティーモデルプロバイダーの要件:** 、`InvokeModel`、、および `ConverseStream`オペレーションを使用する場合`InvokeModelWithResponseStream``Converse`、サードパーティーモデルプロバイダーの要件が適用され、満たされない場合にエラーが発生する可能性があります。Amazon Bedrock を介してサードパーティーモデル (Anthropic Claude など) を使用する場合は、予期しないエラーを避けるためにプロバイダーのユーザーガイドと API ドキュメントを確認してください。たとえば、Anthropic Messages 標準エンドポイントは、最大リクエストサイズ 32 MB をサポートしています。Claude には、リクエストあたり最大 100 `PDF`ページ、最大イメージサイズ 8000x8000 ピクセルなど、特定のコンテンツ要件もあります。リクエストサイズやコンテンツ要件など、Anthropic Claude Messages のリクエストとレスポンスに関する最新情報については、Anthropic Claude ドキュメント[「Anthropic Claude API Overview](https://platform.claude.com/docs/en/api/overview)」、[「Anthropic Claude Messages API Reference](https://docs.anthropic.com/claude/reference/messages_post)」、[「Build with Claude: Vision](https://platform.claude.com/docs/en/build-with-claude/vision) and [Build with Claude: PDF Support](https://platform.claude.com/docs/en/build-with-claude/pdf-support)」を参照してください。

**ヒント**  
Claude では、PDF ドキュメントをリクエストごとに最大 100 ページにする必要があります。大きな PDF ドキュメントがある場合は、それぞれ 100 ページ未満の複数の PDFs に分割するか、より多くのテキストを少数のページに統合することをお勧めします。