Skip to main content
In this hands-on guide we’ll build a local ChromaDB-backed semantic search workflow that persists to disk and demonstrates both ingestion and similarity queries using SentenceTransformers embeddings. What you’ll end up with:
  • A persistent local ChromaDB instance.
  • A simple ingestion flow that chunks text files and upserts embeddings.
  • A query function that runs similarity searches with optional metadata filters.
Dataset used in this demo: several public-domain texts such as The Adventures of Huckleberry Finn, Sherlock Holmes, Beowulf, Complete Works of William Shakespeare, and Frankenstein.
This image shows an open Visual Studio Code window with a file explorer on the left displaying several text files. The terminal at the bottom indicates a command line in use.
Note: this is a demo of using a vector database for retrieval. It is not a complete search engine (there are additional components like LLM-based re-ranking, result aggregation, and QA prompt engineering you would add later).
This lesson/article demonstrates basic ingestion and retrieval with ChromaDB. You will get document hits and snippet-level results but not a fully featured QA system out of the box. Consider adding an LLM re-ranker and result post-processing for production-quality answers.
Tip: If you run into compatibility issues with PersistentClient, check your installed chromadb version and adapt to chromadb.Client(...) or the version-appropriate persistent API. Also consider pinning chromadb and sentence-transformers versions in a requirements.txt for reproducible environments.

Quick overview

The demo demonstrates:
  • Setting a local persistence path for ChromaDB.
  • Using a SentenceTransformers model to produce embeddings.
  • Reading .txt files from data/, chunking long documents into overlapping segments, and creating deterministic chunk IDs for idempotent ingestion.
  • Upserting (idempotent) into a Chroma collection.
  • Running similarity queries with optional where metadata filters.
Files produced in this example:

Environment and dependencies

Create and activate a Python virtual environment, then install required packages.
Note: Installing sentence-transformers can pull several dependencies (transformers, torch) depending on your environment. Consider using a GPU-enabled environment or CPU-only builds as appropriate.

Implementation β€” single consolidated script

Below is a consolidated, cleaned-up example that shows the key steps. Save as ingest_and_query.py (or split into ingest.py and query.py if you prefer to separate concerns).

Idempotency and duplicate handling

  • Deterministic chunk IDs: we use file_stem__{idx:03d} so re-running ingestion with the same files won’t create duplicate vectors.
  • Use collection.upsert(...) for idempotent behavior: it inserts new IDs and replaces existing ones with the same identifier.
  • If you want ingestion to fail on duplicate IDs, use collection.add(...), which raises on duplicates.

Running the script

  1. Place plain-text files under ./data (each book as a .txt).
  2. Run:
The first run may take longer while embeddings are computed and the index is built. Example (trimmed) terminal output:
These results show which document chunks the vector search considers most similar. For human-readable, direct answers, pass the retrieved chunks to an LLM for synthesis and re-ranking.

Production considerations and next steps

  • Add an LLM-based re-ranker or QA system to synthesize precise answers from retrieved chunks.
  • Improve chunking: use sentence- or token-aware splits (e.g., Hugging Face tokenizers) and retain character offsets.
  • Enrich metadata: store title, author, chapter, and location offsets to enable more powerful where filters and provenance.
  • Indexing and scaling: if you scale beyond a laptop, evaluate managed vector DBs, clustering, or distributed deployments for performance and reliability.
  • Security & cost: consider encryption, access controls, and cost of hosted embeddings vs local compute.
This demo shows how to set up and experiment locally with ChromaDB and Sentence Transformers for semantic retrieval.

Watch Video

Practice Lab