learning-it by example

#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.

1. When a RAG answer looks wrong, what does the video say to check first?
2. For the service price and Saturday question with k=1, only Opening hours was retrieved. What did the model say about the price of a service?
3. In the chunk size test, what similarity did the sentence score when it was buried at the end of the long chunk?
4. Why does the video say not to read its similarity scores as a general result?
5. Which of these is one of the four mistakes to avoid named in the video?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. 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.

  2. 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.

  3. 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.

  4. 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

All RAG and vector search videos

Related videos