Chat with a local AI model using .NET
In this quickstart, you learn how to create a conversational .NET console chat app using an OpenAI or Azure OpenAI model. The app uses the Microsoft.Extensions.AI
library so you can write code using AI abstractions rather than a specific SDK. AI abstractions enable you to change the underlying AI model with minimal code changes.
Prerequisites
- Install .NET 8.0 or higher
- Install Ollama locally on your device
- Visual Studio Code (optional)
Run the local AI model
Complete the following steps to configure and run a local AI Model on your device. Many different AI models are available to run locally and are trained for different tasks, such as generating code, analyzing images, generative chat, or creating embeddings. For this quickstart, you'll use the general purpose phi3:mini
model, which is a small but capable generative AI created by Microsoft.
Open a terminal window and verify that Ollama is available on your device:
ollama
If Ollama is available, it displays a list of available commands.
Start Ollama:
ollama serve
If Ollama is running, it displays a list of available commands.
Pull the
phi3:mini
model from the Ollama registry and wait for it to download:ollama pull phi3:mini
After the download completes, run the model:
ollama run phi3:mini
Ollama starts the
phi3:mini
model and provides a prompt for you to interact with it.
Create the .NET app
Complete the following steps to create a .NET console app that will connect to your local phi3:mini
AI model:
In a terminal window, navigate to an empty directory on your device and create a new app with the
dotnet new
command:dotnet new console -o LocalAI
Add the Microsoft.Extensions.AI.Ollama packages to your app:
dotnet add package Microsoft.Extensions.AI.Ollama --prerelease
Open the new app in your editor of choice, such as Visual Studio Code.
code .
Connect to and chat with the AI model
The Semantic Kernel SDK provides many services and features to connect to AI models and manage interactions. In the steps ahead, you'll create a simple app that connects to the local AI and stores conversation history to improve the chat experience.
Open the Program.cs file and replace the contents of the file with the following code:
using Microsoft.Extensions.AI; IChatClient chatClient = new OllamaChatClient(new Uri("http://localhost:11434/"), "phi3:mini"); // Start the conversation with context for the AI model List<ChatMessage> chatHistory = new(); while (true) { // Get user prompt and add to chat history Console.WriteLine("Your prompt:"); var userPrompt = Console.ReadLine(); chatHistory.Add(new ChatMessage(ChatRole.User, userPrompt)); // Stream the AI response and add to chat history Console.WriteLine("AI Response:"); var response = ""; await foreach (var item in chatClient.CompleteStreamingAsync(chatHistory)) { Console.Write(item.Text); response += item.Text; } chatHistory.Add(new ChatMessage(ChatRole.Assistant, response)); Console.WriteLine(); }
The preceding code accomplishes the following:
- Creates an
OllamaChatClient
that implements theIChatClient
interface.- This interface provides a loosely coupled abstraction you can use to chat with AI Models.
- You can later change the underlying chat client implementation to another model, such as Azure OpenAI, without changing any other code.
- Creates a
ChatHistory
object to store the messages between the user and the AI model. - Retrieves a prompt from the user and stores it in the
ChatHistory
. - Sends the chat data to the AI model to generate a response.
Note
Ollama runs on port 11434 by default, which is why the AI model endpoint is set to
http://localhost:11434
.- Creates an
Run the app and enter a prompt into the console to receive a response from the AI, such as the following:
Your prompt: Tell me three facts about .NET. AI response: 1. **Cross-Platform Development:** One of the significant strengths of .NET, particularly its newer iterations (.NET Core and .NET 5+), is cross-platform support. It allows developers to build applications that run on Windows, Linux, macOS, and various other operating systems seamlessly, enhancing flexibility and reducing barriers for a wider range of users. 2. **Rich Ecosystem and Library Support:** .NET has an incredibly rich ecosystem, comprising an extensive collection of libraries (such as those provided by the official NuGet Package Manager), tools, and services. This allows developers to work on web applications (.NET Framework for desktop apps and ASP.NET Core for modern web applications), mobile applications (.NET MAUI or Xamarin.Forms), IoT solutions, AI/ML projects, and much more with a vast array of pre-built components available at their disposal. 3. **Type Safety:** .NET operates under the Common Language Infrastructure (CLI) model and employs managed code for executing applications. This approach inherently offers strong type safety checks which help in preventing many runtime errors that are common in languages like C/C++. It also enables features such as garbage collection, thus relieving developers from manual memory management. These characteristics enhance the reliability of .NET-developed software and improve productivity by catching issues early during development.
The response from the AI is accurate, but also verbose. The stored chat history enables the AI to modify its response. Instruct the AI to shorten the list it provided:
Your prompt: Shorten the length of each item in the previous response. AI Response: **Cross-platform Capabilities:** .NET allows building for various operating systems through platforms like .NET Core, promoting accessibility (Windows, Linux, macOS). **Extensive Ecosystem:** Offers a vast library selection via NuGet and tools for web (.NET Framework), mobile development (Maui/Xamarin.Forms), IoT, AI, providing rich capabilities to developers. **Type Safety & Reliability:** .NET's CLI model enforces strong typing and automatic garbage collection, mitigating runtime errors, thus enhancing application stability.
The updated response from the AI is much shorter the second time. Due to the available chat history, the AI was able to assess the previous result and provide shorter summaries.