Class Reranker
- Namespace
- LMKit.Embeddings
- Assembly
- LM-Kit.NET.dll
Computes embedding-based similarity scores and reorders/reranks documents or text partitions based on their relevance to a given query. Useful for selecting the most relevant items from a set by comparing embedding distances or similarity metrics.
public sealed class Reranker
- Inheritance
-
Reranker
- Inherited Members
Examples
Example: Score a single document against a query
using LMKit.Model;
using LMKit.Embeddings;
using System;
// Load an embedding model
LM model = LM.LoadFromModelID("embeddinggemma-300m");
// Create the reranker
Reranker reranker = new Reranker(model);
// Score document relevance to query
string query = "What are the benefits of exercise?";
string document = "Regular physical activity improves cardiovascular health and mental wellbeing.";
float score = reranker.GetScore(query, document);
Console.WriteLine($"Relevance score: {score:F4}");
Example: Rerank RAG search results
using LMKit.Model;
using LMKit.Embeddings;
using LMKit.Retrieval;
using System;
using System.Linq;
// Load models
LM embeddingModel = LM.LoadFromModelID("embeddinggemma-300m");
// Create RAG engine and reranker
RagEngine rag = new RagEngine(embeddingModel);
Reranker reranker = new Reranker(embeddingModel);
// Import documents
rag.ImportText("Exercise improves heart health.", "docs", "health1");
rag.ImportText("Programming requires logical thinking.", "docs", "tech1");
rag.ImportText("Physical activity reduces stress.", "docs", "health2");
// Search and rerank
string query = "How does exercise affect health?";
var results = rag.FindMatchingPartitions(query, topK: 10);
// Rerank with 50% weight on reranker score
reranker.Rerank(query, results, rerankedAlpha: 0.5f);
// Sort by reranked score and display
foreach (var result in results.OrderByDescending(r => r.RerankedScore))
{
Console.WriteLine($"[{result.RerankedScore:F4}] {result.Payload}");
}
Remarks
Reranking is a technique used to improve retrieval quality by re-scoring initial search results using a more sophisticated model. This is commonly used in RAG (Retrieval-Augmented Generation) pipelines where initial vector similarity search may return results that benefit from refinement.
Key Features
- Score individual query-document pairs with GetScore(string, string, CancellationToken)
- Rerank collections of partitions with blended scoring via Rerank(string, IEnumerable<PartitionSimilarity>, float, CancellationToken)
- Configurable alpha blending between original and reranked scores
- Optional score normalization to [0-1] range
Score Blending Formula
When reranking partition similarities, the final score is computed as:
final_score = (alpha * original_score) + ((1 - alpha) * reranker_score)
Constructors
- Reranker(LM)
Initializes a new instance of the Reranker class with the specified embedding-capable language model.
Fields
- NormalizeScore
When
true, reranking scores will be normalized to a common scale, in the range [0 - 1]. Default value is true.
Properties
- Model
Gets the language model instance used for embedding computations.
Methods
- GetScore(string, TextPartition, CancellationToken)
Computes a similarity score for a single text partition synchronously.
- GetScore(string, IEnumerable<TextPartition>, CancellationToken)
Computes similarity scores for a collection of text partitions synchronously.
- GetScore(string, IEnumerable<string>, CancellationToken)
Computes similarity scores for a collection of documents synchronously.
- GetScore(string, string, CancellationToken)
Computes a similarity score for a single document synchronously.
- GetScoreAsync(string, TextPartition, CancellationToken)
Computes a similarity score for a single text partition asynchronously.
- GetScoreAsync(string, IEnumerable<TextPartition>, CancellationToken)
Computes similarity scores for a collection of text partitions asynchronously.
- GetScoreAsync(string, IEnumerable<string>, CancellationToken)
Computes similarity scores for a collection of documents asynchronously.
- GetScoreAsync(string, string, CancellationToken)
Computes a similarity score for a single document asynchronously.
- Rerank(string, PartitionSimilarity, float, CancellationToken)
Reranks the similarity score of a single PartitionSimilarity synchronously by invoking the reranking model and updating its RerankedScore property with the new blended score.
- Rerank(string, IEnumerable<PartitionSimilarity>, float, CancellationToken)
Reranks the similarity scores of multiple PartitionSimilarity instances synchronously by invoking the reranking model and updating each instance’s RerankedScore property with its new blended score.
- RerankAsync(string, PartitionSimilarity, float, CancellationToken)
Reranks the similarity score of a single PartitionSimilarity asynchronously and updates its RerankedScore property with the new blended score.
- RerankAsync(string, IEnumerable<PartitionSimilarity>, float, CancellationToken)
Reranks the similarity scores of multiple PartitionSimilarity instances asynchronously and updates each instance’s RerankedScore property with its new blended score.