Ollama and Gemma 3: build local RAG with TF-IDF

Build a Python RAG pipeline with Ollama, Gemma 3 4B and TF-IDF retrieval, then test answers against a fictional financial report.

Player not loading? Watch on YouTube

This tutorial builds a local LLM retrieval pipeline around a fictional company's financial report. The presenter argues that RAG remains useful despite large context windows: retrieving relevant passages can reduce the amount of text sent to a model and give users sources to check. He also reports fewer hallucinations when prompts include concrete context, without treating that as a guarantee.

The Python example uses Ollama to run Gemma 3's 4 billion parameter model with a context window configured to about 16K tokens. It uses basic LangChain components and a TF-IDF vectorizer rather than a dedicated vector database. The code splits the document into five chunks, converts them into vectors, scores their similarity to a question and inserts the selected text into a prompt.

The demo compares answers with and without retrieved context. Given the relevant passage, the model returns the fictional Q4 2025 revenue of 847 million and 23% year-over-year growth. The prompt tells it to answer "I don't know" when the context lacks an answer; the presenter cautions that a small model may not always follow that instruction.

The final discussion covers practical limits. Poor document ingestion and unsuitable chunks can produce incorrect retrieval and answers. The example teaches the mechanics of local AI retrieval, while the presenter says production systems require more careful ingestion and chunking.