Skip to main content
net8.0

Local LLM chat completions using Microsoft.Extensions.AI and ONNX Runtime GenAI. IChatClient implementation for running LLMs locally.

Published
Mar 28, 2026
6mo ago
Total Downloads
5.9K
5,895

Install

dotnet add package ElBruno.LocalLLMs --version 0.7.1
ProductVersions
.NET
net8.0

README

ElBruno.LocalLLMs

NuGet NuGet Downloads Build Status License: MIT HuggingFace .NET GitHub stars Twitter Follow

Run local LLMs in .NET through IChatClient 🧠

Run local LLMs in .NET through IChatClient β€” the same interface you'd use for Azure OpenAI, Ollama, or any other provider. Powered by ONNX Runtime GenAI.

Features

  • πŸ”Œ IChatClient implementation β€” seamless integration with Microsoft.Extensions.AI
  • πŸ“¦ Automatic model download β€” models are fetched from HuggingFace on first use
  • πŸš€ Zero friction β€” works out of the box with sensible defaults (Phi-3.5 mini)
  • πŸ–₯️ Multi-hardware β€” CPU, CUDA, and DirectML execution providers
  • πŸ’‰ DI-friendly β€” register with AddLocalLLMs() in ASP.NET Core
  • πŸ”„ Streaming β€” token-by-token streaming via GetStreamingResponseAsync
  • πŸ“Š Multi-model β€” switch between Phi-3.5, Phi-4, Qwen2.5, Llama 3.2, and more
  • 🎯 Fine-tuned models β€” pre-trained Qwen2.5 variants for tool calling and RAG (guide)

Installation

dotnet add package ElBruno.LocalLLMs

Then add one runtime package depending on your target hardware:

# πŸ–₯️ CPU (works everywhere β€” required for CPU-only apps):
dotnet add package Microsoft.ML.OnnxRuntimeGenAI

# 🟒 NVIDIA GPU (CUDA):
dotnet add package Microsoft.ML.OnnxRuntimeGenAI.Cuda

# πŸ”΅ Any Windows GPU β€” AMD, Intel, NVIDIA (DirectML):
dotnet add package Microsoft.ML.OnnxRuntimeGenAI.DirectML

⚠️ Add exactly one runtime package. Do not reference both Microsoft.ML.OnnxRuntimeGenAI and Microsoft.ML.OnnxRuntimeGenAI.Cuda simultaneously β€” the native binaries conflict and GPU support will silently fail.

πŸš€ The library defaults to ExecutionProvider.Auto β€” it tries GPU first and falls back to CPU automatically. No code changes needed.

Quick Start

using ElBruno.LocalLLMs;
using Microsoft.Extensions.AI;

// Create a local chat client (downloads Phi-3.5 mini on first run)
using var client = await LocalChatClient.CreateAsync();

var response = await client.GetResponseAsync([
    new(ChatRole.User, "What is the capital of France?")
]);

Console.WriteLine(response.Text);

Streaming

using ElBruno.LocalLLMs;
using Microsoft.Extensions.AI;

using var client = await LocalChatClient.CreateAsync(new LocalLLMsOptions
{
    Model = KnownModels.Phi35MiniInstruct
});

await foreach (var update in client.GetStreamingResponseAsync([
    new(ChatRole.System, "You are a helpful assistant."),
    new(ChatRole.User, "Explain quantum computing in simple terms.")
]))
{
    Console.Write(update.Text);
}

Model Metadata

Inspect model capabilities at runtime β€” context window size, model name, and vocabulary:

using var client = await LocalChatClient.CreateAsync();

var metadata = client.ModelInfo;
Console.WriteLine($"Model:          {metadata?.ModelName}");
Console.WriteLine($"Context window: {metadata?.MaxSequenceLength}");
Console.WriteLine($"Vocab size:     {metadata?.VocabSize}");

This is useful for prompt-length validation, adaptive chunking, and model selection logic.

Dependency Injection

builder.Services.AddLocalLLMs(options =>
{
    options.Model = KnownModels.Phi35MiniInstruct;
    options.ExecutionProvider = ExecutionProvider.DirectML;
});

// Inject IChatClient anywhere
public class MyService(IChatClient chatClient) { ... }

Supported Models

