Implementation of generative AI abstractions for Ollama.
Deprecated Package Version
Reason: Other
This package is deprecated and the OllamaSharp package is recommended. OllamaSharp provides .NET bindings for the Ollama API, simplifying interactions with Ollama both locally and remotely. No further updates, features, or fixes are planned for the Microsoft.Extensions.AI.Ollama package.
Install
| Product | Versions |
|---|---|
| .NET | |
| .NET Standard | |
| Other |
README
Microsoft.Extensions.AI.Ollama
Provides an implementation of the IChatClient interface for Ollama.
Install the package
From the command-line:
dotnet add package Microsoft.Extensions.AI.Ollama
Or directly in the C# project file:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI.Ollama" Version="[CURRENTVERSION]" />
</ItemGroup>
Usage Examples
Chat
using Microsoft.Extensions.AI;
IChatClient client = new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.1");
Console.WriteLine(await client.CompleteAsync("What is AI?"));
Chat + Conversation History
using Microsoft.Extensions.AI;
IChatClient client = new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.1");
Console.WriteLine(await client.CompleteAsync(
[
new ChatMessage(ChatRole.System, "You are a helpful AI assistant"),
new ChatMessage(ChatRole.User, "What is AI?"),
]));
Chat Streaming
using Microsoft.Extensions.AI;
IChatClient client = new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.1");
await foreach (var update in client.CompleteStreamingAsync("What is AI?"))
{
Console.Write(update);
}
Tool Calling
Known limitations:
- Only a subset of models provided by Ollama support tool calling.
- Tool calling is currently not supported with streaming requests.
using System.ComponentModel;
using Microsoft.Extensions.AI;
IChatClient ollamaClient = new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.1");
IChatClient client = new ChatClientBuilder()
.UseFunctionInvocation()
.Use(ollamaClient);
ChatOptions chatOptions = new()
{
Tools = [AIFunctionFactory.Create(GetWeather)]
};
Console.WriteLine(await client.CompleteAsync("Do I need an umbrella?", chatOptions));
[Description("Gets the weather")]
static string GetWeather() => Random.Shared.NextDouble() > 0.5 ? "It's sunny" : "It's raining";
Caching
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
IDistributedCache cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
IChatClient ollamaClient = new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.1");
IChatClient client = new ChatClientBuilder()
.UseDistributedCache(cache)
.Use(ollamaClient);
for (int i = 0; i < 3; i++)
{
await foreach (var message in client.CompleteStreamingAsync("In less than 100 words, what is AI?"))
{
Console.Write(message);
}
Console.WriteLine();
Console.WriteLine();
}
Telemetry
using Microsoft.Extensions.AI;
using OpenTelemetry.Trace;
// Configure OpenTelemetry exporter
var sourceName = Guid.NewGuid().ToString();
var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddConsoleExporter()
.Build();
IChatClient ollamaClient = new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.1");
IChatClient client = new ChatClientBuilder()
.UseOpenTelemetry(sourceName, c => c.EnableSensitiveData = true)
.Use(ollamaClient);
Console.WriteLine(await client.CompleteAsync("What is AI?"));
Telemetry, Caching, and Tool Calling
using System.ComponentModel;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using OpenTelemetry.Trace;
// Configure telemetry
var sourceName = Guid.NewGuid().ToString();
var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddConsoleExporter()
.Build();
// Configure caching
IDistributedCache cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
// Configure tool calling
var chatOptions = new ChatOptions
{
Tools = [AIFunctionFactory.Create(GetPersonAge)]
};
IChatClient ollamaClient = new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.1");
IChatClient client = new ChatClientBuilder()
.UseDistributedCache(cache)
.UseFunctionInvocation()
.UseOpenTelemetry(sourceName, c => c.EnableSensitiveData = true)
.Use(ollamaClient);
for (int i = 0; i < 3; i++)
{
Console.WriteLine(await client.CompleteAsync("How much older is Alice than Bob?", chatOptions));
}
[Description("Gets the age of a person specified by name.")]
static int GetPersonAge(string personName) =>
personName switch
{
"Alice" => 42,
"Bob" => 35,
_ => 26,
};
Text embedding generation
using Microsoft.Extensions.AI;
IEmbeddingGenerator<string, Embedding<float>> generator =
new OllamaEmbeddingGenerator(new Uri("http://localhost:11434/"), "all-minilm");
var embeddings = await generator.GenerateAsync("What is AI?");
Console.WriteLine(string.Join(", ", embeddings[0].Vector.ToArray()));
Text embedding generation with caching
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
IDistributedCache cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
IEmbeddingGenerator<string, Embedding<float>> ollamaGenerator =
new OllamaEmbeddingGenerator(new Uri("http://localhost:11434/"), "all-minilm");
IEmbeddingGenerator<string, Embedding<float>> generator = new EmbeddingGeneratorBuilder<string, Embedding<float>>()
.UseDistributedCache(cache)
.Use(ollamaGenerator);
foreach (var prompt in new[] { "What is AI?", "What is .NET?", "What is AI?" })
{
var embeddings = await generator.GenerateAsync(prompt);
Console.WriteLine(string.Join(", ", embeddings[0].Vector.ToArray()));
}
Dependency Injection
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
// App Setup
var builder = Host.CreateApplicationBuilder();
builder.Services.AddSingleton<IDistributedCache>(new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())));
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace));
builder.Services.AddChatClient(b => b
.UseDistributedCache()
.UseLogging()
.Use(new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.1")));
var app = builder.Build();
// Elsewhere in the app
var chatClient = app.Services.GetRequiredService<IChatClient>();
Console.WriteLine(await chatClient.CompleteAsync("What is AI?"));
Minimal Web API
using Microsoft.Extensions.AI;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddChatClient(c =>
c.Use(new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.1")));
builder.Services.AddEmbeddingGenerator<string,Embedding<float>>(g =>
g.Use(new OllamaEmbeddingGenerator(endpoint, "all-minilm")));
var app = builder.Build();
app.MapPost("/chat", async (IChatClient client, string message) =>
{
var response = await client.CompleteAsync(message, cancellationToken: default);
return response.Message;
});
app.MapPost("/embedding", async (IEmbeddingGenerator<string,Embedding<float>> client, string message) =>
{
var response = await client.GenerateAsync(message);
return response[0].Vector;
});
app.Run();
Feedback & Contributing
We welcome feedback and contributions in our GitHub repo.
Adoption Assessment
A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.