

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. [AWS](https://github.com/awsdocs/aws-doc-sdk-examples) 

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

# Amazon Bedrock 런타임용 Amazon Titan Text
<a name="bedrock-runtime_code_examples_amazon_titan_text"></a>

다음 코드 예제에서는 Amazon Bedrock 런타임을 AWS SDKs 함께 사용하는 방법을 보여줍니다.

**Topics**
+ [InvokeModel](bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section.md)

# Invoke Model API를 사용하여 Amazon Bedrock에서 Amazon Titan Text 모델 간접 호출
<a name="bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section"></a>

다음 코드 예제에서는 Invoke Model API를 사용하여 Amazon Titan Text에 텍스트 메시지를 보내는 방법을 보여줍니다.

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

**SDK for Python(Boto3)**  
 GitHub에 더 많은 내용이 있습니다. [AWS 코드 예 리포지토리](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/bedrock-runtime#code-examples)에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.
Invoke Model API를 사용하여 텍스트 메시지를 보냅니다.  

```
# 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)
```
+  API 세부 정보는 *AWS SDK for Python (Boto3) API 참조*의 [InvokeModel](https://docs.aws.amazon.com/goto/boto3/bedrock-runtime-2023-09-30/InvokeModel)를 참조하세요.

------