Tier Model Parameters ONNX ID
βšͺ Tiny TinyLlama-1.1B-Chat 1.1B βœ… Native tinyllama-1.1b-chat
βšͺ Tiny SmolLM2-1.7B-Instruct 1.7B βœ… Native smollm2-1.7b-instruct
βšͺ Tiny Qwen2.5-0.5B-Instruct 0.5B βœ… Native qwen2.5-0.5b-instruct
βšͺ Tiny Qwen2.5-1.5B-Instruct 1.5B βœ… Native qwen2.5-1.5b-instruct
βšͺ Tiny Gemma-2B-IT 2B βœ… Native gemma-2b-it
βšͺ Tiny StableLM-2-1.6B-Chat 1.6B πŸ”„ Convert stablelm-2-1.6b-chat
🟒 Small Phi-3.5 mini instruct 3.8B βœ… Native phi-3.5-mini-instruct
🟒 Small Qwen2.5-3B-Instruct 3B βœ… Native qwen2.5-3b-instruct
🟒 Small Llama-3.2-3B-Instruct 3B βœ… Native llama-3.2-3b-instruct
🟒 Small Gemma-2-2B-IT 2B βœ… Native gemma-2-2b-it
🟑 Medium Qwen2.5-7B-Instruct 7B βœ… Native qwen2.5-7b-instruct
🟑 Medium Llama-3.1-8B-Instruct 8B βœ… Native llama-3.1-8b-instruct
🟑 Medium Mistral-7B-Instruct-v0.3 7B βœ… Native mistral-7b-instruct-v0.3
🟑 Medium Gemma-2-9B-IT 9B βœ… Native gemma-2-9b-it
🟑 Medium Phi-4 14B βœ… Native phi-4
🟑 Medium DeepSeek-R1-Distill-Qwen-14B 14B βœ… Native deepseek-r1-distill-qwen-14b
🟑 Medium Mistral-Small-24B-Instruct 24B βœ… Native mistral-small-24b-instruct
πŸ”΄ Large Qwen2.5-14B-Instruct 14B βœ… Native qwen2.5-14b-instruct
πŸ”΄ Large Qwen2.5-32B-Instruct 32B βœ… Native qwen2.5-32b-instruct
πŸ”΄ Large Llama-3.3-70B-Instruct 70B βœ… ONNX llama-3.3-70b-instruct
πŸ”΄ Large Mixtral-8x7B-Instruct-v0.1 8x7B πŸ”„ Convert mixtral-8x7b-instruct-v0.1
πŸ”΄ Large DeepSeek-R1-Distill-Llama-70B 70B πŸ”„ Convert deepseek-r1-distill-llama-70b
πŸ”΄ Large Command-R (35B) 35B πŸ”„ Convert command-r-35b

Fine-Tuned Models

Pre-trained variants optimized for specific tasks. A fine-tuned 0.5B model often matches or exceeds a base 1.5B on its specialized task.

Model Size Task HuggingFace ID
Qwen2.5-0.5B-ToolCalling ~1 GB Tool/function calling elbruno/Qwen2.5-0.5B-LocalLLMs-ToolCalling
Qwen2.5-0.5B-RAG ~1 GB RAG with citations elbruno/Qwen2.5-0.5B-LocalLLMs-RAG
Qwen2.5-0.5B-Instruct ~1 GB General-purpose elbruno/Qwen2.5-0.5B-LocalLLMs-Instruct

See the Supported Models Guide for detailed model cards, performance benchmarks, and selection guidance.

Samples

Sample Description
HelloChat Minimal console chat
StreamingChat Token-by-token streaming
MultiModelChat Switch models at runtime
DependencyInjection ASP.NET Core DI registration
ToolCallingAgent Function calling and tool use
FineTunedToolCalling Fine-tuned model for improved tool calling
RagChatbot RAG pipeline with document retrieval
ConsoleAppDemo Interactive console application

Requirements

  • .NET 8.0 or .NET 10.0
  • CPU (default), NVIDIA GPU (CUDA), or Windows GPU (DirectML)
  • ~2-8 GB disk space per model (depending on size and quantization)

Building from Source

git clone https://github.com/elbruno/ElBruno.LocalLLMs.git
cd ElBruno.LocalLLMs
dotnet restore ElBruno.LocalLLMs.slnx
dotnet build ElBruno.LocalLLMs.slnx
dotnet test ElBruno.LocalLLMs.slnx --framework net8.0

Documentation

🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License β€” see the LICENSE file for details.

πŸ‘‹ About the Author

Made with ❀️ by Bruno Capuano (ElBruno)

πŸ™ Acknowledgments

Changes from Previous Version

No structural changes between v0.7.0 and v0.7.1.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.