Skip to main content

Retrieval-Augmented Generation from First Principles

Generative AI
UConn
An interactive notebook for UConn MS Quantitative Economics students: chunking, embeddings, similarity search, and prompt construction, built from scratch.
Published

October 19, 2025

I put this notebook together for graduate students in UConn’s MS in Quantitative Economics program. It builds a retrieval-augmented generation (RAG) system from scratch so students can see each piece instead of calling a framework.

What it covers

  • Chunking: splitting long documents so the right passage can be found
  • Embeddings: turning text into vectors that carry meaning
  • Similarity search: retrieving the closest chunks with cosine similarity, by top-k, by threshold, or both
  • Visualization: looking at the embedding space with UMAP to see what retrieval is doing
  • Prompt construction: assembling retrieved context into a prompt a language model can answer from

It assumes basic Python and some familiarity with pandas and NumPy. Machine learning background helps but isn’t required.