Run it

From a clone of stackunseen/examples (on Windows, type python where it says python3):

bash
git clone https://github.com/stackunseen/examples
cd examples
cd rag-starter
python3 rag.py "How much can I spend on a hotel in London?"
python3 evaluate.py

What it prints

output
Hotels are reimbursed up to 180 pounds per night in London and 140 pounds per night elsewhere in the United Kingdom. Breakfast is included in the nightly limit when the hotel charges it separately. [travel-and-expenses#hotels]

Retrieved:
  1. travel-and-expenses#hotels
  2. it-and-access#remote-access
  3. data-handling#classification-levels

20 questions, 31 chunks

mode      recall@1  recall@3  recall@5    MRR
bm25          0.80      1.00      1.00   0.89
ngram         0.90      1.00      1.00   0.94
hybrid        0.85      1.00      1.00   0.93

Hybrid search did not rank the expected section first for:
  - What should I do with a suspicious email asking for my password?
    first result: security-basics#passwords-and-sign-in
  - Can I paste customer personal data into ChatGPT?
    first result: procurement#new-software-and-vendors
  - Can I work from Spain for a month?
    first result: remote-work#home-office-equipment

What you should see

The first command answers from the travel policy, cites the section it used, and lists what retrieval returned:

text
Hotels are reimbursed up to 180 pounds per night in London and 140 pounds per night elsewhere in the United Kingdom. Breakfast is included in the nightly limit when the hotel charges it separately. [travel-and-expenses#hotels]

Retrieved:
  1. travel-and-expenses#hotels
  2. it-and-access#remote-access
  3. data-handling#classification-levels

The second scores retrieval before any model is involved. For each of twenty labelled questions it checks whether the right section is in the top 1, 3 or 5 results, which is recall at k. MRR adds how high it ranked by averaging 1 divided by the right section's position, counting 0 when it is not in the top 5.

text
20 questions, 31 chunks

mode      recall@1  recall@3  recall@5    MRR
bm25          0.80      1.00      1.00   0.89
ngram         0.90      1.00      1.00   0.94
hybrid        0.85      1.00      1.00   0.93

Hybrid search did not rank the expected section first for:
  - What should I do with a suspicious email asking for my password?
    first result: security-basics#passwords-and-sign-in
  - Can I paste customer personal data into ChatGPT?
    first result: procurement#new-software-and-vendors
  - Can I work from Spain for a month?
    first result: remote-work#home-office-equipment

How it works

Eight short policy documents are split on their own headings, and every chunk keeps its document title and heading in the indexed text. Two indexes score each question: BM25 over words, and character n-gram vectors that catch word forms and spelling slips, a cheap offline stand-in for embeddings. Reciprocal rank fusion merges the two rankings by position, because their scores live on different scales; that merge is what makes it hybrid search.

The answer quotes the best supported sentences with a citation such as [travel-and-expenses#hotels]. When too few of the question's words appear in any retrieved chunk, it says it could not find the answer and lists the closest sections instead of guessing.

What the numbers say

Two things are worth noticing. The n-gram index alone puts the right section first more often than the fusion does: hybrid search protects you from one index's blind spots, but it does not always win, so measure before you assume. And python3 evaluate.py --chunking windows, the same questions against fixed 40-word chunks that ignore headings, drops hybrid recall at 1 from 0.85 to 0.40 and recall at 5 from 1.00 to 0.60.

The failure to try first

python3 rag.py "Can I work from Spain for a month?" retrieves the home-office section first and quotes it confidently, because it shares the words "work" and "month", while the right section talks about "another country". A confident answer from the wrong section is the failure that ends trust in an assistant. Fix it with query rewriting, an embedding model or a reranker, then rerun the evaluation to prove the fix and check nothing else broke.

Taking it to production

Replace the n-gram index with an embedding model and a vector index, keep BM25 beside it, rerank the top 20 to 50, and grow the labelled set from real questions. Keep test.sh in CI so it fails the build when hybrid recall at 5 drops below 0.9. The reasoning behind each step is in Chunk for the question, not for the token limit, Test retrieval on its own, Hybrid search is the practical default and Reranking is where retrieval becomes useful. To see chunk boundaries move, try the chunking lab.