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
- A code is found inside a sentence
- No code gives None
- An exact code returns the passages that contain it
- An unknown code returns nothing and does not rank
- 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.