Start learning free

LESSON 128 OF 129 · AI ENGINEERING: INTERVIEW QUESTIONS

Rerank What You Retrieved

Practice

The interview question: “Your vector search finds the right chunks, but not in the right order. For Is TP-220 dishwasher safe? a chunk about a mug comes before the one about the teapot. How do you fix the order?” The expected answer: rerank. Give each chunk a second score from the words it shares with the question, blend the two scores, and sort again.

The keyword score is the share of the question's words that appear in the chunk. The final score is 0.7 * vector score + 0.3 * keyword score: meaning still counts most, and an exact name such as a product code lifts the chunk that has it. An example with another question:

question: "Where is order 42?"       words: where, is, order, 42
chunk:    "Order 42 shipped today."  shares: order, 42
keyword score = 2 / 4 = 0.5
final = 0.7 * 0.6 + 0.3 * 0.5 = 0.57   (its vector score is 0.6)

The starting code has the question, the three retrieved chunks with their vector scores, the helpers words, keyword_score and by_final (highest final score first, ties by text), and a loop that prints each chunk's vector score, its final score and its text. With rerank written, the program prints the first three lines below; the check then reranks a second question with your function:

0.71 -> 0.722 Teapot TP-220: not dishwasher safe, wash it by hand.
0.78 -> 0.696 Mug MG-110: dishwasher safe and microwave safe.
0.64 -> 0.523 Dishwasher tips: put cups on the top rack.
Question: LP-310 warm light?
0.6 -> 0.72 Desk lamp LP-310: warm light.
0.9 -> 0.63 Floor lamp: tall and bright.
0.2 -> 0.14 Pen: blue ink.

Your task: write rerank(question, docs). For each doc, compute its keyword score, store the blend in doc["final"], and return the docs sorted with key=by_final.


Assignment
Follow the Coding Tutorial to write rerank below by_final.


Hint
Leave doc["score"] as it is: the print shows it next to the final score. Until the return is there, rerank gives back None and the print loop fails with a TypeError.


STEP 1 OF 5
    Ctrl + Enter to check