A Java developer's guide to Bedrock's new Converse API
Learn how to send your first request to a generative AI model using Amazon Bedrock's Converse API and the AWS SDK for Java.
- In Part 1: Getting Started (this post), you will learn how to send your first request.
- In Part 2: Conversational AI, I'll show you how to implement conversational turns.
- In Part 3: Customizing AI Behavior (upcoming), we'll configure the model with a system prompt and additional inference parameters.
- 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.4</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 BedrockFirstMessage {
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
String inputText = "Explain 'rubber duck debugging' in one line.";
Message message = Message.builder()
.content(ContentBlock.fromText(inputText))
.role(ConversationRole.USER)
.build();
ConversationRole.USER
or ConversationRole.ASSISTANT
) and at least one content block with the message text.converse()
method:1
2
3
ConverseResponse response = client.converse(
request -> request.modelId(modelId).messages(message)
);
1
2
String responseText = response.output().message().content().get(0).text();
System.out.println(responseText);
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
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.ContentBlock;
import software.amazon.awssdk.services.bedrockruntime.model.ConversationRole;
import software.amazon.awssdk.services.bedrockruntime.model.ConverseResponse;
import software.amazon.awssdk.services.bedrockruntime.model.Message;
public class BedrockFirstMessage {
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";
String inputText = "Explain 'rubber duck debugging' in one line.";
Message message = Message.builder()
.content(ContentBlock.fromText(inputText))
.role(ConversationRole.USER)
.build();
ConverseResponse response = client.converse(
request -> request.modelId(modelId).messages(message)
);
String responseText = response.output().message().content().get(0).text();
System.out.println(responseText);
}
}
- Save the code in a file named
BedrockFirstMessage.java
- Compile and run the Java application using your preferred IDE or command-line tools.
1
2
Rubber duck debugging is the process of explaining your code to
a rubber duck (or any inanimate object) to identify and fix bugs.
- Configured your AWS credentials
- Requested access to the model you are using
- Installed the required dependencies
- 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.