Small-to-Big: Parent Document Retrieval

easy
Unit 3 · RAG I0/3 to milestone

Small chunks embed precisely, but they're too short to answer from. Parent document retrieval searches over small chunks, then returns the larger parent sections they came from.

Each chunk is (chunk_id, parent_id, vector). Implement retrieve_parents(query, chunks, k_chunks, k_parents, agg="max"):

  • Rank chunks by cosine similarity to the query (ties: original order) and keep the top k_chunks.
  • Group them by parent and score each parent with agg: "max" (best chunk score) or "sum" (total of its retrieved chunks' scores, which rewards parents matched by several chunks).
  • Return up to k_parents parent ids, best first, breaking ties by which parent appeared first in the chunk ranking.

Also implement dedupe_parents(chunk_ranking, parent_of), the simpler version that keeps the parents of a ranked list of chunk ids in first-appearance order, without duplicates.

#rag#retrieval#chunking2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Small-to-Big: Parent Document Retrieval · TechGradify