![]() |
VOOZH | about |
dotnet add package OllamaSharp --version 5.4.25
NuGet\Install-Package OllamaSharp -Version 5.4.25
<PackageReference Include="OllamaSharp" Version="5.4.25" />
<PackageVersion Include="OllamaSharp" Version="5.4.25" />Directory.Packages.props
<PackageReference Include="OllamaSharp" />Project file
paket add OllamaSharp --version 5.4.25
#r "nuget: OllamaSharp, 5.4.25"
#:package OllamaSharp@5.4.25
#addin nuget:?package=OllamaSharp&version=5.4.25Install as a Cake Addin
#tool nuget:?package=OllamaSharp&version=5.4.25Install as a Cake Tool
👁 nuget version
👁 nuget downloads
👁 Api docs
OllamaSharp provides .NET bindings for the Ollama API, simplifying interactions with Ollama both locally and remotely.
| Topic | Description |
|---|---|
| Getting started | Installation, client setup and first steps |
| Chat and Generate | Chat class, GenerateAsync, streaming, images, structured output, thinking models |
| Model Management | List, pull, push, copy, delete, show, embed |
| Tool support | Function calling with source generators |
| Advanced Configuration | Configuration options that go beyond the basics |
| Native AOT support | Ahead-of-Time compilation guidance |
| API reference | Full auto-generated API docs |
OllamaSharp wraps each Ollama API endpoint in awaitable methods that fully support response streaming.
The following list shows a few simple code examples.
ℹ Try our full featured that's included in this repository
// set up the client
var uri = new Uri("http://localhost:11434");
var ollama = new OllamaApiClient(uri);
// select a model which should be used for further operations
ollama.SelectedModel = "qwen3.5:35b-a3b";
For .NET Native AOT scenarios, create a custom JsonSerializerContext with your types and pass it into the constructor.
[JsonSerializable(typeof(MyCustomType))]
public partial class MyJsonContext : JsonSerializerContext { }
// Use the static factory method for NativeAOT
var ollama = new OllamaApiClient(uri, "qwen3.5:35b-a3b", MyJsonContext.Default);
See the for detailed guidance.
var models = await ollama.ListLocalModelsAsync();
await foreach (var status in ollama.PullModelAsync("qwen3.5:35b-a3b"))
Console.WriteLine($"{status.Percent}% {status.Status}");
GenerateAsync maps to the /api/generate endpoint and is ideal for single-turn, context-free completions.
await foreach (var stream in ollama.GenerateAsync("How are you today?"))
Console.Write(stream.Response);
The Chat class is the recommended way to build conversational applications. It automatically tracks the full message history (including tool calls and their results) across turns so the model always has full context.
// messages including their roles and tool calls will automatically be tracked within the chat object
// and are accessible via the Messages property
var chat = new Chat(ollama);
while (true)
{
var message = Console.ReadLine();
await foreach (var answerToken in chat.SendAsync(message))
Console.Write(answerToken);
}
You can also set a system prompt, send images for vision models, request structured JSON output, and enable thinking mode for reasoning models. See the Chat and Generate documentation for the full guide.
Microsoft built an abstraction library to streamline the usage of different AI providers. This is a really interesting concept if you plan to build apps that might use different providers, like ChatGPT, Claude and local models with Ollama.
I encourage you to read their accouncement Introducing Microsoft.Extensions.AI Preview – Unified AI Building Blocks for .NET.
OllamaSharp is the first full implementation of their IChatClient and IEmbeddingGenerator that makes it possible to use Ollama just like every other chat provider.
To do this, simply use the OllamaApiClient as IChatClient instead of IOllamaApiClient.
// install package Microsoft.Extensions.AI.Abstractions
private static IChatClient CreateChatClient(Arguments arguments)
{
if (arguments.Provider.Equals("ollama", StringComparison.OrdinalIgnoreCase))
return new OllamaApiClient(arguments.Uri, arguments.Model);
else
return new OpenAIChatClient(new OpenAI.OpenAIClient(arguments.ApiKey), arguments.Model); // ChatGPT or compatible
}
The OllamaApiClient implements both interfaces from Microsoft.Extensions.AI, you just need to cast it accordingly:
IChatClient for model inferenceIEmbeddingGenerator<string, Embedding<float>> for embedding generationOllamaSharp can be used with Ollama cloud models as well. Use the constructor that takes an HttpClient and set it up to send the api key as default request header.
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:11434");
client.DefaultRequestHeaders.Add(/* your api key here */);
var ollama = new OllamaApiClient(client);
It can be confusing which library to use with AI in C#. The following paragraph should help you decide which library to start with.
Prefer OllamaSharp if ...
Prefer Microsoft.Extensions.AI if ...
Prefer Semantic Kernel if ...
No matter which one you choose, OllamaSharp should always be the bridge to Ollama behind the scenes as recommended by Microsoft (1) (2) (3).
I would like to thank all the contributors who take the time to improve OllamaSharp. First and foremost mili-tan, who always keeps OllamaSharp in sync with the Ollama API.
The icon and name were reused from the amazing Ollama project.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on OllamaSharp:
| Package | Downloads |
|---|---|
|
Microsoft.SemanticKernel.Connectors.Ollama
Semantic Kernel connector for Ollama. Contains services for text generation, chat completion and text embeddings. |
|
|
CommunityToolkit.Aspire.OllamaSharp
An Aspire client integration for the OllamaSharp library. |
|
|
Microsoft.KernelMemory.AI.Ollama
Provide access to Ollama LLM models in Kernel Memory to generate embeddings and text |
|
|
CommunityToolkit.Aspire.Hosting.Ollama
An Aspire integration leveraging the Ollama container with support for downloading a model on startup. |
|
|
OllamaSharp.ModelContextProtocol
Use tools from model context protocol (MCP) servers with Ollama |
Showing the top 20 popular GitHub repositories that depend on OllamaSharp:
| Repository | Stars |
|---|---|
|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
|
Sylinko/Everywhere
On-screen aware AI assistant for your desktop. Uses current app context, multiple LLMs, and MCP tools to help you act across apps.
|
|
|
testcontainers/testcontainers-dotnet
A library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions.
|
|
|
dotnet/extensions
This repository contains a suite of libraries that provide facilities commonly needed when creating production-ready applications.
|
|
|
microsoft/Generative-AI-for-beginners-dotnet
Five lessons, learn how to really apply AI to your .NET Applications
|
|
|
microsoft/kernel-memory
Research project. A Memory solution for users, teams, and applications.
|
|
|
microsoft/ai-dev-gallery
An open-source project for Windows developers to learn how to add AI with local models and APIs to Windows apps.
|
|
|
colinin/abp-next-admin
这是基于vue-vben-admin 模板适用于abp vNext的前端管理项目
|
|
|
getcellm/cellm
Use LLMs in Excel formulas
|
|
| dotnet/ai-samples | |
|
CommunityToolkit/Aspire
A community project with additional components and extensions for Aspire
|
|
|
anobaka/Bakabase
A local media manager for all types of files. 二次元老司机专用的本地媒体文件管理器,支持管理和处理音视频、本子、图集、小说、哔哩哔哩视频、游戏甚至mod等各类资源,集成steam、DLsite、exhentai等平台。
|
|
|
PowerShell/AIShell
An interactive shell to work with AI-powered assistance providers
|
|
|
junkai-li/NetCoreKevin
🤖基于.NET搭建的企业级中台AI知识库智能体开源架构:Skills技能管理、AI-Qdrant知识库、知识库重排模型、AI联网搜索、多智能体协同、聊天记录压缩策略、智能体权限管控、AgentFramework、RAG检索增强、本地Ollama AI模型调用、智能体技能可控加载、领域事件、一库多租户、Log4、Jwt、CAP、SignalR、Mcp、Ioc、Hangfire、RabbitMQ、Xunit、前端(Vue + Ant Design)
|
|
|
afrise/MCPSharp
MCPSharp is a .NET library that helps you build Model Context Protocol (MCP) servers and clients - the standardized API protocol used by AI assistants and models.
|
|
|
axzxs2001/Asp.NetCoreExperiment
原来所有项目都移动到**OleVersion**目录下进行保留。新的案例装以.net 5.0为主,一部分对以前案例进行升级,一部分将以前的工作经验总结出来,以供大家参考!
|
|
|
rwjdk/MicrosoftAgentFrameworkSamples
Samples demonstrating the Microsoft Agent Framework in C#
|
|
|
signumsoftware/framework
Open Source framework for writing data-centric applications using the latest versions of .Net Core, C# (not-nullable), ASP.NET Web API, Typescript (strict), React, D3 and Sql Server or PostgreeSQL
|
|
|
lindexi/lindexi_gd
博客用到的代码
|
|
|
petabridge/memorizer
Vector-search powered agent memory MCP server
|
| Version | Downloads | Last Updated |
|---|---|---|
| 5.4.25 | 235,336 | 3/24/2026 |
| 5.4.24 | 16,212 | 3/17/2026 |
| 5.4.23 | 40,536 | 3/4/2026 |
| 5.4.22 | 1,941 | 3/4/2026 |
| 5.4.21 | 1,825 | 3/4/2026 |
| 5.4.20 | 1,955 | 3/4/2026 |
| 5.4.19 | 2,155 | 3/4/2026 |
| 5.4.18 | 17,882 | 2/27/2026 |
| 5.4.17 | 2,114 | 2/27/2026 |
| 5.4.16 | 88,184 | 1/28/2026 |
| 5.4.15 | 1,902 | 1/28/2026 |
| 5.4.13 | 4,584 | 1/26/2026 |
| 5.4.12 | 363,553 | 12/5/2025 |
| 5.4.11 | 80,932 | 11/14/2025 |
| 5.4.10 | 17,851 | 11/11/2025 |
| 5.4.9 | 4,971 | 11/10/2025 |
| 5.4.8 | 176,082 | 10/17/2025 |
| 5.4.7 | 47,107 | 9/25/2025 |
| 5.4.6 | 56,823 | 9/22/2025 |
| 5.4.5 | 11,910 | 9/19/2025 |