视觉画布设计
canvas-design
基于设计原则创作静态视觉作品,用于海报与品牌图形。
agentsop-reranker-stage【文档与办公】Adds and tunes a reranker stage for RAG using the retrieve-wide, rerank-narrow pattern. Use when relevant documents appear in the initial top-N but are buried by noise, top-1 precision or MRR is low despite adequate recall, or too many marginal chunks consume context. Covers cross-encoder, API, and local rerankers; N-to-k selection; and latency/cost …
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: agentsop-reranker-stage version: 0.1.0 description: >- Adds and tunes a reranker stage for RAG using the retrieve-wide, rerank-narrow pattern. Use when relevant documents appear in the initial top-N but are buried by noise, top-1 precision or MRR is low despite adequate recall, or too many marginal chunks consume context. Covers cross-encoder, API, and local rerankers; N-to-k selection; and latency/cost tradeoffs. Do not use when retrieval recall itself is failing. when_to_use:
similarity_top_k=20 and no postprocessor — a reranker is missing"
when_not_to_use:Third-person analytical view of how a mature RAG pipeline thinks about the reranker. The skill is for an LLM agent that writes / reviews / debugs retrieval code — it teaches the cross-framework reranking discipline, not one vendor's API. For the per-framework API, descend to
[[llamaindex]](node postprocessors) or[[agentsop-hybrid-retrieval]](the recall stage that feeds the reranker).
This is the C4 gap skill in the Phase-D enhance pass. The reranker SOP
existed only buried inside [[llamaindex]] (OP-03 AddReranker, Stage 3 step 7,
anti-pattern A6). It is the highest-ROI single addition to a naive RAG
pipeline, so it earns a standalone overlay.
Activate when any holds:
[[llamaindex]] Stage 3
lists reranking as the last optimization step, deliberately.OP-03).Do not activate (boundary — see §6):
[[agentsop-hybrid-retrieval]]), or chunking first.Retrieve wide for recall with a cheap bi-encoder; rerank narrow for precision with an expensive cross-encoder that sees query + document together — something the bi-encoder structurally could not do.
The retriever (bi-encoder / vector search) embeds the query and every document separately, offline. Similarity is a dot product of two vectors that never met. This is fast (vectors are precomputed; ANN search is sub-linear) but lossy: the document's vector is a single "topic average" computed without knowledge of the query.
A cross-encoder takes [query, document] as a single joint input and
runs full attention across both, emitting one relevance score. It sees exactly
which query token matches which document token. This is far more accurate — and
far more expensive: it cannot be precomputed, so it runs once per
(query, candidate) pair at query time. Scoring 1M docs this way is infeasible;
scoring 20-50 is cheap.
query ─┐ query ─┐
├─ dot product (precomputed) ├─► [CROSS-ENCODER] ─► score
doc ─┘ ← bi-encoder, FAST, lossy doc ─┘ joint attention, SLOW, sharp
RECALL stage (retrieve top-50) PRECISION stage (rerank → top-5)
The reranker is the bridge: it spends cross-encoder accuracy on a small candidate set the bi-encoder produced cheaply. Wide net, sharp knife.
[[llamaindex]] Stage 3)Prompts first, reranking last. Reranking is high-impact but expensive — exhaust the cheap knobs (prompt, embed model, chunk size, hybrid) before spending per-query cross-encoder latency. But once those are spent, the reranker is usually the single biggest remaining lever (5-15pp faithfulness lift on noisy corpora — [[llamaindex]]
OP-03).
Each stage gates the next. Never skip the baseline measurement.
Before adding anything, prove the symptom is precision, not recall:
[[agentsop-hybrid-retrieval]]) / chunking. A reranker will not help.Raise the retriever's top_k (or top_n) to 20-50. This is the "recall"
stage: cast a wide net so the reranker has the gold doc to find. Hybrid
retrieval ([[agentsop-hybrid-retrieval]]) feeds the reranker an even better candidate
pool because it adds lexical recall the dense retriever misses.
Add a reranker as a post-retrieval step (LlamaIndex node postprocessor;
LangChain ContextualCompressionRetriever — §7). Pick the model per §4 OP-03.
It consumes the wide candidate list and re-scores every candidate against the
query with a cross-encoder.
Truncate to top-k = 3-5 after rerank. This is what reaches the synthesizer. The whole point: the LLM now sees a small, high-precision context instead of a large noisy one.
Re-run the same eval set. Compare before vs after on {MRR, faithfulness, relevancy, p95 latency, per-query cost}. Keep the reranker only if the precision lift justifies the added latency/cost (§5). A reranker that adds 300ms for +1pp is not always worth shipping. Pin N and k as tuned constants.
Each operation: Trigger / Action / Output / Evidence. Full machine-readable
list in intermediate/operation_candidates.json.
OP-03 (#1/#10 = right doc in top-k, wrong top-1);
[[agentsop-hybrid-retrieval]] for the recall path.top_k=4.OP-03.OP-03 (CohereRerank / SentenceTransformerRerank /
ColBERT named); external: "cohere rerank", "bge-reranker", "cross-encoder
rerank RAG".OP-02 TuneChunkSize (same sweep-and-pin
discipline applied to N/k); Stage 3 step 7.[[agentsop-hybrid-retrieval]], BM25 + dense) for the
wide stage, then rerank its fused candidate list. Hybrid maximizes recall into
the pool; rerank maximizes precision out of it. They compose.OP-04
AddHybridBM25 + OP-03 AddReranker (sequential in Stage 3).OP-10 EvalLoop, Stage 2 ("eval loop before
optimizing anything"), Stage 4.困境: A reranker reliably lifts precision but adds a per-query stage: network round-trip (API) or GPU inference (local). On a latency-sensitive surface (chat, autocomplete) the added p95 may violate the SLA even when quality improves.
约束: Cross-encoder cost is per (query, candidate) pair and scales with N ([[llamaindex]] Stage 3: reranking is "high-impact but expensive"). Latency is dominated by N, not k. The bi-encoder stage was chosen precisely because it is fast; the reranker reintroduces query-time compute.
决策步骤:
结果: Reranking is the highest-ROI lever only when latency headroom exists. The decision is SLA-driven, not quality-driven in isolation. Smaller N often recovers most of the lift at a fraction of the latency.
可提取的操作: OP-04, OP-05. Anti-pattern A3 (over-large N).
困境: The hosted API ships in an afternoon, needs no GPU, and tracks SOTA — but bills per search and sends query + candidates to a third party. A local bge-reranker has zero per-call fee and keeps data in-boundary — but needs a GPU, ops ownership, and model-update discipline.
约束: Per-query cost (API) vs fixed infra cost + ops (local); data-residency / compliance; latency (API adds network hop, local adds inference); team's GPU/MLOps capacity.
决策步骤:
rerank(query, nodes) -> nodes
seam so swapping API↔local is a one-line change.结果: Default to the API to validate the lift cheaply (prove the reranker helps before investing in infra), then migrate to local once volume, cost, or residency justify it. The abstraction seam makes the migration safe.
可提取的操作: OP-03, OP-04. Anti-pattern A5 (vendor lock-in, no seam).
| # | Anti-pattern | Correct move |
|---|---|---|
| A1 | Reranking to fix recall — gold doc isn't in top-N | Fix retrieval / hybrid ([[agentsop-hybrid-retrieval]]) / chunking; a reranker only reorders what's already retrieved |
| A2 | Naive similarity_top_k=N then feed all N to the LLM, no rerank | Widen N and rerank to top-3-5 ([[llamaindex]] A6) |
| A3 | Over-large N (rerank 200+ candidates) | Latency scales with N; pick the smallest N that saturates hit-rate (OP-05) |
| A4 | Add reranker first, before prompt/embed/chunk/hybrid | Order law: reranking is last ([[llamaindex]] Stage 3); cheapest knobs first |
| A5 | Hard-wire one vendor SDK throughout the pipeline | Hide behind a rerank(query, nodes) seam so API↔local swaps in one line (Dilemma 2) |
| A6 | Ship reranker without before/after eval | Gate on {MRR, faithfulness, p95, $/query} (OP-07); a reranker that costs latency for no lift is removed |
| A7 | Keep N=k (rerank n candidates, return n) | Reranking only helps when k < N — you must discard the low-scored tail |
| A8 | Re-embed / re-chunk hoping to fix "wrong top-1" | If the right doc is present but buried, that's a rerank job, not a re-ingest |
[[agentsop-hybrid-retrieval]]).index.as_query_engine(similarity_top_k=20) with no node postprocessor →
A2 ([[llamaindex]] PR-smell).top_k still 4 → A7 (N=k, reranker is a no-op).The reranker is one stage with the same shape everywhere: consume a wide candidate list, re-score with a cross-encoder, truncate to top-k.
| Framework / vendor | Reranker primitive | Notes |
|---|---|---|
LlamaIndex ([[llamaindex]]) | Node postprocessor: CohereRerank, SentenceTransformerRerank, ColbertRerank, LLMRerank passed as node_postprocessors=[...] to the query engine; widen similarity_top_k, set top_n on the reranker | The canonical reference; OP-03 AddReranker, Stage 3 step 7, A6 |
| LangChain | ContextualCompressionRetriever wrapping a base retriever with a CohereRerank / CrossEncoderReranker / LLMChainExtractor compressor | Base retriever returns N, compressor reranks/filters to k |
| Cohere Rerank API | cohere.rerank(query, documents, top_n, model="rerank-v3.5") | Hosted cross-encoder; multilingual; per-search billing |
| Voyage rerank API | voyageai.rerank(query, documents, model="rerank-2", top_k) | Hosted; pairs well with Voyage embeddings |
| bge-reranker (local) | FlagReranker("BAAI/bge-reranker-v2-m3") / via sentence-transformers CrossEncoder | Open-weights, self-hosted, no per-call fee, GPU recommended |
| SentenceTransformers cross-encoder | CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2").predict([(q, d), ...]) | Lightest local option; CPU-viable for small N |
| ColBERT / RAGatouille | Late-interaction reranker; token-level scoring, precomputable | Scales to larger N than a full cross-encoder |
| Haystack | TransformersSimilarityRanker / CohereRanker component in the pipeline | Same wide→narrow shape, pipeline-component form |
Activate this skill for the reranking decision (whether, where, how wide, which model, what it costs). Descend to
[[llamaindex]]for node-postprocessor wiring, and to[[agentsop-hybrid-retrieval]]for the recall stage that feeds it.
references/R1-source-evidence.md — every cited claim resolved to a source line.intermediate/operation_candidates.json — machine-readable operation list.[[llamaindex]] SKILL — OP-03 AddReranker, OP-02 TuneChunkSize,
OP-04 AddHybridBM25, OP-10 EvalLoop; Stage 2/3/4; anti-patterns A6/A3;
failure modes #1/#10; the "prompts first, reranking last" order law.[[agentsop-hybrid-retrieval]] — the wide/recall stage (BM25 + dense) that feeds the
reranker; lexical-identity recall.