“Retrieval-Augmented Generation (RAG) augments LLM prompts with dynamic external factual context. Advanced RAG combines Dense Vector Search (semantic capture) with Sparse BM25 Search (exact keyword/identifier matches via Reciprocal Rank Fusion), followed by a Cross-Encoder Re-Ranker model that scores query-document relevance jointly before LLM synthesis.”
Building high-precision Retrieval-Augmented Generation pipelines using BM25 keyword + Dense vector hybrid search, followed by Cross-Encoder re-ranking.
// Advanced RAG with Hybrid RRF & Prompt Assembly
export async function assembleRAGPrompt(
userQuery: string,
retrievedChunks: { text: string; score: number; source: string }[]
): Promise<string> {
const contextBlock = retrievedChunks
.map((c, i) => `[DOC_${i + 1}] (Source: ${c.source})\n${c.text}`)
.join('\n\n');
return `You are an enterprise AI assistant. Answer the question STRICTLY using the context below.
If the answer cannot be found in the context, respond "I do not have sufficient information to answer this."
=== RETRIEVED CONTEXT ===
${contextBlock}
=== USER QUERY ===
${userQuery}
=== GROUNDED ANSWER ===`;
}Hybrid Retrieval: Concurrently execute BM25 keyword search and Dense vector search
Reciprocal Rank Fusion (RRF): Merge sparse and dense candidate lists using RRF score ranking
Cross-Encoder Re-Ranking: Pass query + top 25 chunks through Cohere/BGE-Reranker for high-precision 0-1 scoring
Context Assembly: Top 3 re-ranked chunks injected into system prompt with markdown citations
LLM Grounded Synthesis: Model answers query strictly conditioned on provided context
Hybrid BM25 + Vector Search with Cross-Encoder re-ranking improves RAG answer accuracy from ~64% (Naive RAG) to over 92%.