Files
microsoft--semantic-kernel/dotnet/samples/Demos/OnnxSimpleRAG
wehub-resource-sync b957a53def
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 13:21:23 +08:00
..

Onnx Simple RAG (Retrieval Augmented Generation) Sample

This sample demonstrates how you can do RAG using Semantic Kernel with the ONNX Connector that enables running Local Models straight from files.

In this example we setup two ONNX AI Services:

Important

You can modify to use any other combination of models enabled for ONNX runtime.

Semantic Kernel used Features

Prerequisites

1. Configuring the sample

Downloading the Models

For this example we chose Hugging Face as our repository for download of the local models, go to a directory of your choice where the models should be downloaded and run the following commands:

git clone https://huggingface.co/TaylorAI/bge-micro-v2
git clone https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx

Important

Both BGE-Micro-V2 and Phi-3 models are too large to be downloaded by the git clone command alone if you don't have git-lfs extension installed, for this you may need to download the models manually and overwrite the files in the cloned directories.

Update the Program.cs file lines below with the paths to the models you downloaded in the previous step.

// Path to the folder of your downloaded ONNX PHI-3 model
var chatModelPath = @"C:\path\to\huggingface\Phi-3-mini-4k-instruct-onnx\cpu_and_mobile\cpu-int4-rtn-block-32";

// Path to the file of your downloaded ONNX BGE-MICRO-V2 model
var embeddingModelPath = @"C:\path\to\huggingface\bge-micro-v2\onnx\model.onnx";

// Path to the vocab file your ONNX BGE-MICRO-V2 model
var embeddingVocabPath = @"C:\path\to\huggingface\bge-micro-v2\vocab.txt";

Optional: Change or add any fact text you want your AI to know about to the facts list in the Program.cs file.

foreach (var fact in new[] {
    "My fact 1.",
    "My fact 2." })
{

Configuring the sample

The sample can be configured by using the command line with .NET Secret Manager to avoid the risk of leaking secrets into the repository, branches and pull requests.

Using .NET Secret Manager

dotnet user-secrets set "Onnx:ModelId" ".. Onnx model id" (default: phi-3)
dotnet user-secrets set "Onnx:ModelPath" ".. your Onnx model folder path .." 
dotnet user-secrets set "Onnx:EmbeddingModelPath" ".. your Onnx model file path .."
dotnet user-secrets set "Onnx:EmbeddingVocabPath" ".. your Onnx model vocab file path .."

3. Running the sample

After configuring the sample, to build and run the console application just hit F5.

To build and run the console application from the terminal use the following commands:

dotnet build
dotnet run

Ask to the Model any question you want, the AI will try to answer based on the facts you provided.

Example of a conversation

User > What is semantic kernel SDK used for?

Assistant > The Semantic Kernel SDK is used for developing AI agents and integrating the latest AI models into your C#, Python, or Java codebase. It acts as a lightweight, open-source development kit that facilitates the creation of AI applications with enterprise-grade capabilities. By serving as an efficient middleware, it allows for the rapid development and deployment of AI solutions across various programming environments.

User > When should I use Kernel Memory?

Assistant > Kernel Memory is best suited for tasks that involve large-scale continuous data, require efficient indexing and processing, and benefit from the integration of retrieval-based and generative models, synthetic memory, prompt engineering, and custom semantic memory processing.