Reranker

What is Reranker?

 Reranker is a model that takes a set of candidate documents retrieved from an initial fast search and re-ranks them based on a more precise relevance assessment. It sits between the retriever and the LLM in a RAG pipeline.

Reranker 是一个模型,它接收从初始快速检索中获取的一组候选文档,并基于更精确的相关性评估对它们进行重新排序。在 RAG 流水线中,它位于检索器和 LLM 之间。

Imagine you’re a hiring manager. First, HR does a quick resume screening and gives you 100 candidates (this is the retriever). But HR’s screening is fast and imperfect—it might miss subtle but important qualifications. You then personally read the top 20 resumes in depth, carefully comparing each candidate against the job requirements. You reorder them, putting the best matches at the top. That’s you—the Reranker.

人话比喻: 想象你是一个招聘经理。首先,HR 快速筛选简历,给你 100 个候选人(这就是 检索器/Retriever)。但 HR 的筛选是快速且不完美的——可能会漏掉一些微妙但重要的资质。然后你亲自深入阅读前 20 份简历,仔细比较每个候选人与岗位要求的匹配度。你重新排序,把最匹配的放在最前面。你就是那个 Reranker。

 In a typical RAG pipeline, the first-stage retriever (usually vector search) quickly fetches top-k candidates from a massive database. However, vector search uses bi-encoder architecture—it encodes query and documents independently, then compares them with a single dot product or cosine similarity. This is fast but loses query-specific signal. The result is decent recall (the right doc is usually in top-20) but mediocre top-1 precision (the right doc is often not in top-1).

CN: 在典型的 RAG 流水线中,第一阶段的检索器(通常是向量检索)从海量数据库中快速获取 top-k 候选文档。然而,向量检索使用的是 双编码器(bi-encoder) 架构——它独立编码查询和文档,然后用一个点积或余弦相似度来比较它们。这很快,但会丢失查询特定的信号。结果是 召回率还行(正确的文档通常在 top-20 里),但 top-1 精确率 mediocre(正确的文档往往不在第一名)。

In 2026, rerankers have become standard in any RAG system that prioritises answer quality over absolute latency.

CN: 在 2026 年,reranker 已经成为任何优先考虑答案质量而非绝对延迟的 RAG 系统的标准组件

Reranker Core Content

Two-Stage Retrieval Architecture

Reranking operates within a two-stage pipeline commonly found in modern semantic search and recommendation engines.

CN: 重排序(Reranking)通常在現代语义搜索和推荐引擎中常见的两阶段流水线内运行。

StageComponentPurposeCharacteristics
Stage 1Retriever
检索器
Scan entire database, fetch top-k candidates (e.g., top 100)
扫描整个数据库,获取 top-k 候选(如 top 100)
Fast, prioritizes recall, uses ANN/vector search
快速,优先保证召回率,使用 ANN/向量检索
Stage 2Reranker
重排序器
Deep-analyze the candidate list, reorder by relevance
深度分析候选列表,按相关性重新排序
Slow but precise, prioritizes precision, uses cross-encoder
慢但精准,优先保证精确率,使用交叉编码器

Cross-Encoder vs Bi-Encoder

交叉编码器(Reranker)vs 双编码器(向量检索/检索器)

 This is the most important concept to understand about Reranker.

AspectBi-Encoder (Vector Search / Retriever)Cross-Encoder (Reranker)
Encoding
编码方式
Encodes query and document separately
查询和文档分别编码
Encodes query and document together as [query ⊕ doc]
查询和文档一起编码为 [query ⊕ doc]
Interaction交互方式No interaction between query and doc during encoding
编码时查询和文档无交互
Full cross-attention between query and doc tokens
查询和文档 token 之间完全交叉
OutputProduces embeddings (vectors), then compares via cosine/dot product
产生嵌入向量,然后通过余弦/点积比较
Produces a direct relevance score
产生直接的相关性分数
SpeedVery fast (can search millions in ms)
非常快(毫秒级搜索百万级)
Slow (O(k) forward passes per query)
慢(每个查询需要 O(k) 次前向传播)
PrecisionModerate—loses query-specific details
中等——丢失查询特定的细节
Highest—captures nuanced semantics
最高——捕捉细微的语义
Use caseFirst-stage retrieval (top-100)
第一阶段检索(top-100)
Second-stage reranking (top-100 → top-3/5)
第二阶段重排序(top-100 → top-3/5)

