Amazon Bedrock Runtime examples using SDK for Java 2.x - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon Bedrock Runtime examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for Java 2.x with Amazon Bedrock Runtime.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

AI21 Labs Jurassic-2

The following code example shows how to send a text message to AI21 Labs Jurassic-2models, using the Invoke Model API.

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 Amazon Code Examples Repository.

Asynchronously use the Invoke Model API to send a text message.

/** * Asynchronously invokes the AI21 Labs Jurassic-2 model to run an inference * based on the provided input. * * @param prompt The prompt that you want Jurassic to complete. * @return The inference response generated by the model. */ public static String invokeJurassic2(String prompt) { /* * The different model providers have individual request and response formats. * For the format, ranges, and default values for Anthropic Claude, refer to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html */ String jurassic2ModelId = "ai21.j2-mid-v1"; BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); String payload = new JSONObject() .put("prompt", prompt) .put("temperature", 0.5) .put("maxTokens", 200) .toString(); InvokeModelRequest request = InvokeModelRequest.builder() .body(SdkBytes.fromUtf8String(payload)) .modelId(jurassic2ModelId) .contentType("application/json") .accept("application/json") .build(); CompletableFuture<InvokeModelResponse> completableFuture = client.invokeModel(request) .whenComplete((response, exception) -> { if (exception != null) { System.out.println("Model invocation failed: " + exception); } }); String generatedText = ""; try { InvokeModelResponse response = completableFuture.get(); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); generatedText = responseBody .getJSONArray("completions") .getJSONObject(0) .getJSONObject("data") .getString("text"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.err.println(e.getMessage()); } catch (ExecutionException e) { System.err.println(e.getMessage()); } return generatedText; }

Use the Invoke Model API to send a text message.

/** * Invokes the AI21 Labs Jurassic-2 model to run an inference based on the * provided input. * * @param prompt The prompt for Jurassic to complete. * @return The generated response. */ public static String invokeJurassic2(String prompt) { /* * The different model providers have individual request and response formats. * For the format, ranges, and default values for AI21 Labs Jurassic-2, refer * to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html */ String jurassic2ModelId = "ai21.j2-mid-v1"; BedrockRuntimeClient client = BedrockRuntimeClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); String payload = new JSONObject() .put("prompt", prompt) .put("temperature", 0.5) .put("maxTokens", 200) .toString(); InvokeModelRequest request = InvokeModelRequest.builder() .body(SdkBytes.fromUtf8String(payload)) .modelId(jurassic2ModelId) .contentType("application/json") .accept("application/json") .build(); InvokeModelResponse response = client.invokeModel(request); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); String generatedText = responseBody .getJSONArray("completions") .getJSONObject(0) .getJSONObject("data") .getString("text"); return generatedText; }
  • For API details, see InvokeModel in Amazon SDK for Java 2.x API Reference.

Amazon Titan Image Generator

The following code example shows how to invoke Amazon Titan Image G1 on Amazon Bedrock to generate an image.

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 Amazon Code Examples Repository.

Asynchronously invoke the Amazon Titan Image Generator G1 model to generate images.

/** * Invokes the Amazon Titan image generation model to create an image using the * input * provided in the request body. * * @param prompt The prompt that you want Amazon Titan to use for image * generation. * @param seed The random noise seed for image generation (Range: 0 to * 2147483647). * @return A Base64-encoded string representing the generated image. */ public static String invokeTitanImage(String prompt, long seed) { /* * The different model providers have individual request and response formats. * For the format, ranges, and default values for Titan Image models refer to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan- * image.html */ String titanImageModelId = "amazon.titan-image-generator-v1"; BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); var textToImageParams = new JSONObject().put("text", prompt); var imageGenerationConfig = new JSONObject() .put("numberOfImages", 1) .put("quality", "standard") .put("cfgScale", 8.0) .put("height", 512) .put("width", 512) .put("seed", seed); JSONObject payload = new JSONObject() .put("taskType", "TEXT_IMAGE") .put("textToImageParams", textToImageParams) .put("imageGenerationConfig", imageGenerationConfig); InvokeModelRequest request = InvokeModelRequest.builder() .body(SdkBytes.fromUtf8String(payload.toString())) .modelId(titanImageModelId) .contentType("application/json") .accept("application/json") .build(); CompletableFuture<InvokeModelResponse> completableFuture = client.invokeModel(request) .whenComplete((response, exception) -> { if (exception != null) { System.out.println("Model invocation failed: " + exception); } }); String base64ImageData = ""; try { InvokeModelResponse response = completableFuture.get(); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); base64ImageData = responseBody .getJSONArray("images") .getString(0); } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.err.println(e.getMessage()); } catch (ExecutionException e) { System.err.println(e.getMessage()); } return base64ImageData; }

