Objectives: LO-3.1, LO-3.2, LO-3.3
Prerequisites: the Module 3 reading and Lab 2
Time: about 2.5 hours
In this lab you rewrite three vague prompts for the bicycle repair shop and score them with the prompt rubric. Then you move the shop’s routing policy into a request’s system prompt, write a checker for the routing format, and run it over 30 responses.
The tickets come from data/tickets.json. A person wrote the 30 responses in
data/format-responses.json for this course, so they are illustrative, not model output. No
cell sends anything over the network or calls a model.
Use a fresh kernel and run the cells from top to bottom. Fill each answer cell, then run its
check, which prints passed or fails with a message that says what is wrong. The checks fail
on purpose until their exercises are complete.
Setup¶
import sys
import re
from promptlab import load_json
tickets = load_json("tickets.json")
responses = load_json("format-responses.json")
labels = ("repair", "billing", "account")
print("Python", sys.version.split()[0])Score a first draft¶
Use the prompt rubric, which scores five criteria from 0 to 2. “Sort this” earns 1 for task and 0 for the other criteria. Read the three tickets below, and decide which text in each prompt is stable policy and which is the request itself.
print(tickets[0]["text"], tickets[13]["text"], tickets[18]["text"], sep="\n")Exercise L3.1 (LO-3.1, ★★, about 25 minutes)¶
Rewrite three prompts. Replace “Sort this,” “Help them,” and “Extract the info” with complete prompts for routing T01, answering T14, and extracting the requested profile change in T19. Return a list of three strings in that order. Score the drafts and revisions out of 10 on paper, with evidence for each score.
def revisions():
raise NotImplementedError("L3.1")assert len(revisions()) == 3, f"Return three prompts, not {len(revisions())}."
for prompt, index in zip(revisions(), [0, 13, 18], strict=True):
ticket = tickets[index]
assert ticket["text"] in prompt, \
f"Include ticket {ticket['id']}'s text exactly: {ticket['text']!r}."
assert len(prompt.split()) >= 20, \
f"The prompt for {ticket['id']} has {len(prompt.split())} words; supply the " \
"requirements and context."
print("passed")Exercise L3.2 (LO-3.2, ★★, about 25 minutes)¶
Move policy out of the ticket. Write route_request(ticket), which returns a request
dictionary in the Messages API’s shape (Module 2) with the keys model, max_tokens,
system, and messages. Use a single user turn containing the ticket exactly. The system
prompt must name all three queues and specify the output. Leave room in max_tokens for
thinking, as Module 2 explains. Explain the separation in your notes.
def route_request(ticket):
raise NotImplementedError("L3.2")r = route_request(tickets[0]["text"])
missing = {"model", "max_tokens", "system", "messages"} - set(r)
assert not missing, f"The request is missing {sorted(missing)}."
assert isinstance(r["model"], str) and r["model"], \
"Name a model by its ID, such as claude-opus-5-5."
assert all(label in r["system"] for label in labels), \
f"Name every queue in the system prompt: {', '.join(labels)}."
assert r["messages"] == [{"role": "user", "content": tickets[0]["text"]}], \
"Send exactly one user turn whose content is the ticket, unchanged."
assert isinstance(r["max_tokens"], int) and r["max_tokens"] > 0, \
f"max_tokens must be a positive integer, not {r['max_tokens']!r}."
print("passed")Exercise L3.3 (LO-3.3, ★★, about 25 minutes)¶
Parse the output contract. Write parse_label(text). Return the queue from a string that is
exactly Label: repair, Label: billing, or Label: account. Return None for all other
strings. Match the whole string, so that even a trailing newline is rejected.
def parse_label(text):
raise NotImplementedError("L3.3")assert parse_label("Label: billing") == "billing", \
f"'Label: billing' should give 'billing', not {parse_label('Label: billing')!r}."
for malformed in ["Label: other", "Label: repair\n", "label: repair", " Label: account"]:
assert parse_label(malformed) is None, \
f"{malformed!r} doesn't match the contract exactly, so return None."
print("passed")Exercise L3.4 (LO-3.3, ★★, about 25 minutes)¶
Audit 30 responses. Write audit(rows), which returns a dictionary with the counts
"valid" and "invalid". Call parse_label once per row. Explain in your notes why the
resulting percentage describes the sample’s formatting alone.
def audit(rows):
raise NotImplementedError("L3.4")counts = audit(responses)
assert counts == {"valid": 15, "invalid": 15}, \
f"Got {counts}; parse each row's text once, and count None as invalid."
assert audit([]) == {"valid": 0, "invalid": 0}, \
"An empty list has zero of each, and both keys are still present."
print(responses[0]["source"])
print("passed")Summary¶
You wrote task-specific instructions, split stable policy from per-request data, and checked a hand-written set of responses for format. A format count says nothing about whether each label was right; that needs reference labels, which Module 7 grades.