OllamaSharp
OllamaSharp provides .NET bindings for the Ollama API, simplifying interactions with Ollama both locally and remotely.
Verified
Core facts
Read by a script from the official API, not rewritten by a model.
- Repository
- https://github.com/awaescher/OllamaSharp
- License
- MIT
- Languages
- C#
- Maintainer
- awaescher
- Verified
What it is
OllamaSharp provides .NET bindings for the Ollama API. OllamaSharp wraps each Ollama API endpoint in awaitable methods that fully support response streaming.
Main features
The project README says interaction with Ollama takes just a few lines of code.
OllamaSharp can stream responses directly to the application.
OllamaSharp gives real-time progress feedback on tasks like model pulling.
OllamaSharp supports vision models.
The OllamaSharp Tool support documentation covers function calling with source generators.
Getting started
The OllamaSharp Getting started documentation covers installation, client setup and first steps.
OllamaSharp can also be used with Ollama cloud models.
For cloud models, use the constructor that takes an HttpClient and set it up to send the api key as default request header.
For .NET Native AOT scenarios, create a custom JsonSerializerContext with your types and pass it into the constructor.
The README recommends the Chat class for building conversational applications.
The Chat class automatically tracks the full message history, including tool calls and their results, across turns so the model has full context.
// 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";[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);// 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);
}var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:11434");
client.DefaultRequestHeaders.Add(/* your api key here */);
var ollama = new OllamaApiClient(client);Who it is for
Microsoft built an abstraction library, Microsoft.Extensions.AI, to streamline the usage of different AI providers. The OllamaApiClient implements both interfaces from Microsoft.Extensions.AI. Of these, IChatClient is used for model inference.
The IEmbeddingGenerator interface is used for embedding generation. SystemOneAsync maps to the System One endpoint, which applies to Ollama v0.35.0 or later. Instead of generating text, a System One model such as nimble answers choice, yes/no and score questions about a given state and returns probabilities.
The README also states that a system prompt can be set, images sent for vision models, structured JSON output requested, and thinking mode enabled for reasoning models. The README says to prefer OllamaSharp if you plan to use Ollama models only. The README says to prefer OllamaSharp if you want to use the native Ollama API, not only chats and embeddings but model management, usage information and more.
The README says to prefer Microsoft.Extensions.AI if you only need chat and embedding functionality. The README says to prefer Microsoft.Extensions.AI if you want to be able to use different providers like Ollama, OpenAI, Hugging Face, etc. The README says to prefer Semantic Kernel if you want to build agentic systems.
The README says to prefer Semantic Kernel if you need advanced prompt techniques like variable substitution and templating. The OllamaSharp Model Management documentation covers list, pull, push, copy, delete, show and embed. The OllamaSharp Chat and Generate documentation covers the Chat class, GenerateAsync, streaming, images, structured output and thinking models.
The OllamaSharp API reference is full auto-generated API docs.
Sources
This page is written from the official sources below. Open them to check the original text.
- GitHub READMEhttps://github.com/awaescher/OllamaSharpFetched
Verified ·
This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.