AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-03-23 · Documentation low

File: code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section.md

Summary

Updated code example to use standardized model invocation pattern, added error handling, simplified prompt, and changed model parameters. Added JSON parsing structure and updated documentation comments.

Security assessment

Changes focus on code structure, error handling, and example simplification. No security vulnerabilities or security features are mentioned. Region change and parameter adjustments are operational rather than security-related.

Diff

diff --git a/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section.md b/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section.md
index 98902534e..690dea292 100644
--- a/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section.md
+++ b/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section.md
@@ -377,7 +377,6 @@ Use the Invoke Model API to generate a short story.
-     * 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
+     * This example demonstrates how to use the Amazon Titan foundation models to generate text.
+     * It shows how to:
+     * - Set up the Amazon Bedrock runtime client
+     * - Create a request payload
+     * - Configure and send a request
+     * - Process the response
@@ -386,4 +385,2 @@ Use the Invoke Model API to generate a short story.
-        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()
+        invokeModel().also { println(it) }
+    }
@@ -391,2 +388,7 @@ Use the Invoke Model API to generate a short story.
-        val response = invokeModel(prompt, "amazon.titan-text-lite-v1")
-        println("Generated story:\n$response")
+    // Data class for parsing the model's response
+    @Serializable
+    private data class BedrockResponse(val results: List<Result>) {
+        @Serializable
+        data class Result(
+            val outputText: String,
+        )
@@ -395,7 +397,16 @@ Use the Invoke Model API to generate a short story.
-    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 = """
+    // Initialize JSON parser with relaxed configuration
+    private val json = Json { ignoreUnknownKeys = true }
+    
+    suspend fun invokeModel(): String {
+        // Create and configure the Bedrock runtime client
+        BedrockRuntimeClient { region = "us-east-1" }.use { client ->
+    
+            // Specify the model ID. For the latest available models, see:
+            // https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
+            val modelId = "amazon.titan-text-lite-v1"
+    
+            // Create the request payload with optional configuration parameters
+            // For detailed parameter descriptions, see:
+            // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-text.html
+            val prompt = "Describe the purpose of a 'hello world' program in one line."
+            val request = """
@@ -403 +414 @@ Use the Invoke Model API to generate a short story.
-                        "inputText": "${prompt.replace(Regex("\\s+"), " ").trim()}",
+                    "inputText": "$prompt",
@@ -405,4 +416,2 @@ Use the Invoke Model API to generate a short story.
-                            "maxTokenCount": 1000,
-                            "stopSequences": [],
-                            "temperature": 1,
-                            "topP": 0.7
+                        "maxTokenCount": 500,
+                        "temperature": 0.5
@@ -411,2 +420,11 @@ Use the Invoke Model API to generate a short story.
-                """.trimIndent().toByteArray()
-            }
+            """.trimIndent()
+    
+            // Send the request and process the model's response
+            runCatching {
+                // Send the request to the model
+                val response = client.invokeModel(
+                    InvokeModelRequest {
+                        this.modelId = modelId
+                        body = request.toByteArray()
+                    },
+                )
@@ -414,2 +432,2 @@ Use the Invoke Model API to generate a short story.
-            val response = client.invokeModel(request)
-            val responseBody = response.body.toString(Charsets.UTF_8)
+                // Convert the response bytes to a JSON string
+                val jsonResponse = response.body.toString(Charsets.UTF_8)
@@ -417,6 +435,11 @@ Use the Invoke Model API to generate a short story.
-            val jsonParser = Json { ignoreUnknownKeys = true }
-            return jsonParser
-                .decodeFromString<BedrockResponse>(responseBody)
-                .results
-                .first()
-                .outputText
+                // Parse the JSON into a Kotlin object
+                val parsedResponse = json.decodeFromString<BedrockResponse>(jsonResponse)
+    
+                // Extract and return the generated text
+                return parsedResponse.results.firstOrNull()!!.outputText
+            }.getOrElse { error ->
+                error.message?.let { msg ->
+                    System.err.println("ERROR: Can't invoke '$modelId'. Reason: $msg")
+                }
+                throw RuntimeException("Failed to generate text with model $modelId", error)
+            }
@@ -426,5 +448,0 @@ Use the Invoke Model API to generate a short story.
-    @Serializable
-    private data class BedrockResponse(val results: List<Result>)
-    
-    @Serializable
-    private data class Result(val outputText: String)