Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 6.2 — Mixing Scores (and Why It Is Hard)
RAG and Vector Search · Example 2 of 2

A weighted sum, and how it breaks

D15 is a passage added to the handbook for this example only. The toy embedder knows none of its words, so its vector score is 0.

The obvious way to combine BM25 and cosine is to add the scores. They live on different scales, so you must normalise first, and even then the weights you tuned stop working when the data changes.

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 mix(keyword, vector, alpha) that takes two lists of scores already scaled to 0..1 and returns the list of mixed scores. Print the result for [1.0, 0.0], [0.0, 1.0] and alpha = 0.7.

02

Write min_max(scores) that returns zeros when all scores are equal. Print it for [2, 4, 6] and for [3, 3].

Explain it without notes

01

Why can't you simply add a BM25 score and a cosine similarity?

02

How does min-max normalisation work, and what is one weakness?

03

Explain how adding a single document can change the order of two unrelated documents in a weighted-sum hybrid.

04

Why can't a min-max normalised score be used to decide that no passage is good enough?

A weighted sum, and how it breaks
Sign in to run this example in your browser.

Expected output

150 dollar fee
  alpha 0.5:       D12=0.798, D8=0.500, D1=0.455
  alpha 0.2:       D8=0.800, D1=0.728, D12=0.677
  alpha 0.5 + D15: D15=1.000, D12=0.505, D8=0.408
I forgot my login, how do I get back in?
  alpha 0.5:       D3=0.883, D10=0.572, D1=0.500
  alpha 0.2:       D3=0.953, D10=0.371, D5=0.338
  alpha 0.5 + D15: D3=0.875, D10=0.572, D1=0.500