
Customizing AI Behavior: System prompts and inference parameters with Java and Bedrock's Converse API
In this tutorial, you'll learn how to configure a generative AI model with a system prompt and additional inference parameters, using the Bedrock 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, I'll show you how to implement conversational turns.
- In Part 3: Customizing AI Behavior (this post), 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.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 BedrockCustomized {
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";
USER
role:1
2
3
4
5
6
String inputText = "What is 'rubber duck debugging'.";
Message message = Message.builder()
.content(ContentBlock.fromText(inputText))
.role(ConversationRole.USER)
.build();
1
String systemPrompt = "You will always respond in rhymes.";
1
2
3
4
InferenceConfiguration config = InferenceConfiguration.builder()
.maxTokens(100)
.temperature(0.5F)
.build();
converse()
method:1
2
3
4
5
6
ConverseResponse response = client.converse(request -> request
.modelId(modelId)
.messages(message)
.inferenceConfig(config)
.system(SystemContentBlock.fromText(systemPrompt))
);
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
33
34
35
36
37
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.*;
public class BedrockCustomized {
public static void main(String[] args) {
BedrockRuntimeClient client = BedrockRuntimeClient.builder()
.credentialsProvider(DefaultCredentialsProvider.create())
.region(Region.US_EAST_1)
.build();
var modelId = "anthropic.claude-3-haiku-20240307-v1:0";
Message message = Message.builder()
.role(ConversationRole.USER)
.content(ContentBlock.fromText("What is 'rubber duck debugging'?"))
.build();
String systemPrompt = "You will always respond in rhymes.";
InferenceConfiguration config = InferenceConfiguration.builder()
.maxTokens(100)
.temperature(0.5F)
.build();
ConverseResponse response = client.converse(request -> request
.modelId(modelId)
.messages(message)
.inferenceConfig(config)
.system(SystemContentBlock.fromText(systemPrompt))
);
String responseText = response.output().message().content().get(0).text();
System.out.println(responseText);
}
}
- Save the code in a file named
BedrockCustomized.java
- Compile and run the Java application using your preferred IDE or command-line tools.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Rubber duck debugging, a clever way,
To solve problems without delay.
You talk to a duck, real or toy,
And the solution you will enjoy.
By explaining the issue out loud,
Your mind becomes less clouded.
The duck listens, never judges,
And the answer often just nudges.
It's a technique that's quite profound,
When you're stuck, it helps you rebound.
So next time you're in a bind,
Grab a duck and let your thoughts unwind.
- 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.