Command Palette

Search for a command to run...

Hectal
Phase 1: Search Before Vectors
RAG and Vector Search · Exercise 5 of 5 Previous
Intermediate

Look up exact codes before ranking

docs is a dict of passage id to text. A code looks like letters, a hyphen and digits, such as ERR-4012.

Write find_code(question) returning the first code found in the question (matching [A-Za-z]+-[A-Za-z0-9]+, so a mistyped code is still treated as a code, exact case kept), or None.

Write search(question, docs, rank): if the question holds a code, return the ids of passages whose text contains that exact code (in the order of docs), or [] when none do (do not call rank). Otherwise return rank(question).

What Check tests

  1. A code is found inside a sentence
  2. No code gives None
  3. An exact code returns the passages that contain it
  4. An unknown code returns nothing and does not rank
  5. Questions without a code use the ranker

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.