Command Palette

Search for a command to run...

Hectal
Phase 6: Hybrid Search and Re-ranking
RAG and Vector Search · Exercise 4 of 5 PreviousNext
Advanced

Pick diverse results with MMR

Write mmr(query, vectors, lam, k). vectors is a dict from name to vector (a list of numbers). cosine(a, b) is given in the starter.

Pick k names one at a time. In each round score every unpicked name as lam * cosine(query, vector) - (1 - lam) * redundancy, where redundancy is the highest cosine between that name's vector and any already picked vector (0.0 if none are picked). Take the highest score; on a tie take the name that comes first in the dict. Return the picked names in order.

What Check tests

  1. lambda 1 is plain top-k by relevance
  2. A lower lambda swaps a copy for a different passage
  3. k larger than the number of names returns all of them
  4. Ties take the name that comes first
  5. The first pick ignores redundancy

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.