AI 320 — Applied AI, Retrieval and Reliable Automation · Weeks 6–8
Retrieval, chunking and evidence provenance
Full teaching sequence and cumulative subject work
Study the complete on-site source sections below, then work the original example, changed-condition problem and transfer laboratory. Study CS50 AI information retrieval material within Language; compare retrieval with the search and probability foundations. Use a small versioned fictional document collection.
Retrieval relevance, authorization and contradictory passages
Retrieval ranks candidate evidence; it does not decide what the evidence means. Chunk size can separate a rule from its exception, while keyword overlap can rank a negated or outdated statement highly. Evaluate retrieval on whether required evidence is present, and evaluate answering on whether the conclusion follows from that evidence.
Authorization should restrict the candidate corpus before inaccessible passages can influence the answer. Filtering only the displayed citation can still leak private information through generated text. Record stable source IDs, versions and passage locations for reconstruction.
Worked problem
A policy chunk says returns are accepted within 30 days. The following chunk excludes custom orders. A custom-order question retrieves only the first chunk. Explain the failure and repair.
Trace the complete reasoning
The retrieved general rule omits an applicable exception. The answer cannot safely infer universal eligibility.
Preserve section context or retrieve neighboring exception-bearing passages, then evaluate the complete rule for the actual order type.
Create an evaluation case requiring both chunks and score evidence sufficiency separately from fluent answer quality.
Restrict retrieval to the requester’s authorized documents first. A correct answer based on private unauthorized text is still a boundary failure.
Independent transfer problem
Two authorized passages of the same version conflict about the deadline. What should an evidence-bounded assistant do?
Compare the transfer solution
Identify and cite the conflict, avoid selecting a convenient deadline without a justified precedence rule, and route to clarification or review. Capture both locators in the result. A retrieval score is not an authority hierarchy, so the top-ranked passage does not automatically resolve contradictory policy.
Learning objectives
- Explain and trace the mechanisms in retrieval, chunking and evidence provenance.
- Apply the method to the worked example and changed-condition exercise.
- Identify assumptions, failure cases and the evidence needed to justify a result.
Study the mechanism
Retrieval selects material relevant to a question. Keyword methods match terms; embedding methods compare learned representations. Semantic similarity is not the same as factual support. A passage can be topically similar while answering a different question, applying to another jurisdiction or being superseded by a later version.
Chunking divides documents into retrieval units. Very small chunks can lose context; very large chunks can dilute relevant details and exceed useful context budgets. Preserve document ID, title, date, version, location and access restrictions with each chunk. Retrieval must respect authorization before content enters the model context; hiding a confidential citation after generation does not undo disclosure.
Measure retrieval separately from answer generation. If the relevant passage is absent, improving the prompt cannot recover missing evidence reliably. Create questions with known supporting passages and assess whether the retriever returns them. Then check whether the answer uses the evidence correctly. A similarity threshold is a heuristic, not a calibrated probability that the answer is true. Version filters, exact identifiers and source-aware reranking may matter more than using a larger model.
Worked example
import re
def tokens(text):
return set(re.findall(r"[a-z0-9]+", text.lower()))
def retrieve(question, docs):
q = tokens(question)
ranked = [(len(q & tokens(d["text"])), d) for d in docs]
return [d for score, d in sorted(ranked, key=lambda pair: pair[0], reverse=True) if score > 0]
assert retrieve("refund policy", [{"id":"A", "text":"Refund policy requires receipt"}])[0]["id"] == "A"This transparent lexical baseline counts distinct shared tokens. It has no synonym handling, weighting, phrase model or authorization filter. It can retrieve a passage containing the words refund and policy even when it describes an exception rather than the general rule. Its simplicity makes failure diagnosis easier before a more complex retriever is introduced.
Check your understanding
Why must access filtering occur before model context construction?
- Because exposing a restricted passage to generation already crosses the boundary
- Because hiding the link afterward always removes the disclosure
- Because semantic ranking itself grants permission
Answer and reasoning for every option
Correct: Because exposing a restricted passage to generation already crosses the boundary Authorization governs the data the system may use, not just displayed links.
Reconsider: Because hiding the link afterward always removes the disclosure Generated text can reveal content even without its citation.
Reconsider: Because semantic ranking itself grants permission Similarity and access rights are unrelated properties.
Transfer laboratory: Retrieval recall versus answer accuracy
Ten questions each have one known relevant passage. The retriever returns it for eight. Generation is correct on six of those eight. Compute retrieval recall and end-to-end supported accuracy.
Work through the full solution
Retrieval recall is 8/10=80%. Conditional generation success is 6/8=75%. End-to-end supported accuracy is 6/10=60%, assuming the remaining two retrieval failures cannot be correctly supported. Keep these denominators visible. Improving generation alone cannot repair every retrieval miss, and an unsupported lucky answer should not count as grounded success.
Evidence of understanding
Report 80%, 75% and 60% with their distinct denominators.
Complete on-site teaching readings
These are full attributed source sections, not previews. Read them alongside the original Academy explanation and code.
- 8.2. Feature extraction · The scikit-learn developers
- 1.6. Nearest Neighbors · The scikit-learn developers
- 8.8. Pairwise metrics, Affinities and Kernels · The scikit-learn developers
- re — Regular expression operations · Python Software Foundation and Python documentation contributors
- collections — Container datatypes · Python Software Foundation and Python documentation contributors
- LLM08:2025 Vector and Embedding Weaknesses · OWASP GenAI Security Project contributors
- LLM02:2025 Sensitive Information Disclosure · OWASP GenAI Security Project contributors
Open the complete practice sequence and cumulative assessment →