Chunking Documents for RAG

easy
Unit 3 · RAG I0/3 to milestone

Long documents must be split into chunks before embedding. Overlap between chunks preserves context that would otherwise be cut at a boundary.

Implement chunk_words(text, chunk_size, overlap) that:

  • Splits text on whitespace into words.
  • Returns a list of strings, each containing at most chunk_size words joined by single spaces.
  • Consecutive chunks share exactly overlap words.
  • Stops once the final word has been included (no trailing chunk that is fully contained in the previous one).
  • Raise ValueError if overlap >= chunk_size.

Example

chunk_words("a b c d e f g", chunk_size=3, overlap=1)
# ['a b c', 'c d e', 'e f g']
#rag#preprocessing3 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Chunking Documents for RAG · TechGradify