Skip to content

Commit 0755edc

Browse files
committed
added the Bedrock converse example
1 parent 57a3bf9 commit 0755edc

File tree

5 files changed

+638
-0
lines changed

5 files changed

+638
-0
lines changed

javav2/example_code/bedrock-runtime/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@
4545
<groupId>software.amazon.awssdk</groupId>
4646
<artifactId>sts</artifactId>
4747
</dependency>
48+
<dependency>
49+
<groupId>com.fasterxml.jackson.core</groupId>
50+
<artifactId>jackson-databind</artifactId>
51+
<version>2.16.1</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>software.amazon.awssdk</groupId>
55+
<artifactId>netty-nio-client</artifactId>
56+
</dependency>
4857
<dependency>
4958
<groupId>org.json</groupId>
5059
<artifactId>json</artifactId>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.example.bedrockruntime.scenario;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.json.JSONArray;
5+
import org.json.JSONObject;
6+
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
7+
import software.amazon.awssdk.core.document.Document;
8+
import software.amazon.awssdk.core.retry.RetryMode;
9+
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
10+
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
11+
import software.amazon.awssdk.regions.Region;
12+
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient;
13+
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
14+
import software.amazon.awssdk.services.bedrockruntime.model.BedrockRuntimeException;
15+
import software.amazon.awssdk.services.bedrockruntime.model.ContentBlock;
16+
import software.amazon.awssdk.services.bedrockruntime.model.ConverseRequest;
17+
import software.amazon.awssdk.services.bedrockruntime.model.ConverseResponse;
18+
import software.amazon.awssdk.services.bedrockruntime.model.Message;
19+
import software.amazon.awssdk.services.bedrockruntime.model.SystemContentBlock;
20+
import software.amazon.awssdk.services.bedrockruntime.model.Tool;
21+
import software.amazon.awssdk.services.bedrockruntime.model.ToolConfiguration;
22+
import software.amazon.awssdk.services.bedrockruntime.model.ToolInputSchema;
23+
import software.amazon.awssdk.services.bedrockruntime.model.ToolSpecification;
24+
25+
import java.time.Duration;
26+
import java.util.ArrayList;
27+
import java.util.HashMap;
28+
import java.util.List;
29+
import java.util.Map;
30+
import com.fasterxml.jackson.databind.ObjectMapper;
31+
import java.util.concurrent.CompletableFuture;
32+
33+
public class BedrockActions {
34+
35+
private static volatile BedrockRuntimeAsyncClient bedrockRuntimeClient;
36+
37+
private BedrockRuntimeAsyncClient getClient() {
38+
if (bedrockRuntimeClient == null) {
39+
/*
40+
The `NettyNioAsyncHttpClient` class is part of the AWS SDK for Java, version 2,
41+
and it is designed to provide a high-performance, asynchronous HTTP client for interacting with AWS services.
42+
It uses the Netty framework to handle the underlying network communication and the Java NIO API to
43+
provide a non-blocking, event-driven approach to HTTP requests and responses.
44+
*/
45+
46+
SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder()
47+
.maxConcurrency(50) // Adjust as needed.
48+
.connectionTimeout(Duration.ofSeconds(60)) // Set the connection timeout.
49+
.readTimeout(Duration.ofSeconds(60)) // Set the read timeout.
50+
.writeTimeout(Duration.ofSeconds(60)) // Set the write timeout.
51+
.build();
52+
53+
ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder()
54+
.apiCallTimeout(Duration.ofMinutes(2)) // Set the overall API call timeout.
55+
.apiCallAttemptTimeout(Duration.ofSeconds(90)) // Set the individual call attempt timeout.
56+
.retryStrategy(RetryMode.STANDARD)
57+
.build();
58+
59+
bedrockRuntimeClient = BedrockRuntimeAsyncClient.builder()
60+
.region(Region.US_EAST_1)
61+
.httpClient(httpClient)
62+
.overrideConfiguration(overrideConfig)
63+
.build();
64+
}
65+
return bedrockRuntimeClient;
66+
}
67+
68+
/**
69+
* Sends an asynchronous converse request to the AI model.
70+
*
71+
* @param modelId the unique identifier of the AI model to be used for the converse request
72+
* @param systemPrompt the system prompt to be included in the converse request
73+
* @param conversation a list of messages representing the conversation history
74+
* @param toolSpec the specification of the tool to be used in the converse request
75+
* @return the converse response received from the AI model
76+
*/
77+
public ConverseResponse sendConverseRequestAsync(String modelId, String systemPrompt, List<Message> conversation, ToolSpecification toolSpec) {
78+
List<Tool> toolList = new ArrayList<>();
79+
Tool tool = Tool.builder()
80+
.toolSpec(toolSpec)
81+
.build();
82+
83+
toolList.add(tool);
84+
85+
ToolConfiguration configuration = ToolConfiguration.builder()
86+
.tools(toolList)
87+
.build();
88+
89+
SystemContentBlock block = SystemContentBlock.builder()
90+
.text(systemPrompt)
91+
.build();
92+
93+
ConverseRequest request = ConverseRequest.builder()
94+
.modelId(modelId)
95+
.system(block)
96+
.messages(conversation)
97+
.toolConfig(configuration)
98+
.build();
99+
100+
try {
101+
ConverseResponse response = getClient().converse(request).join();
102+
return response;
103+
104+
} catch (Exception ex) {
105+
ex.printStackTrace();
106+
}
107+
108+
return null;
109+
}
110+
}
111+
112+

0 commit comments

Comments
 (0)