Microsoft.ML.OnnxRuntimeGenAI 0.14.1 (original) (raw)

About

Run Llama, Phi (language and multi modal!), DeepSeek, Gemma, Mistral with ONNX Runtime.

This API gives you an easy, flexible and performant way of running LLMs on device using .NET/C#.

It implements the generative AI loop for ONNX models, including pre and post processing, inference with ONNX Runtime, logits processing, search and sampling, and KV cache management.

Key Features

Sample

// See https://aka.ms/new-console-template for more information
using Microsoft.ML.OnnxRuntimeGenAI;

using OgaHandle ogaHandle = new OgaHandle();

// Specify the location of your downloaded model.
// Many models are published on HuggingFace e.g. 
// https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx
string modelPath = "..."
Console.WriteLine("Model path: " + modelPath);

using Model model = new Model(modelPath);
using Tokenizer tokenizer = new Tokenizer(model);
using var tokenizerStream = tokenizer.CreateStream();

// Set your prompt here
string prompt = "public static bool IsPrime(int number)";
var sequences = tokenizer.Encode($"<|user|>{prompt}<|end|><|assistant|>");

using GeneratorParams generatorParams = new GeneratorParams(model);
generatorParams.SetSearchOption("max_length", 512);

using var generator = new Generator(model, generatorParams);
generator.AppendTokenSequences(sequences);
.
while (!generator.IsDone())
{
    generator.GenerateNextToken();
    Console.Write(tokenizerStream.Decode(generator.GetSequence(0)[^1]));
}

Generates the following output:

Here's a complete implementation of the `IsPrime` function in C# that checks if a given number is prime. The function includes basic input validation and comments for clarity.
using System;

namespace PrimeChecker
{
    public class PrimeChecker
    {
        /// <summary>
        /// Checks if the given number is prime.
        /// </summary>
        /// <param name="number">The number to check.</param>
        /// <returns>true if the number is prime; otherwise, false.</returns>
        public static bool IsPrime(int number)
        {
            // Input validation
            if (number < 2)
            {
                return false;
            }

            // 2 is the only even prime number
            if (number == 2)
            {
                return true;
            }

            // Exclude even numbers greater than 2
            if (number % 2 == 0)
            {
                return false;
            }

            // Check for factors up to the square root of the number
            int limit = (int)Math.Floor(Math.Sqrt(number));
            for (int i = 3; i <= limit; i += 2)
            {
                if (number % i == 0)
                {
                    return false;
                }
            }

            return true;
        }

        static void Main(string[] args)
        {
            int number = 29;
            bool isPrime = PrimeChecker.IsPrime(number);

            Console.WriteLine($"Is {number} prime? {isPrime}");
        }
    }
}
This implementation checks if a number is prime by iterating only up to the square root of the number, which is an optimization over checking all numbers up to the number itself. It also excludes even numbers greater than 2, as they cannot be prime.

Source code repository

ONNX Runtime is an open source project. See:

Documentation

See ONNX Runtime GenAI Documentation

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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-android31.0 is compatible. net9.0-browser was computed. net9.0-ios was computed. net9.0-ios15.4 is compatible. net9.0-maccatalyst was computed. net9.0-maccatalyst14.0 is compatible. 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.
.NET Core netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed.
.NET Standard netstandard2.0 is compatible. netstandard2.1 was computed.
.NET Framework net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed.
MonoAndroid monoandroid was computed.
MonoMac monomac was computed.
MonoTouch monotouch was computed.
native native is compatible.
Tizen tizen40 was computed. tizen60 was computed.
Xamarin.iOS xamarinios was computed.
Xamarin.Mac xamarinmac was computed.
Xamarin.TVOS xamarintvos was computed.
Xamarin.WatchOS xamarinwatchos was computed.

NuGet packages (13)

Showing the top 5 NuGet packages that depend on Microsoft.ML.OnnxRuntimeGenAI:

Package Downloads
Microsoft.SemanticKernel.Connectors.Onnx Semantic Kernel connectors for the ONNX runtime. Contains clients for text embedding generation. 265.1K
Microsoft.KernelMemory.AI.Onnx Provide access to ONNX LLM models in Kernel Memory to generate text 211.8K
LMSupply.Generator A .NET library for local LLM text generation using ONNX Runtime GenAI. Supports streaming, chat templates, and automatic hardware detection with CUDA/DirectML GPU acceleration. 13.5K
feiyun0112.SemanticKernel.Connectors.OnnxRuntimeGenAI.CPU Semantic Kernel connector for Microsoft.ML.OnnxRuntimeGenAI. 6.1K
Richasy.AgentKernel.Connectors.Onnx Agent Kernel connectors for Onnx. 5.6K

GitHub repositories (6)

Showing the top 6 popular GitHub repositories that depend on Microsoft.ML.OnnxRuntimeGenAI:

Repository Stars
microsoft/semantic-kernel Integrate cutting-edge LLM technology quickly and easily into your apps 28.1K
microsoft/kernel-memory Research project. A Memory solution for users, teams, and applications. 2.2K
axzxs2001/Asp.NetCoreExperiment 原来所有项目都移动到**OleVersion**目录下进行保留。新的案例装以.net 5.0为主,一部分对以前案例进行升级,一部分将以前的工作经验总结出来,以供大家参考! 359
rwjdk/MicrosoftAgentFrameworkSamples Samples demonstrating the Microsoft Agent Framework in C# 284
Build5Nines/SharpVector Lightweight, In-memory, Semantic Search, Text Vector Database to embed in any .NET Application 139
kelnishi/WACS Pure C# WebAssembly toolchain for .NET - interpreter, AOT transpiler, NativeAOT builder, and component-model bindgen 119

Include prerelease

Include vulnerable

Include deprecated

Version Downloads Last Updated
0.14.1 2,032 6/2/2026
0.14.0 1,500 5/26/2026
0.13.2 4,804 5/1/2026
0.13.1 7,633 4/7/2026
0.13.0 690 4/5/2026
0.12.2 9,540 3/4/2026
0.12.1 2,125 2/24/2026
0.12.0 1,840 2/13/2026
0.11.4 102,151 12/12/2025
0.11.3 1,175 12/8/2025
0.11.2 7,856 11/18/2025
0.11.1 831 11/16/2025
0.11.0 824 11/14/2025
0.10.0 98,983 10/10/2025
0.9.2 27,784 9/16/2025
0.9.1 5,672 9/9/2025
0.9.0 9,037 8/6/2025
0.8.3 71,736 6/30/2025
0.8.2 6,702 6/5/2025
0.8.1 10,274 5/30/2025

Release Def:
Branch: refs/heads/rel-0.14.1
Commit: a30f479af016cb098688726831a9acbb8d19f0b2