Re-embedding to catch a swapped vector
The embedder counts four letters, which is a toy. With a real model, vectors from the same text can differ slightly between runs or versions, so compare within a tolerance, and pin the model version.
Anyone who can add a document can change the answers. Decide who may add what, record where every passage came from and how far it is trusted, and check that stored vectors still match their text.
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 pick(hits) where each hit is (doc_id, trust) and trust is "reviewed" or "untrusted". Return the id of the first reviewed hit, or of the first hit if none is reviewed, or None for an empty list.
Write content_hash(text) using h = (h * 31 + ord(char)) % 1000003 starting from 7, and tampered(rows) where rows maps id to (text, stored_hash) and returns the ids whose stored hash differs.
Explain it without notes
What is data poisoning in a RAG system, and why does ranking not protect against it?
What do an allow-list, provenance and trust levels each contribute?
How can a vector in the store be wrong, and how do you detect it?
Two retrieved passages disagree. What does your system do?
Expected output
before the check: U1=165, D2=60, D8=55
integrity check: mismatch for U1
after quarantine: D2=60, D8=55, D4=49