NextBuild

Working with LLMs in .NET using Microsoft․Extensions․AI

Working with LLMs in .NET using Microsoft․Extensions․AI

Working with LLMs in .NET using Microsoft․Extensions․AI

I've been experimenting to integrating LLMs into .NET apps, and I want to share what I've learned about using Microsoft․Extensions․AI.

Large Language Models (LLMs) have revolutionized how we approach AI-powered applications. While many developers are familiar with cloud-based solutions like OpenAI's GPT models, running LLMs locally has become increasingly accessible thanks to projects like Ollama.

In this article, we'll explore how to use LLMs in .NET applications using Microsoft.Extensions.AI, a powerful abstraction that extends the Semantic Kernel SDK.


Large language models (LLMs)

Large language models (LLMs) are a category of foundation models trained on immense amounts of data making them capable of understanding and generating natural language and other types of content to perform a wide range of tasks.




Ollama

Ollama is an open-source tool that runs large language models (LLMs) directly on a local machine. This makes it particularly appealing to AI developers, researchers, and businesses concerned with data control and privacy.

By running models locally, you maintain full data ownership and avoid the potential security risks associated with cloud storage. Offline AI tools like Ollama also help reduce latency and reliance on external servers, making them faster and more reliable.


Microsoft.Extensions.AI

Microsoft.Extensions.AI is a set of core .NET libraries developed in collaboration with developers across the .NET ecosystem, including Semantic Kernel. 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.


Getting Started

A few NuGet packages (I built this using a .NET 9 console application):


Install-Package Microsoft.Extensions.AI # The base AI library
Install-Package Microsoft.Extensions.AI.Ollama # Ollama provider implementation
Install-Package Microsoft.Extensions.Hosting # For building the DI container


Simple Chat Completion

Let's start with a basic example of chat completion. Here's the minimal setup:

using Microsoft.Extensions.AI;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

// Configure the OllamaChatClient
builder.Services.AddSingleton(_ =>
new OllamaChatClient(new Uri("http://localhost:11434/"), "phi4"));

Nothing fancy here - we're just setting up dependency injection and asking a simple question. If you're used to using raw API calls, you'll notice how clean this feels.


Implementing Chat with History


using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.AI;

namespace OllamaMVC.Controllers
{
public class ChatController : Controller
{
private readonly IChatClient _chatClient;
private static List _chatHistory = new();

public ChatController(IChatClient chatClient)
{
_chatClient = chatClient;
}
[HttpGet]
public IActionResult Index()
{
return View();
}

[HttpPost]
public async Task Index(string userPrompt)
{
Console.WriteLine($"User Prompt: {userPrompt}");

if (!string.IsNullOrWhiteSpace(userPrompt))
{
_chatHistory.Add(new ChatMessage(ChatRole.User, userPrompt));
var responseText = string.Empty;
await foreach (var item in _chatClient.CompleteStreamingAsync(_chatHistory))
{
Console.Write(item.Text);
responseText += item.Text;
}

_chatHistory.Add(new ChatMessage(ChatRole.Assistant, responseText));
ViewBag.Response = responseText;
Console.WriteLine($"AI Response: {responseText}");
}
else
{
ViewBag.Error = "Prompt cannot be empty.";
Console.WriteLine("Error: Prompt is empty.");
}

return View();
}

}
}

That's All Set
  • Share:

  • Share on X