Practice: RnR¶
Three exercises that build on the labs, then one open build task. Do them in your own copy (a fork) of this repo, commit as you go, and keep short notes.
Read the labs first: ai/moderation.py, ai/mod_prompts.py, evals/moderation_eval.py, ai/llm.py, ai/guardrails.py, obs/tracing.py, 05_golden_set_eval.py, 09_cost_meter.py.
Where your work goes:
- Product code: in
ai/,evals/and new test files intests/(thedb_filefixture intests/conftest.pybuilds a small database per test). - Your own files:
my-work/in this folder. - Notes:
my-work/rnr.md. For each exercise, a few lines on what you changed, why, and what you would do differently in production.
Some rules below say Decide. Make the choice yourself and write it in your notes.
If your team gave you an ASSIGNMENTS.md, it makes those choices for you: follow it.
ex1: A seller scorecard by month (no model)¶
Why: the Sellers page shows S03 at 83% on time. The average hides the October drop, one of the three planted stories.
- Add
ai/scorecard.pywithmonthly(conn, seller_id=None) -> list[dict]: per seller and month, orders, on-time % (as the Sellers page counts it), return rate and the average published rating. - Add
drops(rows) -> list[dict]: a month whose on-time % or rating falls more than a threshold below the seller's average of the three months before. - Skip months with too few orders to judge.
- Decide: the threshold, and the minimum orders per month.
- Add model-free tests on the test database: S03 in October is flagged.
Done when the tests pass and your notes list every flag on data/rnr.db, each marked real (planted) or noise.
ex2: A regression gate for prompt changes¶
Why: every prompt change ships with an eval run. Make that a command that can say no, not a table someone reads.
- Create
my-work/gate.py:python my-work/gate.py --baseline mod-v3 --candidate mod-v4loads both saved runs withlatest()(or runs--n 50when there is none) and compares them withscore(). - It fails (exit code 1) when bad published rises at all, when honest rejected or
model_right_pctgets worse by more than your limits, or when tokens per review rise by more than your limit. It prints a table and the reason. - Decide: the limits. Write down why bad published gets zero tolerance and the others don't.
- Add
mod-v4toai/mod_prompts.pywith one change only, run lab 05 on it, then run the gate. - Add model-free tests for the gate with hand-made
Rowlists.
Done when the tests pass and your notes have the gate's table for mod-v4 and its verdict.
ex3: Seller reply drafts with output guardrails¶
Why: a model writing to buyers in the seller's name can promise refunds, leak a phone number or blame the buyer. Output needs guardrails as much as input.
- Add
ai/seller_reply.pywithdraft(conn, review_id, chat_fn=None): one call throughgenerate("seller_reply", ..., prompt_version="reply-v1")on the published (redacted) review and its aspects, fenced withwrap_untrusted(). - Check the draft in code: no phone, email or link (
redact_pii(),detect_spam()), no ₹ amount or % (no promises), it names the review's main complaint, and it stays under 500 characters. - A failing draft gets one retry with the problems listed, then it is held for a person. A draft is never posted automatically.
- Wrap it in a span (
reply.draft), so a draft shows up as a trace on the Ops page. - Decide: how to catch a reply that blames the buyer: a word list, or a second, narrow model question? Write down the cost of each.
- Add model-free tests with a fake
chat_fn.
Done when the tests pass and your notes have, for 10 negative reviews, how many drafts passed, how many were held and why, and the ₹ per 1,000 drafts from the cost meter.
Names these exercises build on¶
Please keep these names as they are: ai.llm.generate, ai.moderation.moderate, ACTIVE_PROMPT, PROMPTS in ai/mod_prompts.py, evals.moderation_eval.run, score, latest, save, Row, ai.guardrails.redact_pii, detect_spam, wrap_untrusted, obs.tracing.span. Checks and later topics rely on them.
Build it yourself: a "why did this drop?" insights agent¶
No starter code and no steps this time: a short brief, like one you'd get at work. Plan for a day: this one uses MCP and agents on top of RnR.
Your code goes in my-work/build/; reuse your scorecard from ex1, ../mcp-server/host.py and run() from ../agents/agent/. Notes go in a "Build" section of my-work/rnr.md.
The brief. A category manager asks "why did S03's rating drop in October?" or "why are buyers unhappy with TMT-550D-16?". Build an agent that finds the cause in the data and shows its evidence, instead of guessing.
It must:
- Read the data through a read-only Python MCP server over the RnR database, with a few well-named tools (for example monthly seller stats, returns by batch, reviews by aspect and month).
- Answer with numbers and review IDs, and check in code that every number and ID came from a tool result.
- Say "no clear cause" when the data shows none, rather than inventing one.
- Be scored by a trajectory eval on at least 5 questions: the three planted stories and two with no story. For each: the right tools, the cause found or not, calls and seconds.
- Trace every question as one trace (one span per model call and tool call), so a run can be read on the Ops page.
Decide: what counts as "found the cause"? What budget does one question get?
Done when:
pytest -q my-work/buildpasses with model-free tests: the server's tools on the test database, the answer check and the eval scorer.- A demo answers the 5 questions on qwen3:8b.
- Your notes have causes found (out of 3), false causes on the two questions with no story, and ₹ per question from the rate card.
Or bring your own problem. Build the same pattern on a problem from your own work instead. Write a five-line brief first (who uses it, what goes in, what comes out, what must never go wrong, how you'll measure it), then meet the same "Done when" list. Use synthetic data only, never real customer data.