StackPractices
intermediate By Mathias Paulenko

Local RAG Pipeline with ChromaDB and Sentence Transformers

Implement retrieval-augmented generation locally with ChromaDB, sentence-transformers embeddings, and LLM generation without external API dependencies

Topics: ai

RAG (Retrieval-Augmented Generation) combines document retrieval with LLM generation. ChromaDB is a vector database that runs in-process on your machine — nothing to deploy. Paired with sentence-transformers for local embeddings, you get a complete RAG pipeline without API keys or cloud dependencies. Below: chunking, embedding, retrieval, and generation.

When to Use This

  • Private document Q&A where data can’t leave your machine
  • Prototyping RAG pipelines before scaling to managed vector databases
  • Offline or air-gapped environments
  • Local development and CI tests that need a real vector store without network calls
  • Comparing embedding models locally before paying for an embedding API
  • Skip this approach when you need many concurrent writers, multi-instance sharing, or a corpus in the millions of chunks — that’s what managed stores like Pinecone are for

Prerequisites

  • Python 3.10+
  • chromadb package (pip install chromadb)
  • sentence-transformers package (pip install sentence-transformers)
  • An LLM (OpenAI API, Ollama, or any LLM provider). Add openai or requests to the install depending on which you use.

Solution

1. Install Dependencies

pip install chromadb sentence-transformers openai

2. Document Chunking

from dataclasses import dataclass

@dataclass
class Chunk:
    id: str
    text: str
    metadata: dict

def chunk_text(
    text: str,
    chunk_size: int = 500,
    overlap: int = 50,
) -> list[Chunk]:
    """Split text into overlapping chunks.

    Args:
        text: Input text to chunk.
        chunk_size: Target characters per chunk.
        overlap: Number of characters to overlap between chunks.

    Returns:
        List of Chunk objects.
    """
    chunks = []
    start = 0
    chunk_idx = 0

    while start < len(text):
        end = start + chunk_size
        chunk_text_str = text[start:end]

        chunks.append(Chunk(
            id=f"chunk_{chunk_idx}",
            text=chunk_text_str,
            metadata={"chunk_index": chunk_idx, "char_start": start},
        ))

        start = end - overlap
        chunk_idx += 1

    return chunks

def chunk_documents(documents: list[dict]) -> list[Chunk]:
    """Chunk multiple documents."""
    all_chunks = []
    for doc_idx, doc in enumerate(documents):
        chunks = chunk_text(doc["text"])
        for chunk in chunks:
            # Prefix the ID with the doc index so chunks from different
            # documents never collide in ChromaDB.
            chunk.id = f"doc_{doc_idx}_{chunk.id}"
            chunk.metadata.update({
                "source": doc.get("source", "unknown"),
                "title": doc.get("title", ""),
            })
        all_chunks.extend(chunks)
    return all_chunks

3. Initialize ChromaDB with Local Embeddings

import chromadb

class LocalRAG:
    def __init__(self, persist_dir: str = "./chroma_db"):
        self.client = chromadb.PersistentClient(path=persist_dir)
        self.collection = self.client.get_or_create_collection(
            name="documents",
            metadata={"hnsw:space": "cosine"},
        )

    def add_chunks(self, chunks: list[Chunk]) -> None:
        """Add chunks to the ChromaDB collection."""
        self.collection.upsert(
            ids=[c.id for c in chunks],
            documents=[c.text for c in chunks],
            metadatas=[c.metadata for c in chunks],
        )

    def query(
        self,
        query_text: str,
        n_results: int = 5,
    ) -> list[dict]:
        """Retrieve relevant chunks for a query.

        Args:
            query_text: User query.
            n_results: Number of chunks to retrieve.

        Returns:
            List of {text, metadata, distance} dicts.
        """
        results = self.collection.query(
            query_texts=[query_text],
            n_results=n_results,
        )

        return [
            {
                "text": doc,
                "metadata": meta,
                "distance": dist,
            }
            for doc, meta, dist in zip(
                results["documents"][0],
                results["metadatas"][0],
                results["distances"][0],
            )
        ]

4. Build the RAG Pipeline

from openai import OpenAI

