New Tool Radar

Local AI

How to Call a Local Ollama Model from C# with Semantic Kernel

Call a local Ollama model from a C# console app with Semantic Kernel's Ollama connector, using your pulled model and no cloud key.

Schematic diagram with three steps: a C# console app adds the Semantic Kernel Ollama connector, the kernel points at localhost 11434 with model llama3.1, and InvokePromptAsync returns a local reply.

Semantic Kernel is Microsoft’s open-source SDK for adding AI to .NET applications, and it has an Ollama connector that lets a C# app talk to a model running on your own computer. The connector is a prerelease package, and the connection is one method call: register Ollama chat completion on the kernel builder with your model name and your local endpoint. No cloud account and no API key are involved.

Create the project and add the packages

You need the .NET 8 SDK or later, Ollama installed, and a model downloaded; the guide uses the llama3.1 (8B) model. Create a console project and add the two packages:

dotnet new console -o OllamaKernel
dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.SemanticKernel.Connectors.Ollama --version 1.34.0-alpha

Microsoft Learn lists the connector package as Microsoft.SemanticKernel.Connectors.Ollama v1.45.0-alpha and warns that some information relates to prerelease product that may be substantially modified before release. The connector is marked experimental, so expect warnings; the guide notes they can be ignored for now.

Build the kernel and ask a question

Create the kernel with the builder, using the method Microsoft Learn documents as AddOllamaChatCompletion with a modelId and an endpoint URI. The connector is experimental, so the code disables warning SKEXP0070:

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Ollama;

#pragma warning disable SKEXP0070
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddOllamaChatCompletion(modelId: "llama3.1", endpoint: new Uri("http://localhost:11434"));
Kernel kernel = builder.Build();

Then send a prompt and read the result. The guide’s program loops on console input and calls:

var result = await kernel.InvokePromptAsync(input, new(settings));

For the practice run, use the single question “Why is the sky blue?” as the input, and check that the printed result answers it about the sky. Run the app with dotnet build followed by dotnet run. The default port for Ollama is 11434, and you can change it by setting the OLLAMA_HOST environment variable.

Try it: three checks from packages to reply

Check What you should see What it means
Model pulled ollama pull llama3.1 finishes The model is on your machine
Project builds dotnet build completes, with only prerelease warnings about the experimental connector The packages and the kernel code are in place
Reply dotnet run, then asking “Why is the sky blue?”, prints an answer about the sky The C# app reached your local model

Expected result. We checked Microsoft Learn, which documents the AddOllamaChatCompletion method, its modelId and endpoint parameters, and the prerelease package, and the Semantic Kernel and Ollama guide, which gives the project commands, the kernel setup, and the InvokePromptAsync call. We did not create the project, install the packages, or run the app on this machine.

Limits

The Ollama connector is experimental and prerelease: Microsoft Learn carries a prerelease warning, the code needs #pragma warning disable SKEXP0070, and the API may change before release, so pin the package version the guide used rather than assuming the newest one matches. This is a .NET route only; Python developers should use the Ollama Python library or a Python framework instead. The model must be pulled before the app runs, and the endpoint must be your Ollama server — the default is http://localhost:11434. The connector can also carry plugins the model can call as tools, but this guide covers one plain question and answer only. We did not test any of this ourselves.

Sources

See all