Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 AWS
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
SDK for Kotlin を使用した Amazon Bedrock ランタイムの例
次のコード例は、Amazon Bedrock ランタイムで AWS SDK for Kotlin を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。
各例には完全なソースコードへのリンクが含まれており、コードの設定方法と実行方法に関する手順を確認できます。
Amazon Titan Text
次のコード例は、Invoke Model 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 の詳細については、 AWS SDK for Kotlin API InvokeModel
」を参照してください。
-