跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

rerank()

The rerank() function provides advanced reranking capabilities for vector search results by combining semantic relevance, vector similarity, and position-based scoring.

function rerank(
results: QueryResult[],
query: string,
model: MastraLanguageModel,
options: RerankerFunctionOptions,
): Promise<RerankResult[]>

Usage example
Usage example的直接链接

import { ModelRouterLanguageModel } from '@mastra/core/llm'
import { rerank } from '@mastra/rag'

const model = new ModelRouterLanguageModel('openai/gpt-5.6-sol')

const rerankedResults = await rerank(vectorSearchResults, 'How do I deploy to production?', model, {
weights: {
semantic: 0.5,
vector: 0.3,
position: 0.2,
},
topK: 3,
})

Parameters
Parameters的直接链接

results:

QueryResult[]
The vector search results to rerank

query:

string
The search query text used to evaluate relevance

model:

MastraLanguageModel
The language Model to use for reranking

options:

RerankerFunctionOptions
Options for the reranking model
RerankerFunctionOptions

weights?:

WeightConfig
Weights for different scoring components (must add up to 1)
number

semantic?:

number (default: 0.4)
Weight for semantic relevance
number

vector?:

number (default: 0.4)
Weight for vector similarity
number

position?:

number (default: 0.2)
Weight for position-based scoring

queryEmbedding?:

number[]
Embedding of the query

topK?:

number
Number of top results to return

The rerank function accepts any LanguageModel from the Vercel AI SDK. When using the Cohere model rerank-v3.5, it will automatically use Cohere's reranking capabilities.

备注

For semantic scoring to work properly during re-ranking, each result must include the text content in its metadata.text field.

Returns
Returns的直接链接

The function returns an array of RerankResult objects:

result:

QueryResult
The original query result

score:

number
Combined reranking score. Usually between 0 and 1, but query-based adjustments can raise it slightly above 1.

details:

ScoringDetails
Detailed scoring information

ScoringDetails
scoringdetails的直接链接

semantic:

number
Semantic relevance score (0-1)

vector:

number
Vector similarity score (0-1)

position:

number
Position-based score (0-1)

queryAnalysis?:

object
Query analysis details
number

magnitude:

Magnitude of the query
number[]

dominantFeatures:

Dominant features of the query
On this page