Last updated

The Retrieval Step

Definition: Retrieval is the "R" in RAG — embedding the user's question and finding the document chunks whose vectors are most similar to it.

Putting the pieces together

You have now seen embeddings, similarity, and chunking. Retrieval combines them: turn the question into a vector, compare it to every chunk's vector, and keep the top matches.

Run a mini retriever

The demo below is a complete (tiny) retriever in pure Python: it embeds three documents and a question with a simple word-count embedding, scores them by cosine similarity, and returns the best match. This is exactly what RAG does — just with real embeddings and a vector database.

💡 Top-K: real systems usually return the top 3 to 5 chunks, not just one, so the model has enough context to answer well.

Try it Yourself
Output

          
Ad · responsive