Rayson.Ollama
2.0.3
dotnet add package Rayson.Ollama --version 2.0.3
NuGet\Install-Package Rayson.Ollama -Version 2.0.3
<PackageReference Include="Rayson.Ollama" Version="2.0.3" />
<PackageVersion Include="Rayson.Ollama" Version="2.0.3" />
<PackageReference Include="Rayson.Ollama" />
paket add Rayson.Ollama --version 2.0.3
#r "nuget: Rayson.Ollama, 2.0.3"
#:package Rayson.Ollama@2.0.3
#addin nuget:?package=Rayson.Ollama&version=2.0.3
#tool nuget:?package=Rayson.Ollama&version=2.0.3
Rayson.Ollama
.NET client library for Ollama with Docker support.
Installation
dotnet add package Rayson.Ollama
Note: This package creates 5 files in your project when you build, these are your ollama configuration files.
QuickStart
- Install the NuGet package and Build your project
- Move
ollama.Dockerfileand*.shscripts to suitable location - Merge
ollama.docker-compose.ymlinto your compose - Update
ollama>build>contextto files location - Configure
CUSTOM_MODELSwith your desired models - Configure
Ollama__Urlfor your deployment scenario (see Configuration>Docker section below) - Optionally add GPU support, see comment in compose file
Usage:
Using .NET's ServiceCollection for dependency injection:
using Rayson.Ollama.Extensions
services.AddOllama();
using Rayson.Ollama
var ollama = sp.GetRequiredService<IOllamaService>();
// Generate text (non-streaming)
var result = await ollama.GenerateAsync("Hello", "smollm:135m");
Console.WriteLine(result.Response);
// Generate with streaming
await foreach (var chunk in ollama.GenerateStreamAsync("Hello", "smollm:135m"))
{
Console.Write(chunk.Response);
}
// Chat (non-streaming)
var chatResult = await ollama.ChatAsync(
[new() { Role = "user", Content = "Hello" }],
"smollm:135m");
Console.WriteLine(chatResult.Message?.Content);
// Chat with streaming
await foreach (var chunk in ollama.ChatStreamAsync(
[new() { Role = "user", Content = "Hello" }],
"smollm:135m"))
{
Console.Write(chunk.Message?.Content);
}
// Generate embeddings
var embedding = await ollama.EmbedAsync("Hello", "nomic-embed-text");
Console.WriteLine(embedding.Embedding.Count);
Prerequisites
NVIDIA GPU Support
If using an NVIDIA GPU, install the NVIDIA Container Toolkit:
# Configure the repository
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey \
| sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
curl -fsSL https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list \
| sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' \
| sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt-get update
# Install the toolkit
sudo apt-get install -y nvidia-container-toolkit
# Configure Docker
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker
After installation, enable NVIDIA GPU support in ollama.docker-compose.yml by uncommenting the deploy section.
Docker Setup
Running Ollama
docker compose up --build
Note: By default, Ollama models are persisted in a Docker volume (
ollama_data). This preserves your downloaded models across container restarts.
Healthcheck
The docker-compose includes a healthcheck that polls the Ollama API every 10 seconds. You can check the health status with:
docker compose ps
Using Multiple Models
By default, the container starts with smollm:135m and nomic-embed-text. To configure models, edit the CUSTOM_MODELS environment variable in ollama.docker-compose.yml:
environment:
- CUSTOM_MODELS=["smollm:135m","nomic-embed-text"]
The CUSTOM_MODELS environment variable accepts a JSON array of model names. The entrypoint script will check if each model exists and pull it only if missing.
Configuration
Docker
Ollama container
| Environment Variable | Default | Description |
|---|---|---|
CUSTOM_MODELS |
["smollm:135m","nomic-embed-text"] |
Models to check/pull on startup |
Note: The
OLLAMA_HOSTin the Ollama container is hardcoded to0.0.0.0so connections from other containers are accepted. This could raise security issues for your environment.
Consumer container
| Environment Variable | Default | Description |
|---|---|---|
Ollama__Url |
http://ollama:11434 |
URL for calling containers configuration to reach Ollama API (Docker internal network) |
Connecting to Ollama
The Ollama__Url environment variable specifies the address other containers use to reach Ollama. Set this based on your deployment scenario:
- Container-to-container (recommended): Use
http://ollama:11434(Docker internal network) - Host machine access: Use
http://localhost:11434and addnetwork_mode: hostto your compose:
services:
your-app:
network_mode: host
.NET Client
Configure the Ollama URL in your .NET application to match the Ollama__Url value from your docker-compose:
Via docker-compose.yml:
services:
webapp:
build: .
image: my-example-app:latest
environment:
- Ollama__Url=http://ollama:11434
The URL should match the Ollama__Url value in your docker-compose.
API Reference
IOllamaService
// Generate text (non-streaming)
Task<GenerateResponse> GenerateAsync(string prompt, string model, GenerateOptions? options = null, CancellationToken ct = default)
// Generate text with streaming
IAsyncEnumerable<GenerateResponse> GenerateStreamAsync(string prompt, string model, GenerateOptions? options = null, CancellationToken ct = default)
// Chat (non-streaming)
Task<ChatResponse> ChatAsync(IList<OllamaMessage> messages, string model, ChatOptions? options = null, CancellationToken ct = default)
// Chat with streaming
IAsyncEnumerable<ChatResponse> ChatStreamAsync(IList<OllamaMessage> messages, string model, ChatOptions? options = null, CancellationToken ct = default)
// Generate embeddings
Task<EmbeddingsResponse> EmbedAsync(string input, string model, CancellationToken ct = default)
// List available models
Task<IEnumerable<ModelInfo>> ListModelsAsync(CancellationToken ct = default)
// Pull a model
Task PullModelAsync(string model, IProgress<string>? progress = null, CancellationToken ct = default)
// Delete a model
Task DeleteModelAsync(string model, CancellationToken ct = default)
Options
// Options for Generate/GenerateStream
public class GenerateOptions
{
public string? Model { get; set; }
public string? Prompt { get; set; }
public string? System { get; set; }
public string? Template { get; set; }
public string? Context { get; set; }
public bool Raw { get; set; }
public double? Temperature { get; set; }
public double? TopP { get; set; }
public int? TopK { get; set; }
public double? RepeatPenalty { get; set; }
public int? NumPredict { get; set; }
public IList<string>? Stop { get; set; }
}
// Options for Chat/ChatStream
public class ChatOptions
{
public string? Model { get; set; }
public string? System { get; set; }
public IList<OllamaMessage>? Messages { get; set; }
public double? Temperature { get; set; }
public double? TopP { get; set; }
public int? TopK { get; set; }
public double? RepeatPenalty { get; set; }
public int? NumPredict { get; set; }
public IList<string>? Stop { get; set; }
}
Response Types
public class OllamaMessage
{
public string? Role { get; set; }
public string? Content { get; set; }
}
public class OllamaResponse
{
public string? Model { get; set; }
public bool Done { get; set; }
public string? DoneReason { get; set; }
public long? TotalDuration { get; set; }
public long? LoadDuration { get; set; }
public int? PromptEvalCount { get; set; }
public int? EvalCount { get; set; }
public long? EvalDuration { get; set; }
}
public class GenerateResponse : OllamaResponse
{
public string? Response { get; set; }
}
public class ChatResponse : OllamaResponse
{
public OllamaMessage? Message { get; set; }
}
public record ModelInfo(string Name, string ModifiedAt, long Size);
public record EmbeddingsResponse(IReadOnlyList<double> Embedding);
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Http (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 9.0.0)
- System.Text.Json (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.