Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.2 — Rewriting the Query
RAG and Vector Search · Example 2 of 2

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.

01

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"}.

02

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

01

What does a rule-based rewrite do, and what can it not do?

02

Why keep the original question when you have a rewrite?

03

What can go wrong when a model is asked to rewrite a query, and how do you guard against it?

04

How do you decide whether a model rewrite is worth its extra call?

A model rewrite with a guard and a cost count
Sign in to run this example in your browser.

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