Skip to content

Commit

Permalink
C#: code example for calling the ListFoundationModels action Amazon B…
Browse files Browse the repository at this point in the history
…edrock (awsdocs#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.
  • Loading branch information
François Bouteruche authored and meyertst-aws committed Jan 4, 2024
1 parent 9fc14d1 commit ac4b7bb
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .doc_gen/metadata/bedrock_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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}
14 changes: 14 additions & 0 deletions dotnetv3/Bedrock/Actions/BedrockActions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Bedrock" Version="3.7.301.2" />
</ItemGroup>

</Project>
75 changes: 75 additions & 0 deletions dotnetv3/Bedrock/Actions/HelloBedrock.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <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);

}

// snippet-start:[Bedrock.dotnetv3.BedrockActions.ListFoundationModels]

/// <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);
}
}

// snippet-end:[Bedrock.dotnetv3.BedrockActions.ListFoundationModels]

/// <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)}");
}
}
}
// snippet-end:[Bedrock.dotnetv3.BedrockActions.HelloBedrock]
39 changes: 39 additions & 0 deletions dotnetv3/Bedrock/BedrockExamples.sln
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions dotnetv3/Bedrock/Tests/BedrockTest.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Bedrock tests.
/// </summary>
public class BedrockTest
{
private readonly AmazonBedrockClient bedrockClient;

/// <summary>
/// Constructor for the test class.
/// </summary>
public BedrockTest()
{
bedrockClient = new AmazonBedrockClient(Amazon.RegionEndpoint.USEast1);
}

/// <summary>
/// List foundation models. Should not be null.
/// </summary>
/// <returns>Async task.</returns>
[Fact]
[Order(1)]
[Trait("Category", "Integration")]
public async Task ListFoundationModelsAsync_ShouldNotBeNull()
{
var result = await bedrockClient.ListFoundationModelsAsync(new ListFoundationModelsRequest());
Assert.NotEmpty(result.ModelSummaries);
}
}
27 changes: 27 additions & 0 deletions dotnetv3/Bedrock/Tests/BedrockTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Bedrock" Version="3.7.301.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="Xunit.Extensions.Ordering" Version="1.4.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions dotnetv3/Bedrock/Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -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")]
9 changes: 9 additions & 0 deletions dotnetv3/DotNetV3Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit ac4b7bb

Please sign in to comment.