Invoke the Amazon Titan Image Generator G1 model to generate images.

/** * Invokes the Amazon Titan image generation model to create an image using the * input * provided in the request body. * * @param prompt The prompt that you want Amazon Titan to use for image * generation. * @param seed The random noise seed for image generation (Range: 0 to * 2147483647). * @return A Base64-encoded string representing the generated image. */ public static String invokeTitanImage(String prompt, long seed) { /* * The different model providers have individual request and response formats. * For the format, ranges, and default values for Titan Image models refer to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan- * image.html */ String titanImageModelId = "amazon.titan-image-generator-v1"; BedrockRuntimeClient client = BedrockRuntimeClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); var textToImageParams = new JSONObject().put("text", prompt); var imageGenerationConfig = new JSONObject() .put("numberOfImages", 1) .put("quality", "standard") .put("cfgScale", 8.0) .put("height", 512) .put("width", 512) .put("seed", seed); JSONObject payload = new JSONObject() .put("taskType", "TEXT_IMAGE") .put("textToImageParams", textToImageParams) .put("imageGenerationConfig", imageGenerationConfig); InvokeModelRequest request = InvokeModelRequest.builder() .body(SdkBytes.fromUtf8String(payload.toString())) .modelId(titanImageModelId) .contentType("application/json") .accept("application/json") .build(); InvokeModelResponse response = client.invokeModel(request); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); String base64ImageData = responseBody .getJSONArray("images") .getString(0); return base64ImageData; }
  • For API details, see InvokeModel in Amazon SDK for Java 2.x API Reference.

Amazon Titan Text

The following code example shows how to send a text message to Amazon Titan Text models, using the Invoke Model API.

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 Amazon Code Examples Repository.

Send your first prompt to Amazon Titan Text.

// Send a prompt to Amazon Titan Text and print the response. public class TextQuickstart { public static void main(String[] args) { // Create a Bedrock Runtime client in the AWS Region of your choice. var client = BedrockRuntimeClient.builder() .region(Region.US_EAST_1) .build(); // You can replace the modelId with any other Titan Text Model. All current model IDs // are documented at https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html var modelId = "amazon.titan-text-premier-v1:0"; // Define the prompt to send. var prompt = "Describe the purpose of a 'hello world' program in one line."; // Create a JSON payload using the model's native structure. var nativeRequest = new JSONObject().put("inputText", prompt); // Encode and send the request. var response = client.invokeModel(req -> req .body(SdkBytes.fromUtf8String(nativeRequest.toString())) .modelId(modelId)); // Decode the response body. var responseBody = new JSONObject(response.body().asUtf8String()); // Extract and print the response text. var responseText = responseBody.getJSONArray("results").getJSONObject(0).getString("outputText"); System.out.println(responseText); } }

Invoke Titan Text with a system prompt and additional inference parameters.

