
Developing Java Applications using Amazon Bedrock
Learn how to build Java applications that leverage the power of Generative AI foundation models provided by Amazon Bedrock.
bedrock
and bedrockruntime
to your project as shown below.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bedrock</artifactId>
<version>2.20.162</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bedrockruntime</artifactId>
<version>2.20.162</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
</dependencies>
json
is not mandatory. It was added to help with the examples of the following sections—but you are free to use another JSON parser library of your preference. With that done, let's start developing our first example.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class CohereTextGeneration {
private static final String MODEL_ID = "cohere.command-text-v14";
private static final String PROMPT = """
Extract the band name from the contract:
This Music Recording Agreement ("Agreement") is made effective as of the 13 day of December,
2021 by and between Good Kid, a Toronto-based musical group (“Artist”) and Universal Music Group,
a record label with license number 545345 (“Recording Label"). Artist and Recording Label may each
be referred to in this Agreement individually as a "Party" and collectively as the "Parties."
Work under this Agreement shall begin on March 15, 2022.
""";
}
BedrockRuntimeClient
class. It allows you to connect with the Amazon Bedrock service and send requests to it.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
public class CohereTextGeneration {
private static final String MODEL_ID = "cohere.command-text-v14";
private static final String PROMPT = """
Extract the band name from the contract:
This Music Recording Agreement ("Agreement") is made effective as of the 13 day of December,
2021 by and between Good Kid, a Toronto-based musical group (“Artist”) and Universal Music Group,
a record label with license number 545345 (“Recording Label"). Artist and Recording Label may each
be referred to in this Agreement individually as a "Party" and collectively as the "Parties."
Work under this Agreement shall begin on March 15, 2022.
""";
public static void main(String... args) throws Exception {
try (BedrockRuntimeClient bedrockClient = BedrockRuntimeClient.builder()
.region(Region.US_EAST_1)
.credentialsProvider(ProfileCredentialsProvider.create())
.build()) {
}
}
}
BedrockRuntimeClient
class implements the java.lang.AutoCloseable
interface, which means you can control the lifecycle of the instance with a try-with-resources statement. With the instance of the BedrockRuntimeClient created, you must assemble a request to send to the service. This is done in two parts. First, you must create a string with the request body payload that will be used by the service to process the request. Second, you need to include that body payload into a InvokeModelRequest
object.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
public class CohereTextGeneration {
private static final String MODEL_ID = "cohere.command-text-v14";
private static final String PROMPT = """
Extract the band name from the contract:
This Music Recording Agreement ("Agreement") is made effective as of the 13 day of December,
2021 by and between Good Kid, a Toronto-based musical group (“Artist”) and Universal Music Group,
a record label with license number 545345 (“Recording Label"). Artist and Recording Label may each
be referred to in this Agreement individually as a "Party" and collectively as the "Parties."
Work under this Agreement shall begin on March 15, 2022.
""";
public static void main(String... args) throws Exception {
try (BedrockRuntimeClient bedrockClient = BedrockRuntimeClient.builder()
.region(Region.US_EAST_1)
.credentialsProvider(ProfileCredentialsProvider.create())
.build()) {
String bedrockBody = BedrockRequestBody.builder()
.withModelId(MODEL_ID)
.withPrompt(PROMPT)
.withInferenceParameter("temperature", 0.40)
.withInferenceParameter("p", 0.75)
.withInferenceParameter("k", 0)
.withInferenceParameter("max_tokens", 200)
.build();
InvokeModelRequest invokeModelRequest = InvokeModelRequest.builder()
.modelId(MODEL_ID)
.body(SdkBytes.fromString(bedrockBody, Charset.defaultCharset()))
.build();
}
}
}
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
47
48
49
public class CohereTextGeneration {
private static final String MODEL_ID = "cohere.command-text-v14";
private static final String PROMPT = """
Extract the band name from the contract:
This Music Recording Agreement ("Agreement") is made effective as of the 13 day of December,
2021 by and between Good Kid, a Toronto-based musical group (“Artist”) and Universal Music Group,
a record label with license number 545345 (“Recording Label"). Artist and Recording Label may each
be referred to in this Agreement individually as a "Party" and collectively as the "Parties."
Work under this Agreement shall begin on March 15, 2022.
""";
public static void main(String... args) throws Exception {
try (BedrockRuntimeClient bedrockClient = BedrockRuntimeClient.builder()
.region(Region.US_EAST_1)
.credentialsProvider(ProfileCredentialsProvider.create())
.build()) {
String bedrockBody = BedrockRequestBody.builder()
.withModelId(MODEL_ID)
.withPrompt(PROMPT)
.withInferenceParameter("temperature", 0.40)
.withInferenceParameter("p", 0.75)
.withInferenceParameter("k", 0)
.withInferenceParameter("max_tokens", 200)
.build();
InvokeModelRequest invokeModelRequest = InvokeModelRequest.builder()
.modelId(MODEL_ID)
.body(SdkBytes.fromString(bedrockBody, Charset.defaultCharset()))
.build();
InvokeModelResponse invokeModelResponse = bedrockClient.invokeModel(invokeModelRequest);
JSONObject responseAsJson = new JSONObject(invokeModelResponse.body().asUtf8String());
System.out.println("🤖 Response: ");
System.out.println(responseAsJson
.getJSONArray("generations")
.getJSONObject(0)
.getString("text"));
}
}
}
1
2
3
4
5
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
🤖 Response:
Good Kid