- Combine sparse BM25 keyword search with dense vector similarity.
- Apply a cross-encoder re-ranking stage to eliminate false-positive contexts.
- Preserve table schemas and section hierarchies during chunking.
- Provide exact citation line metadata to prevent ungrounded hallucinations.
1. Why Naive Vector Search Fails in the Enterprise
Most RAG (Retrieval-Augmented Generation) tutorials recommend embedding text chunks with OpenAI `text-embedding-3` and finding the nearest neighbors via cosine similarity. In production enterprise scenarios, this naive approach fails consistently.
Dense vector embeddings understand semantic meaning, but they struggle with exact keyword precision—such as exact SKU numbers (`SKU-8492-X`), legal paragraph identifiers (`Section 4.2(b)`), or invoice numbers. A user searching for a specific code gets a semantically related paragraph that contains completely wrong data.
2. The Threeaid 3-Stage Hybrid Retrieval Pipeline
To deliver 98.9% answer accuracy across dense legal and technical documentation, we engineered a 3-stage pipeline:
1. **Dual Ingestion (Dense + Sparse):** When a document is chunked, we compute dense vector embeddings via `pgvector` / Pinecone, alongside a sparse lexical inverted index via BM25 (or PostgreSQL full-text search). 2. **Reciprocal Rank Fusion (RRF):** Both retrieval methods execute concurrently. We combine their candidate lists using reciprocal rank scoring, ensuring both keyword precision and semantic relevance are represented. 3. **Cross-Encoder Re-Ranking Stage:** The top 25 candidates are fed into a lightweight cross-encoder model (e.g. Cohere Re-rank or BGE-Reranker-v2). The cross-encoder evaluates the exact question-to-context relationship, pruning irrelevant chunks down to the top 4 most pertinent passages.
3. Document Chunking with Markdown Structure Awareness
Standard chunking by character count splits tables and paragraphs arbitrarily across boundaries. Our parser preserves markdown hierarchy, ensuring tables are rendered with header context preserved on every row.
