Skip to main content

An introduction to Microsoft.Extensions.AI–Part I

Last year, when the AI hype really exploded, the 'go to' library to build AI solutions in .NET at that time from Microsoft was Semantic Kernel. So although at that time still in preview, I started using Semantic Kernel and never looked back. Later Microsoft introduced Microsoft.Extensions.AI but I never had the time to take a good look at it.

Now I finally found some time to explore it further. My goal is to write a few posts in which I recreate an application that I originally created in Semantic Kernel to see how far we can get. But that will be mainly for the upcoming posts. In this post we focus on the basics to get started.

What is Microsoft.Extensions.AI?

Microsoft.Extensions.AI libraries provide a unified approach for representing generative AI components and enable seamless integration and interoperability with various AI services. Think of it as the dependency injection and logging abstractions you already know and love but specifically designed for AI services.

These libraries provide a unified layer of C# abstractions for interacting with AI services, such as small and large language models (SLMs and LLMs), embeddings, and middleware. Whether you're working with OpenAI, Azure OpenAI, or other AI providers, Microsoft.Extensions.AI gives you a consistent programming model that abstracts away the implementation details.

Remark: Although Microsoft.Extensions.AI was originally created as a separate library, Microsoft is actively working on integrating Microsoft.Extensions.AI with Semantic Kernel providing a consistent experience.

Getting started

To get started you will typically need at least these 2 NuGet packages:

dotnet add package Microsoft.Extensions.AI

dotnet add package Microsoft.Extensions.AI.Abstractions

Depending on how you want to interact with an LLM, you’ll need some extra packages. In our case we’ll start with a local language model through AI Foundry Local. Therefore, add the following NuGet package:

dotnet add package Microsoft.Extensions.AI.OpenAI

Remark: AI Foundry Local exposes an Open AI compatible API.

Now we can start writing some code. If you want to build a chat interface, you only need 2 lines of code:

Let’s start our AI Foundry local API:

foundry model run phi-3.5-mini

And run our app:


Great! That is a good starting point.

In a next post I’ll integrate Microsoft.Extensions.AI in an ASP.NET core application.

More information

Microsoft.Extensions.AI libraries - .NET | Microsoft Learn

Microsoft.Extensions.AI: Integrating AI into your .NET applications

Introducing Microsoft.Extensions.AI Preview - Unified AI Building Blocks for .NET - .NET Blog

Microsoft.Extensions.AI: Simplifying AI Integration for .NET Partners | Semantic Kernel

Supercharging On-Device AI: Foundry Local + Semantic Kernel

Popular posts from this blog

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

Cleaner switch expressions with pattern matching in C#

Ever find yourself mapping multiple string values to the same result? Being a C# developer for a long time, I sometimes forget that the C# has evolved so I still dare to chain case labels or reach for a dictionary. Of course with pattern matching this is no longer necessary. With pattern matching, you can express things inline, declaratively, and with zero repetition. A small example I was working on a small script that should invoke different actions depending on the environment. As our developers were using different variations for the same environment e.g.  "tst" alongside "test" , "prd" alongside "prod" .  We asked to streamline this a long time ago, but as these things happen, we still see variations in the wild. This brought me to the following code that is a perfect example for pattern matching: The or keyword here is a logical pattern combinator , not a boolean operator. It matches if either of the specified pattern...