Skip to content

Commit

Permalink
Python & Java - Bedrock: Add GetFoundationModel, enhance ListFoundati…
Browse files Browse the repository at this point in the history
…onModels (#6106)
  • Loading branch information
DennisTraub authored Feb 21, 2024
1 parent c87479c commit 4cfd730
Show file tree
Hide file tree
Showing 17 changed files with 609 additions and 115 deletions.
24 changes: 23 additions & 1 deletion .doc_gen/metadata/bedrock_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ bedrock_GetFoundationModel:
title_abbrev: Get details about an &BR; foundation model
synopsis: get details about an &BR; foundation model.
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock
excerpts:
- description: Get details about a foundation model using the synchronous &BR; client.
snippet_tags:
- bedrock.java2.get_foundation_model.main
- description: Get details about a foundation model using the asynchronous &BR; client.
snippet_tags:
- bedrock.java2.get_foundation_model_async.main
JavaScript:
versions:
- sdk_version: 3
Expand All @@ -45,6 +56,14 @@ bedrock_GetFoundationModel:
- description: Get details about a foundation model.
snippet_files:
- javascriptv3/example_code/bedrock/actions/get-foundation-model.js
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock
excerpts:
- description: Get details about a foundation model.
snippet_tags:
- python.example_code.bedrock.GetFoundationModel
services:
bedrock: {GetFoundationModel}

Expand All @@ -68,9 +87,12 @@ bedrock_ListFoundationModels:
- sdk_version: 2
github: javav2/example_code/bedrock
excerpts:
- description: List the available &BRlong; foundation models.
- description: List the available &BRlong; foundation models using the synchronous &BR; client.
snippet_tags:
- bedrock.java2.list_foundation_models.main
- description: List the available &BRlong; foundation models using the asynchronous &BR; client.
snippet_tags:
- bedrock.java2.list_foundation_models_async.main
JavaScript:
versions:
- sdk_version: 3
Expand Down
1 change: 1 addition & 0 deletions .tools/validation/validator_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def skip(path: Path) -> bool:
"imaging/model/GetImageSetMetadataRequest",
"cd5e746ec203c8c3c61647e0886a8df8c1e78e41",
"imaging/model/StartDICOMImportJobRequest",
"src/main/java/com/example/s3/ListBuckets",
}


Expand Down
3 changes: 2 additions & 1 deletion javav2/example_code/bedrock/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav

Code excerpts that show you how to call individual service functions.

- [List available Amazon Bedrock foundation models](src/main/java/com/example/bedrock/ListFoundationModels.java#L36) (`ListFoundationModels`)
- [Get details about an Amazon Bedrock foundation model](src/main/java/com/example/bedrock/sync/GetFoundationModel.java#L56) (`GetFoundationModel`)
- [List available Amazon Bedrock foundation models](src/main/java/com/example/bedrock/sync/ListFoundationModels.java#L57) (`ListFoundationModels`)


<!--custom.examples.start-->
Expand Down
2 changes: 1 addition & 1 deletion javav2/example_code/bedrock/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.21.17</version>
<version>2.24.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down

This file was deleted.

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]
}
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]
}
Loading

0 comments on commit 4cfd730

Please sign in to comment.