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

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
.txtfiles fromdata/, 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
wheremetadata filters.
Environment and dependencies
Create and activate a Python virtual environment, then install required packages.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 asingest_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
- Place plain-text files under
./data(each book as a.txt). - Run:
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
wherefilters 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.
Links and references
- ChromaDB: https://www.trychroma.com/
- Sentence Transformers: https://www.sbert.net/
- ChromaDB docs (API & persistence): https://www.trychroma.com/docs