RYLAIX// UNIVERSE
All field notes

1 min read

Hybrid RAG: FTS5 + Dense Vectors + Cross-Encoder

Why vector embeddings alone fail for exact technical keywords and how hybrid ranking achieves high precision.

  • Applied AI
  • Python
  • RAG
  • Search

Where Pure Vector Search Fails

Dense vector embeddings excel at semantic similarity, but fail in technical domains where queries contain exact identifiers: error codes (0x80040154), Jira ticket IDs (PROJ-1402), or function signatures (calculate_backrun_share).

If a developer asks How do I fix PROJ-1402?, vector embeddings often return generic articles about bug fixes rather than the exact ticket.

Query -> [ BM25 / SQLite FTS5 ]  (Exact Lexical Match)         -> [ OpenAI Embeddings ]   (Semantic Vector Match)  --> Reciprocal Rank Fusion -> Cross-Encoder -> Top 5

The Three-Stage Retrieval Pipeline

  1. Stage 1 - Lexical Candidate Retrieval: SQLite FTS5 extracts documents containing exact keywords, IDs, and symbols in under 5ms.
  2. Stage 2 - Dense Vector Search: cosine similarity over text embeddings surfaces conceptual matches.
  3. Stage 3 - Cross-Encoder Reranking: a lightweight cross-encoder scores the combined top-20 candidates together with the prompt, outputting the final 5 highest-relevance context chunks for the LLM.

This hybrid architecture improved citation precision in production from 68% to 94%.