ollama-dotnet 0.6.1

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package ollama-dotnet --version 0.6.1
                    
NuGet\Install-Package ollama-dotnet -Version 0.6.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ollama-dotnet" Version="0.6.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ollama-dotnet" Version="0.6.1" />
                    
Directory.Packages.props
<PackageReference Include="ollama-dotnet" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ollama-dotnet --version 0.6.1
                    
#r "nuget: ollama-dotnet, 0.6.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ollama-dotnet@0.6.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ollama-dotnet&version=0.6.1
                    
Install as a Cake Addin
#tool nuget:?package=ollama-dotnet&version=0.6.1
                    
Install as a Cake Tool

Ollama .NET Library

Typed C# client for integrating .NET 10+ applications with Ollama.

Prerequisites

ollama pull gemma3

See Ollama Models for available models.

Install

dotnet add package O_Llama

Basic usage

using ollama_dotnet;

using var client = new OllamaClient();

var response = client.Chat(new ChatRequest
{
    Model = "gemma3",
    Messages =
    [
        new Message
        {
            Role = "user",
            Content = "Why is the sky blue?"
        }
    ]
});

Console.WriteLine(response.Message.Content);

The default local endpoint is http://127.0.0.1:11434.

Streaming responses

foreach (var part in client.ChatStream(new ChatRequest
{
    Model = "gemma3",
    Messages =
    [
        new Message
        {
            Role = "user",
            Content = "Why is the sky blue?"
        }
    ]
}))
{
    Console.Write(part.Message.Content);
}

Synchronous streaming methods:

  • GenerateStream
  • ChatStream
  • PullStream
  • PushStream
  • CreateStream

Cloud models

Use cloud models through a local Ollama installation:

ollama signin
ollama pull gpt-oss:120b-cloud
foreach (var part in client.ChatStream(new ChatRequest
{
    Model = "gpt-oss:120b-cloud",
    Messages =
    [
        new Message
        {
            Role = "user",
            Content = "Why is the sky blue?"
        }
    ]
}))
{
    Console.Write(part.Message.Content);
}

See Ollama Cloud Models for current model names.

Cloud API

Create an API key at Ollama.com > Settings > Keys, then set it in the environment.

PowerShell:

$env:OLLAMA_API_KEY = "your_api_key"

Other shells:

export OLLAMA_API_KEY=your_api_key

The client reads OLLAMA_API_KEY automatically:

using var client = new OllamaClient(host: OllamaConstants.WebApiHost);

var response = client.Chat(new ChatRequest
{
    Model = "gpt-oss:120b",
    Messages =
    [
        new Message { Role = "user", Content = "Why is the sky blue?" }
    ]
});

An explicit header can be supplied instead:

using var client = new OllamaClient(
    host: OllamaConstants.WebApiHost,
    headers: new Dictionary<string, string>
    {
        ["Authorization"] = "Bearer your_api_key"
    });

Custom client

Pass an existing HttpClient when your application owns handlers, proxies, diagnostics, or dependency injection:

var httpClient = new HttpClient
{
    BaseAddress = new Uri("http://localhost:11434")
};

using var client = new OllamaClient(httpClient: httpClient);

var response = client.Generate(new GenerateRequest
{
    Model = "gemma3",
    Prompt = "Why is the sky blue?"
});

An injected HttpClient is not disposed by OllamaClient.

using var client = new OllamaClient(
    host: "http://localhost:11434",
    followRedirects: true,
    timeout: TimeSpan.FromMinutes(5),
    headers: new Dictionary<string, string>
    {
        ["x-application-name"] = "my-app"
    });

Async client

AsyncOllamaClient supports asynchronous calls and cancellation:

 using var client = new AsyncOllamaClient();

var response = await client.ChatAsync(
    new ChatRequest
    {
        Model = "gemma3",
        Messages =
        [
            new Message { Role = "user", Content = "Why is the sky blue?" }
        ]
    },
    cancellationToken);

Console.WriteLine(response.Message.Content);

Async streaming returns IAsyncEnumerable<T>:

