Table of Contents

👉 Try the demo: https://github.com/LM-Kit/lm-kit-net-samples/tree/main/console_net/rag-and-knowledge/reranker/cross_encoder_reranker

Cross-Encoder Reranker Lab for C# .NET RAG Pipelines


🎯 Purpose of the Demo

An interactive console app that prints two rankings side-by-side on the same query and corpus: a cheap embedding-only cosine ranking, and a cross-encoder reranker. The lab makes the precision lift visible on your own data and corpus.

All processing runs on-device.


👥 Industry Target Audience

  • RAG / search engineers evaluating reranker uplift on a real corpus.
  • Knowledge bases: improve top-K precision before sending to an LLM.
  • Internal search: combat the "right answer is at rank 7" problem.
  • Compliance / legal: precision matters more than recall in evidence search.

🚀 Problem Solved

Embedding similarity is a coarse, symmetric signal: a passage that mentions the query tokens often outranks one that answers the query. A cross-encoder reads query + passage together and produces a relevance score that captures answer-ness. The lab puts both side-by-side so the reranker's effect is obvious immediately.


💻 Application Overview

Interactive menu (no command-line arguments) with three modes:

Mode What it does
Demo Built-in 9-passage corpus about LM-Kit. Type a query; see both rankings.
Custom Type a query and any number of passages (blank line ends).
File Load passages from a UTF-8 text file (one per line). Optional CSV export of both rankings.
Quit Exit.

Each comparison embeds the query once, embeds the corpus in batch, runs the reranker over the corpus, and prints both ordered lists with scores in two columns.

✨ Key Features

  • LM.LoadFromModelID("qwen3-embedding:0.6b") for the bi-encoder.
  • LM.LoadFromModelID("bge-m3-reranker") for the cross-encoder.
  • Embedder.GetQueryEmbeddings / GetEmbeddings / GetCosineSimilarity: stage-1 ranking.
  • Reranker.GetScore(query, IEnumerable<string>): batched stage-2 ranking.
  • CSV export: portable two-column comparison for reports.

🧠 Models

  • qwen3-embedding:0.6b (~600 MB) for embeddings.
  • bge-m3-reranker for cross-encoder reranking.

🛠️ Getting Started

📋 Prerequisites

  • .NET 8.0 or later

▶️ Running the Application

git clone https://github.com/LM-Kit/lm-kit-net-samples
cd lm-kit-net-samples/console_net/rag-and-knowledge/reranker/cross_encoder_reranker
dotnet run

Both models load once at startup. Pick a mode from the menu.

Share