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
-