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.

VS Code Planning mode

After the introduction of Plan mode in Visual Studio , it now also found its way into VS Code. Planning mode, or as I like to call it 'Hannibal mode', extends GitHub Copilot's Agent Mode capabilities to handle larger, multi-step coding tasks with a structured approach. Instead of jumping straight into code generation, Planning mode creates a detailed execution plan. If you want more details, have a look at my previous post . Putting plan mode into action VS Code takes a different approach compared to Visual Studio when using plan mode. Instead of a configuration setting that you can activate but have limited control over, planning is available as a separate chat mode/agent: I like this approach better than how Visual Studio does it as you have explicit control when plan mode is activated. Instead of immediately diving into execution, the plan agent creates a plan and asks some follow up questions: You can further edit the plan by clicking on ‘Open in Editor’: ...