Skip to content
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 1

What 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

  1. 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
  2. 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
  3. Lesson 3: Retrieve the Most Relevant ChunksRank chunks by cosine similarity to a question and select the top few to provide context.7 min
  4. Lesson 4: Build a Prompt with Retrieved ContextCombine the question and the retrieved chunks into a clear instruction for a language model.5 min
  5. 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
  6. 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
8,884 open ai engineer roles on Rolebound right nowSee the roles

Ready to use this in a job?

Rolebound applies to matching roles for you.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.