Objectives: LO-5.1, LO-5.2, LO-5.3, LO-6.2, LO-7.1, LO-7.2, LO-7.3, LO-7.4, and LO-3.3
again
When: after Module 7, as the
suggested pace places it
Points: 20, for scoring yourself; the course has no grades
Time: about 3 hours
Build and audit a small dispatch evaluation. Use the bundled cases for the numeric checks and write a separate six-case test set of your own. Include typical tickets, edge cases, and misleading wording. Reserve at least two cases for a final check and write one sentence explaining the expected queue for each. Label every illustrative response you write.
Replace the indicated lines in each answer cell and run the check below it. Restart the kernel and run all cells before you record your result. Keep the tests unchanged. The course’s policies apply.
Setup¶
from collections import Counter
from math import isclose, sqrt
from random import Random
import re
from statistics import mean
from xml.etree import ElementTree
from promptlab import load_json
cases = load_json("evaluation/cases.json")
judgments = load_json("evaluation/judgments.json")Provided code¶
Problem 4 uses this paired bootstrap, the one you wrote in Lab 7’s Exercise L7.5. Run the cell and use the function as it is.
def paired_bootstrap(a, b, *, repeats=2000, seed=101):
"""Estimate B minus A, with a 95% percentile interval, from paired case scores."""
if len(a) != len(b) or not a or repeats < 2:
raise ValueError("Provide equal nonempty case scores and at least two resamples.")
deltas = [right - left for left, right in zip(a, b, strict=True)]
rng = Random(seed)
samples = sorted(mean(rng.choices(deltas, k=len(deltas))) for _ in range(repeats))
return mean(deltas), samples[int(0.025 * (repeats - 1))], samples[int(0.975 * (repeats - 1))]Problem 1: Select a contrast set (LO-5.1, LO-5.2, LO-5.3, 4 points)¶
Select exactly one example from each queue in the order repair, billing, account. Use Jaccard similarity on lowercased alphabetic word sets. Break ties by ID. Raise ValueError if a queue has no examples. Write the selected rows as input/output example blocks in your prompt draft.
def select(query, pool):
# YOUR CODE HERE
raise NotImplementedError()pool = load_json("tickets.json")
query, others = pool[13], pool[:13] + pool[14:]
examples = select(query["text"], others)
assert [r["label"] for r in examples] == ["repair", "billing", "account"], \
"Return one row for each queue, in the order repair, billing, account."
assert examples[1]["id"] == "T10", \
f"The billing row most like {query['id']} is T10; your function chose {examples[1]['id']}."
print("passed")Problem 2: Parse and grade a classifier (LO-6.2, LO-3.3, LO-7.2, 3 points)¶
Parse each response using the exact label contract. Return the number of correct runs for a row and version. Compare text to the reference exactly. In a separate prompt draft, ask for tagged evidence and exactly one tagged answer and explain what would be validated.
def correct_runs(row, version):
# YOUR CODE HERE
raise NotImplementedError()assert correct_runs({"expected": "repair", "A": ["Label: repair", "repair"]}, "A") == 1, \
"Only the exact text \"Label: repair\" counts as a correct run."
print("passed")Problem 3: Require one final section (LO-6.2, 3 points)¶
Extract exactly one nonempty answer tag, trimming its contents. Raise ValueError for malformed or repeated sections. Use re.DOTALL.
def extract(text):
# YOUR CODE HERE
raise NotImplementedError()assert extract("<why>invoice</why><answer>billing</answer>") == "billing", \
"Return the contents of the answer tag."
print("passed")Problem 4: Keep cases paired (LO-7.1, LO-7.3, LO-7.4, 5 points)¶
Use only rows in split. For each version compute normalized case scores in the input order. Return paired_bootstrap on those scores using the helper defaults. Explain how you protect split integrity.
def compare(rows, split):
# YOUR CODE HERE
raise NotImplementedError()estimate, lo, hi = compare(cases, "dev")
assert lo <= estimate <= hi, "Return the estimate and then the interval that contains it."
assert estimate > 0, "On the development cases, B scores higher than A."
print("passed")Problem 5: Agreement and intervals (LO-7.2, LO-7.3, 5 points)¶
Implement agreement from Lab 7 and Wilson’s interval using z=1.96. Return a four-tuple with observed agreement, kappa (None when undefined), and the Wilson interval endpoints around passes of total. Raise ValueError for an empty list of judgments, a nonpositive total, or passes outside the range from 0 to total.
def stats(rows, passes, total):
# YOUR CODE HERE
raise NotImplementedError()obs, k, lo, hi = stats(judgments, 8, 10)
assert isclose(obs, 0.8), f"The human and the judge agree on 24 of 30 rows; got {obs}."
assert 0 < k < obs, "Kappa is below observed agreement when chance agreement is positive."
assert 0.48 < lo < 0.50 and 0.94 < hi < 0.95, f"Wilson's interval for 8 of 10; got {lo}, {hi}."
print("passed")Tools you used¶
Say which tools you used and how, or write “none.”