Reranker Workflow

  1. Query → Vector Database → Retrieve Top-K (e.g., top-100)
  2. Top-100 → Reranker → Score each (query, doc) pair → Reorder
  3. Top-N (e.g., top-3) → LLM → Generate Answer

Implementation

与向量检索集成的完整 RAG Pipeline

# ============================================================
# 完整的 RAG Pipeline:向量检索 + Reranker
# Complete RAG Pipeline: Vector Search + Reranker
# ============================================================

from typing import List, Tuple
import numpy as np


class RAGPipelineWithReranker:
    """
    Complete RAG pipeline with vector retrieval and reranker.
    包含向量检索和重排序器的完整 RAG 流水线。
    
    Architecture: Query → Vector Retriever (top-100) → Reranker (top-3) → LLM
    架构:查询 → 向量检索器 (top-100) → 重排序器 (top-3) → LLM
    """
    
    def __init__(self, 
                 vector_retriever,  # 向量检索器实例 / Vector retriever instance
                 reranker: RerankerEngine,  # 重排序器实例 / Reranker instance
                 llm_client):  # LLM 客户端 / LLM client
        """
        Initialize the RAG pipeline.
        初始化 RAG 流水线。
        """
        self.retriever = vector_retriever
        self.reranker = reranker
        self.llm = llm_client
        
    def query(self, question: str, retrieval_k: int = 100, rerank_top_n: int = 3) -> str:
        """
        Execute a full RAG query.
        执行完整的 RAG 查询。
        
        Args:
            question: User question / 用户问题
            retrieval_k: Number of documents to retrieve from vector DB / 从向量DB检索的文档数
            rerank_top_n: Number of documents to pass to LLM after reranking / 重排序后传给LLM的文档数
            
        Returns:
            Generated answer / 生成的答案
        """
        
        # 步骤1: 向量检索(快速,高召回)
        # Step 1: Vector retrieval (fast, high recall)
        # 从向量数据库中检索 top-K 候选文档
        # Retrieve top-K candidate documents from vector DB
        retrieved_docs = self.retriever.search(question, top_k=retrieval_k)
        # retrieved_docs 是 List[str] / retrieved_docs is List[str]
        
        # 步骤2: Reranker 精排(慢速,高精度)
        # Step 2: Reranker fine-ranking (slow, high precision)
        # 对检索结果进行重排序,只取 top-N
        # Rerank retrieval results, take only top-N
        reranked = self.reranker.rerank(question, retrieved_docs, top_k=rerank_top_n)
        # reranked 是 List[Tuple[str, float]] / reranked is List[Tuple[str, float]]
        
        # 提取重排序后的文档文本 / Extract reranked document texts
        top_docs = [doc for doc, _ in reranked]
        
        # 步骤3: 构建上下文并调用 LLM
        # Step 3: Build context and call LLM
        context = "\n\n".join(top_docs)
        prompt = f"""
        Based on the following context, answer the question.
        
        Context:
        {context}
        
        Question: {question}
        
        Answer:
        """
        
        # 调用 LLM 生成答案 / Call LLM to generate answer
        answer = self.llm.generate(prompt)
        
        return answer

Key Takeaways

要点ENCN
Reranker 是两阶段检索的第二阶段Reranker is the second stage in two-stage retrievalReranker 是两阶段检索中的第二阶段
Reranker 使用交叉编码器,查询和文档联合编码Reranker uses cross-encoder, query and doc encoded jointlyReranker 使用交叉编码器,查询和文档联合编码
向量检索(双编码器)优先保证召回率Vector search (bi-encoder) prioritizes recall向量检索(双编码器)优先保证召回率
Reranker(交叉编码器)优先保证精确率Reranker (cross-encoder) prioritizes precisionReranker(交叉编码器)优先保证精确率
经典模式:检索 top-100 → 重排序到 top-3Classic pattern: retrieve top-100 → rerank to top-3经典模式:检索 top-100 → 重排序到 top-3
Reranker 不替代向量检索,而是提升其精确率Reranker does not replace vector search; it boosts its precisionReranker 不替代向量检索,而是提升其精确率
BGE-Reranker 是开源的主流选择BGE-Reranker is a mainstream open-source choiceBGE-Reranker 是开源的主流选择
Cohere Rerank 是企业级的 API 服务Cohere Rerank is an enterprise-grade API serviceCohere Rerank 是企业级的 API 服务
Reranker 的代价是延迟和计算成本Reranker’s cost is latency and compute overheadReranker 的代价是延迟和计算成本
长文档自动分块,取最大分数Long documents are auto-chunked, take max score长文档自动分块,取最大分数