Capstone: A Production Prompt Pipeline (Ticket Triage)
This is the finale: you wire the whole module into one production prompt pipeline for support-ticket triage. A raw ticket comes in; you must defend against injection, respect a token budget, assemble a few-shot prompt, and then validate whatever JSON the model returns. The model classifies the ticket (that one step is the LLM, and it is the only ungraded part). Everything around it is the engineering you own.
Build the prompt assembler. build_triage_prompt(ticket, budget=200):
- Runs the ticket through injection detection. If suspicious, the ticket body becomes
[blocked input]and the raw payload never enters the prompt. - Otherwise truncates the ticket to
budgetcharacters (tickets can be huge; you pay per token). - Assembles a
[system]instruction, then two few-shotTicket: ... / JSON: ...examples, then the fenced real ticket, ending on a trailing[assistant]line.
Build the output validator. parse_triage(model_json) json.loads the model's reply inside a try/except and checks two enums: category in {billing, technical, account, general} and urgency in {low, medium, high}. Return {"ok": bool, "errors": [...]}.
parse_triage('{"category": "billing", "urgency": "high"}') # ok True
parse_triage('{"category": "weather", "urgency": "high"}') # ok False
parse_triage('not json at all') # ok False, no crashIf your tutor is on, an on-device model can produce the JSON that parse_triage then validates, closing the loop live. But grading only touches the deterministic parts: the prompt has the right structure, blocks injections, truncates oversized tickets, and the validator accepts good output while rejecting bad. Reuse is_suspicious and wrap_user_input from the injection lesson, included in the starter. Press Run to grade.
Write build_triage_prompt(ticket, budget=200): detect injection (suspicious -> ticket body becomes [blocked input], payload absent), else truncate to budget chars; assemble a [system] line, two few-shot Ticket:/JSON: examples, the fenced real ticket, and a trailing [assistant]. Write parse_triage(model_json) that json.loads in a try/except and validates category in {billing,technical,account,general} and urgency in {low,medium,high}, returning {"ok", "errors"}.
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.