await foreach (var part in client.ChatStreamAsync(
    new ChatRequest
    {
        Model = "gemma3",
        Messages =
        [
            new Message { Role = "user", Content = "Why is the sky blue?" }
        ]
    },
    cancellationToken))
{
    Console.Write(part.Message.Content);
}

Async streaming methods:

  • GenerateStreamAsync
  • ChatStreamAsync
  • PullStreamAsync
  • PushStreamAsync
  • CreateStreamAsync

API

The client follows the Ollama REST API.

Chat

var response = client.Chat(new ChatRequest
{
    Model = "gemma3",
    Messages =
    [
        new Message { Role = "user", Content = "Why is the sky blue?" }
    ]
});

Generate

var response = client.Generate(new GenerateRequest
{
    Model = "gemma3",
    Prompt = "Why is the sky blue?"
});

List

var response = client.List();
foreach (var model in response.Models)
    Console.WriteLine(model.Name);

Show

var response = client.Show("gemma3");
Console.WriteLine(response.Details?.Family);

Create

var response = client.Create(new CreateRequest
{
    Model = "example",
    From = "gemma3",
    System = "You are a concise assistant."
});

Copy

var response = client.Copy("gemma3", "user/gemma3");
Console.WriteLine(response.Status);

Delete

var response = client.Delete("gemma3");
Console.WriteLine(response.Status);

Pull

var response = client.Pull(new PullRequest { Model = "gemma3" });

Push

var response = client.Push(new PushRequest { Model = "user/gemma3" });

Embed

var response = client.Embed(new EmbedRequest
{
    Model = "gemma3",
    Input = "The sky is blue because of Rayleigh scattering."
});

var vector = response.Embeddings[0];

Batch input is supported:

var response = client.Embed(new EmbedRequest
{
    Model = "gemma3",
    Input = new[]
    {
        "The sky is blue because of Rayleigh scattering.",
        "Grass is green because of chlorophyll."
    }
});

Legacy embeddings

var response = client.Embeddings(new EmbeddingsRequest
{
    Model = "gemma3",
    Prompt = "The sky is blue because of Rayleigh scattering."
});

Running models

var response = client.Ps();
foreach (var model in response.Models)
    Console.WriteLine($"{model.ModelName} - {model.SizeVram} bytes VRAM");

Web search and fetch

These operations require OLLAMA_API_KEY and use https://ollama.com:

var search = client.WebSearch("latest developments in space exploration");
foreach (var result in search.Results)
    Console.WriteLine($"{result.Title}: {result.Url}");

var page = client.WebFetch("https://ollama.com");
Console.WriteLine(page.Content);

Upload a blob

var digest = client.CreateBlob("model-data.bin");
Console.WriteLine(digest);

Images

Images can be supplied as Base64 data, bytes, or a file path:

var response = client.Generate(new GenerateRequest
{
    Model = "gemma3",
    Prompt = "Describe this image.",
    Images =
    [
        new Image("image-data-base64"),
        new Image(File.ReadAllBytes("image.jpg")),
        new Image("image.jpg", filePath: true)
    ]
});

Images are serialized as Base64 strings.

Request options

var response = client.Generate(new GenerateRequest
{
    Model = "gemma3",
    Prompt = "Write a short poem about the ocean.",
    Options = new Options
    {
        Temperature = 0.7,
        NumPredict = 128,
        TopP = 0.9,
        Stop = ["THE END"]
    }
});

Request properties use init accessors where possible, reducing accidental mutation after a request is configured.

Errors

Errors are raised for unsuccessful HTTP responses and errors reported in streaming responses:

try
{
    client.Chat(new ChatRequest
    {
        Model = "does-not-yet-exist",
        Messages =
        [
            new Message { Role = "user", Content = "Hello" }
        ]
    });
}
catch (ResponseException exception)
{
    Console.WriteLine($"Ollama error: {exception.Error}");
    Console.WriteLine($"Status code: {exception.StatusCode}");

    if (exception.StatusCode == 404)
        client.Pull(new PullRequest { Model = "does-not-yet-exist" });
}
catch (ConnectionException exception)
{
    Console.WriteLine(exception.Message);
}

ResponseException exposes the parsed error through Error and the HTTP status through StatusCode. ConnectionException indicates that Ollama could not be reached.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated