#02 RAG and vector search 6 min
RAG From Scratch in Python: Chunk, Embed, Retrieve (Local)
Build a working RAG system in plain Python with Ollama, and see exactly what it retrieves. No framework, no vector database, no API key.
Add a note and re-index
Copy the code folder first. At the end of notes.txt leave one blank line, then add a new note that starts with the word Parking and a colon, followed by one sentence about bike parking. Predict the first line that python rag.py prints, then run it. Next add an ask() call with a question about parking and k=1, and predict which chunk name it prints.
A hint
Every blank line is a chunk boundary. The name printed is the text before the first colon in the chunk.
Merge two notes into one chunk
In notes.txt delete the blank line between the Returns note and the Shipping note. Predict how many chunks are indexed now. Then change the first question to ask how many days shipping takes, run with k=1, and predict which name is printed and whether the answer can still be right.
A hint
load_chunks splits only on blank lines. ask prints just the text before the first colon of each chunk, so a merged chunk shows one name.
Raise k and read the retrieved list
Change the first ask call so that k is 3. Predict how many names appear after 'retrieved:' and whether the first name will be the same as with k=1. Run it. Then say, using the video's advice, what you would check first if the answer looked wrong.
A hint
retrieve sorts the chunks by score and keeps the first k, so a larger k only adds chunks after the ones you already had.
Move the sentence in the truncation test
In truncation.py change the number 120 to 5 and predict whether the long chunk still scores near zero, then run it. Put 120 back. Now build long the other way round, with short first and the filler after it. Predict which end of a long text the model keeps and what the long chunk scores now. Run it to test your idea.
A hint
The video says Ollama cuts off text that is too long without a warning. Work out which part of the text is cut from what the scores tell you.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
Seven made-up notes, a tiny embedding model (all-minilm) and a few questions on one laptop: not a benchmark. The truncation trap is shown with one example in Ollama. The advice pages were read as search results; the output on screen is the evidence.
Up next in RAG and vector search
More in RAG and vector search
Related videos
- #05 Structured Outputs in Python: Valid JSON Is Not Correct JSON LLM engineering and evals
- #293 RAG for Beginners: The Idea, Then 37 Lines of Python RAG and vector search
- #40 Chunking Strategies Compared on One Document RAG and vector search
- #47 6 RAG Failure Modes Reproduced: Which Ones Actually Break RAG and vector search
