Reranker
The Reranker
class provides advanced reranking capabilities for vector search results by combining semantic relevance, vector similarity, and position-based scoring.
You initialize it with a model and a set of weights to adjust the importance of each of the three scoring components).
It has one method, rerank()
, which takes a query and a set of results, and returns a set of reranked results.
If you want to have an agent running your RAG pipeline, the Reranker class can be used through the createVectorQueryTool
.
Basic Usage
import { Reranker } from "@mastra/rag";
const reranker = new Reranker({
semanticProvider: "agent",
agentProvider: {
provider: "openai",
name: "gpt-4"
},
weights: {
semantic: 0.5,
vector: 0.3,
position: 0.2
}
});
const results = await reranker.rerank({
query: "How do I deploy to production?",
vectorStoreResults: searchResults,
topK: 3
});
Reranker Initialization config
semanticProvider:
'cohere' | 'agent'
Provider to use for semantic scoring
weights?:
WeightConfig
Weights for different scoring components
cohereApiKey?:
string
Required when using Cohere provider
cohereModel?:
string
Specific Cohere model to use
agentProvider?:
{ provider: string; name: string }
Required when using Agent provider
rerank() Params
query:
string
The search query text
vectorStoreResults:
QueryResult[]
Results from vector store to rerank
queryEmbedding?:
number[]
Original query embedding for advanced scoring
topK?:
number
Number of top results to return
rerank() Returns
The rerank method returns an array of RerankResult
objects.
Here’s the type definition of a RerankResult
:
result:
QueryResult
The original query result
score:
number
Combined reranking score (0-1)
details:
RerankDetails
Detailed scoring information