Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.8 — Build: Keyword Search for AskDocs
RAG and Vector Search · Example 1 of 2

AskDocs v1: BM25 behind the same interface

The whole product as it stands after Phase 1. Only retrieve and its Index are new compared with Phase 0.

AskDocs version 1 swaps Phase 0's word counting for an inverted index with BM25, behind the same retrieve(question, k) interface, and a small harness measures the change.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Add a function explain(question, doc_id) to a small BM25 setup that returns the list of (term, part) pairs for one passage, using the formula from Topic 1.4 with k1 = 1.2, b = 0.75. Test on two passages: "late fee" and "monthly pass" with the question "late".

02

Write refuse_if_weak(hits, min_score) that returns None when the best score is under min_score (or there are no hits) and the hits otherwise. Print the results for scores [2.43, 0.64] and [1.09] with a bar of 2.0.

Explain it without notes

01

Why can AskDocs swap its retriever without touching answer?

02

How does the Index avoid scoring passages that cannot match?

03

Why did the hit rate at 3 fall from 0.90 (Topic 1.6) to 0.70 here?

04

Why is MIN_SCORE = 2.0 only a guess?

AskDocs v1: BM25 behind the same interface
Sign in to run this example in your browser.

Expected output

Q: What does the monthly pass cost?
   sources: D8 (5.23), D10 (2.43)
   answer:  The monthly pass costs 25 dollars and includes unlimited rides of up to 45 minutes. [D8]
Q: When do shops close on public holidays?
   sources: D2 (10.39)
   answer:  Shops open at 7 am and close at 9 pm every day. [D2]
Q: I forgot my login, how do I get back in?
   sources: D1 (2.62), D10 (2.38), D3 (2.01)
   answer:  A ride cancelled at least 2 hours before its start time is refunded in full. [D1]
Q: Do you rent scooters?
   sources: none
   answer:  I can't find that in the handbook.