![]() |
VOOZH | about |
dotnet add package Ollama --version 1.15.1
NuGet\Install-Package Ollama -Version 1.15.1
<PackageReference Include="Ollama" Version="1.15.1" />
<PackageVersion Include="Ollama" Version="1.15.1" />Directory.Packages.props
<PackageReference Include="Ollama" />Project file
paket add Ollama --version 1.15.1
#r "nuget: Ollama, 1.15.1"
#:package Ollama@1.15.1
#addin nuget:?package=Ollama&version=1.15.1Install as a Cake Addin
#tool nuget:?package=Ollama&version=1.15.1Install as a Cake Tool
👁 Nuget package
👁 dotnet
👁 License: MIT
👁 Discord
using var ollama = new OllamaClient();
// or if you have a custom server
// using var ollama = new OllamaClient(baseUri: new Uri("http://10.10.5.85:11434"));
var models = await ollama.ListAsync();
// Pulling a model and reporting progress
await foreach (var response in ollama.PullAsStreamAsync("all-minilm"))
{
Console.WriteLine($"{response.Status}. Progress: {response.Completed}/{response.Total}");
}
// or just pull the model and wait for it to finish
await ollama.PullAsStreamAsync("all-minilm").EnsureSuccessAsync();
// Generating an embedding
var embedding = await ollama.EmbedAsync(
model: "all-minilm",
input: "hello");
// Streaming a completion directly into the console
var enumerable = ollama.GenerateAsStreamAsync("llama3.2", "answer 5 random words");
await foreach (var response in enumerable)
{
Console.WriteLine($"> {response.Response}");
}
var lastResponse = await ollama.GenerateAsync("llama3.2", "answer 123");
Console.WriteLine(lastResponse.Response);
var chat = ollama.Chat("mistral");
while (true)
{
var message = await chat.SendAsync("answer 123");
Console.WriteLine(message.Content);
var newMessage = Console.ReadLine();
await chat.SendAsync(newMessage);
}
using var ollama = new OllamaClient();
var chat = ollama.Chat("mistral");
var message = await chat.SendAsync(
message: "answer 5 random words",
onResponseChunk: (isFirstChunk, chunk) =>
{
if (isFirstChunk)
{
Console.Write("> ");
}
Console.Write(chunk);
});
Console.WriteLine();
Console.WriteLine(message.Content);
using var ollama = new OllamaClient();
var chat = ollama.Chat(
model: "llama3.2",
systemMessage: "You are a helpful weather assistant.",
autoCallTools: true);
var service = new WeatherService();
chat.AddToolService(service.AsTools().AsOllamaTools(), service.AsCalls());
try
{
_ = await chat.SendAsync("What is the current temperature in Dubai, UAE in Celsius?");
}
finally
{
Console.WriteLine(chat.PrintMessages());
}
> System:
You are a helpful weather assistant.
> User:
What is the current temperature in Dubai, UAE in Celsius?
> Assistant:
Tool calls:
GetCurrentWeather({"location":"Dubai, UAE","unit":"celsius"})
> Tool:
{"location":"Dubai, UAE","temperature":22,"unit":"celsius","description":"Sunny"}
> Assistant:
The current temperature in Dubai, UAE is 22°C.
using CSharpToJsonSchema;
public enum Unit
{
Celsius,
Fahrenheit,
}
public class Weather
{
public string Location { get; set; } = string.Empty;
public double Temperature { get; set; }
public Unit Unit { get; set; }
public string Description { get; set; } = string.Empty;
}
[GenerateJsonSchema]
public interface IWeatherFunctions
{
[Description("Get the current weather in a given location")]
public Task<Weather> GetCurrentWeatherAsync(
[Description("The city and state, e.g. San Francisco, CA")] string location,
Unit unit = Unit.Celsius,
CancellationToken cancellationToken = default);
}
public class WeatherService : IWeatherFunctions
{
public Task<Weather> GetCurrentWeatherAsync(string location, Unit unit = Unit.Celsius, CancellationToken cancellationToken = default)
{
return Task.FromResult(new Weather
{
Location = location,
Temperature = 22.0,
Unit = unit,
Description = "Sunny",
});
}
}
The SDK implements IChatClient and IEmbeddingGenerator:
using Ollama;
using Microsoft.Extensions.AI;
using var ollama = new OllamaClient();
// IChatClient
IChatClient chatClient = ollama;
var response = await chatClient.GetResponseAsync(
[new ChatMessage(ChatRole.User, "Hello!")],
new ChatOptions { ModelId = "llama3.2" });
Console.WriteLine(response.Text);
// IEmbeddingGenerator
IEmbeddingGenerator<string, Embedding<float>> generator = ollama;
var embeddings = await generator.GenerateAsync(
["Hello, world!"],
new EmbeddingGenerationOptions { ModelId = "all-minilm" });
Console.WriteLine($"Dimensions: {embeddings[0].Vector.Length}");
Projects built on top of this SDK:
LangMate - A modular and extensible AI chat application platform built on this SDK:
Icon and name were reused from the amazing Ollama project.
The project was forked from this repository,
after which automatic code generation was applied based on the official Ollama OpenAPI specification.
await using var container = await Environment.PrepareAsync(TestModels.Chat);
var chat = container.Client.Chat(TestModels.Chat);
var message = await chat.SendAsync("answer 5 random words");
Console.WriteLine(message.Content);
await using var environment = await Environment.PrepareAsync(TestModels.Chat);
IChatClient client = environment.Client;
var updates = client.GetStreamingResponseAsync(
messages:
[
new MeaiChatMessage(MeaiChatRole.User, "Generate 5 random words.")
],
options: new ChatOptions
{
ModelId = TestModels.Chat,
});
var deltas = new List<string>();
await foreach (var update in updates)
{
if (!string.IsNullOrWhiteSpace(update.Text))
{
deltas.Add(update.Text);
}
}
await using var container = await Environment.PrepareAsync(TestModels.Chat);
var chat = container.Client.Chat(TestModels.Chat);
var deltas = new List<string>();
var message = await chat.SendAsync(
message: "answer 5 random words",
onResponseChunk: (isFirstChunk, chunk) =>
{
if (isFirstChunk)
{
Console.Write("> ");
}
if (chunk != null)
{
deltas.Add(chunk);
Console.Write(chunk);
}
});
Console.WriteLine();
await using var environment = await Environment.PrepareAsync(TestModels.Chat);
IChatClient client = environment.Client;
var response = await client.GetResponseAsync(
messages:
[
new MeaiChatMessage(MeaiChatRole.User, "Generate 5 random words.")
],
options: new ChatOptions
{
ModelId = TestModels.Chat,
});
using var client = new OllamaClient();
IChatClient chatClient = client;
var metadata = chatClient.GetService<ChatClientMetadata>();
using var client = new OllamaClient();
IChatClient chatClient = client;
var result = chatClient.GetService<ChatClientMetadata>(serviceKey: "unknown");
using var client = new OllamaClient();
IChatClient chatClient = client;
var self = chatClient.GetService<OllamaClient>();
await using var environment = await Environment.PrepareAsync(TestModels.Embeddings);
IEmbeddingGenerator<string, Embedding<float>> generator = environment.Client;
var result = await generator.GenerateAsync(
values: ["Hello, world!", "Goodbye, world!"],
options: new EmbeddingGenerationOptions
{
ModelId = TestModels.Embeddings,
});
using var client = new OllamaClient();
IEmbeddingGenerator<string, Embedding<float>> generator = client;
var metadata = generator.GetService<EmbeddingGeneratorMetadata>();
using var client = new OllamaClient();
IEmbeddingGenerator<string, Embedding<float>> generator = client;
var result = generator.GetService<EmbeddingGeneratorMetadata>(serviceKey: "unknown");
using var client = new OllamaClient();
IEmbeddingGenerator<string, Embedding<float>> generator = client;
var self = generator.GetService<OllamaClient>();
await using var environment = await Environment.PrepareAsync(TestModels.Embeddings);
IEmbeddingGenerator<string, Embedding<float>> generator = environment.Client;
var result = await generator.GenerateAsync(
values: ["Hello, world!"],
options: new EmbeddingGenerationOptions
{
ModelId = TestModels.Embeddings,
});
await using var container = await Environment.PrepareAsync(TestModels.Embeddings);
var embeddingResponse = await container.Client.EmbedAsync(
model: TestModels.Embeddings,
input: "hello");
await using var container = await Environment.PrepareAsync(TestModels.Chat);
var enumerable = container.Client.GenerateAsStreamAsync(TestModels.Chat, "answer 5 random words");
await foreach (var response in enumerable)
{
Console.WriteLine($"> {response.Response}");
}
var lastResponse = await container.Client.GenerateAsync(TestModels.Chat, "answer 123");
Console.WriteLine(lastResponse.Response);
await using var container = await Environment.PrepareAsync(TestModels.Chat);
var response = await container.Client.GenerateAsync(new GenerateRequest
{
Model = TestModels.Chat,
Prompt = "answer me just \"123\"",
Options = new ModelOptions
{
Temperature = 0,
},
});
Console.WriteLine(response.Response);
await using var container = await Environment.PrepareAsync(environmentType: EnvironmentType.Container);
var models = await container.Client.ListAsync();
var initialCount = models.Models?.Count ?? 0;
await container.Client.PullAsStreamAsync(TestModels.Embeddings).EnsureSuccessAsync();
models = await container.Client.ListAsync();
models.Models.Any(model =>
model.Model != null &&
await using var container = await Environment.PrepareAsync(environmentType: EnvironmentType.Container);
await foreach (var response in container.Client.PullAsStreamAsync(TestModels.Embeddings))
{
Console.WriteLine($"{response.Status}. Progress: {response.Completed}/{response.Total}");
}
var responses = await container.Client.PullAsStreamAsync(TestModels.Embeddings);
responses[^1].EnsureSuccess();
await container.Client.PullAsStreamAsync(TestModels.Embeddings).EnsureSuccessAsync();
await using var container = await Environment.PrepareAsync(TestModels.Reader);
var enumerable = container.Client.GenerateAsStreamAsync(
TestModels.Reader,
"""
<html>
<body>
<h3>Why is the sky blue?</h3>
<p>The sky appears blue because of the way light from the sun is reflected by the atmosphere. The atmosphere is made up of gases, including nitrogen and oxygen, which scatter light in all directions. This scattering causes the sunlight to appear as a rainbow of colors, with red light scattered more than other colors.
</p>
</body>
</html>
""");
await foreach (var response in enumerable)
{
Console.Write(response.Response);
}
// ### Why is the sky blue?
//
// The sky appears blue because of the way light from the sun is reflected by the atmosphere. The atmosphere is made up of gases, including nitrogen and oxygen, which scatter light in all directions. This scattering causes the sunlight to appear as a rainbow of colors, with red light scattered more than other colors.
var service = new WeatherService();
var tools = service.AsTools();
foreach (var tool in tools)
{
}
var ollamaTools = tools.AsOllamaTools();
foreach (var tool in ollamaTools)
{
}
await using var container = await Environment.PrepareAsync(TestModels.Chat);
var messages = new List<ChatMessage>
{
"You are a helpful weather assistant. Use the provided tools for weather questions.".AsSystemMessage(),
"What is the current temperature in Dubai, UAE in Celsius?".AsUserMessage(),
};
var model = TestModels.Chat;
try
{
var service = new WeatherService();
var tools = service.AsTools().AsOllamaTools();
var response = await container.Client.ChatAsync(
model,
messages,
tools: tools,
options: new ModelOptions
{
Temperature = 0,
Seed = 1,
});
var assistantMessage = response.Message ?? throw new InvalidOperationException("Expected a response message.");
messages.Add(assistantMessage.ToChatMessage());
foreach (var call in assistantMessage.ToolCalls!)
{
var argumentsAsJson = call.Function?.Arguments == null
? string.Empty
: call.Function.Arguments.AsJson();
var json = await service.CallAsync(
functionName: call.Function?.Name ?? string.Empty,
argumentsAsJson: argumentsAsJson);
messages.Add(json.AsToolMessage());
}
response = await container.Client.ChatAsync(
model,
messages,
tools: tools,
options: new ModelOptions
{
Temperature = 0,
Seed = 1,
});
messages.Add((response.Message ?? throw new InvalidOperationException("Expected a response message.")).ToChatMessage());
}
finally
{
Console.WriteLine(Ollama.Chat.PrintMessages(messages));
}
await using var container = await Environment.PrepareAsync(TestModels.Chat);
var chat = container.Client.Chat(
model: TestModels.Chat,
systemMessage: "You are a helpful weather assistant. Use the provided tools for weather questions.",
autoCallTools: true);
chat.Options = new ModelOptions
{
Temperature = 0,
Seed = 1,
};
var service = new WeatherService();
chat.AddToolService(service.AsTools().AsOllamaTools(), service.AsCalls());
try
{
_ = await chat.SendAsync("What is the current temperature in Dubai, UAE in Celsius?");
}
finally
{
Console.WriteLine(chat.PrintMessages());
}
Priority place for bugs: https://github.com/tryAGI/Ollama/issues
Priority place for ideas and general questions: https://github.com/tryAGI/Ollama/discussions
Discord: https://discord.gg/Ca2xhfBf3v
This project is supported by JetBrains through the Open Source Support Program.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
Showing the top 5 NuGet packages that depend on Ollama:
| Package | Downloads |
|---|---|
|
LangChain
LangChain meta-package with the most used things. |
|
|
LangChain.Providers.Ollama
Ollama Chat model provider. |
|
|
Richasy.AgentKernel.Connectors.Ollama
Agent Kernel connectors for Ollama. |
|
|
LangMate.Core
LangMate.Core is a lightweight, extensible .NET SDK designed to make working with Ollama-powered local AI models seamless and developer-friendly. It abstracts away the complexity of managing conversations, interacting with Ollama endpoints, and persisting chat history — all while offering resiliency, caching, and extensibility. |
|
|
LangMate.Abstractions
LangMate.Abstractions shared interfaces, contracts, and data transfer objects used across LangMate SDK. |
Showing the top 1 popular GitHub repositories that depend on Ollama:
| Repository | Stars |
|---|---|
|
tryAGI/LangChain
C# implementation of LangChain. We try to be as close to the original as possible in terms of abstractions, but are open to new entities.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 1.15.2-dev.3 | 38 | 6/16/2026 |
| 1.15.2-dev.2 | 75 | 5/21/2026 |
| 1.15.1 | 520 | 4/30/2026 |
| 1.15.1-dev.110 | 123 | 4/1/2026 |
| 1.15.1-dev.106 | 67 | 4/1/2026 |
| 1.15.1-dev.105 | 78 | 3/29/2026 |
| 1.15.1-dev.104 | 69 | 3/29/2026 |
| 1.15.1-dev.101 | 162 | 3/28/2026 |
| 1.15.1-dev.99 | 76 | 3/28/2026 |
| 1.15.1-dev.97 | 72 | 3/27/2026 |
| 1.15.1-dev.88 | 73 | 3/20/2026 |
| 1.15.1-dev.87 | 67 | 3/20/2026 |
| 1.15.1-dev.86 | 69 | 3/20/2026 |
| 1.15.1-dev.84 | 66 | 3/20/2026 |
| 1.15.1-dev.83 | 68 | 3/20/2026 |
| 1.15.1-dev.81 | 63 | 3/19/2026 |
| 1.15.1-dev.79 | 66 | 3/19/2026 |
| 1.15.1-dev.78 | 69 | 3/19/2026 |
| 1.15.1-dev.77 | 64 | 3/19/2026 |
| 1.15.1-dev.76 | 63 | 3/19/2026 |