Logo

dev-resources.site

for different kinds of informations.

Local AI apps with C#, Semantic Kernel and Ollama

Published at
1/1/2025
Categories
ollama
semantickernel
csharp
dotnet
Author
skriyas
Author
7 person written this
skriyas
open
Local AI apps with C#, Semantic Kernel and Ollama

Welcome to my first post ever! Enough talk. Let's get started.

Impatient? GitHub.

Make sure that you have the following things with you in your local:

  • dotnet 8.0 and above
  • local ollama instance with an SLM like llama3.2

Project Setup

This is going to be a quick console app. Make sure to install all the nugets mentioned below.

dotnet new console -n sk-console -o sk-console
cd sk-console\
dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.SemanticKernel.Connectors.Ollama --prerelease
Enter fullscreen mode Exit fullscreen mode

*At the time of this writing, Semantic Kernel's Ollama connector is still in preview. So you might want to update the package command.

Coding Time

In your program.cs file

  • use the necessary packages
  • create a builder for Semantic Kernel using the Kernel class and inject the AddOllamaChatCompletion() service.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;

var builder = Kernel.CreateBuilder();
var uri = new Uri("http://localhost:11434");

#pragma warning disable SKEXP0070 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
builder.Services.AddOllamaChatCompletion("llama3.2", uri);
#pragma warning restore SKEXP0070 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
Enter fullscreen mode Exit fullscreen mode
  • Once after building the kernel get the chatCompletionService using which the chat invoked.
var kernel = builder.Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

try
{
    ChatMessageContent chatMessage = await chatCompletionService
                                    .GetChatMessageContentAsync("Hi, can you tell me a dad joke");
    Console.WriteLine(chatMessage.ToString());
}
Enter fullscreen mode Exit fullscreen mode

For my next post, I’ll implement use the same implementation in a WebAPI project.

That's it. Good day!

semantickernel Article's
25 articles in total
Favicon
Semantic Kernel: Crea un API para GeneraciΓ³n de Texto con Ollama y Aspire
Favicon
Azure OpenAI Error Handling in Semantic Kernel
Favicon
Local AI apps with C#, Semantic Kernel and Ollama
Favicon
Working with multiple language models in Semantic Kernel
Favicon
Lightweight AI Evaluation with SemanticKernel
Favicon
Chatbot with Semantic Kernel - Part 4: Whisper πŸ‘‚
Favicon
Step-by-Step Guide: Write Your First AI Storyteller with Ollama (llama3.2) and Semantic Kernel in C#
Favicon
Run LLMs Locally with Ollama & Semantic Kernel in .NET: A Quick Start
Favicon
OpenAI chat completion with Json output format
Favicon
Chatbot with Semantic Kernel - Part 3: Inspector & tokens πŸ”Ž
Favicon
Building a Digital Dungeon Master with Semantic Kernel, C#, and Azure
Favicon
Chatbot with Semantic Kernel - Part 2: Plugins 🧩
Favicon
Chatbot with Semantic Kernel - Part 1: Setup and first steps πŸ‘£
Favicon
Building a Semantic Kernel with F# for Enhanced AI Interaction
Favicon
Smooth Streaming Control with System.Threading.Channels in GPT API Programming
Favicon
Discover the Future of AI with My New Book: Developing GenAI Applications with LangChain, Semantic Kernel and Powered by LLMs
Favicon
Introducing Semantic Kernel
Favicon
Natural Programming ❀️️ with TypeChat & Semantic Kernel
Favicon
How to Use #SemanticKernel, Plugins – 2/N
Favicon
#SemanticKernel – πŸ“ŽChat Service demo running Phi-2 LLM locally with #LMStudio
Favicon
Harnessing Semantic Kernel for LLM Integration
Favicon
A Quick Walkthrough of Semantic Kernel's Kusto Connector for Vector Database Integration
Favicon
Semantic Tests for SemanticKernel Plugins using skUnit
Favicon
SemanticValidation: A Library for Semantic Checks with OpenAI
Favicon
Getting Started with Semantic Kernel and C#

Featured ones: