Skip to main content
This guide walks you through building a minimal Retrieval-Augmented Generation (RAG) pipeline end-to-end using Ollama for embedding and generation and ChromaDB for vector storage and retrieval. The goal is a compact, runnable demo that indexes a single in-memory document, retrieves it by semantic similarity, and produces a grounded answer from a local LLM. Goals
  • Use Ollama for embeddings and LLM generation.
  • Use ChromaDB (Chroma) for persistence and similarity search.
  • Run a minimal RAG round trip with one in-memory document.
Key components Prerequisites / context
  • A data/ folder with Project Gutenberg text files (optional for this tiny demo). Example sources: Project Gutenberg.
  • Ollama running locally. Start it with ollama serve if not running.
  • Two Ollama models available locally: an embedding model and an LLM model (examples below).
Install dependencies Create a Python virtual environment and install the required packages:
Pull the Ollama models you plan to use locally (examples shown):
Note: The model names are examples; use the models available in your Ollama environment.
Different versions of the Ollama Python client or model endpoints may expect either prompt= or input= when calling ollama.embeddings(...). The demo code below tries both to remain compatible across versions.
Warning about re-running the demo
When you re-run the demo, Chroma’s add() may raise an exception if the same id already exists in the collection. The demo handles this by catching the error and continuing — this is safe for quick iterations.
Create the application: app_v1.py Below is a compact and corrected version of the demo application. It includes:
  • Helper functions for embeddings and generation that handle Ollama client variations.
  • A Chroma collection getter using a persistent path and cosine similarity.
  • Two subcommands: init (quick environment checks) and demo (index one tiny document, retrieve, and answer a question).
Run the demo
  1. Run the init check:
Expected output (example):
  1. Run the tiny RAG demo:
Expected output (example):
What this demonstrates
  • Ollama (local) can produce embeddings and generate text for grounding answers.
  • ChromaDB persists embeddings and returns semantically similar documents.
  • Minimal RAG flow: query → embed → retrieve → LLM (grounded on retrieved context) → answer.
Next steps
  • Chunk and ingest larger documents from the data/ folder with overlap-aware chunking.
  • Improve prompt engineering and retrieval strategies (e.g., hybrid search, reranking).
  • Evaluate retrieval accuracy and build hallucination mitigation strategies.
  • Consider model selection and latency trade-offs for production deployments.
Links and references

Watch Video