Free courses
AI EngineeringBeginnerFree
Build a Retrieval-Augmented Generation System in Python
Build a retrieval-augmented generation system from scratch in Python. By the end, you'll have a tool that reads a document, retrieves relevant passages, and generates an answer.
6 lessons 41 minNo account needed
Start lesson 1What you will learn
- Split a text document into overlapping chunks.
- Convert text chunks into simple numerical vectors using character n-grams.
- Retrieve the most relevant chunks for a question using cosine similarity.
- Construct a prompt that includes retrieved context and a question.
- Build a small command-line tool that answers questions from a text file using retrieval.
Before you start
- Able to write and run a simple Python script.
- Understand lists, dictionaries, loops and functions.
- Basic command-line usage (open terminal, run script with arguments).
- No prior knowledge of machine learning or language models needed.
Lessons
- Lesson 1: Split a Document into Overlapping ChunksRead a plain-text file and split it into small overlapping passages so no meaning is lost at the edges.7 min
- Lesson 2: Represent Chunks as Vectors with Character N-gramsConvert each chunk into a fixed-length numerical vector using character n-gram frequencies, so chunks can be compared mathematically.6 min
- Lesson 3: Retrieve the Most Relevant ChunksRank chunks by cosine similarity to a question and select the top few to provide context.7 min
- Lesson 4: Build a Prompt with Retrieved ContextCombine the question and the retrieved chunks into a clear instruction for a language model.5 min
- Lesson 5: Generate an Answer with a Placeholder Language ModelUse a simple function that stands in for a language model to produce an answer from the prompt, so you can test the full pipeline.6 min
- Lesson 6: Assemble the Command-Line ToolWrap the previous steps into one script that takes a document file and a question as arguments and prints an answer.10 min