-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C#: code example for calling the ListFoundationModels action Amazon B…
…edrock (#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
Dec 7, 2023
1 parent
ce1bc7a
commit b418f6f
Showing
8 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters