

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

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