Introducing AWSSDK.Extensions.Bedrock.MEAI

Introducing AWSSDK.Extensions.Bedrock.MEAI
Logo for Amazon Bedrock

Are you looking to add GenAI functionality to your .NET app? AWSSDK.Extensions.Bedrock.MEAI makes it easy.

Let's define what some of these words mean, then check out how to use this NuGet Package in our apps! You're also welcome to skip the definitions and jump directly to the code below.

What is MEAI?

"MEAI" in the package name is an acronym for Microsoft Extensions AI a new library that is now the recommended approach for all us .NET developers to integrate GenAI into our apps going forward.

Under the hood, MEAI provides an abstraction layer that allows us to easily swap GenAI tools and models while keeping the code in our business logic and unit tests exactly the same. If you're not already using Microsoft AI Extensions for .NET, I HIGHLY recommend implementing it; your future self will thank you. 💯

What is Bedrock?

"Bedrock" in the package name refers to Amazon Bedrock, an AWS service that provides us access to over 40+ foundation models from popular providers such as Anthropic, Meta, Mistral, DeepSeek and more.

Just for fun, let's ask the recently released Amazon Q Developer CLI, to get a better explanation:

Code

Now that we have those pesky definitions out of the way, let's create a .NET Console app that uses AWSSDK.Extensions.Bedrock.MEAI!

You can find the completed code sample here: https://github.com/TheCodeTraveler/Bedrock-MEAI-Sample

Let's first create a File -> New Console App in Visual Studio.

Next, add the AWSSDK.Extensions.Bedrock.MEAI NuGet Package to our app. If you're unfamiliar with adding NuGet Packages, you can find more information here.

Now we can finally write some code!

 using AiChatClient;
 using Amazon;
 using Amazon.BedrockRuntime;
 using Microsoft.Extensions.AI;

 IAmazonBedrockRuntime runtime = new AmazonBedrockRuntimeClient("Insert AwsCredentials AccessKeyId", "Insert AwsCredentials SecretAccessKey", RegionEndpoint.USEast1);
 IChatClient client = runtime.AsChatClient(); // Abstraction provided by Microsoft.Extensions.AI

 var chatMessage = new ChatMessage { Text = "Is this working?" };

 await foreach (var response in client.GetStreamingResponseAsync(chatMessage, new() { ModelId = "anthropic.claude-v2.1" }))
 {
 	Console.Write(response.Text);
 } 

Now let's click Run and see what happens 👀

It works! And the crazy part is it only took a couple lines of code!

You can find the completed code sample here: https://github.com/TheCodeTraveler/Bedrock-MEAI-Sample

Conclusion

As .NET developers, we should all be leveraging the Microsoft AI Extensions for .NET when adding GenAI to our apps.

As new LLMs debut and as new GenAI services, like Amazon Bedrock continue to grow, we are all but guaranteed that we'll be swapping models and services in our production apps for the foreseeable future. But thanks to tools like AWSSDK.Extensions.Bedrock.MEAI,

Next, I'll be exploring how to use this to build a ChatGPT clone using AWSSDK.Extensions.Bedrock.MEAI + .NET MAUI. Make sure to subscribe to this blog and to watch this GitHub repo for the latest updates! 👇

GitHub - TheCodeTraveler/Bedrock-MEAI-Sample: A sample application demonstrating AWSSDK.Extensions.Bedrock.MEAI
A sample application demonstrating AWSSDK.Extensions.Bedrock.MEAI - TheCodeTraveler/Bedrock-MEAI-Sample