
Conversational AI: Add chatting capabilities to your Java app
Discover how to implement multi-turn conversations and maintain context using Amazon Bedrock's Converse API and the AWS SDK for Java.
Series: No Python? No Problem! A Java Developer's Guide to Amazon Bedrock's Converse API (3 articles)
- 2Conversational AI: Add chatting capabilities to your Java app This article
[ Open the JavaScript edition ]
Welcome to Part 2 of my tutorial series on Amazon Bedrock's Converse API!
Today, we'll explore how to implement a conversation using Amazon Bedrock's Converse API. You'll learn how to send messages to an AI model, maintain a conversation history, and handle multi-turn conversations. By the end of this tutorial, you'll have the skills to add chatting capabilities to your own applications.
Note: The examples in this edition use Java. I've also prepared a JavaScript edition , and many more examples in Python, C#, etc.
Series overview
This series guides you through Amazon Bedrock's Converse API:
- In Part 1: Getting Started , you learned how to send your first request.
- In Part 2: Conversational AI (this post), I'll show you how to implement conversational turns.
- In Part 3: Customizing AI Behavior , we'll configure the model with a system prompt and
Future posts will cover extracting invocation metrics and metadata, sending and receiving model-specific parameters and fields, processing model responses in real-time, the new tool-use feature, and more.
Let's dive in and start building! 💻
Step-by-step: Add a conversation history and chat with the AI
Prerequisites
Before you begin, ensure all prerequisites are in place. You should have:
- An AWS account
- The AWS CLI installed and configured with your credentials
- A Java Development Kit (JDK) version 17 or later and a build tool like Apache Maven installed
- Requested access to the model you want to use
Step 1: Set up a new Java project
If you haven't done so already, create a new Java project using Maven and add the Bedrock Runtime and STS dependencies to your
pom.xml file:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...
<properties>
<aws.sdk.version>2.26.9</aws.sdk.version>
</properties>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bedrockruntime</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
</dependencies>
...Note: Replace the
aws.sdk.version with the latest version of the AWS SDK for Java .Step 2: Create an instance of the Bedrock Runtime client
Create an instance of the BedrockRuntimeClient, specifying the AWS region where the model is available:
1
2
3
4
5
6
7
8
9
10
11
12
import ... // You can find all required imports in the complete example below.
public class App {
public static void main(String[] args) {
BedrockRuntimeClient client = BedrockRuntimeClient.builder()
.credentialsProvider(DefaultCredentialsProvider.create())
.region(Region.US_EAST_1)
.build();
// ...
}
}Step 3: Specify the model ID
Specify the ID of the model you want to use.
In this example, we'll use Claude 3 Haiku:
1
String modelId = "anthropic.claude-3-haiku-20240307-v1:0";You can find the complete list of models supporting the Converse API and a list of all available model IDs in the documentation.
Step 4: Send an initial message
Prepare the first user message and add it to a new list, representing the conversation:
1
2
3
4
5
6
ArrayList<Message> conversation = new ArrayList<>();
conversation.add(Message.builder()
.role(ConversationRole.USER)
.content(c -> c.text("What is the capital of Australia?"))
.build());The conversation list represents the conversation history, with each element containing a role (
ConversationRole.USER or ConversationRole.ASSISTANT) and at least one content block with the message text.Send the conversation to the model using the Bedrock Runtime client's
converse() method:1
2
3
ConverseResponse firstResponse = client.converse(
request -> request.modelId(modelId).messages(conversation)
);The converse method sends the conversation to the specified model and returns its response.
Print out the model's response:
1
2
String firstResponseText = firstResponse.output().message().content().get(0).text();
System.out.println("First response: " + firstResponseText);Step 5: Add the model response to the conversation
To maintain context for the next request, add the model's response to the conversation:
1
conversation.add(firstResponse.output().message());Step 6: Send a follow-up message
Prepare a second user message and add it to the conversation:
1
2
3
4
conversation.add(Message.builder()
.content(c -> c.text("What was my first question?"))
.role(ConversationRole.USER)
.build());Send the updated conversation to the model:
1
2
3
ConverseResponse secondResponse = client.converse(
request -> request.modelId(modelId).messages(conversation)
);And finally, print out the model's second response:
1
2
String secondResponseText = secondResponse.output().message().content().get(0).text();
System.out.println("Second response: " + secondResponseText);Let's run the program
With the code complete, let's run it and see the AI engage in a multi-turn conversation!
Here's the full code for reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
import software.amazon.awssdk.services.bedrockruntime.model.ConversationRole;
import software.amazon.awssdk.services.bedrockruntime.model.Message;
import java.util.ArrayList;
public class BedrockConversation {
public static void main(String[] args) {
BedrockRuntimeClient client = BedrockRuntimeClient.builder()
.credentialsProvider(DefaultCredentialsProvider.create())
.region(Region.US_EAST_1)
.build();
String modelId = "anthropic.claude-3-haiku-20240307-v1:0";
ArrayList<Message> conversation = new ArrayList<>();
conversation.add(Message.builder()
.role(ConversationRole.USER)
.content(c -> c.text("What is the capital of Australia?"))
.build());
ConverseResponse firstResponse = client.converse(
request -> request.modelId(modelId).messages(conversation)
);
String firstResponseText = firstResponse.output().message().content().get(0).text();
System.out.println("First response: " + firstResponseText);
conversation.add(firstResponse.output().message());
conversation.add(Message.builder()
.content(c -> c.text("What was my first question?"))
.role(ConversationRole.USER)
.build());
ConverseResponse secondResponse = client.converse(
request -> request.modelId(modelId).messages(conversation)
);
String secondResponseText = secondResponse.output().message().content().get(0).text();
System.out.println("Second response: " + secondResponseText);
}
}To run it:
- Save the code in a file named
BedrockConversation.java - Compile and run the Java application using your preferred IDE or command-line tools.
If everything is set up correctly, you should see the model's responses printed in the console:
1
2
First response: The capital of Australia is Canberra.
Second response: Your first question was "What is the capital of Australia?".If you encounter any errors, double check that you have:
- Configured your AWS credentials
- Requested access to the model you are using
- Installed the required dependencies
- Specified the correct region and model ID
Next steps
Congratulations on implementing a multi-turn conversation with Bedrock's Converse API!Ready for more? Here are some ideas to keep exploring:
- Write a chat loop that repeatedly prompts the user for input and prints the model's responses.
- Experiment with different models and compare their responses. Here's the list of all models supporting the Converse API.
- Challenge yourself to rewrite this program in another language. Here are examples in Python, JavaScript, C#, and more .
- Dive deeper into the Converse API in the Amazon Bedrock User Guide .
In Part 3 of this series (upcoming), we'll learn how to customize the AI's behavior with a system prompt and inference parameters.
I'd love to see what you build with Amazon Bedrock! Feel free to share your projects or ask questions in the comments.
Thanks for following along and happy building! 💻🤖
Series: No Python? No Problem! A Java Developer's Guide to Amazon Bedrock's Converse API (3 articles)
- 2Conversational AI: Add chatting capabilities to your Java app This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article