- Buddhika Nelum Prabath
- Posts
- Build a chatbot with Microsoft.Extensions.AI
Build a chatbot with Microsoft.Extensions.AI
Unified AI Building Blocks for .NET
Introduction to Microsoft AI Extention
Microsoft has recently launched the Microsoft.AI.Abstraction and Microsoft.Extensions.AI libraries, which are currently in preview mode. These new packages provide the .NET ecosystem with crucial abstractions, enabling seamless integration of AI services into .NET applications and simplifying the development process for AI-powered solutions.
As I work with various AI tools for my current .NET projects, I appreciate having these packages available in the .NET ecosystem. They allow developers to seamlessly integrate AI capabilities into their applications, making development faster and more manageable while avoiding complex setup processes.
Benefits of Microsoft.Extentions.AI
Unified API: Delivers a consistent set of APIs and conventions for integrating AI services into .NET applications.
Flexibility: Allows .NET library authors to use AI services without being tied to a specific provider, making it adaptable to any provider.
Ease of Use: Enables .NET developers to experiment with different packages using the same underlying abstractions, maintaining a single API throughout their application.
Componentization: Simplifies adding new capabilities and facilitates the componentization and testing of applications.
Let’s do some coding
I'm going to create a simple chat application to demonstrate how we can use these packages to build various applications. For example, I'll develop a console-based AI assistant that answers questions about medical issues.
Before we start, ensure you have the following installed on your computer if you don't already.
Prerequisites
.NET 9 SDK
Visual Studio
Open AI API Key
Open Visual Studio and create a new console application. I name it AIBot.
Figure 1
Stick to the default settings here and create the project.
Figure 2
To begin creating the bot using OpenAI, we need to install the necessary NuGet package in our project. Navigate to your project, then select “Manage NuGet Packages ” In the search bar, look for the
Microsoft.Extensions.AI.OpenAI
package. If you can’t find the package, make sure to check the "Include prerelease” box.
Next, you need an OpenAI API key, which you can obtain by registering on the official OpenAI website.
I removed the boilerplate code that was automatically generated by Visual Studio. and wrote a simple program to start a conversation with a bot. Additionally, I included chat history to make the conversation more reliable and added an initial prompt to instruct the AI on how to handle user questions.
using Microsoft.Extensions.AI;
using OpenAI;
using System.Text.Json;
class Program
{
static async Task Main(string[] args)
{
IChatClient chatClient = new OpenAIClient("OPEN_AI_API_KEY").AsChatClient(modelId: "gpt-4o-mini");
var propmt = "You're a friendly, intelligent assistant who supports people with their medical issues. Please provide correct answers and do not answer any questions outside the domain. Make sure your answer is as simple and short as possible.";
var history = new List<ChatHistory>();
if (history.Count == 0)
{
history.Add(new ChatHistory
{
Role = "AiBot",
Message = propmt,
});
}
Console.WriteLine("AiBot -: Hi, I am AiBot, Feel free to ask anything!");
while (true)
{
Console.Write("User -: ");
var query = Console.ReadLine();
history.Add(new ChatHistory
{
Role = "User",
Message = query
});
var response = await chatClient.CompleteAsync(JsonSerializer.Serialize(history));
Console.WriteLine($"AiBot -: {response.Message.Text}");
history.Add(new ChatHistory
{
Role = "AiBot",
Message = response.Message.Text
});
}
}
}
public class ChatHistory
{
public string Role { get; set; } = string.Empty;
public string Message { get; set; } = string.Empty;
}
Here is the sample conversation I had with my bot.
I keep a history in an in-memory list to provide all previous conversations to the AI for accurate responses.
Instead of OpenAI, you can use other LLM providers like Ollama or AzureOpenAIClient. The only part you need to change is below.
Azure OpenAI
Install the Microsoft.Extensions.AI.OpenAI
, Azure.AI.OpenAI
, and Azure.Identity
NuGet packages.
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Extensions.AI;
IChatClient client =
new AzureOpenAIClient(
new Uri(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")),
new DefaultAzureCredential())
.AsChatClient(modelId: "gpt-4o-mini");
Ollama
install the Microsoft.Extensions.AI.Ollama
NuGet package.
using Microsoft.Extensions.AI;
IChatClient client =
new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.1");
As you can see it’s very simple to switch models,
Conclusion
I understand that this example is basic, but I wanted to provide you with a foundational understanding of how this works. We will explore this topic in more depth later.
Similarly to chat, we can also use a package to generate embeddings. However, that is a topic for another day.
You can find the GitHub repository for the above code here.