OnnxCrossEncoderReranker<T>

Models & Types · AiDotNet.RetrievalAugmentedGeneration.Rerankers

A production-ready cross-encoder reranker that scores (query, document) pairs with a real transformer model loaded via ONNX Runtime (e.g. BAAI/bge-reranker or ms-marco-MiniLM).

Inheritance: RerankerBase<T> → OnnxCrossEncoderReranker<T>

Implements: IDisposable

For Beginners

This is the “batteries-included” cross-encoder reranker.

A cross-encoder reads the query and a document together and outputs a single number telling you how relevant the document is to the query. Higher means more relevant. This class handles all of the plumbing:

  • Loading the model (ONNX file) and its tokenizer.
  • Building the joint input the model expects for each pair.
  • Running the model (in batches for speed).
  • Sorting your documents from most to least relevant.

Typical models to use:

  • BAAI/bge-reranker-base / bge-reranker-large (single relevance logit).
  • cross-encoder/ms-marco-MiniLM-L-6-v2 (single relevance logit).

Model loading is lazy: nothing is opened until the first rerank call. If the model file is missing, a clear FileNotFoundException is thrown — there is no silent lexical fallback.

How It Works

Unlike CrossEncoderReranker (which requires the caller to supply a Func<string, string, T> scorer), this reranker works out of the box: point it at a cross-encoder ONNX model and its tokenizer, and it will tokenize each (query, document) pair jointly ([CLS] query [SEP] document [SEP]), run the model to produce a relevance logit, and reorder documents by that logit (highest first).

Constructors

Constructor Summary
OnnxCrossEncoderReranker(string modelPath, string tokenizerPath, int maxLength, int maxPairsToScore, int batchSize, int scoreLabelIndex) Initializes a new instance of the OnnxCrossEncoderReranker class.

Properties

Property Summary
bool ModifiesScores { get; } Gets a value indicating whether this reranker modifies relevance scores.

Methods

Method Summary
T ScorePair(string query, string document) Scores a single (query, document) pair and returns the relevance score as T.
void Dispose() Releases the ONNX session and tokenizer resources.