Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 6.6 — Diverse Results
RAG and Vector Search · Example 1 of 2

Maximal marginal relevance by hand

The vectors are three-number toys so you can check the arithmetic. Real embeddings have hundreds of numbers; the formula is the same.

When the same fact is stored in several places, the top results are copies of one passage and the context window fills with repeats. Maximal marginal relevance trades a little relevance for variety, and a de-duplication step removes true copies.

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

Write jaccard(a, b) over the sets of lower-case words (re.findall(r"[a-z0-9]+", ...)). Print it for "the bike is red" and "the bike is blue" with 2 decimals.

02

Write mmr_score(relevance, redundancy, lam) and print the score of a passage with relevance 0.9 and redundancy 0.95 for lam = 0.7 and lam = 0.3, with 3 decimals.

Explain it without notes

01

Why do duplicate passages hurt a RAG answer, even though each one is relevant?

02

Explain the MMR formula and what lambda does.

03

Why does word overlap fail to detect a paraphrased duplicate, and what do you use instead?

04

At which points in the pipeline would you remove duplicates, and why there?

Maximal marginal relevance by hand
Sign in to run this example in your browser.

Expected output

relevance: refund A 0.901, refund B 0.900, refund C 0.892, late fee 0.852, monthly pass 0.502
lambda 1.0: refund A, refund B, refund C
lambda 0.7: refund A, late fee, refund B
lambda 0.5: refund A, late fee, monthly pass
lambda 0.7 round by round:
  round 1: refund A 0.631  refund B 0.630  refund C 0.624  late fee 0.596  monthly pass 0.351
  round 2: refund B 0.330  refund C 0.325  late fee 0.429  monthly pass 0.196
  round 3: refund B 0.330  refund C 0.325  monthly pass 0.192