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.
- 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
- 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
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>
...
aws.sdk.version
with the latest version of the AWS SDK for Java.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();
// ...
}
}
1
String modelId = "anthropic.claude-3-haiku-20240307-v1:0";
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());
ConversationRole.USER
or ConversationRole.ASSISTANT
) and at least one content block with the message text.converse()
method:1
2
3
ConverseResponse firstResponse = client.converse(
request -> request.modelId(modelId).messages(conversation)
);
1
2
String firstResponseText = firstResponse.output().message().content().get(0).text();
System.out.println("First response: " + firstResponseText);
1
conversation.add(firstResponse.output().message());
1
2
3
4
conversation.add(Message.builder()
.content(c -> c.text("What was my first question?"))
.role(ConversationRole.USER)
.build());
1
2
3
ConverseResponse secondResponse = client.converse(
request -> request.modelId(modelId).messages(conversation)
);
1
2
String secondResponseText = secondResponse.output().message().content().get(0).text();
System.out.println("Second response: " + secondResponseText);
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);
}
}
- Save the code in a file named
BedrockConversation.java
- Compile and run the Java application using your preferred IDE or command-line tools.
1
2
First response: The capital of Australia is Canberra.
Second response: Your first question was "What is the capital of Australia?".
- Configured your AWS credentials
- Requested access to the model you are using
- Installed the required dependencies
- 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.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.