AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-10-10 · Documentation low

File: code-library/latest/ug/java_2_bedrock-runtime_code_examples.md

Summary

Removed Amazon Titan Text examples and replaced with Amazon Titan Text Embeddings V2 documentation. Updated all Anthropic Claude examples to use Claude 3 Haiku model with new request/response formats.

Security assessment

Changes involve model version updates (Titan Text -> Titan Text Embeddings V2, Claude model updates) and API request/response format adjustments. No security vulnerabilities, patching, or security feature additions are mentioned in the diff.

Diff

diff --git a/code-library/latest/ug/java_2_bedrock-runtime_code_examples.md b/code-library/latest/ug/java_2_bedrock-runtime_code_examples.md
index 69c8132d5..b93a7a064 100644
--- a//code-library/latest/ug/java_2_bedrock-runtime_code_examples.md
+++ b//code-library/latest/ug/java_2_bedrock-runtime_code_examples.md
@@ -5 +5 @@
-ScenariosAmazon NovaAmazon Nova CanvasAmazon Titan Image GeneratorAmazon Titan TextAmazon Titan Text EmbeddingsAnthropic ClaudeCohere CommandMeta LlamaMistral AIStable Diffusion
+ScenariosAmazon NovaAmazon Nova CanvasAmazon Titan Image GeneratorAmazon Titan Text EmbeddingsAnthropic ClaudeCohere CommandMeta LlamaMistral AIStable Diffusion
@@ -27,2 +26,0 @@ Each example includes a link to the complete source code, where you can find ins
-  * Amazon Titan Text
-
@@ -1702 +1700,143 @@ Create an image with the Amazon Titan Image Generator.
-## Amazon Titan Text
+## Amazon Titan Text Embeddings
+
+The following code example shows how to:
+
+  * Get started creating your first embedding.
+
+  * Create embeddings configuring the number of dimensions and normalization (V2 only).
+
+
+
+
+**SDK for Java 2.x**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/bedrock-runtime#code-examples). 
+
+Create your first embedding with Titan Text Embeddings V2.
+    
+    
+    // Generate and print an embedding with Amazon Titan Text Embeddings.
+    
+    import org.json.JSONObject;
+    import org.json.JSONPointer;
+    import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
+    import software.amazon.awssdk.core.SdkBytes;
+    import software.amazon.awssdk.core.exception.SdkClientException;
+    import software.amazon.awssdk.regions.Region;
+    import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
+    
+    public class InvokeModel {
+    
+        public static String invokeModel() {
+    
+            // Create a Bedrock Runtime client in the AWS Region you want to use.
+            // Replace the DefaultCredentialsProvider with your preferred credentials provider.
+            var client = BedrockRuntimeClient.builder()
+                    .credentialsProvider(DefaultCredentialsProvider.create())
+                    .region(Region.US_EAST_1)
+                    .build();
+    
+            // Set the model ID, e.g., Titan Text Embeddings V2.
+            var modelId = "amazon.titan-embed-text-v2:0";
+    
+            // The InvokeModel API uses the model's native payload.
+            // Learn more about the available inference parameters and response fields at:
+            // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-embed-text.html
+            var nativeRequestTemplate = "{ \"inputText\": \"{{inputText}}\" }";
+    
+            // The text to convert into an embedding.
+            var inputText = "Please recommend books with a theme similar to the movie 'Inception'.";
+    
+            // Embed the prompt in the model's native request payload.
+            String nativeRequest = nativeRequestTemplate.replace("{{inputText}}", inputText);
+    
+            try {
+                // Encode and send the request to the Bedrock Runtime.
+                var response = client.invokeModel(request -> request
+                        .body(SdkBytes.fromUtf8String(nativeRequest))
+                        .modelId(modelId)
+                );
+    
+                // Decode the response body.
+                var responseBody = new JSONObject(response.body().asUtf8String());
+    
+                // Retrieve the generated text from the model's response.
+                var text = new JSONPointer("/embedding").queryFrom(responseBody).toString();
+                System.out.println(text);
+    
+                return text;
+    
+            } catch (SdkClientException e) {
+                System.err.printf("ERROR: Can't invoke '%s'. Reason: %s", modelId, e.getMessage());
+                throw new RuntimeException(e);
+            }
+        }
+    
+        public static void main(String[] args) {
+            invokeModel();
+        }
+    }
+    
+    
+
+Invoke Titan Text Embeddings V2 configuring the number of dimensions and normalization.
+    
+    
+        /**
+         * Invoke Amazon Titan Text Embeddings V2 with additional inference parameters.
+         *
+         * @param inputText  - The text to convert to an embedding.
+         * @param dimensions - The number of dimensions the output embeddings should have.
+         *                   Values accepted by the model: 256, 512, 1024.
+         * @param normalize  - A flag indicating whether or not to normalize the output embeddings.
+         * @return The {@link JSONObject} representing the model's response.
+         */
+        public static JSONObject invokeModel(String inputText, int dimensions, boolean normalize) {
+    
+            // Create a Bedrock Runtime client in the AWS Region of your choice.
+            var client = BedrockRuntimeClient.builder()
+                    .region(Region.US_WEST_2)
+                    .build();
+    
+            // Set the model ID, e.g., Titan Embed Text v2.0.
+            var modelId = "amazon.titan-embed-text-v2:0";
+    
+            // Create the request for the model.
+            var nativeRequest = """
+                    {
+                        "inputText": "%s",
+                        "dimensions": %d,
+                        "normalize": %b
+                    }
+                    """.formatted(inputText, dimensions, normalize);
+    
+            // Encode and send the request.
+            var response = client.invokeModel(request -> {
+                request.body(SdkBytes.fromUtf8String(nativeRequest));
+                request.modelId(modelId);
+            });
+    
+            // Decode the model's response.
+            var modelResponse = new JSONObject(response.body().asUtf8String());
+    
+            // Extract and print the generated embedding and the input text token count.
+            var embedding = modelResponse.getJSONArray("embedding");
+            var inputTokenCount = modelResponse.getBigInteger("inputTextTokenCount");
+            System.out.println("Embedding: " + embedding);
+            System.out.println("\nInput token count: " + inputTokenCount);
+    
+            // Return the model's native response.
+            return modelResponse;
+        }
+    
+    
+
+  * For API details, see [InvokeModel](https://docs.aws.amazon.com/goto/SdkForJavaV2/bedrock-runtime-2023-09-30/InvokeModel) in _AWS SDK for Java 2.x API Reference_. 
+
+
+
+
+## Anthropic Claude
@@ -1704 +1844 @@ Create an image with the Amazon Titan Image Generator.
-The following code example shows how to send a text message to Amazon Titan Text, using Bedrock's Converse API.
+The following code example shows how to send a text message to Anthropic Claude, using Bedrock's Converse API.
@@ -1713 +1853 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-Send a text message to Amazon Titan Text, using Bedrock's Converse API.
+Send a text message to Anthropic Claude, using Bedrock's Converse API.
@@ -1716 +1856 @@ Send a text message to Amazon Titan Text, using Bedrock's Converse API.
-    // Use the Converse API to send a text message to Amazon Titan Text.
+    // Use the Converse API to send a text message to Anthropic Claude.
@@ -1738,2 +1878,2 @@ Send a text message to Amazon Titan Text, using Bedrock's Converse API.
-            // Set the model ID, e.g., Titan Text Premier.
-            var modelId = "amazon.titan-text-premier-v1:0";
+            // Set the model ID, e.g., Claude 3 Haiku.
+            var modelId = "anthropic.claude-3-haiku-20240307-v1:0";
@@ -1760 +1900 @@ Send a text message to Amazon Titan Text, using Bedrock's Converse API.
-                var responseText = response.output().message().content().get(0).text();
+                var responseText = response.output().message().content().getFirst().text();
@@ -1778,2 +1918 @@ Send a text message to Amazon Titan Text, using Bedrock's Converse API.
-
-Send a text message to Amazon Titan Text, using Bedrock's Converse API with the async Java client.
+Send a text message to Anthropic Claude, using Bedrock's Converse API with the async Java client.
@@ -1782 +1921 @@ Send a text message to Amazon Titan Text, using Bedrock's Converse API with the
-    // Use the Converse API to send a text message to Amazon Titan Text
+    // Use the Converse API to send a text message to Anthropic Claude
@@ -1806,2 +1945,2 @@ Send a text message to Amazon Titan Text, using Bedrock's Converse API with the
-            // Set the model ID, e.g., Titan Text Premier.
-            var modelId = "amazon.titan-text-premier-v1:0";
+            // Set the model ID, e.g., Claude 3 Haiku.
+            var modelId = "anthropic.claude-3-haiku-20240307-v1:0";
@@ -1833 +1972 @@ Send a text message to Amazon Titan Text, using Bedrock's Converse API with the
-                    String responseText = response.output().message().content().get(0).text();
+                    String responseText = response.output().message().content().getFirst().text();
@@ -1865 +2004 @@ Send a text message to Amazon Titan Text, using Bedrock's Converse API with the
-The following code example shows how to send a text message to Amazon Titan Text, using Bedrock's Converse API and process the response stream in real-time.
+The following code example shows how to send a text message to Anthropic Claude, using Bedrock's Converse API and process the response stream in real-time.
@@ -1874 +2013 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-Send a text message to Amazon Titan Text, using Bedrock's Converse API and process the response stream in real-time.
+Send a text message to Anthropic Claude, using Bedrock's Converse API and process the response stream in real-time.
@@ -1877 +2016 @@ Send a text message to Amazon Titan Text, using Bedrock's Converse API and proce
-    // Use the Converse API to send a text message to Amazon Titan Text
+    // Use the Converse API to send a text message to Anthropic Claude
@@ -1901,2 +2040,2 @@ Send a text message to Amazon Titan Text, using Bedrock's Converse API and proce
-            // Set the model ID, e.g., Titan Text Premier.
-            var modelId = "amazon.titan-text-premier-v1:0";
+            // Set the model ID, e.g., Claude 3 Haiku.