-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Java V2 Add the Bedrock Converse Java example #7356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...ode/bedrock-runtime/src/main/java/com/example/bedrockruntime/scenario/BedrockActions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package com.example.bedrockruntime.scenario; | ||
|
|
||
| import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; | ||
| import software.amazon.awssdk.core.retry.RetryMode; | ||
| import software.amazon.awssdk.http.async.SdkAsyncHttpClient; | ||
| import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient; | ||
| import software.amazon.awssdk.services.bedrockruntime.model.*; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class BedrockActions { | ||
|
|
||
| private static volatile BedrockRuntimeAsyncClient bedrockRuntimeClient; | ||
|
|
||
| private BedrockRuntimeAsyncClient getClient() { | ||
| if (bedrockRuntimeClient == null) { | ||
| /* | ||
| The `NettyNioAsyncHttpClient` class is part of the AWS SDK for Java, version 2, | ||
| and it is designed to provide a high-performance, asynchronous HTTP client for interacting with AWS services. | ||
| It uses the Netty framework to handle the underlying network communication and the Java NIO API to | ||
| provide a non-blocking, event-driven approach to HTTP requests and responses. | ||
| */ | ||
|
|
||
| SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() | ||
| .maxConcurrency(50) // Adjust as needed. | ||
| .connectionTimeout(Duration.ofSeconds(60)) // Set the connection timeout. | ||
| .readTimeout(Duration.ofSeconds(60)) // Set the read timeout. | ||
| .writeTimeout(Duration.ofSeconds(60)) // Set the write timeout. | ||
| .build(); | ||
|
|
||
| ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() | ||
| .apiCallTimeout(Duration.ofMinutes(2)) // Set the overall API call timeout. | ||
| .apiCallAttemptTimeout(Duration.ofSeconds(90)) // Set the individual call attempt timeout. | ||
| .retryStrategy(RetryMode.STANDARD) | ||
| .build(); | ||
|
|
||
| bedrockRuntimeClient = BedrockRuntimeAsyncClient.builder() | ||
| .region(Region.US_EAST_1) | ||
| .httpClient(httpClient) | ||
| .overrideConfiguration(overrideConfig) | ||
| .build(); | ||
| } | ||
| return bedrockRuntimeClient; | ||
| } | ||
|
|
||
| // snippet-start:[bedrockruntime.java2.converse.main] | ||
| /** | ||
| * Sends an asynchronous converse request to the AI model. | ||
| * | ||
| * @param modelId the unique identifier of the AI model to be used for the converse request | ||
| * @param systemPrompt the system prompt to be included in the converse request | ||
| * @param conversation a list of messages representing the conversation history | ||
| * @param toolSpec the specification of the tool to be used in the converse request | ||
| * @return the converse response received from the AI model | ||
| */ | ||
| public ConverseResponse sendConverseRequestAsync(String modelId, String systemPrompt, List<Message> conversation, ToolSpecification toolSpec) { | ||
| List<Tool> toolList = new ArrayList<>(); | ||
| Tool tool = Tool.builder() | ||
| .toolSpec(toolSpec) | ||
| .build(); | ||
|
|
||
| toolList.add(tool); | ||
|
|
||
| ToolConfiguration configuration = ToolConfiguration.builder() | ||
| .tools(toolList) | ||
| .build(); | ||
|
|
||
| SystemContentBlock block = SystemContentBlock.builder() | ||
| .text(systemPrompt) | ||
| .build(); | ||
|
|
||
| ConverseRequest request = ConverseRequest.builder() | ||
| .modelId(modelId) | ||
| .system(block) | ||
| .messages(conversation) | ||
| .toolConfig(configuration) | ||
| .build(); | ||
|
|
||
| try { | ||
| ConverseResponse response = getClient().converse(request).join(); | ||
| return response; | ||
|
|
||
| } catch (ModelNotReadyException ex) { | ||
| throw new RuntimeException("Model is not ready: " + ex.getMessage(), ex); | ||
| } catch (BedrockRuntimeException ex) { | ||
| throw new RuntimeException("Failed to converse with Bedrock model: " + ex.getMessage(), ex); | ||
| } | ||
| } | ||
| // snippet-end:[bedrockruntime.java2.converse.main] | ||
| } | ||
|
|
||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.