- Newest
- Most votes
- Most comments
To invoke a Bedrock Agent using the Java SDK, you can follow these steps:
-
Set up the BedrockAgentRuntimeAsyncClient:
BedrockAgentRuntimeAsyncClient client = BedrockAgentRuntimeAsyncClient.builder() .region(Region.US_EAST_1) .credentialsProvider(DefaultCredentialsProvider.create()) .build();
-
Create the InvokeAgentRequest:
InvokeAgentRequest request = InvokeAgentRequest.builder() .agentId(agentId) .agentAliasId(agentAliasId) .sessionId(sessionId) .inputText(inputText) .enableTrace(true) .sessionState(SessionState.builder() .sessionAttributes(Map.of("Authorization", authToken)) .build()) .build();
-
Implement the InvokeAgentResponseHandler:
InvokeAgentResponseHandler handler = InvokeAgentResponseHandler.builder() .onResponse(response -> { log.info("Response Received from Agent: {}", response); // Process the response here }) .onStream(publisher -> { publisher.subscribe(event -> { if (event instanceof CompletionEvent) { CompletionEvent completion = (CompletionEvent) event; log.info("Completion: {}", completion.completion()); chatResponse.responses.add(completion.completion()); } }); }) .onError(error -> { log.error("Error occurred: ", error); }) .build();
-
Invoke the agent and handle the response:
CompletableFuture<Void> future = client.invokeAgent(request, handler); try { future.get(30, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { log.error("Error invoking Bedrock agent: ", e); }
-
Don't forget to close the client when you're done:
client.close();
To address the connection pool issue, you can configure the HTTP client used by the SDK:
BedrockAgentRuntimeAsyncClient client = BedrockAgentRuntimeAsyncClient.builder() .region(Region.US_EAST_1) .credentialsProvider(DefaultCredentialsProvider.create()) .httpClientBuilder( NettyNioAsyncHttpClient.builder() .maxConcurrency(100) .connectionTimeout(Duration.ofSeconds(5)) ) .build();
This configuration increases the max connections and sets a connection timeout. Adjust these values based on your needs.
Remember to handle the response stream properly, as Bedrock agents may return partial results as they become available. The onStream
method in the response handler allows you to process these partial results as they arrive.
Also, make sure you're using the latest version of the AWS SDK for Java, as improvements and bug fixes are regularly released.
Sources
Community | A Java developer's guide to Bedrock's new Converse API
Solving 'Read Timed Out' Error and High Latencies in Amazon Bedrock with AWS Java SDK Client | AWS re:Post
Amazon Bedrock Runtime examples using SDK for Java 2.x - AWS SDK Code Examples
Amazon Bedrock Runtime examples using SDK for Java 2.x - AWS SDK for Java 2.x
I was able to finally figure out what I was looking for. If anyone else stumbles across this maybe this will help. Essentially the way I understand it is, that you have to be able to handle the different types of events that come back from the agent. Typically (not always) the responses are chunked back as text. Hopefully this helps someone else.
InvokeAgentResponseHandler handler = InvokeAgentResponseHandler.builder()
.onResponse(response -> {
log.info("Response Received from Agent: {}", response);
// Process the response here
})
.onEventStream(publisher -> {
publisher.subscribe(event -> {
log.info("On Event Stream: {}", event);
log.info("On Event Stream SDK type: {}", event.sdkEventType());
log.info("On Event Stream Class: {}", event.getClass());
log.info("On Event Stream sdk Fields: {}", event.sdkFields());
event.accept(new InvokeAgentResponseHandler.Visitor() {
@Override
public void visitDefault(ResponseStream event) {
InvokeAgentResponseHandler.Visitor.super.visitDefault(event);
log.info("[visitDefault] - {}", event.toString());
}
@Override
public void visitChunk(PayloadPart event) {
InvokeAgentResponseHandler.Visitor.super.visitChunk(event);
log.info("[visitChunk] - {}", event.toString());
String payloadAsString = event.bytes().asUtf8String();
log.info("Chunked Data = {}", payloadAsString);
chatResponse.responses.add(payloadAsString);
}
@Override
public void visitFiles(FilePart event) {
InvokeAgentResponseHandler.Visitor.super.visitFiles(event);
log.info("[visitFiles] - {}", event.toString());
}
@Override
public void visitReturnControl(ReturnControlPayload event) {
InvokeAgentResponseHandler.Visitor.super.visitReturnControl(event);
log.info("[visitReturnControl] - {}", event.toString());
}
@Override
public void visitTrace(TracePart event) {
InvokeAgentResponseHandler.Visitor.super.visitTrace(event);
log.info("[visitTrace] - {}", event.toString());
}
});
});
})
.onError(error -> {
log.error("Error occurred: ", error);
})
.build();
// Invoke the agent
CompletableFuture<Void> agentInvocation = client.invokeAgent(request, handler);
//Wait for it to complete
agentInvocation.get(agentTimeoutSeconds, TimeUnit.SECONDS);
You can find more Java samples https://github.com/MichaelShapira/bedrock-java-samples/tree/main - including Agents example
Relevant content
- Accepted Answerasked 4 months ago
- AWS OFFICIALUpdated 9 months ago
- AWS OFFICIALUpdated 7 months ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated a year ago