AWS bedrock documentation change
Summary
Updated code example for Titan Text model invocation with simplified prompt, error handling, response parsing, and parameter adjustments
Security assessment
Changes focus on code example restructuring and error handling improvements without mentioning security controls or vulnerabilities
Diff
diff --git a/bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section.md b/bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section.md index b5c672c1d..ae9ae9975 100644 --- a/bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section.md +++ b/bedrock/latest/userguide/bedrock-runtime_example_bedrock-runtime_InvokeModel_TitanText_section.md @@ -375,7 +375,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 @@ -384,4 +383,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) } + } @@ -389,2 +386,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, + ) @@ -393,7 +395,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 = """ @@ -401 +412 @@ Use the Invoke Model API to generate a short story. - "inputText": "${prompt.replace(Regex("\\s+"), " ").trim()}", + "inputText": "$prompt", @@ -403,4 +414,2 @@ Use the Invoke Model API to generate a short story. - "maxTokenCount": 1000, - "stopSequences": [], - "temperature": 1, - "topP": 0.7 + "maxTokenCount": 500, + "temperature": 0.5 @@ -409,2 +418,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() + }, + ) @@ -412,2 +430,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) @@ -415,6 +433,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) + } @@ -424,5 +446,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)