Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Core / Securing RAG and data

Securing RAG and data cheat sheet

Build a RAG service and map it (lesson 1)

TaskCommand or code
Build the TF-IDF index of the chunksindex = Index(load())
Search the best chunks for a questionindex.search(question, 3)
Search only the chunks that may be readindex.search(question, 3, allow=ids)
Build the prompt: rules, then sources, then questionbuild_prompt(question, chunks)
Ask with a fake model in a testanswer(index, question, model=echo)
Show who can write to a filels -l kb.json

Poison a knowledge base (lesson 2)

TaskCommand or code
Find the place of a chunk in the rankingposition(index, question, "pub-returns-update")
Count the answers that hold a markerpython3 detect.py recorded-02.json "90 days"
Ask the model with five seeds and countpython3 live02.py

Provenance and review (lesson 3)

TaskCommand or code
Hash a text with SHA-256hashlib.sha256(text.encode()).hexdigest()
Hash what an approval covers: id, level, source, owner, textchunk_hash(chunk)
Record the approval of reviewed chunkspython3 provenance.py approve kb.json approvals.json all
Screen chunks: accept, queue, rejectpython3 provenance.py check incoming.json approvals.json
Read the exit code of the last commandecho "exit code $?"
Keep only accepted chunks[c for c in chunks if screen(chunks, approvals)[c["id"]][0] == "accept"]

Access control in retrieval (lesson 4)

TaskCommand or code
List the ids that a user may readallowed_ids("gus", kb)
Filter before rankinganswer(index, q, allow=allowed_ids(user, kb))
Run the table of user, question, idspython3 test_access.py
Run the same table without the filterpython3 test_access.py nofilter
Test a filter on the text of answerspython3 late_filter.py

Embeddings, index files and ranking tricks (lesson 5)

TaskCommand or code
Save the index to a filepython3 peek.py
Write an index file for the public level, and count the secret words in it and in the full indexpython3 partition.py
Probe a score side channel, then the fixpython3 probe.py, python3 probe.py safe
Share of different words in a textdistinct_ratio(text)
Measure a stuffed chunk, with and without defencespython3 stuff.py

Private data (lesson 6)

TaskCommand or code
Redact emails, cards and staff idsclean, counts = redact(text)
Check a number with the Luhn sumluhn("4111111111111111")
Write a log line without private datasafe_log(log, user, text)
Keep only chunks of the last 90 dayskeep_recent(chunks, "2026-11-21", 90)
Run the redaction testspython3 test_redact.py

Measure quality and leakage (lesson 7)

TaskCommand or code
Run the plain service on the labelled setpython3 evaluate.py plain kb.json poison.json
Run the controlled servicepython3 evaluate.py controlled kb.json poison.json
Show only the summary line and the exit codepython3 evaluate.py plain | tail -1 then echo "exit code ${PIPESTATUS[0]}" (bash; zsh: ${pipestatus[1]})
Set the thresholdsMIN_PRECISION, MAX_LEAKS in evaluate.py

Architecture and deletion (lesson 8)

TaskCommand or code
Check where each control sitspython3 archcheck.py arch.json
Rebuild the index after a deletestore.rebuild()
Make a changed level reach the indexstore.relabel(cid, level)
Test that deleted and changed text is gonepython3 test_store.py

Govern the knowledge base (lesson 9)

TaskCommand or code
Report the gaps of the cataloguepython3 govcheck.py catalogue catalogue.csv kb.json 2026-11-21
Test a proposal for a new sourcepython3 govcheck.py source proposal.json

The finished service (lesson 10) and the review (lesson 11)

TaskCommand or code
Build the lab folder after lesson 6, from the zipsh kr-lab.sh 06-keep-private-data-out-of-prompts-logs-and-stores ~/krlab/securing-rag
Make the files that lessons 3 and 5 createbash catchup.sh
Approve the tickets (needed by lesson 10 and its suite)python3 provenance.py approve tickets.json approvals.json all
Run the test suitepython3 test_securerag.py
Run the service and show what it held backpython3 securerag.py
Test the colleague's servicepython3 scenario_tests.py

Safety habits