Week 12 · Capstone · Answer questions from documents with citations
Compare a naive answerer with a retriever that can abstain.
Compare a naive answerer with a retriever that can abstain.
Work through the problem
Each document states a versioned fact. The task asks for a field and an optional plan. A confident answer without a plan is risky when multiple plans qualify. A simple keyword baseline always quotes its top hit. A second system matches field and plan terms, returns an evidence ID, and abstains on ties or absent facts.
Evaluate exact answer values and cite the evidence ID. Report coverage separately so a system cannot earn a high answer score by answering almost nothing. The toy benchmark includes an explicit plan, an ambiguous request, and a question missing from all documents.
Run the Python lab
docs = [
("A","basic","retention","30 days"),
("B","plus","retention","90 days"),
("C","plus","export","CSV"),
]
test = [
("basic retention","30 days"),
("plus retention","90 days"),
("retention",None),
("plus export","CSV"),
("basic export",None),
("refund time",None),
]
def answer(query, strict):
words = set(query.split())
ranked = sorted(docs,key=lambda d:-len(words&set(d[1:3])))
if strict:
match = [d for d in ranked if d[1] in words and d[2] in words]
if len(match)!=1:
return None,None
chosen = match[0]
else:
chosen = ranked[0]
return chosen[3],chosen[0]
for strict in [False,True]:
results = [answer(q,strict) for q,_ in test]
accuracy = sum(got==want for (got,_),(_,want) in zip(results,test))/len(test)
print(strict,"accuracy",round(accuracy,2),"answered",sum(x is not None for x,_ in results),results)
assert answer("plus retention",True)==("90 days","B")
Output from the lab
False accuracy 0.5 answered 6 [('30 days', 'A'), ('90 days', 'B'), ('30 days', 'A'), ('CSV', 'C'), ('30 days', 'A'), ('30 days', 'A')]
True accuracy 1.0 answered 3 [('30 days', 'A'), ('90 days', 'B'), (None, None), ('CSV', 'C'), (None, None), (None, None)]
Practice
- How would you test paraphrases such as “how long do you keep basic records”?
Check your reasoning
- Label equivalent requests and supporting documents before evaluating. Then compare a semantic or hybrid retriever with the keyword baseline.
Further reading · Jurafsky and Martin · Speech and Language Processing