Use ListFoundationModels with an AWS SDK - Amazon Bedrock

Use ListFoundationModels with an AWS SDK

The following code examples show how to use ListFoundationModels.

.NET
AWS SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

List the available Bedrock foundation models.

/// <summary> /// List foundation models. /// </summary> /// <param name="bedrockClient"> The Amazon Bedrock client. </param> private static async Task ListFoundationModelsAsync(AmazonBedrockClient bedrockClient) { Console.WriteLine("List foundation models with no filter"); try { ListFoundationModelsResponse response = await bedrockClient.ListFoundationModelsAsync(new ListFoundationModelsRequest() { }); if (response?.HttpStatusCode == System.Net.HttpStatusCode.OK) { foreach (var fm in response.ModelSummaries) { WriteToConsole(fm); } } else { Console.WriteLine("Something wrong happened"); } } catch (AmazonBedrockException e) { Console.WriteLine(e.Message); } }
Go
SDK for Go V2
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

List the available Bedrock foundation models.

import ( "context" "log" "github.com/aws/aws-sdk-go-v2/service/bedrock" "github.com/aws/aws-sdk-go-v2/service/bedrock/types" ) // FoundationModelWrapper encapsulates Amazon Bedrock actions used in the examples. // It contains a Bedrock service client that is used to perform foundation model actions. type FoundationModelWrapper struct { BedrockClient *bedrock.Client } // ListPolicies lists Bedrock foundation models that you can use. func (wrapper FoundationModelWrapper) ListFoundationModels(ctx context.Context) ([]types.FoundationModelSummary, error) { var models []types.FoundationModelSummary result, err := wrapper.BedrockClient.ListFoundationModels(ctx, &bedrock.ListFoundationModelsInput{}) if err != nil { log.Printf("Couldn't list foundation models. Here's why: %v\n", err) } else { models = result.ModelSummaries } return models, err }
Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

List the available Amazon Bedrock foundation models using the synchronous Amazon Bedrock client.

/** * Lists Amazon Bedrock foundation models that you can use. * You can filter the results with the request parameters. * * @param bedrockClient The service client for accessing Amazon Bedrock. * @return A list of objects containing the foundation models' details */ public static List<FoundationModelSummary> listFoundationModels(BedrockClient bedrockClient) { try { ListFoundationModelsResponse response = bedrockClient.listFoundationModels(r -> {}); List<FoundationModelSummary> models = response.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 (SdkClientException e) { System.err.println(e.getMessage()); throw new RuntimeException(e); } }

List the available Amazon Bedrock foundation models using the asynchronous Amazon Bedrock client.

/** * 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); } }
JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

List the available foundation models.

import { fileURLToPath } from "node:url"; import { BedrockClient, ListFoundationModelsCommand, } from "@aws-sdk/client-bedrock"; /** * List the available Amazon Bedrock foundation models. * * @return {FoundationModelSummary[]} - The list of available bedrock foundation models. */ export const listFoundationModels = async () => { const client = new BedrockClient(); const input = { // byProvider: 'STRING_VALUE', // byCustomizationType: 'FINE_TUNING' || 'CONTINUED_PRE_TRAINING', // byOutputModality: 'TEXT' || 'IMAGE' || 'EMBEDDING', // byInferenceType: 'ON_DEMAND' || 'PROVISIONED', }; const command = new ListFoundationModelsCommand(input); const response = await client.send(command); return response.modelSummaries; }; // Invoke main function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { const models = await listFoundationModels(); console.log(models); }
Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

List the available Amazon Bedrock foundation models.

suspend fun listFoundationModels(): List<FoundationModelSummary>? { BedrockClient { region = "us-east-1" }.use { bedrockClient -> val response = bedrockClient.listFoundationModels(ListFoundationModelsRequest {}) response.modelSummaries?.forEach { model -> println("==========================================") println(" Model ID: ${model.modelId}") println("------------------------------------------") println(" Name: ${model.modelName}") println(" Provider: ${model.providerName}") println(" Input modalities: ${model.inputModalities}") println(" Output modalities: ${model.outputModalities}") println(" Supported customizations: ${model.customizationsSupported}") println(" Supported inference types: ${model.inferenceTypesSupported}") println("------------------------------------------\n") } return response.modelSummaries } }
PHP
SDK for PHP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

List the available Amazon Bedrock foundation models.

public function listFoundationModels() { $bedrockClient = new BedrockClient([ 'region' => 'us-west-2', 'profile' => 'default' ]); $response = $bedrockClient->listFoundationModels(); return $response['modelSummaries']; }
Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

List the available Amazon Bedrock foundation models.

def list_foundation_models(self): """ List the available Amazon Bedrock foundation models. :return: The list of available bedrock foundation models. """ try: response = self.bedrock_client.list_foundation_models() models = response["modelSummaries"] logger.info("Got %s foundation models.", len(models)) return models except ClientError: logger.error("Couldn't list foundation models.") raise

For a complete list of AWS SDK developer guides and code examples, see Using Amazon Bedrock with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.