class RAGPipeline:
    def __init__(self, persist_dir: str = "./chroma_db"):
        self.rag = LocalRAG(persist_dir)
        self.llm = OpenAI()

    def ingest(self, documents: list[dict]) -> None:
        """Ingest documents into the vector store."""
        chunks = chunk_documents(documents)
        self.rag.add_chunks(chunks)
        print(f"Ingested {len(chunks)} chunks from {len(documents)} documents")

    def retrieve(self, query: str, n_results: int = 5) -> list[dict]:
        """Retrieve relevant chunks for a query."""
        return self.rag.query(query, n_results)

    def generate(
        self,
        query: str,
        context_chunks: list[dict],
    ) -> str:
        """Generate an answer using retrieved context.

        Args:
            query: User question.
            context_chunks: Retrieved chunks from retrieve().

        Returns:
            LLM-generated answer.
        """
        context = "\n\n".join(
            f"[Source: {c['metadata'].get('source', 'unknown')}]\n{c['text']}"
            for c in context_chunks
        )

        response = self.llm.chat.completions.create(
            model="gpt-4o-mini",
            messages=[
                {
                    "role": "system",
                    "content": (
                        "Answer the question based on the provided context. "
                        "If the context does not contain the answer, say "
                        "'I don't have enough information to answer this.'"
                    ),
                },
                {
                    "role": "user",
                    "content": f"Context:\n{context}\n\nQuestion: {query}",
                },
            ],
            temperature=0.2,
        )

        return response.choices[0].message.content

    def ask(self, query: str, n_results: int = 5) -> dict:
        """Full RAG pipeline: retrieve + generate.

        Returns:
            Dict with answer, sources, and retrieved chunks.
        """
        chunks = self.retrieve(query, n_results)
        answer = self.generate(query, chunks)

        return {
            "answer": answer,
            "sources": [
                {"source": c["metadata"].get("source"), "text": c["text"][:200]}
                for c in chunks
            ],
            "num_chunks": len(chunks),
        }

5. Use the Pipeline

pipeline = RAGPipeline()

# Ingest documents
pipeline.ingest([
    {
        "title": "Redis Guide",
        "source": "redis-docs.md",
        "text": "Redis is an in-memory store used for caching, sessions, and messaging.",
    },
    {
        "title": "PostgreSQL Guide",
        "source": "postgres-docs.md",
        "text": "PostgreSQL is a relational database with JSON and full-text search.",
    },
])

# Ask questions
result = pipeline.ask("What data structures does Redis support?")
print(result["answer"])
print(f"Sources: {[s['source'] for s in result['sources']]}")

A runnable single-file version of this pipeline (with the Ollama backend and the reranker) lives in the companion repo.

6. Using with Ollama for Fully Local RAG

For a fully local setup, pair this pipeline with Ollama for private inference. The class below swaps the OpenAI call for a plain HTTP call to the Ollama server:

import requests

class LocalLLMRAG(RAGPipeline):
    def __init__(
        self,
        persist_dir: str = "./chroma_db",
        ollama_url: str = "http://localhost:11434",
    ):
        self.rag = LocalRAG(persist_dir)
        self.ollama_url = ollama_url

    def generate(self, query: str, context_chunks: list[dict]) -> str:
        context = "\n\n".join(c["text"] for c in context_chunks)

        response = requests.post(
            f"{self.ollama_url}/api/generate",
            json={
                "model": "llama3",
                "prompt": f"Context:\n{context}\n\nQuestion: {query}\n\nAnswer:",
                "stream": False,
            },
        )

        return response.json()["response"]

# Fully local — no API keys needed
pipeline = LocalLLMRAG()
pipeline.ingest(documents)
result = pipeline.ask("How does Redis handle caching?")

How It Works

flowchart diagram: Documents
  1. Chunking splits documents into ~500-character segments with 50-character overlap. Without overlap, a fact that lands on a chunk boundary gets cut in half and neither chunk retrieves it cleanly. Smaller chunks give more precise retrieval but lose surrounding context; larger chunks carry more context but dilute the embedding and cost more tokens at generation time.
  2. ChromaDB uses sentence-transformers (all-MiniLM-L6-v2 by default) to embed chunks and queries. Embeddings are generated locally in-process — no API calls.
  3. Retrieval embeds the query and finds the nearest chunks by cosine similarity. ChromaDB uses HNSW (Hierarchical Navigable Small World) for fast approximate nearest neighbor search.
  4. Generation concatenates the retrieved chunks as context and sends them to the LLM with the user’s question. The model answers using only what retrieval gave it.
  5. PersistentClient writes the store to disk — restart the process and your embeddings are still there.
  6. Updates need upsert(): collection.add() throws on duplicate IDs. add_chunks uses upsert(), so re-ingesting an edited document overwrites its old chunks instead of erroring out.

all-MiniLM-L6-v2 is a solid default, not the only choice:

ModelDimensionsSpeedWhen to pick it
all-MiniLM-L6-v2384FastestDefault; English text, small corpora
BAAI/bge-base-en-v1.5768MediumBetter recall on English docs, still light
intfloat/multilingual-e5-large1024SlowestNon-English or mixed-language corpora

