Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 17.7 — Capstone: Build the Core Library
Python · Example 1 of 2

Loader, chunker with overlap, toy embedder and NumPy retriever

Change size and overlap in chunk_text and watch the chunk count and the scores change. Try an overlap equal to the size and read the error.

Build the heart of docqa: a chunker with overlap, a toy embedder, NumPy top-k retrieval, a prompt builder with numbered sources, a ModelClient interface with a scripted fake and a real client, all typed and easy to test.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write cosine(a, b) using NumPy for two 1-D arrays, returning 0.0 if either has zero length (norm).

02

Write top_k_indexes(scores, k) that returns the indexes of the k highest scores, highest first, with ties broken by lower index.

03

Write build_prompt(question, chunks) that returns a string with one numbered line per chunk, then the question on the last line.

Explain it without notes

01

Why do chunks overlap, and what are the edge cases of an overlapping chunker?

02

Why can a dot product serve as cosine similarity here?

03

Why use a Protocol for the model client and a scripted fake in tests?

04

What are the risks of putting retrieved text into a prompt, and how do you reduce them?

05

How do you measure whether retrieval is good, separately from whether the model's answer is good?

Loader, chunker with overlap, toy embedder and NumPy retriever
Sign in to run this example in your browser.

Expected output

chunks: 10 | matrix shape: (10, 256)
Q: can I get a refund
   0.392  refunds.txt#1  for a full refund. Items must be unused ...
   0.236  refunds.txt#0  You can return any item within 30 days o...
Q: reset my password
   0.612  account.txt#0  Reset your password from the sign in pag...
   0.408  account.txt#1  forgot password link. We email a reset l...
Q: how long do refunds take
   0.320  refunds.txt#1  for a full refund. Items must be unused ...
   0.289  shipping.txt#0  Standard delivery takes 3 to 5 business ...