SemanticKernel.Rankers.Pipelines
1.3.1
dotnet add package SemanticKernel.Rankers.Pipelines --version 1.3.1
NuGet\Install-Package SemanticKernel.Rankers.Pipelines -Version 1.3.1
<PackageReference Include="SemanticKernel.Rankers.Pipelines" Version="1.3.1" />
<PackageVersion Include="SemanticKernel.Rankers.Pipelines" Version="1.3.1" />
<PackageReference Include="SemanticKernel.Rankers.Pipelines" />
paket add SemanticKernel.Rankers.Pipelines --version 1.3.1
#r "nuget: SemanticKernel.Rankers.Pipelines, 1.3.1"
#:package SemanticKernel.Rankers.Pipelines@1.3.1
#addin nuget:?package=SemanticKernel.Rankers.Pipelines&version=1.3.1
#tool nuget:?package=SemanticKernel.Rankers.Pipelines&version=1.3.1
SemanticKernel.Rankers.Pipelines
This project provides reusable pipeline implementations for RAG systems with support for chaining multiple rankers in cascade.
Components
CascadeRerankPipeline
A flexible pipeline implementation that chains multiple rankers in cascade. Each stage filters the top-K results for the next stage until the final top-M results are produced.
Features:
- IRanker Implementation: Fully implements the
IRanker
interface with support for both string documents andVectorSearchResult<T>
objects - Cascade Filtering: Each ranker stage filters results for the next stage based on configurable parameters
- Configurable Thresholds: Support for score thresholds, top-K, and top-M filtering
- Async Streaming: Efficient async enumerable processing for large document sets
- Type Safety: Full generic support for different document types
BM25ThenLMRankerPipeline
A specialized two-stage retrieval pipeline for RAG systems:
- BM25 Retrieval: High-recall lexical retrieval to fetch top-K candidates.
- LM Re-ranking: LLM-based scoring, sorting, and filtering to select top-M high-precision passages.
API Methods
The CascadeRerankPipeline
implements all IRanker
methods:
RankAsync (String Documents)
IAsyncEnumerable<(string DocumentText, double Score)> RankAsync(
string query,
IAsyncEnumerable<string> documents,
int topN = 5)
RankAsync (VectorSearchResult Documents)
IAsyncEnumerable<(VectorSearchResult<T> Result, double Score)> RankAsync<T>(
string query,
IAsyncEnumerable<VectorSearchResult<T>> documents,
Expression<Func<T, string>> textProperty,
int topN = 5)
ScoreAsync (String Documents)
IAsyncEnumerable<(string DocumentText, double Score)> ScoreAsync(
string query,
IAsyncEnumerable<string> documents)
ScoreAsync (VectorSearchResult Documents)
IAsyncEnumerable<(VectorSearchResult<T> Result, double Score)> ScoreAsync<T>(
string query,
IAsyncEnumerable<VectorSearchResult<T>> searchResults,
Expression<Func<T, string>> textProperty)
Usage
Basic String Document Ranking
using SemanticKernel.Rankers.Pipelines;
using SemanticKernel.Rankers.BM25;
using SemanticKernel.Rankers.LMRanker;
// Create rankers
var bm25Ranker = new BM25Reranker();
var lmRanker = new LMRanker(/* configuration */);
// Configure pipeline
var config = new CascadeRerankPipelineConfig
{
TopK = 20, // Pass top 20 from each stage to next
TopM = 5, // Final output: top 5 results
ScoreThreshold = 0.1 // Minimum score threshold
};
// Create pipeline
var pipeline = new CascadeRerankPipeline(
new List<IRanker> { bm25Ranker, lmRanker },
config);
// Use for ranking
var documents = new[] { "doc1", "doc2", "doc3" };
var query = "search query";
await foreach (var (docText, score) in pipeline.RankAsync(query, ToAsyncEnumerable(documents), topN: 3))
{
Console.WriteLine($"Document: {docText}, Score: {score}");
}
VectorSearchResult Ranking
// For custom document types
public class MyDocument
{
public string Content { get; set; }
public string Title { get; set; }
}
// Rank using content property
await foreach (var (result, score) in pipeline.RankAsync(
query,
ToAsyncEnumerable(searchResults),
doc => doc.Content, // Extract text using this property
topN: 5))
{
Console.WriteLine($"Document: {result.Record.Title}, Score: {score}");
}
Configuration
- TopK: Number of top results to pass from each intermediate stage (default: 20)
- TopM: Number of final results to output from the last stage (default: 5)
- ScoreThreshold: Minimum score threshold for results (default: 0.0)
Configure K, M, batching, model, thresholds, and prompts via pipeline-specific configuration classes.
BM25ThenLMRankerPipeline Implementation Details
The BM25ThenLMRankerPipeline
is a specialized implementation using the cascade pipeline with BM25 and LM rankers.
Example Usage
var bm25 = new BM25Reranker(...);
var lmRanker = new LMRanker(...);
var config = new BM25ThenLMRankerPipelineConfig { TopK = 20, TopM = 5 };
var pipeline = new BM25ThenLMRankerPipeline(bm25, lmRanker, config);
var result = await pipeline.RetrieveAndRankAsync(query, corpus);
Console.WriteLine($"Top {result.TopMResults.Count} results retrieved in {result.BM25Time + result.LMTime}");
Features
- Use
BM25ThenLMRankerPipeline.RetrieveAndRankAsync(query, corpus)
to get top-M results. - Observability: timings, token usage, scores, and selection decisions are returned in
PipelineResult
. - Improve answerability and reduce hallucinations by prioritizing relevant passages.
- Balance recall (BM25) and precision (LM re-ranking) with configurable trade-offs.
- Keep latency and cost within budget.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. 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. |
-
net8.0
- SemanticKernel.Rankers.Abstractions (>= 1.3.1)
- SemanticKernel.Rankers.BM25 (>= 1.3.1)
- SemanticKernel.Rankers.LMRanker (>= 1.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.