RAG in Python: Ask Your Own Documents
You build one small program from the first lesson to the last: a command-line assistant that answers questions about a company handbook. It cuts the documents into passages, finds the few that bear on a question in PostgreSQL with pgvector, answers from them, names the passages it used, and says so when the handbook holds no answer. Embeddings run free on your laptop. You already write basic Python; you need no experience with language models, embeddings or databases.
What this course covers
- Why the Model Does Not Know Your Documents: Explain why a language model cannot answer from documents it never saw, show that it answers correctly once the right page is in its prompt, and say what retrieval adds.
- Split Documents into Chunks: Split a set of Markdown documents into chunks that each hold one topic and know where they came from, and explain how chunk size and overlap change what a search can find.
- Embeddings: Search by Meaning: Turn chunks and a question into embeddings with a local model, rank the chunks by cosine similarity, and explain why this finds a passage that shares no words with the question.
- Store Chunks and Vectors in Postgres: Store chunks and their embeddings in a PostgreSQL table with pgvector from Python, and load them so that running the load again changes nothing.
- Find the Nearest Chunks with SQL: Retrieve the chunks nearest to a question with one SQL query, choose the distance operator that fits the embeddings, and say when a table needs a vector index.
- Answer from the Retrieved Passages: Join retrieval and generation into one command that answers a question from the passages found for it, and choose how many passages to send.
- Cite the Passages Behind an Answer: Make every answer name the passages it rests on, check those citations in code, and print the sources a reader can open.
- Say "I Don't Know": Make the assistant decline when its documents hold no answer, before the model is called and after, and choose the distance threshold from questions with and without an answer.