

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# InvokeModel을 사용하여 단일 프롬프트 제출
<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) 참조하세요.

필수 필드는 다음과 같습니다.


****  

| Field | 사용 사례: | 
| --- | --- | 
| modelId | 사용할 프롬프트 관리의 모델, 추론 프로파일 또는 프롬프트를 지정합니다. 이 값을 찾는 방법을 알아보려면 [API를 사용하여 프롬프트 제출 및 응답 생성](inference-api.md) 섹션을 참조하세요. | 
| 본문 | 모델의 추론 파라미터를 지정합니다. 여러 모델의 추론 파라미터를 확인하려면 [파운데이션 모델의 추론 요청 파라미터 및 응답 필드](model-parameters.md) 섹션을 참조하세요. modelId 필드의 프롬프트 관리에서 프롬프트를 지정하는 경우 이 필드를 생략합니다(포함하면 무시됨). | 

다음 필드는 선택 사항입니다.


****  

| Field | 사용 사례: | 
| --- | --- | 
| accept | 요청 본문의 미디어 유형을 지정합니다. 자세한 내용은 [Swagger 웹사이트](https://swagger.io/specification/)의 Media Types를 참조하세요. | 
| contentType | 응답 본문의 미디어 유형을 지정합니다. 자세한 내용은 [Swagger 웹사이트](https://swagger.io/specification/)의 미디어 유형을 참조하세요. | 
| 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를 사용한 Amazon Bedrock 런타임의 코드 예제](service_code_examples_bedrock-runtime.md) 주제에서 예제를 선택합니다.
+ [파운데이션 모델의 추론 요청 파라미터 및 응답 필드](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="w2aac15c32c33c17c19c13b1"></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="w2aac15c32c33c17c19c13b3"></a>

다음 예제에서는 서비스 계층이 있는 OpenAI GPT 모델을 사용하여 텍스트 프롬프트에 대한 텍스트 응답을 생성하여 요청의 우선순위를 지정합니다. 원하는 방법의 탭을 선택한 후 다음 단계를 따릅니다.

------
#### [ 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="w2aac15c32c33c17c19c13b5"></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="w2aac15c32c33c17c19c13b9"></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="w2aac15c32c33c17c19c13c11"></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="w2aac15c32c33c17c19c13c13"></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="w2aac15c32c33c17c19c13c15"></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="w2aac15c32c33c17c19c13c17"></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단어 에세이 쓰기* 프롬프트를 사용하여 Python으로 스트리밍 텍스트를 생성하는 [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) API의 사용 방법을 보여줍니다.

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