Introduction
In this article, we will learn about the abstraction of AI services introduced by Microsoft, which is named 'Microsoft.Extensions.AI'. The intention of this package is to provide us with middleware that reduces the overhead of configuring Generative AI services in .NET applications. This package will work as a base for Semantic Kernel, which will unify the development of .NET Generative AI packages.
The main advantage of using this package is that we don't need to independently install several packages, such as Azure Open AI, Ollama, etc. packages separately and build a framework following coding principles. Rather, we can use this package to get up and running quickly following the dependency injection pattern in .NET.
How to choose between Microsoft.SemanticKernel and Microsoft.Extensions.AI?
When you are building a standard solution that can solve all your problems with Semantic Kernel, then choose Semantic Kernel. If you need more granular control and want to customize the solution you are building, opt-in for the Microsoft.Extensions.AI package and start building everything from scratch by using Microsoft.Extensions.AI. A detailed comparison is below,
Micorosoft.SemanticKernel |
Microsoft.Extension.AI |
Standardized and more code organized, such as prompt placement, configurations, etc., with code patterns in place. |
The base code is organized, but conventions and code patterns need to be set up. |
Extensive support for different Generative AI models. |
Works as a base package. |
It is used for rapid generative AI app development requiring complex architecture. |
It is used for Rapid Generative AI app development requiring simple or custom architecture. |
Prerequisite
- Ollama Server (tinyllama model)
- .NET 9
- Visual Studio 2022
- Code Walkthrough
In this article, we will explore using the open-source model platform Ollama to communicate with Generative AI models using the Microsoft.Extension.AI package.
Follow the steps below for code setup:
- Create a console application in .NET 9.
- Install these packages: Microsoft.Extensions.AI.Ollama and Microsoft.Extensions.AI
Our csproj file will look like this.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI" Version="9.0.0-preview.9.24556.5" />
<PackageReference Include="Microsoft.Extensions.AI.Ollama" Version="9.0.0-preview.9.24556.5" />
</ItemGroup>
</Project>
3. Write the below code in the console application:
using Microsoft.Extensions.AI;
namespace GenAI.ConsoleApp
{
internal class Program
{
static async Task Main(string[] args)
{
while (true)
{
Console.WriteLine("Me: "); //prints Me: on screen
var inputStr = Console.ReadLine(); //accepts user input
if(inputStr == "exit()") //if input is 'exit()' application exits
{
break;
}
IChatClient client = new OllamaChatClient(new Uri("http://localhost:11434/"), "tinyllama"); //Initialize chat client
var response = await client.CompleteAsync(inputStr); //receives output asynchronously
Console.WriteLine();//Appends new line
Console.WriteLine($"AI: {response.Message}"); //prints response message
}
}
}
}
4. Write something in my case "Hi, How are you". Below is the output, with the code on the left and the output on the right.
Now, in Web API
1. Create Web API in .NET 9
2. Install the above packages. Our csproj will look like below,
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.AI" Version="9.0.0-preview.9.24556.5" />
<PackageReference Include="Microsoft.Extensions.AI.Ollama" Version="9.0.0-preview.9.24556.5" />
</ItemGroup>
</Project>
3. Add DI to our Program.cs, file will look like below:
using Microsoft.Extensions.AI;
namespace GenAI.WebAPI
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddChatClient(builder =>
builder.Use(new OllamaChatClient(new Uri("http://localhost:11434/"), "tinyllama"))
); //Ollama DI Added
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}
4. Modify our weather forecast and get an endpoint to summarize weather like the below.
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.AI;
namespace GenAI.WebAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
private readonly IChatClient chatClient;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IChatClient chatClient)
{
_logger = logger;
this.chatClient = chatClient;
}
[HttpGet(Name = "GetWeatherForecast")]
public async Task<IActionResult> Get()
{
var weather = Enumerable.Range(1,1).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.First();
var result = await chatClient.CompleteAsync($"In 20 words Summarize {weather.Summary} weather : {weather.Date} Temperature F: {weather.TemperatureF}, Degree C: {weather.TemperatureC}");
return Ok(new { weathersummary = $"{result.Message}"});
}
}
}
5. Run the API and open http://localhost:5259/WeatherForecast in the browser. Our API response will look like below.
Refresh it multiple times to see different responses and have fun :)
Below are a few samples.
{"weathersummary":"Scorching weather: Life can get hot and uncomfortable when the temperature reaches above 111°F (44°C) in a day."}
{"weathersummary":"Chilly and sunny with warm temperatures of 80°F (27°C) and pleasant blue skies."}
I have uploaded the code. Try different variants and play with the code to learn more.
Thanks for reading till the end.
Do share in your comments how you feel about this new Microsoft package.