document-reranking
Skill0 starsChanged 6 months agoMITView on GitHub
Re-ranks retrieved documents after initial vector search using a cross-encoder or LLM judge for higher precision. Use when improving retrieval quality in RAG pipelines, implementing cross-encoder re-ranking, using Cohere Re-rank API, or building an LLM-as-judge re-ranker.
In this repository65 other files it gives its agents.
- AGENTS.mdAGENTS.md
- skills/building-agents/agents.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- llms-full.txtllms.txt
- llms.txtllms.txt
- adding-guardrailsSkill
- agent-health-timeoutSkill
- agent-permission-scopingSkill
- agent-session-isolationSkill
- agent-step-loggingSkill
- agent-supervisor-routerSkill
- agent-telemetrySkill
- async-agent-backgroundSkill
- building-agentsSkill
- building-teamsSkill
- building-toolsSkill
- calendar-scheduling-toolSkill
- callback-tracing-handlerSkill
- chain-of-thought-promptingSkill
- chat-model-configurationSkill
- code-execution-sandboxSkill
- common-agent-patternsSkill
- context-window-managementSkill
- conversation-memorySkill
- conversation-state-dbSkill
- database-query-toolSkill
- document-loadersSkill
- email-notification-toolSkill
- embedding-model-ingestionSkill
- fallback-model-chainSkill
- few-shot-injectionSkill
- filesystem-toolSkill
- human-in-the-loopSkill
- hybrid-searchSkill
- inspecting-streamsSkill
- integrating-with-laurenSkill
- json-schema-enforcementSkill
- llm-provider-connectionSkill
- llm-rate-limitingSkill
- long-term-vector-memorySkill
- managing-memorySkill
- migrating-to-lauren-aiSkill
- multi-agent-delegationSkill
- multi-tool-orchestrationSkill
- multi-turn-tool-stateSkill
- output-guardrailsSkill
- plan-and-execute-agentSkill
- prompt-injection-defenseSkill
- prompt-versioningSkill
- rag-pipelineSkill
- react-agent-loopSkill
- response-validation-retrySkill
- rest-api-toolSkill
- securing-agentsSkill
- semantic-cacheSkill
- streaming-response-handlerSkill
- structured-output-parsingSkill
- sub-agent-spawningSkill
- system-prompt-templatingSkill
- testing-agentsSkill
- The first 60 shown.
The file
Files
Use
codemap find "SymbolName"to locate any symbol before reading — it gives exact file + line range and is faster than grep across the whole repo.
Re-ranking Retrieved Documents
Overview
Initial vector search retrieves by similarity but may include off-topic results. Re-ranking scores query–document pairs jointly, improving precision at the cost of an extra model call.
Query + Top-K candidates
│
▼
DocumentReranker.rerank()
│
▼
Re-scored and sorted results (top-N returned)DocumentReranker (in-process)
A lightweight word-overlap re-ranker for development and testing:
from dataclasses import dataclass
from typing import Callable
@dataclass
class SearchResult:
doc_id: str
text: str
score: float
class DocumentReranker:
def __init__(self, score_fn: Callable[[str, str], float] | None = None):
self._score_fn = score_fn or self._simple_overlap
def _simple_overlap(self, query: str, doc: str) -> float:
q_words = set(query.lower().split())
d_words = set(doc.lower().split())
return len(q_words & d_words) / max(len(q_words), 1)
def rerank(
self,
query: str,
results: list[SearchResult],
top_k: int | None = None,
) -> list[SearchResult]:
reranked = sorted(
results,
key=lambda r: self._score_fn(query, r.text),
reverse=True,
)
return reranked[:top_k] if top_k else rerankedCohere Re-rank API
For production-grade re-ranking, use the Cohere re-rank model:
import cohere
co = cohere.AsyncClient()
class CohereReranker:
async def rerank(
self,
query: str,
results: list[SearchResult],
top_k: int | None = None,
) -> list[SearchResult]:
documents = [r.text for r in results]
resp = await co.rerank(
model="rerank-english-v3.0",
query=query,
documents=documents,
top_n=top_k or len(documents),
)
reranked = []
for item in resp.results:
original = results[item.index]
reranked.append(SearchResult(
doc_id=original.doc_id,
text=original.text,
score=item.relevance_score,
))
return rerankedCustom cross-encoder
from sentence_transformers import CrossEncoder
model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
def cross_encoder_score(query: str, doc: str) -> float:
return float(model.predict([(query, doc)])[0])
reranker = DocumentReranker(score_fn=cross_encoder_score)Pipeline: retrieve then re-rank
kb = KnowledgeBase(store=InMemoryVectorStore())
await kb.load(TextLoader("docs/faq.txt"))
reranker = DocumentReranker()
async def retrieve_and_rerank(query: str, top_k: int = 3) -> list[SearchResult]:
# Step 1: broad retrieval
memory_results = await kb.search(query, top_k=20)
# Step 2: convert to SearchResult
candidates = [
SearchResult(doc_id=r.id, text=r.content, score=r.score)
for r in memory_results
]
# Step 3: re-rank to top_k
return reranker.rerank(query, candidates, top_k=top_k)Reference
lauren_ai._knowledge:KnowledgeBase,TextLoaderlauren_ai._memory._vector:InMemoryVectorStore- Skills:
rag-pipeline,hybrid-search,vector-store-integration
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

