0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · 课时

对检索结果重新排序

在将上下文传递给 LLM 之前,使用交叉编码器模型对初始向量搜索候选结果重新排序,从而提升 RAG 的准确性。

对检索结果重新排序 是 CoddyKit 上的免费 Vector Databases: Pinecone, Weaviate & pgvector 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Vector Databases: Pinecone, Weaviate & pgvector 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Reranking Idea

Vector search is fast but approximate. Reranking takes the top candidates and reorders them with a more accurate, slower model — the two-stage retrieve-then-rerank pattern.

Bi-Encoder vs Cross-Encoder

Bi-encoders embed query and document separately (fast, used for retrieval). Cross-encoders score query+document together (slow, far more accurate) — ideal for reranking a small set.

Two-Stage Pipeline

Step 1: retrieve top 50 with the vector DB. Step 2: rerank those 50, keep the top 5. You get cross-encoder quality at near vector-search speed.

candidates = vector_db.search(query, k=50)
ranked = reranker.rank(query, candidates)
top = ranked[:5]

Using a Cross-Encoder

A cross-encoder takes pairs and outputs a relevance score. Higher means more relevant.

from sentence_transformers import CrossEncoder
model = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
scores = model.predict([(query, doc) for doc in candidates])

Sorting by Score

Pair each document with its score and sort descending to produce the final order.

ranked = sorted(zip(candidates, scores), key=lambda x: x[1], reverse=True)
for doc, score in ranked[:5]:
    print(round(score, 3), doc[:40])

Hosted Reranking APIs

Services like Cohere Rerank and Jina offer hosted rerankers via API, removing the need to self-host a model.

POST /v1/rerank
{ "query": "...", "documents": [...], "top_n": 5 }

Choosing Candidate Count

Retrieve enough candidates that the true best answer is in the set (high recall), but not so many that reranking gets slow. 20-100 is typical.

Latency Trade-offs

Reranking adds latency proportional to candidate count. Cache results, batch the cross-encoder calls, and tune the candidate count to your SLA.

Reranking + Filters

Apply metadata filters during retrieval, then rerank only the filtered set. This keeps the reranker focused on valid candidates.

Measuring the Gain

Compare nDCG or hit rate with and without reranking on a labeled set. Reranking commonly delivers a large accuracy boost on noisy corpora.

When to Skip It

If your corpus is small and clean, or latency is critical, plain vector search may suffice. Reranking pays off most on large, diverse datasets.

Quick Check

Test your reranking knowledge.

Recap

You learned the retrieve-then-rerank pattern, the difference between bi- and cross-encoders, how to score and sort candidates, and when reranking is worth its latency cost.

常见问题解答

「对检索结果重新排序」课时是免费的吗?

是的 — 「对检索结果重新排序」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vector Databases: Pinecone, Weaviate & pgvector 课程的其余内容,请升级到 CoddyKit PRO。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。

「对检索结果重新排序」这节课中我会学到什么?

在将上下文传递给 LLM 之前,使用交叉编码器模型对初始向量搜索候选结果重新排序,从而提升 RAG 的准确性。 你通过在浏览器中直接运行的动手代码来练习 Vector Databases: Pinecone, Weaviate & pgvector,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Vector Databases: Pinecone, Weaviate & pgvector 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Vector Databases: Pinecone, Weaviate & pgvector 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「对检索结果重新排序」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Vector Databases: Pinecone, Weaviate & pgvector 课中编写并运行代码吗?

能。每节 Vector Databases: Pinecone, Weaviate & pgvector 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 查询转换技术
  2. 多阶段 RAG 流水线
  3. 评估 RAG 系统性能
  4. 对检索结果重新排序
← 返回 Vector Databases: Pinecone, Weaviate & pgvector