RAG in Python: Ask Your Own Documents
BeginnerAnswers with sources on Postgres and pgvector, and an honest "I don't know".
You build
A command-line assistant that answers questions about a small company handbook from passages it finds in PostgreSQL with pgvector, cites the passages it used, and says it does not know when the handbook holds no answer.
You need
- Python basics: functions, lists, dictionaries, reading files and using installed packages (the course Python for AI from Zero is one way to get them)
- Running commands in a terminal
To build it on your own machine
This is optional and comes with a plan. The course pack brings the starting files, a ready container and a check for every lesson. You need only Docker and git, plus your own AI coding agent if you want a tutor.
What you learn
Give the model the few passages that matter, and make it show where each answer came from
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.
- A Small, Precise Context. A model knows nothing about your documents until they are in its prompt. Retrieval is how you choose what goes there: a few passages that bear on the question instead of everything you have, which keeps answers accurate, quick and cheap.
- Search by Meaning. Documents are cut into chunks that each hold one topic, and an embedding model turns every chunk and every question into a vector, so a question finds the passage that answers it even when the two share no words.
- Vectors in Postgres. The chunks, their sources and their vectors live in one ordinary PostgreSQL table with the pgvector extension, and one SQL query returns the nearest passages. You learn which distance to use and when a table needs an index.
- Answers You Can Check. The model answers from the retrieved passages and cites them, your code checks every citation, and the assistant declines when the documents hold no answer instead of inventing one.
Lessons
Why the Model Does Not Know Your Documents
What a model knows, A model call, Context in the prompt, Retrieval-augmented generation
Split Documents into Chunks
Why chunk, Splitting along structure, Chunk size, Overlap
Embeddings: Search by Meaning
Where word matching fails, Embeddings, Embedding models, Cosine similarity
Store Chunks and Vectors in Postgres
Why a database, Tables and rows, The vector type, A database driver
Find the Nearest Chunks with SQL
Nearest-neighbour queries, Distance operators, Distance and similarity, Exact search
Answer from the Retrieved Passages
Retrieve, then answer, The augmented prompt, How many passages, Inspecting the context
Cite the Passages Behind an Answer
Why cite, Numbered passages, Asking for citations, Checking citations
Say "I Don't Know"
Why models fill gaps, A way to decline, Search always returns rows, A distance threshold
Where it fits
Course 2 of 3 on the AI Engineer path.