-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python & Java - Bedrock: Add GetFoundationModel, enhance ListFoundati…
…onModels (#6106)
- Loading branch information
1 parent
c87479c
commit 4cfd730
Showing
17 changed files
with
609 additions
and
115 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
59 changes: 0 additions & 59 deletions
59
javav2/example_code/bedrock/src/main/java/com/example/bedrock/ListFoundationModels.java
This file was deleted.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
...example_code/bedrock/src/main/java/com/example/bedrock/async/GetFoundationModelAsync.java
This file contains 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.bedrock.async; | ||
|
||
// snippet-start:[bedrock.java2.get_foundation_model_async.import] | ||
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.bedrock.BedrockAsyncClient; | ||
import software.amazon.awssdk.services.bedrock.model.FoundationModelDetails; | ||
import software.amazon.awssdk.services.bedrock.model.GetFoundationModelResponse; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
// snippet-end:[bedrock.java2.get_foundation_model_async.import] | ||
|
||
/** | ||
* Before running this Java V2 code example, set up your development | ||
* environment, including your credentials. | ||
* | ||
* For more information, see the following documentation topic: | ||
* | ||
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html | ||
*/ | ||
public class GetFoundationModelAsync { | ||
public static void main(String[] args) { | ||
final String usage = """ | ||
Usage: | ||
<modelId> [<region>]\s | ||
Where: | ||
modelId - The ID of the foundation model you want to use. | ||
region - (Optional) The AWS region where the Agent is located. Default is 'us-east-1'. | ||
"""; | ||
|
||
if (args.length < 1 || args.length > 2) { | ||
System.out.println(usage); | ||
System.exit(1); | ||
} | ||
|
||
String modelId = args[0]; | ||
Region region = args.length == 2 ? Region.of(args[1]) : Region.US_EAST_1; | ||
|
||
System.out.println("Initializing the Amazon Bedrock async client..."); | ||
System.out.printf("Region: %s%n", region.toString()); | ||
|
||
BedrockAsyncClient client = BedrockAsyncClient.builder() | ||
.credentialsProvider(DefaultCredentialsProvider.create()) | ||
.region(region) | ||
.build(); | ||
|
||
getFoundationModel(client, modelId); | ||
} | ||
|
||
// snippet-start:[bedrock.java2.get_foundation_model_async.main] | ||
/** | ||
* Get details about an Amazon Bedrock foundation model. | ||
* | ||
* @param bedrockClient The async service client for accessing Amazon Bedrock. | ||
* @param modelIdentifier The model identifier. | ||
* @return An object containing the foundation model's details. | ||
*/ | ||
public static FoundationModelDetails getFoundationModel(BedrockAsyncClient bedrockClient, String modelIdentifier) { | ||
try { | ||
CompletableFuture<GetFoundationModelResponse> future = bedrockClient.getFoundationModel( | ||
r -> r.modelIdentifier(modelIdentifier) | ||
); | ||
|
||
FoundationModelDetails model = future.get().modelDetails(); | ||
|
||
System.out.println(" Model ID: " + model.modelId()); | ||
System.out.println(" Model ARN: " + model.modelArn()); | ||
System.out.println(" Model Name: " + model.modelName()); | ||
System.out.println(" Provider Name: " + model.providerName()); | ||
System.out.println(" Lifecycle status: " + model.modelLifecycle().statusAsString()); | ||
System.out.println(" Input modalities: " + model.inputModalities()); | ||
System.out.println(" Output modalities: " + model.outputModalities()); | ||
System.out.println(" Supported customizations: " + model.customizationsSupported()); | ||
System.out.println(" Supported inference types: " + model.inferenceTypesSupported()); | ||
System.out.println(" Response streaming supported: " + model.responseStreamingSupported()); | ||
|
||
return model; | ||
|
||
} catch (ExecutionException e) { | ||
if (e.getMessage().contains("ValidationException")) { | ||
throw new IllegalArgumentException(e.getMessage()); | ||
} else { | ||
System.err.println(e.getMessage()); | ||
throw new RuntimeException(e); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
System.err.println(e.getMessage()); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
// snippet-end:[bedrock.java2.get_foundation_model_async.main] | ||
} |
95 changes: 95 additions & 0 deletions
95
...ample_code/bedrock/src/main/java/com/example/bedrock/async/ListFoundationModelsAsync.java
This file contains 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,95 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.example.bedrock.async; | ||
|
||
// snippet-start:[bedrock.java2.list_foundation_models_async.import] | ||
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.bedrock.BedrockAsyncClient; | ||
import software.amazon.awssdk.services.bedrock.model.FoundationModelSummary; | ||
import software.amazon.awssdk.services.bedrock.model.ListFoundationModelsResponse; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
// snippet-end:[bedrock.java2.list_foundation_models_async.import] | ||
|
||
/** | ||
* Before running this Java V2 code example, set up your development | ||
* environment, including your credentials. | ||
* | ||
* For more information, see the following documentation topic: | ||
* | ||
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html | ||
*/ | ||
public class ListFoundationModelsAsync { | ||
|
||
private static Region region; | ||
|
||
public static void main(String[] args) { | ||
final String usage = """ | ||
Usage: | ||
[<region>]\s | ||
Where: | ||
region - (Optional) The AWS region where the Agent is located. Default is 'us-east-1'. | ||
"""; | ||
|
||
if (args.length > 1) { | ||
System.out.println(usage); | ||
System.exit(1); | ||
} | ||
|
||
region = args.length == 1 ? Region.of(args[0]) : Region.US_EAST_1; | ||
|
||
System.out.println("Initializing the Amazon Bedrock client..."); | ||
System.out.printf("Region: %s%n", region.toString()); | ||
|
||
BedrockAsyncClient bedrockClient = BedrockAsyncClient.builder() | ||
.credentialsProvider(DefaultCredentialsProvider.create()) | ||
.region(region) | ||
.build(); | ||
|
||
listFoundationModels(bedrockClient); | ||
} | ||
|
||
// snippet-start:[bedrock.java2.list_foundation_models_async.main] | ||
/** | ||
* Lists Amazon Bedrock foundation models that you can use. | ||
* You can filter the results with the request parameters. | ||
* | ||
* @param bedrockClient The async service client for accessing Amazon Bedrock. | ||
* @return A list of objects containing the foundation models' details | ||
*/ | ||
public static List<FoundationModelSummary> listFoundationModels(BedrockAsyncClient bedrockClient) { | ||
try { | ||
CompletableFuture<ListFoundationModelsResponse> future = bedrockClient.listFoundationModels(r -> {}); | ||
|
||
List<FoundationModelSummary> models = future.get().modelSummaries(); | ||
|
||
if (models.isEmpty()) { | ||
System.out.println("No available foundation models in " + region.toString()); | ||
} else { | ||
for (FoundationModelSummary model : models) { | ||
System.out.println("Model ID: " + model.modelId()); | ||
System.out.println("Provider: " + model.providerName()); | ||
System.out.println("Name: " + model.modelName()); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
return models; | ||
|
||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
System.err.println(e.getMessage()); | ||
throw new RuntimeException(e); | ||
} catch (ExecutionException e) { | ||
System.err.println(e.getMessage()); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
// snippet-end:[bedrock.java2.list_foundation_models_async.main] | ||
} |
Oops, something went wrong.