쿠키 기본 설정 선택

당사는 사이트와 서비스를 제공하는 데 필요한 필수 쿠키 및 유사한 도구를 사용합니다. 고객이 사이트를 어떻게 사용하는지 파악하고 개선할 수 있도록 성능 쿠키를 사용해 익명의 통계를 수집합니다. 필수 쿠키는 비활성화할 수 없지만 '사용자 지정' 또는 ‘거부’를 클릭하여 성능 쿠키를 거부할 수 있습니다.

사용자가 동의하는 경우 AWS와 승인된 제3자도 쿠키를 사용하여 유용한 사이트 기능을 제공하고, 사용자의 기본 설정을 기억하고, 관련 광고를 비롯한 관련 콘텐츠를 표시합니다. 필수가 아닌 모든 쿠키를 수락하거나 거부하려면 ‘수락’ 또는 ‘거부’를 클릭하세요. 더 자세한 내용을 선택하려면 ‘사용자 정의’를 클릭하세요.

SDK for Kotlin을 사용한 Amazon Bedrock 런타임 예제 - AWS SDK 코드 예제

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS

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

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS

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

SDK for Kotlin을 사용한 Amazon Bedrock 런타임 예제

다음 코드 예제에서는 Amazon Bedrock 런타임과 함께 AWS SDK for Kotlin을 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

각 예시에는 전체 소스 코드에 대한 링크가 포함되어 있으며, 여기에서 컨텍스트에 맞춰 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있습니다.

Amazon Titan Text

다음 코드 예제에서는 모델 간접 호출 API를 사용하여 Amazon Titan Text로 텍스트 메시지를 보내는 방법을 보여줍니다.

SDK for Kotlin
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

모델 호출 API를 사용하여 짧은 스토리를 생성합니다.

import aws.sdk.kotlin.services.bedrockruntime.BedrockRuntimeClient import aws.sdk.kotlin.services.bedrockruntime.model.InvokeModelRequest import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json /** * Before running this Kotlin code example, set up your development environment, including your credentials. * * This example demonstrates how to invoke the Titan Text model (amazon.titan-text-lite-v1). * Remember that you must enable the model before you can use it. See notes in the README.md file. * * For more information, see the following documentation topic: * https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html */ suspend fun main() { val prompt = """ Write a short, funny story about a time-traveling cat who ends up in ancient Egypt at the time of the pyramids. """.trimIndent() val response = invokeModel(prompt, "amazon.titan-text-lite-v1") println("Generated story:\n$response") } suspend fun invokeModel(prompt: String, modelId: String): String { BedrockRuntimeClient { region = "eu-central-1" }.use { client -> val request = InvokeModelRequest { this.modelId = modelId contentType = "application/json" accept = "application/json" body = """ { "inputText": "${prompt.replace(Regex("\\s+"), " ").trim()}", "textGenerationConfig": { "maxTokenCount": 1000, "stopSequences": [], "temperature": 1, "topP": 0.7 } } """.trimIndent().toByteArray() } val response = client.invokeModel(request) val responseBody = response.body.toString(Charsets.UTF_8) val jsonParser = Json { ignoreUnknownKeys = true } return jsonParser .decodeFromString<BedrockResponse>(responseBody) .results .first() .outputText } } @Serializable private data class BedrockResponse(val results: List<Result>) @Serializable private data class Result(val outputText: String)
  • API 세부 정보는 SDK for Kotlin API 참조의 InvokeModel을 참조하세요. AWS

다음 코드 예제에서는 모델 간접 호출 API를 사용하여 Amazon Titan Text로 텍스트 메시지를 보내는 방법을 보여줍니다.

SDK for Kotlin
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

모델 호출 API를 사용하여 짧은 스토리를 생성합니다.

import aws.sdk.kotlin.services.bedrockruntime.BedrockRuntimeClient import aws.sdk.kotlin.services.bedrockruntime.model.InvokeModelRequest import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json /** * Before running this Kotlin code example, set up your development environment, including your credentials. * * This example demonstrates how to invoke the Titan Text model (amazon.titan-text-lite-v1). * Remember that you must enable the model before you can use it. See notes in the README.md file. * * For more information, see the following documentation topic: * https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html */ suspend fun main() { val prompt = """ Write a short, funny story about a time-traveling cat who ends up in ancient Egypt at the time of the pyramids. """.trimIndent() val response = invokeModel(prompt, "amazon.titan-text-lite-v1") println("Generated story:\n$response") } suspend fun invokeModel(prompt: String, modelId: String): String { BedrockRuntimeClient { region = "eu-central-1" }.use { client -> val request = InvokeModelRequest { this.modelId = modelId contentType = "application/json" accept = "application/json" body = """ { "inputText": "${prompt.replace(Regex("\\s+"), " ").trim()}", "textGenerationConfig": { "maxTokenCount": 1000, "stopSequences": [], "temperature": 1, "topP": 0.7 } } """.trimIndent().toByteArray() } val response = client.invokeModel(request) val responseBody = response.body.toString(Charsets.UTF_8) val jsonParser = Json { ignoreUnknownKeys = true } return jsonParser .decodeFromString<BedrockResponse>(responseBody) .results .first() .outputText } } @Serializable private data class BedrockResponse(val results: List<Result>) @Serializable private data class Result(val outputText: String)
  • API 세부 정보는 SDK for Kotlin API 참조의 InvokeModel을 참조하세요. AWS

프라이버시사이트 이용 약관쿠키 기본 설정
© 2025, Amazon Web Services, Inc. 또는 계열사. All rights reserved.