awaescher / OllamaSharp
The easiest way to use Ollama in .NET
AI Architecture Analysis
This repository is indexed by RepoMind. By analyzing awaescher/OllamaSharp in our AI interface, you can instantly generate complete architecture diagrams, visualize control flows, and perform automated security audits across the entire codebase.
Our Agentic Context Augmented Generation (Agentic CAG) engine loads full source files into context on-demand, avoiding the fragmentation of traditional RAG systems. Ask questions about the architecture, dependencies, or specific features to see it in action.
Repository Overview (README excerpt)
Crawler viewOllamaSharp 🦙 OllamaSharp provides .NET bindings for the Ollama API, simplifying interactions with Ollama both locally and remotely. **🏆 Recommended by Microsoft** Features • **Ease of use:** Interact with Ollama in just a few lines of code. • **Reliability**: Powering Microsoft Semantic Kernel, .NET Aspire and Microsoft.Extensions.AI • **API coverage:** Covers every single Ollama API endpoint, including chats, embeddings, listing models, pulling and creating new models, and more. • **Real-time streaming:** Stream responses directly to your application. • **Progress reporting:** Real-time progress feedback on tasks like model pulling. • **Tools engine:** Sophisticated tool support with source generators. • **Multi modality:** Support for vision models. • **Native AOT support:** Opt-in support for Native AOT for improved performance. 📖 Documentation | Topic | Description | |---|---| | Getting started | Installation, client setup and first steps | | Chat and Generate | class, , 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 | Usage 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 demo application that's included in this repository** Initializing Native AOT Support For .NET Native AOT scenarios, create a custom JsonSerializerContext with your types and pass it into the constructor. See the Native AOT documentation for detailed guidance. Listing all models that are available locally Pulling a model and reporting progress Generating a completion directly into the console maps to the endpoint and is ideal for single-turn, context-free completions. Building interactive chats The 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. 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. Usage with Microsoft.Extensions.AI 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 and that makes it possible to use Ollama just like every other chat provider. To do this, simply use the as instead of . The implements both interfaces from Microsoft.Extensions.AI, you just need to cast it accordingly: • for model inference • for embedding generation Cloud models aka Ollama Turbo OllamaSharp can be used with Ollama cloud models as well. Use the constructor that takes an and set it up to send the api key as default request header. OllamaSharp vs. Microsoft.Extensions.AI vs. Semantic Kernel 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 ... • you plan to use Ollama models only • you want to use the native Ollama API, not only chats and embeddings but model management, usage information and more Prefer Microsoft.Extensions.AI if ... • you only need chat and embedding functionality • you want to be able to use different providers like Ollama, OpenAI, Hugging Face, etc. Prefer Semantic Kernel if ... • you need the highest flexibility with different providers, plugins, middlewares, caching, memory and more • you need advanced prompt techniques like variable substitution and templating • you want to build agentic systems No matter which one you choose, OllamaSharp should always be the bridge to Ollama behind the scenes as recommended by Microsoft (1) (2) (3). Thanks **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.