Pick the model for your language mix first, then worry about reranking — a better base embedding model helps more than any post-processing.

Variants

Filtering by Metadata

def query_with_filter(
    self,
    query_text: str,
    n_results: int = 5,
    where: dict | None = None,
) -> list[dict]:
    results = self.collection.query(
        query_texts=[query_text],
        n_results=n_results,
        where=where,
    )
    return results

# Only search within a specific source
results = pipeline.rag.query_with_filter(
    "caching strategies",
    where={"source": "redis-docs.md"},
)

Custom Embedding Model

from sentence_transformers import SentenceTransformer

class CustomEmbeddingRAG(LocalRAG):
    def __init__(self, model_name: str = "BAAI/bge-large-en-v1.5"):
        self.embedder = SentenceTransformer(model_name)
        self.client = chromadb.PersistentClient(path="./chroma_db")
        self.collection = self.client.get_or_create_collection(
            name="documents",
            embedding_function=self._embed_fn,
        )

    def _embed_fn(self, input: list[str]) -> list[list[float]]:
        return self.embedder.encode(input).tolist()

Note: once a collection is created with an embedding dimension, you can’t swap to a model with different output size. Create a new collection or delete the persisted directory before switching.

Reranking Retrieved Chunks

Reranking is the single biggest quality lever in a small RAG pipeline. Vector search is fast but noisy; a cross-encoder scores each (query, chunk) pair directly and is far more precise. For keyword + vector fusion instead of pure vector search, see the hybrid search pattern.

from sentence_transformers import CrossEncoder

class RerankedRAG(RAGPipeline):
    def __init__(self):
        super().__init__()
        self.reranker = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")

    def retrieve(
        self,
        query: str,
        n_results: int = 20,
        top_k: int = 5,
    ) -> list[dict]:
        # Retrieve more chunks than needed
        chunks = self.rag.query(query, n_results)

        # Rerank with cross-encoder
        pairs = [(query, c["text"]) for c in chunks]
        scores = self.reranker.predict(pairs)

        # Sort by reranker score and return top_k
        ranked = sorted(zip(chunks, scores), key=lambda x: x[1], reverse=True)
        return [c for c, _ in ranked[:top_k]]

Best Practices

  • Use overlap in chunking — 10-20% overlap keeps context at chunk boundaries from being lost
  • Include source metadata — lets you cite sources in the generated answer
  • Use a low temperature for generation — 0.1-0.3 reduces hallucination in factual Q&A
  • Rerank for better accuracy — retrieve 3-5x more chunks and rerank with a cross-encoder for top-k
  • Namespace by document ID — prefix chunk IDs with the document so updates and deletes don’t leave orphan chunks behind

Common Mistakes

  • Chunks too large — the LLM may miss details in long context; keep chunks to 200-500 words
  • No overlap — relevant information split at chunk boundaries is lost
  • Not persisting ChromaDB — EphemeralClient loses data on restart; use PersistentClient
  • Retrieving too few chunks — 3-5 is a good default; too few misses relevant context
  • Using add() for updates — re-ingesting a document with add() fails on duplicate IDs; use upsert() and stable ID prefixes
  • Evaluating by vibe — eyeballing a few answers hides retrieval regressions; measure with a small eval set (see RAG evaluation with RAGAS)

Further Reading

Frequently Asked Questions

What embedding model does ChromaDB use by default?

all-MiniLM-L6-v2 from sentence-transformers (384 dimensions). It embeds quickly and handles English well; weaker on other languages.

Can I use ChromaDB in production?

It runs in-process, so it works for single-node apps and sidecars. Once you scale beyond one instance, move to Chroma's client-server mode or a managed store (Pinecone, Weaviate).

How much memory does ChromaDB use?

Around 1KB per vector with the default model — expect ~100MB of RAM for a 100K-chunk collection.

Should I use a cross-encoder reranker?

Yes if answer quality matters. Pull 20 candidates with vector search, then let the cross-encoder pick the best 5 — precision jumps.

How do I update a document without re-ingesting everything?

Use upsert() with stable chunk IDs — it overwrites in place. If the document shrank, delete(ids=...) the stale chunks first. Prefixing IDs per document keeps this painless.

ChromaDB vs. FAISS vs. pgvector — which one for local RAG?

ChromaDB is the simplest: embedded store, metadata filtering, persistence out of the box. FAISS is faster at raw search but has no metadata or persistence layer of its own. pgvector fits if your data already lives in PostgreSQL.