Amazon Bedrock examples using Amazon SDK for .NET - Amazon SDK for .NET
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon Bedrock examples using Amazon SDK for .NET

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for .NET with Amazon Bedrock.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Get started

The following code examples show how to get started using Amazon Bedrock.

Amazon SDK for .NET
Note

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

using Amazon; using Amazon.Bedrock; using Amazon.Bedrock.Model; namespace ListFoundationModelsExample { /// <summary> /// This example shows how to list foundation models. /// </summary> internal class HelloBedrock { /// <summary> /// Main method to call the ListFoundationModelsAsync method. /// </summary> /// <param name="args"> The command line arguments. </param> static async Task Main(string[] args) { // Specify a region endpoint where Amazon Bedrock is available. For a list of supported region see https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html#bedrock-regions AmazonBedrockClient bedrockClient = new(RegionEndpoint.USWest2); await ListFoundationModelsAsync(bedrockClient); } /// <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); } } /// <summary> /// Write the foundation model summary to console. /// </summary> /// <param name="foundationModel"> The foundation model summary to write to console. </param> private static void WriteToConsole(FoundationModelSummary foundationModel) { Console.WriteLine($"{foundationModel.ModelId}, Customization: {String.Join(", ", foundationModel.CustomizationsSupported)}, Stream: {foundationModel.ResponseStreamingSupported}, Input: {String.Join(", ", foundationModel.InputModalities)}, Output: {String.Join(", ", foundationModel.OutputModalities)}"); } } }
Topics

Actions

The following code example shows how to use ListFoundationModels.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon 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); } }