From ac4b7bb6dcdf7f50a3ba5349455636a13f96cac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Bouteruche?= Date: Thu, 7 Dec 2023 06:27:49 -0800 Subject: [PATCH] C#: code example for calling the ListFoundationModels action Amazon Bedrock (#5752) * Initial version of ListFoundationModelsExample project * Update code example to match other .NET code example structure * Update bedrock_metadata.yaml file and add snippet tags in HelloBedrock.cs * Add a test project. * Add the BedrockActions and BedrockTests projects to DotNetV3Examples.sln file. --- .doc_gen/metadata/bedrock_metadata.yaml | 17 +++++ .../Bedrock/Actions/BedrockActions.csproj | 14 ++++ dotnetv3/Bedrock/Actions/HelloBedrock.cs | 75 +++++++++++++++++++ dotnetv3/Bedrock/BedrockExamples.sln | 39 ++++++++++ dotnetv3/Bedrock/Tests/BedrockTest.cs | 36 +++++++++ dotnetv3/Bedrock/Tests/BedrockTests.csproj | 27 +++++++ dotnetv3/Bedrock/Tests/GlobalUsings.cs | 12 +++ dotnetv3/DotNetV3Examples.sln | 9 +++ 8 files changed, 229 insertions(+) create mode 100644 dotnetv3/Bedrock/Actions/BedrockActions.csproj create mode 100644 dotnetv3/Bedrock/Actions/HelloBedrock.cs create mode 100644 dotnetv3/Bedrock/BedrockExamples.sln create mode 100644 dotnetv3/Bedrock/Tests/BedrockTest.cs create mode 100644 dotnetv3/Bedrock/Tests/BedrockTests.csproj create mode 100644 dotnetv3/Bedrock/Tests/GlobalUsings.cs diff --git a/.doc_gen/metadata/bedrock_metadata.yaml b/.doc_gen/metadata/bedrock_metadata.yaml index 972bd52f425..475510a88b5 100644 --- a/.doc_gen/metadata/bedrock_metadata.yaml +++ b/.doc_gen/metadata/bedrock_metadata.yaml @@ -13,6 +13,15 @@ bedrock_Hello: - description: snippet_tags: - gov2.bedrock.Hello + .NET: + versions: + - sdk_version: 3 + github: dotnetv3/Bedrock + sdkguide: + excerpts: + - description: + snippet_tags: + - Bedrock.dotnetv3.BedrockActions.HelloBedrock services: bedrock: {ListFoundationModels} @@ -63,5 +72,13 @@ bedrock_ListFoundationModels: - description: List the available &BRlong; foundation models. snippet_tags: - python.example_code.bedrock.ListFoundationModels + .NET: + versions: + - sdk_version: 3 + github: dotnetv3/Bedrock + excerpts: + - description: List the available Bedrock foundation models. + snippet_tags: + - Bedrock.dotnetv3.BedrockActions.ListFoundationModels services: bedrock: {ListFoundationModels} diff --git a/dotnetv3/Bedrock/Actions/BedrockActions.csproj b/dotnetv3/Bedrock/Actions/BedrockActions.csproj new file mode 100644 index 00000000000..48756aef89f --- /dev/null +++ b/dotnetv3/Bedrock/Actions/BedrockActions.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/dotnetv3/Bedrock/Actions/HelloBedrock.cs b/dotnetv3/Bedrock/Actions/HelloBedrock.cs new file mode 100644 index 00000000000..3c68c6d3cb5 --- /dev/null +++ b/dotnetv3/Bedrock/Actions/HelloBedrock.cs @@ -0,0 +1,75 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// snippet-start:[Bedrock.dotnetv3.BedrockActions.HelloBedrock] +using Amazon; +using Amazon.Bedrock; +using Amazon.Bedrock.Model; + +namespace ListFoundationModelsExample +{ + /// + /// This example shows how to list foundation models. + /// + internal class HelloBedrock + { + /// + /// Main method to call the ListFoundationModelsAsync method. + /// + /// The command line arguments. + 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); + + } + + // snippet-start:[Bedrock.dotnetv3.BedrockActions.ListFoundationModels] + + /// + /// List foundation models. + /// + /// The Amazon Bedrock client. + 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); + } + } + + // snippet-end:[Bedrock.dotnetv3.BedrockActions.ListFoundationModels] + + /// + /// Write the foundation model summary to console. + /// + /// The foundation model summary to write to console. + 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)}"); + } + } +} +// snippet-end:[Bedrock.dotnetv3.BedrockActions.HelloBedrock] \ No newline at end of file diff --git a/dotnetv3/Bedrock/BedrockExamples.sln b/dotnetv3/Bedrock/BedrockExamples.sln new file mode 100644 index 00000000000..0c01c36e80a --- /dev/null +++ b/dotnetv3/Bedrock/BedrockExamples.sln @@ -0,0 +1,39 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34309.116 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BedrockActions", "Actions\BedrockActions.csproj", "{C47E3B3E-0040-4CB6-AB92-EF4395C1EB83}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{0DD1E95E-9EF2-4E43-86B3-F636736BE054}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{898AFE57-24C6-4D79-81C2-614873B38F62}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BedrockTests", "Tests\BedrockTests.csproj", "{5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C47E3B3E-0040-4CB6-AB92-EF4395C1EB83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C47E3B3E-0040-4CB6-AB92-EF4395C1EB83}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C47E3B3E-0040-4CB6-AB92-EF4395C1EB83}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C47E3B3E-0040-4CB6-AB92-EF4395C1EB83}.Release|Any CPU.Build.0 = Release|Any CPU + {5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {C47E3B3E-0040-4CB6-AB92-EF4395C1EB83} = {0DD1E95E-9EF2-4E43-86B3-F636736BE054} + {5486426B-A8E8-4C6A-BEE2-83DD7CDB68A6} = {898AFE57-24C6-4D79-81C2-614873B38F62} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3D82D9F6-BE3C-40F1-9224-B8E4D746FC2E} + EndGlobalSection +EndGlobal diff --git a/dotnetv3/Bedrock/Tests/BedrockTest.cs b/dotnetv3/Bedrock/Tests/BedrockTest.cs new file mode 100644 index 00000000000..d21612b9ec2 --- /dev/null +++ b/dotnetv3/Bedrock/Tests/BedrockTest.cs @@ -0,0 +1,36 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +using Amazon.Bedrock; +using Amazon.Bedrock.Model; + +namespace BedrockTests; + +/// +/// Bedrock tests. +/// +public class BedrockTest +{ + private readonly AmazonBedrockClient bedrockClient; + + /// + /// Constructor for the test class. + /// + public BedrockTest() + { + bedrockClient = new AmazonBedrockClient(Amazon.RegionEndpoint.USEast1); + } + + /// + /// List foundation models. Should not be null. + /// + /// Async task. + [Fact] + [Order(1)] + [Trait("Category", "Integration")] + public async Task ListFoundationModelsAsync_ShouldNotBeNull() + { + var result = await bedrockClient.ListFoundationModelsAsync(new ListFoundationModelsRequest()); + Assert.NotEmpty(result.ModelSummaries); + } +} \ No newline at end of file diff --git a/dotnetv3/Bedrock/Tests/BedrockTests.csproj b/dotnetv3/Bedrock/Tests/BedrockTests.csproj new file mode 100644 index 00000000000..03f960debd4 --- /dev/null +++ b/dotnetv3/Bedrock/Tests/BedrockTests.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/dotnetv3/Bedrock/Tests/GlobalUsings.cs b/dotnetv3/Bedrock/Tests/GlobalUsings.cs new file mode 100644 index 00000000000..158ebd88747 --- /dev/null +++ b/dotnetv3/Bedrock/Tests/GlobalUsings.cs @@ -0,0 +1,12 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +global using Xunit; +global using Xunit.Extensions.Ordering; + +// Optional. +[assembly: CollectionBehavior(DisableTestParallelization = true)] +// Optional. +[assembly: TestCaseOrderer("Xunit.Extensions.Ordering.TestCaseOrderer", "Xunit.Extensions.Ordering")] +// Optional. +[assembly: TestCollectionOrderer("Xunit.Extensions.Ordering.CollectionOrderer", "Xunit.Extensions.Ordering")] \ No newline at end of file diff --git a/dotnetv3/DotNetV3Examples.sln b/dotnetv3/DotNetV3Examples.sln index 4c237f18ea4..f7bb48232a7 100644 --- a/dotnetv3/DotNetV3Examples.sln +++ b/dotnetv3/DotNetV3Examples.sln @@ -739,6 +739,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StepFunctionsBasics", "Step EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StepFunctionsActions", "StepFunctions\Actions\StepFunctionsActions.csproj", "{0340F970-AE33-4ABD-9B2B-E7CF266E33CC}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BedrockActions", "Bedrock\Actions\BedrockActions.csproj", "{F2975A40-3BCD-4AE0-BB14-03704E6B3F79}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Bedrock", "Bedrock", "{5810E90E-DBA6-438A-8FB3-A1769B2E4ECA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1677,6 +1681,10 @@ Global {0340F970-AE33-4ABD-9B2B-E7CF266E33CC}.Debug|Any CPU.Build.0 = Debug|Any CPU {0340F970-AE33-4ABD-9B2B-E7CF266E33CC}.Release|Any CPU.ActiveCfg = Release|Any CPU {0340F970-AE33-4ABD-9B2B-E7CF266E33CC}.Release|Any CPU.Build.0 = Release|Any CPU + {F2975A40-3BCD-4AE0-BB14-03704E6B3F79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F2975A40-3BCD-4AE0-BB14-03704E6B3F79}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F2975A40-3BCD-4AE0-BB14-03704E6B3F79}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F2975A40-3BCD-4AE0-BB14-03704E6B3F79}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2014,6 +2022,7 @@ Global {6A9E4CE0-E5B2-4136-B796-BFE673954ACA} = {66F40B36-B02C-4B6E-9978-592BB0040FD8} {8638D5FD-CFEB-4FBA-A8C0-B65F31B8AE93} = {66F40B36-B02C-4B6E-9978-592BB0040FD8} {0340F970-AE33-4ABD-9B2B-E7CF266E33CC} = {66F40B36-B02C-4B6E-9978-592BB0040FD8} + {F2975A40-3BCD-4AE0-BB14-03704E6B3F79} = {5810E90E-DBA6-438A-8FB3-A1769B2E4ECA} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {08502818-E8E1-4A91-A51C-4C8C8D4FF9CA}