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)通常在現代语义搜索和推荐引擎中常见的两阶段流水线内运行。
Cross-Encoder vs Bi-Encoder
交叉编码器(Reranker)vs 双编码器(向量检索/检索器)
This is the most important concept to understand about Reranker.
Reranker Workflow
- Query → Vector Database → Retrieve Top-K (e.g., top-100)
- Top-100 → Reranker → Score each (query, doc) pair → Reorder
- 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