/** * Invoke Titan Text with a system prompt and additional inference parameters, * using Titan's native request/response structure. * * @param userPrompt - The text prompt to send to the model. * @param systemPrompt - A system prompt to provide additional context and instructions. * @return The {@link JSONObject} representing the model's response. */ public static JSONObject invokeWithSystemPrompt(String userPrompt, String systemPrompt) { // Create a Bedrock Runtime client in the AWS Region of your choice. var client = BedrockRuntimeClient.builder() .region(Region.US_EAST_1) .build(); // Set the model ID, e.g., Titan Text Premier. var modelId = "amazon.titan-text-premier-v1:0"; /* Assemble the input text. * For best results, use the following input text format: * {{ system instruction }} * User: {{ user input }} * Bot: */ var inputText = """ %s User: %s Bot: """.formatted(systemPrompt, userPrompt); // Format the request payload using the model's native structure. var nativeRequest = new JSONObject() .put("inputText", inputText) .put("textGenerationConfig", new JSONObject() .put("maxTokenCount", 512) .put("temperature", 0.7F) .put("topP", 0.9F) ) .toString(); // Encode and send the request. var response = client.invokeModel(request -> { request.body(SdkBytes.fromUtf8String(nativeRequest)); request.modelId(modelId); }); // Decode the native response body. var nativeResponse = new JSONObject(response.body().asUtf8String()); // Extract and print the response text. var responseText = nativeResponse.getJSONArray("results").getJSONObject(0).getString("outputText"); System.out.println(responseText); // Return the model's native response. return nativeResponse; }

Create a chat-like experience with Titan Text, using a conversation history.

/** * Create a chat-like experience with a conversation history, using Titan's native * request/response structure. * * @param prompt - The text prompt to send to the model. * @param conversation - A String representing previous conversational turns in the format * User: {{ previous user prompt}} * Bot: {{ previous model response }} * ... * @return The {@link JSONObject} representing the model's response. */ public static JSONObject invokeWithConversation(String prompt, String conversation) { // Create a Bedrock Runtime client in the AWS Region of your choice. var client = BedrockRuntimeClient.builder() .region(Region.US_EAST_1) .build(); // Set the model ID, e.g., Titan Text Premier. var modelId = "amazon.titan-text-premier-v1:0"; /* Append the new prompt to the conversation. * For best results, use the following text format: * User: {{ previous user prompt}} * Bot: {{ previous model response }} * User: {{ new user prompt }} * Bot: """ */ conversation = conversation + """ %nUser: %s Bot: """.formatted(prompt); // Format the request payload using the model's native structure. var nativeRequest = new JSONObject().put("inputText", conversation); // Encode and send the request. var response = client.invokeModel(request -> { request.body(SdkBytes.fromUtf8String(nativeRequest.toString())); request.modelId(modelId); }); // Decode the native response body. var nativeResponse = new JSONObject(response.body().asUtf8String()); // Extract and print the response text. var responseText = nativeResponse.getJSONArray("results").getJSONObject(0).getString("outputText"); System.out.println(responseText); // Return the model's native response. return nativeResponse; }
  • For API details, see InvokeModel in Amazon SDK for Java 2.x API Reference.

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 Amazon Code Examples Repository.

Create your first embedding with Titan Text Embeddings V2.

// Generate and print an embedding with Amazon Titan Text Embeddings. public class TextEmbeddingsQuickstart { public static void main(String[] args) { // 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 Text Embeddings V2. var modelId = "amazon.titan-embed-text-v2:0"; // The text to convert into an embedding. var inputText = "Please recommend books with a theme similar to the movie 'Inception'."; // Create a JSON payload using the model's native structure. var request = new JSONObject().put("inputText", inputText); // Encode and send the request. var response = client.invokeModel(req -> req .body(SdkBytes.fromUtf8String(request.toString())) .modelId(modelId)); // Decode the model's native response body. var nativeResponse = new JSONObject(response.body().asUtf8String()); // Extract and print the generated embedding. var embedding = nativeResponse.getJSONArray("embedding"); System.out.println(embedding); } }

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 in Amazon SDK for Java 2.x API Reference.

Anthropic Claude

The following code example shows how to send a text message to Anthropic Claude models, using the Invoke Model API.

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 Amazon Code Examples Repository.

Invoke Claude 2.x using the synchronous client (scroll down for an async example).

/** * Invokes the Anthropic Claude 2 model to run an inference based on the * provided input. * * @param prompt The prompt for Claude to complete. * @return The generated response. */ public static String invokeClaude(String prompt) { /* * The different model providers have individual request and response formats. * For the format, ranges, and default values for Anthropic Claude, refer to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html */ String claudeModelId = "anthropic.claude-v2"; // Claude requires you to enclose the prompt as follows: String enclosedPrompt = "Human: " + prompt + "\n\nAssistant:"; BedrockRuntimeClient client = BedrockRuntimeClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); String payload = new JSONObject() .put("prompt", enclosedPrompt) .put("max_tokens_to_sample", 200) .put("temperature", 0.5) .put("stop_sequences", List.of("\n\nHuman:")) .toString(); InvokeModelRequest request = InvokeModelRequest.builder() .body(SdkBytes.fromUtf8String(payload)) .modelId(claudeModelId) .contentType("application/json") .accept("application/json") .build(); InvokeModelResponse response = client.invokeModel(request); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); String generatedText = responseBody.getString("completion"); return generatedText; }

Invoke Claude 2.x using the asynchronous client.

/** * Asynchronously invokes the Anthropic Claude 2 model to run an inference based * on the provided input. * * @param prompt The prompt that you want Claude to complete. * @return The inference response from the model. */ public static String invokeClaude(String prompt) { /* * The different model providers have individual request and response formats. * For the format, ranges, and default values for Anthropic Claude, refer to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html */ String claudeModelId = "anthropic.claude-v2"; // Claude requires you to enclose the prompt as follows: String enclosedPrompt = "Human: " + prompt + "\n\nAssistant:"; BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); String payload = new JSONObject() .put("prompt", enclosedPrompt) .put("max_tokens_to_sample", 200) .put("temperature", 0.5) .put("stop_sequences", List.of("\n\nHuman:")) .toString(); InvokeModelRequest request = InvokeModelRequest.builder() .body(SdkBytes.fromUtf8String(payload)) .modelId(claudeModelId) .contentType("application/json") .accept("application/json") .build(); CompletableFuture<InvokeModelResponse> completableFuture = client.invokeModel(request) .whenComplete((response, exception) -> { if (exception != null) { System.out.println("Model invocation failed: " + exception); } }); String generatedText = ""; try { InvokeModelResponse response = completableFuture.get(); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); generatedText = responseBody.getString("completion"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.err.println(e.getMessage()); } catch (ExecutionException e) { System.err.println(e.getMessage()); } return generatedText; }
  • For API details, see InvokeModel in Amazon SDK for Java 2.x API Reference.

The following code example shows how to send a text message to Anthropic Claude models, using the Invoke Model API, and print the response stream.

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 Amazon Code Examples Repository.

Use the Invoke Model API to send a text message and print the response stream.

/** * Invokes Anthropic Claude 2 via the Messages API and processes the response stream. * <p> * To learn more about the Anthropic Messages API, go to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html * * @param prompt The prompt for the model to complete. * @return A JSON object containing the complete response along with some metadata. */ public static JSONObject invokeMessagesApiWithResponseStream(String prompt) { BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder() .credentialsProvider(ProfileCredentialsProvider.create()) .region(Region.US_EAST_1) .build(); String modelId = "anthropic.claude-v2"; // Prepare the JSON payload for the Messages API request var payload = new JSONObject() .put("anthropic_version", "bedrock-2023-05-31") .put("max_tokens", 1000) .append("messages", new JSONObject() .put("role", "user") .append("content", new JSONObject() .put("type", "text") .put("text", prompt) )); // Create the request object using the payload and the model ID var request = InvokeModelWithResponseStreamRequest.builder() .contentType("application/json") .body(SdkBytes.fromUtf8String(payload.toString())) .modelId(modelId) .build(); // Create a handler to print the stream in real-time and add metadata to a response object JSONObject structuredResponse = new JSONObject(); var handler = createMessagesApiResponseStreamHandler(structuredResponse); // Invoke the model with the request payload and the response stream handler client.invokeModelWithResponseStream(request, handler).join(); return structuredResponse; } private static InvokeModelWithResponseStreamResponseHandler createMessagesApiResponseStreamHandler(JSONObject structuredResponse) { AtomicReference<String> completeMessage = new AtomicReference<>(""); Consumer<ResponseStream> responseStreamHandler = event -> event.accept(InvokeModelWithResponseStreamResponseHandler.Visitor.builder() .onChunk(c -> { // Decode the chunk var chunk = new JSONObject(c.bytes().asUtf8String()); // The Messages API returns different types: var chunkType = chunk.getString("type"); if ("message_start".equals(chunkType)) { // The first chunk contains information about the message role String role = chunk.optJSONObject("message").optString("role"); structuredResponse.put("role", role); } else if ("content_block_delta".equals(chunkType)) { // These chunks contain the text fragments var text = chunk.optJSONObject("delta").optString("text"); // Print the text fragment to the console ... System.out.print(text); // ... and append it to the complete message completeMessage.getAndUpdate(current -> current + text); } else if ("message_delta".equals(chunkType)) { // This chunk contains the stop reason var stopReason = chunk.optJSONObject("delta").optString("stop_reason"); structuredResponse.put("stop_reason", stopReason); } else if ("message_stop".equals(chunkType)) { // The last chunk contains the metrics JSONObject metrics = chunk.optJSONObject("amazon-bedrock-invocationMetrics"); structuredResponse.put("metrics", new JSONObject() .put("inputTokenCount", metrics.optString("inputTokenCount")) .put("outputTokenCount", metrics.optString("outputTokenCount")) .put("firstByteLatency", metrics.optString("firstByteLatency")) .put("invocationLatency", metrics.optString("invocationLatency"))); } }) .build()); return InvokeModelWithResponseStreamResponseHandler.builder() .onEventStream(stream -> stream.subscribe(responseStreamHandler)) .onComplete(() -> // Add the complete message to the response object structuredResponse.append("content", new JSONObject() .put("type", "text") .put("text", completeMessage.get()))) .build(); }

Meta Llama

The following code example shows how to send a text message to Meta Llama 2, using the Invoke Model API.

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 Amazon Code Examples Repository.

Use the Invoke Model API to send a text message.

// Send a prompt to Meta Llama 2 and print the response. public class InvokeModelQuickstart { public static void main(String[] args) { // 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., Llama 2 Chat 13B. var modelId = "meta.llama2-13b-chat-v1"; // Define the user message to send. var userMessage = "Describe the purpose of a 'hello world' program in one line."; // Embed the message in Llama 2's prompt format. var prompt = "<s>[INST] " + userMessage + " [/INST]"; // Create a JSON payload using the model's native structure. var request = new JSONObject() .put("prompt", prompt) // Optional inference parameters: .put("max_gen_len", 512) .put("temperature", 0.5F) .put("top_p", 0.9F); // Encode and send the request. var response = client.invokeModel(req -> req .body(SdkBytes.fromUtf8String(request.toString())) .modelId(modelId)); // Decode the native response body. var nativeResponse = new JSONObject(response.body().asUtf8String()); // Extract and print the response text. var responseText = nativeResponse.getString("generation"); System.out.println(responseText); } } // Learn more about the Llama 2 prompt format at: // https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-2
  • For API details, see InvokeModel in Amazon SDK for Java 2.x API Reference.

The following code example shows how to send a text message to Meta Llama 2, using the Invoke Model API, and print the response stream.

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 Amazon Code Examples Repository.

Send your first prompt to Meta Llama 3.

// Send a prompt to Meta Llama 2 and print the response stream in real-time. public class InvokeModelWithResponseStreamQuickstart { public static void main(String[] args) { // Create a Bedrock Runtime client in the AWS Region of your choice. var client = BedrockRuntimeAsyncClient.builder() .region(Region.US_WEST_2) .build(); // Set the model ID, e.g., Llama 2 Chat 13B. var modelId = "meta.llama2-13b-chat-v1"; // Define the user message to send. var userMessage = "Describe the purpose of a 'hello world' program in one line."; // Embed the message in Llama 2's prompt format. var prompt = "<s>[INST] " + userMessage + " [/INST]"; // Create a JSON payload using the model's native structure. var request = new JSONObject() .put("prompt", prompt) // Optional inference parameters: .put("max_gen_len", 512) .put("temperature", 0.5F) .put("top_p", 0.9F); // Create a handler to extract and print the response text in real-time. var streamHandler = InvokeModelWithResponseStreamResponseHandler.builder() .subscriber(event -> event.accept( InvokeModelWithResponseStreamResponseHandler.Visitor.builder() .onChunk(c -> { var chunk = new JSONObject(c.bytes().asUtf8String()); if (chunk.has("generation")) { System.out.print(chunk.getString("generation")); } }).build()) ).build(); // Encode and send the request. Let the stream handler process the response. client.invokeModelWithResponseStream(req -> req .body(SdkBytes.fromUtf8String(request.toString())) .modelId(modelId), streamHandler ).join(); } } // Learn more about the Llama 2 prompt format at: // https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-2

The following code example shows how to send a text message to Meta Llama 3, using the Invoke Model API.

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 Amazon Code Examples Repository.

Use the Invoke Model API to send a text message.

// Send a prompt to Meta Llama 3 and print the response. public class InvokeModelQuickstart { public static void main(String[] args) { // 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., Llama 3 8B Instruct. var modelId = "meta.llama3-8b-instruct-v1:0"; // Define the user message to send. var userMessage = "Describe the purpose of a 'hello world' program in one line."; // Embed the message in Llama 3's prompt format. var prompt = MessageFormat.format(""" <|begin_of_text|> <|start_header_id|>user<|end_header_id|> {0} <|eot_id|> <|start_header_id|>assistant<|end_header_id|> """, userMessage); // Create a JSON payload using the model's native structure. var request = new JSONObject() .put("prompt", prompt) // Optional inference parameters: .put("max_gen_len", 512) .put("temperature", 0.5F) .put("top_p", 0.9F); // Encode and send the request. var response = client.invokeModel(req -> req .body(SdkBytes.fromUtf8String(request.toString())) .modelId(modelId)); // Decode the native response body. var nativeResponse = new JSONObject(response.body().asUtf8String()); // Extract and print the response text. var responseText = nativeResponse.getString("generation"); System.out.println(responseText); } } // Learn more about the Llama 3 prompt format at: // https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/#special-tokens-used-with-meta-llama-3
  • For API details, see InvokeModel in Amazon SDK for Java 2.x API Reference.

The following code example shows how to send a text message to Meta Llama 3, using the Invoke Model API, and print the response stream.

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 Amazon Code Examples Repository.

Use the Invoke Model API to send a text message and print the response stream.

// Send a prompt to Meta Llama 3 and print the response stream in real-time. public class InvokeModelWithResponseStreamQuickstart { public static void main(String[] args) { // Create a Bedrock Runtime client in the AWS Region of your choice. var client = BedrockRuntimeAsyncClient.builder() .region(Region.US_WEST_2) .build(); // Set the model ID, e.g., Llama 3 8B Instruct. var modelId = "meta.llama3-8b-instruct-v1:0"; // Define the user message to send. var userMessage = "Describe the purpose of a 'hello world' program in one line."; // Embed the message in Llama 3's prompt format. var prompt = MessageFormat.format(""" <|begin_of_text|> <|start_header_id|>user<|end_header_id|> {0} <|eot_id|> <|start_header_id|>assistant<|end_header_id|> """, userMessage); // Create a JSON payload using the model's native structure. var request = new JSONObject() .put("prompt", prompt) // Optional inference parameters: .put("max_gen_len", 512) .put("temperature", 0.5F) .put("top_p", 0.9F); // Create a handler to extract and print the response text in real-time. var streamHandler = InvokeModelWithResponseStreamResponseHandler.builder() .subscriber(event -> event.accept( InvokeModelWithResponseStreamResponseHandler.Visitor.builder() .onChunk(c -> { var chunk = new JSONObject(c.bytes().asUtf8String()); if (chunk.has("generation")) { System.out.print(chunk.getString("generation")); } }).build()) ).build(); // Encode and send the request. Let the stream handler process the response. client.invokeModelWithResponseStream(req -> req .body(SdkBytes.fromUtf8String(request.toString())) .modelId(modelId), streamHandler ).join(); } } // Learn more about the Llama 3 prompt format at: // https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/#special-tokens-used-with-meta-llama-3

Mistral AI

The following code example shows how to send a text message to Mistral AI models, using the Invoke Model API.

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 Amazon Code Examples Repository.

Asynchronously use the Invoke Model API to send a text message.

/** * Asynchronously invokes the Mistral 7B model to run an inference based on the provided input. * * @param prompt The prompt for Mistral to complete. * @return The generated response. */ public static List<String> invokeMistral7B(String prompt) { BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder() .region(Region.US_WEST_2) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); // Mistral instruct models provide optimal results when // embedding the prompt into the following template: String instruction = "<s>[INST] " + prompt + " [/INST]"; String modelId = "mistral.mistral-7b-instruct-v0:2"; String payload = new JSONObject() .put("prompt", instruction) .put("max_tokens", 200) .put("temperature", 0.5) .toString(); CompletableFuture<InvokeModelResponse> completableFuture = client.invokeModel(request -> request .accept("application/json") .contentType("application/json") .body(SdkBytes.fromUtf8String(payload)) .modelId(modelId)) .whenComplete((response, exception) -> { if (exception != null) { System.out.println("Model invocation failed: " + exception); } }); try { InvokeModelResponse response = completableFuture.get(); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); JSONArray outputs = responseBody.getJSONArray("outputs"); return IntStream.range(0, outputs.length()) .mapToObj(i -> outputs.getJSONObject(i).getString("text")) .toList(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.err.println(e.getMessage()); } catch (ExecutionException e) { System.err.println(e.getMessage()); } return List.of(); }

Use the Invoke Model API to send a text message.

/** * Invokes the Mistral 7B model to run an inference based on the provided input. * * @param prompt The prompt for Mistral to complete. * @return The generated responses. */ public static List<String> invokeMistral7B(String prompt) { BedrockRuntimeClient client = BedrockRuntimeClient.builder() .region(Region.US_WEST_2) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); // Mistral instruct models provide optimal results when // embedding the prompt into the following template: String instruction = "<s>[INST] " + prompt + " [/INST]"; String modelId = "mistral.mistral-7b-instruct-v0:2"; String payload = new JSONObject() .put("prompt", instruction) .put("max_tokens", 200) .put("temperature", 0.5) .toString(); InvokeModelResponse response = client.invokeModel(request -> request .accept("application/json") .contentType("application/json") .body(SdkBytes.fromUtf8String(payload)) .modelId(modelId)); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); JSONArray outputs = responseBody.getJSONArray("outputs"); return IntStream.range(0, outputs.length()) .mapToObj(i -> outputs.getJSONObject(i).getString("text")) .toList(); }
  • For API details, see InvokeModel in Amazon SDK for Java 2.x API Reference.

Scenarios

The following code example shows how to create playgrounds to interact with Amazon Bedrock foundation models through different modalities.

SDK for Java 2.x

The Java Foundation Model (FM) Playground is a Spring Boot sample application that showcases how to use Amazon Bedrock with Java. This example shows how Java developers can use Amazon Bedrock to build generative AI-enabled applications. You can test and interact with Amazon Bedrock foundation models by using the following three playgrounds:

  • A text playground.

  • A chat playground.

  • An image playground.

The example also lists and displays the foundation models you have access to, along with their characteristics. For source code and deployment instructions, see the project in GitHub.

Services used in this example
  • Amazon Bedrock Runtime

The following code example shows how to prepare and send a prompt to a variety of large-language models (LLMs) on Amazon Bedrock

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 Amazon Code Examples Repository.

Invoke multiple foundation models on Amazon Bedrock.

package com.example.bedrockruntime; import software.amazon.awssdk.services.bedrockruntime.model.BedrockRuntimeException; import java.io.FileOutputStream; import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Base64; import java.util.Random; import static com.example.bedrockruntime.InvokeModel.*; /** * Demonstrates the invocation of the following models: * Anthropic Claude 2, AI21 Labs Jurassic-2, Meta Llama 2 Chat, and Stability.ai * Stable Diffusion XL. */ public class BedrockRuntimeUsageDemo { private static final Random random = new Random(); private static final String CLAUDE = "anthropic.claude-v2"; private static final String JURASSIC2 = "ai21.j2-mid-v1"; private static final String MISTRAL7B = "mistral.mistral-7b-instruct-v0:2"; private static final String MIXTRAL8X7B = "mistral.mixtral-8x7b-instruct-v0:1"; private static final String STABLE_DIFFUSION = "stability.stable-diffusion-xl"; private static final String TITAN_IMAGE = "amazon.titan-image-generator-v1"; public static void main(String[] args) { BedrockRuntimeUsageDemo.textToText(); BedrockRuntimeUsageDemo.textToTextWithResponseStream(); BedrockRuntimeUsageDemo.textToImage(); } private static void textToText() { String prompt = "In one sentence, what is a large-language model?"; BedrockRuntimeUsageDemo.invoke(CLAUDE, prompt); BedrockRuntimeUsageDemo.invoke(JURASSIC2, prompt); BedrockRuntimeUsageDemo.invoke(MISTRAL7B, prompt); BedrockRuntimeUsageDemo.invoke(MIXTRAL8X7B, prompt); } private static void invoke(String modelId, String prompt) { invoke(modelId, prompt, null); } private static void invoke(String modelId, String prompt, String stylePreset) { System.out.println("\n" + new String(new char[88]).replace("\0", "-")); System.out.println("Invoking: " + modelId); System.out.println("Prompt: " + prompt); try { switch (modelId) { case CLAUDE: printResponse(invokeClaude(prompt)); break; case JURASSIC2: printResponse(invokeJurassic2(prompt)); break; case MISTRAL7B: for (String response : invokeMistral7B(prompt)) { printResponse(response); } break; case MIXTRAL8X7B: for (String response : invokeMixtral8x7B(prompt)) { printResponse(response); } break; case STABLE_DIFFUSION: createImage(STABLE_DIFFUSION, prompt, random.nextLong() & 0xFFFFFFFFL, stylePreset); break; case TITAN_IMAGE: createImage(TITAN_IMAGE, prompt, random.nextLong() & 0xFFFFFFFL); break; default: throw new IllegalStateException("Unexpected value: " + modelId); } } catch (BedrockRuntimeException e) { System.out.println("Couldn't invoke model " + modelId + ": " + e.getMessage()); throw e; } } private static void createImage(String modelId, String prompt, long seed) { createImage(modelId, prompt, seed, null); } private static void createImage(String modelId, String prompt, long seed, String stylePreset) { String base64ImageData = (modelId.equals(STABLE_DIFFUSION)) ? invokeStableDiffusion(prompt, seed, stylePreset) : invokeTitanImage(prompt, seed); String imagePath = saveImage(modelId, base64ImageData); System.out.printf("Success: The generated image has been saved to %s%n", imagePath); } private static void textToTextWithResponseStream() { String prompt = "What is a large-language model?"; BedrockRuntimeUsageDemo.invokeWithResponseStream(CLAUDE, prompt); } private static void invokeWithResponseStream(String modelId, String prompt) { System.out.println(new String(new char[88]).replace("\0", "-")); System.out.printf("Invoking %s with response stream%n", modelId); System.out.println("Prompt: " + prompt); try { Claude2.invokeMessagesApiWithResponseStream(prompt); } catch (BedrockRuntimeException e) { System.out.println("Couldn't invoke model " + modelId + ": " + e.getMessage()); throw e; } } private static void textToImage() { String imagePrompt = "stylized picture of a cute old steampunk robot"; String stylePreset = "photographic"; BedrockRuntimeUsageDemo.invoke(STABLE_DIFFUSION, imagePrompt, stylePreset); BedrockRuntimeUsageDemo.invoke(TITAN_IMAGE, imagePrompt); } private static void printResponse(String response) { System.out.printf("Generated text: %s%n", response); } private static String saveImage(String modelId, String base64ImageData) { try { String directory = "output"; URI uri = InvokeModel.class.getProtectionDomain().getCodeSource().getLocation().toURI(); Path outputPath = Paths.get(uri).getParent().getParent().resolve(directory); if (!Files.exists(outputPath)) { Files.createDirectories(outputPath); } int i = 1; String fileName; do { fileName = String.format("%s_%d.png", modelId, i); i++; } while (Files.exists(outputPath.resolve(fileName))); byte[] imageBytes = Base64.getDecoder().decode(base64ImageData); Path filePath = outputPath.resolve(fileName); try (FileOutputStream fileOutputStream = new FileOutputStream(filePath.toFile())) { fileOutputStream.write(imageBytes); } return filePath.toString(); } catch (Exception e) { System.out.println(e.getMessage()); System.exit(1); } return null; } }

Stability AI Diffusion

The following code example shows how to invoke Stability.ai Stable Diffusion XL on Amazon Bedrock to generate an image.

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 Amazon Code Examples Repository.

Asynchronously invoke the Stability.ai Stable Diffusion XL foundation model to generate images.

/** * Asynchronously invokes the Stability.ai Stable Diffusion XL model to create * an image based on the provided input. * * @param prompt The prompt that guides the Stable Diffusion model. * @param seed The random noise seed for image generation (use 0 or omit * for a random seed). * @param stylePreset The style preset to guide the image model towards a * specific style. * @return A Base64-encoded string representing the generated image. */ public static String invokeStableDiffusion(String prompt, long seed, String stylePreset) { /* * The different model providers have individual request and response formats. * For the format, ranges, and available style_presets of Stable Diffusion * models refer to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html */ String stableDiffusionModelId = "stability.stable-diffusion-xl-v1"; BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); JSONArray wrappedPrompt = new JSONArray().put(new JSONObject().put("text", prompt)); JSONObject payload = new JSONObject() .put("text_prompts", wrappedPrompt) .put("seed", seed); if (stylePreset != null && !stylePreset.isEmpty()) { payload.put("style_preset", stylePreset); } InvokeModelRequest request = InvokeModelRequest.builder() .body(SdkBytes.fromUtf8String(payload.toString())) .modelId(stableDiffusionModelId) .contentType("application/json") .accept("application/json") .build(); CompletableFuture<InvokeModelResponse> completableFuture = client.invokeModel(request) .whenComplete((response, exception) -> { if (exception != null) { System.out.println("Model invocation failed: " + exception); } }); String base64ImageData = ""; try { InvokeModelResponse response = completableFuture.get(); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); base64ImageData = responseBody .getJSONArray("artifacts") .getJSONObject(0) .getString("base64"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.err.println(e.getMessage()); } catch (ExecutionException e) { System.err.println(e.getMessage()); } return base64ImageData; }

Invoke the Stability.ai Stable Diffusion XL foundation model to generate images.

/** * Invokes the Stability.ai Stable Diffusion XL model to create an image based * on the provided input. * * @param prompt The prompt that guides the Stable Diffusion model. * @param seed The random noise seed for image generation (use 0 or omit * for a random seed). * @param stylePreset The style preset to guide the image model towards a * specific style. * @return A Base64-encoded string representing the generated image. */ public static String invokeStableDiffusion(String prompt, long seed, String stylePreset) { /* * The different model providers have individual request and response formats. * For the format, ranges, and available style_presets of Stable Diffusion * models refer to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html */ String stableDiffusionModelId = "stability.stable-diffusion-xl"; BedrockRuntimeClient client = BedrockRuntimeClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); JSONArray wrappedPrompt = new JSONArray().put(new JSONObject().put("text", prompt)); JSONObject payload = new JSONObject() .put("text_prompts", wrappedPrompt) .put("seed", seed); if (!(stylePreset == null || stylePreset.isEmpty())) { payload.put("style_preset", stylePreset); } InvokeModelRequest request = InvokeModelRequest.builder() .body(SdkBytes.fromUtf8String(payload.toString())) .modelId(stableDiffusionModelId) .contentType("application/json") .accept("application/json") .build(); InvokeModelResponse response = client.invokeModel(request); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); String base64ImageData = responseBody .getJSONArray("artifacts") .getJSONObject(0) .getString("base64"); return base64ImageData; }
  • For API details, see InvokeModel in Amazon SDK for Java 2.x API Reference.