AI News·26 Nov 2026·via Dev Community

    I Built BM25 From Scratch to See If It Could Keep Up With Embeddings

    Developer Kumail built a question-answering assistant over his personal work documents and challenged common Retrieval Augmented Generation (RAG) practices. By implementing and comparing traditional BM25 keyword search with modern embedding models, his experiment found that while embeddings performed better, a hybrid approach combining both methods yielded the most compelling results.

    Developer Kumail recently created 'Ask Kumail,' a question-answering assistant designed to provide information from his own professional documents. He initiated this project to test the common assumption in Retrieval Augmented Generation (RAG) tutorials that modern embedding models are the primary solution for document retrieval. Kumail aimed to directly compare these newer techniques against BM25, a keyword-based ranking algorithm in use since the 1990s.

    The 'Ask Kumail' system operates on a knowledge base comprising eight markdown files, including his resume and seven project READMEs. This corpus is divided into 80 distinct sections, or 'chunks,' each carrying a small header to provide context, such as 'Document: PawPass / Section: Tech Stack'. Kumail explicitly noted that this is a small dataset, stating that his findings are specific to this corpus and not intended to be a general verdict on BM25 versus embeddings.

    For the traditional approach, Kumail implemented BM25 from scratch. His version incorporated standard features like inverse document frequency, term frequency with diminishing returns, and length normalization. He also customized the tokenization process, which included adding his own name to the stopword list, as 'Kumail' appeared in nearly every document and thus carried little informational value as a search term.

    The modern approach involved using Gemini embeddings, specifically `gemini-embedding-001`, with a fallback to Vercel AI Gateway's `text-embedding-3-small` model. These models convert document chunks and queries into numerical representations, which are then compared using cosine similarity. Query embeddings are cached to avoid redundant API calls, streamlining the process.

    The most interesting outcome, according to Kumail, emerged from a hybrid strategy that merged both BM25 and embedding rankings. This fusion was achieved using reciprocal rank fusion, where each list contributed a score based on rank rather than raw values. This method was chosen because BM25 scores and cosine similarities operate on fundamentally different scales, making a direct summation impractical. Fusing on rank allows the strengths of both keyword matching and semantic understanding to contribute to the final retrieval.

    In the final answering stage, the top five retrieved chunks were sent to a chat model. Strict instructions were given to the model: every factual sentence had to cite a passage, the model must state if it did not know the answer instead of guessing, and text within passages was treated as data, not commands. The system was designed to degrade gracefully, falling back to BM25 only if the embedding API failed, or returning the best-matching passage if the chat model failed, ensuring a resilient user experience.

    Share this article

    Go deeper
    Original reporting by Dev CommunityWe don't republish, read the full story →

    Related reading

    6 stories