A model rewrite with a guard and a cost count
The model is a scripted stand-in with one deliberately bad reply (the late-fee answer). The 450 ms per call is an assumed number: measure your own provider.
Rewriting turns the question the user typed into the question the retriever should see: first with plain rules (lower-case, drop greetings, fix known typos), then with a model for what rules cannot do. Keep the original, guard the model's reply, and count what the extra call costs.
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 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write rewrite(question, drop, fix) that lower-cases the question, removes any word in drop, applies fix (a dict of replacements) and joins with spaces. Print the rewrite of "Hi pls where is my refnd" for drop = {"hi", "pls"} and fix = {"refnd": "refund"}.
Write accept(reply, original, max_words) that returns the reply when it is non-empty and has at most max_words words, otherwise the original. Print the result for an empty reply, a good reply and a long reply with max_words = 6.
Explain it without notes
What does a rule-based rewrite do, and what can it not do?
Why keep the original question when you have a rewrite?
What can go wrong when a model is asked to rewrite a query, and how do you guard against it?
How do you decide whether a model rewrite is worth its extra call?
Expected output
Q: hours?
rewrite: What time do the shops open and close?
hit before: D1 (1) after: D2 (3)
Q: I can't get in
rewrite: How do I reset my password?
hit before: none after: D3 (2)
Q: pass price
rewrite: How much does the monthly pass cost?
hit before: D8 (1) after: D8 (2)
Q: late fee
rewrite: rejected (31 words), keeping the original
hit before: D6 (1) after: D6 (1)
extra model calls: 4, added latency: 1800 ms, words sent and received: 118