CohereReranker<T>
Models & Types · AiDotNet.RetrievalAugmentedGeneration.RerankingStrategies
Reranks documents using Cohere’s specialized reranking API for state-of-the-art relevance scoring.
Inheritance: RerankerBase<T> → CohereReranker<T>
For Beginners
Think of this as getting a second, expert opinion on search results:
Without reranking:
- Fast vector search finds 100 “similar” documents
- Returns top 5 based on vector distance
- Good enough, but not perfect
With Cohere reranking:
- Fast vector search finds 100 candidates
- Cohere expert looks at each one carefully
- Reorders them by true relevance
- Returns truly best 5
Example: Query: “How to prevent overfitting in neural networks?”
Initial retrieval (vector search):
- “Neural networks architecture guide” (score: 0.82)
- “Overfitting in machine learning” (score: 0.80)
- “Regularization techniques for deep learning” (score: 0.78)
After Cohere reranking:
- “Regularization techniques for deep learning” (NEW score: 0.95) ← Actually best match!
- “Overfitting in machine learning” (NEW score: 0.88)
- “Neural networks architecture guide” (NEW score: 0.72) ← Less relevant
var reranker = new CohereReranker<double>(
apiKey: "your-cohere-api-key",
model: "rerank-english-v3.0"
);
var documentStore = new InMemoryDocumentStore<double>(vectorDimension: 768);
var embeddingModel = new StubEmbeddingModel<double>(embeddingDimension: 768);
string[] passages =
{
"Vaccines expose the immune system to a harmless antigen so it builds memory cells.",
"Neural networks architecture guide",
"Booster doses refresh the antibody response a vaccine first trained."
};
for (int i = 0; i < passages.Length; i++)
documentStore.Add(new VectorDocument<double>(
new Document<double>($"passage-{i}", passages[i]), embeddingModel.Embed(passages[i])));
var retriever = new DenseRetriever<double>(documentStore, embeddingModel);
var query = "How do vaccines train the immune system?";
var initialResults = retriever.Retrieve(query, topK: 20); // Get 20 candidates
var reranked = reranker.Rerank(query, initialResults); // Rerank to improve order
var final = reranked.Take(5); // Return best 5
Why use CohereReranker:
- Significantly improves answer quality (10-30% better precision)
- Cohere models are specifically trained for reranking (not repurposed)
- Handles complex queries better than vector search alone
- Industry-standard approach used by major search systems
When NOT to use it:
- Simple keyword queries (not worth the API cost)
- Real-time systems with strict latency requirements (adds ~100-300ms)
- Very high query volume (API costs add up)
- When initial retrieval is already highly accurate
How It Works
CohereReranker integrates with Cohere’s dedicated reranking models (e.g., rerank-english-v3.0) which are specifically trained for relevance scoring rather than general language understanding. Unlike bi-encoder models that compare query and document embeddings independently, Cohere’s reranker uses cross-encoder architecture to analyze query-document pairs jointly, producing more accurate relevance scores. This two-stage retrieval approach (fast bi-encoder retrieval → precise cross-encoder reranking) is industry best practice, significantly improving answer quality at manageable cost. The implementation provides a heuristic fallback using term overlap, proximity, and length penalties, but is designed to call Cohere’s API in production for superior performance. Reranking is particularly effective when applied to the top 20-100 results from initial retrieval, balancing accuracy gains against API costs.
Constructors
| Constructor | Summary |
|---|---|
CohereReranker(string apiKey, string model) |
Initializes a new instance of the CohereReranker class. |
Properties
| Property | Summary |
|---|---|
bool ModifiesScores { get; } |
Gets a value indicating whether this reranker modifies relevance scores. |