๐Ÿ“ LLM & AI

ChromaDB: Vector Database for RAG

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
284
๐ŸŒณ
Level
Advanced

ChromaDB is an embeddable vector database. It stores texts and their embeddings, and can search by semantic meaning. No separate server required.

Installation

uv add chromadb
# First install downloads ~500 MB (the embedding model)

PersistentClient โ€” Saving Data to Disk

import chromadb

# Data is saved to the ./chroma_db folder
client = chromadb.PersistentClient(path="./chroma_db")

# Create a collection or retrieve an existing one
collection = client.get_or_create_collection(name="documents")

chromadb.Client() โ€” stores data in RAM only; it is lost on restart.

add() โ€” Adding Documents

collection.add(
    documents=["Python is an interpreted language.", "Django is a web framework."],
    ids=["doc_0", "doc_1"],
    metadatas=[{"source": "intro.txt"}, {"source": "frameworks.txt"}]
)

Three required fields:
- documents โ€” the texts
- ids โ€” unique strings (duplicates are not allowed)
- metadatas โ€” dictionaries with metadata (optional but useful)

ChromaDB automatically generates embeddings using its built-in model.

results = collection.query(
    query_texts=["How do I build a web application?"],
    n_results=3,
    include=["documents", "metadatas", "distances"]
)

for doc, meta, dist in zip(
    results["documents"][0],
    results["metadatas"][0],
    results["distances"][0]
):
    relevance = 1 - dist
    print(f"[{relevance:.0%}] {meta['source']}: {doc[:80]}")

Interpreting Distance

distance is the cosine distance, ranging from 0 to 2:

Distance Relevance Interpretation
0.0โ€“0.3 70โ€“100% Very similar
0.3โ€“0.6 40โ€“70% Moderately similar
> 0.6 < 40% Weak match
# Filter by threshold
THRESHOLD = 0.5
relevant = [
    (doc, meta) for doc, meta, dist
    in zip(results["documents"][0], results["metadatas"][0], results["distances"][0])
    if dist < THRESHOLD
]

Checking for Duplicates Before Adding

existing_ids = set(collection.get(include=[])["ids"])

new_docs, new_ids, new_metas = [], [], []
for doc, id_, meta in zip(documents, ids, metadatas):
    if id_ not in existing_ids:
        new_docs.append(doc)
        new_ids.append(id_)
        new_metas.append(meta)

if new_docs:
    collection.add(documents=new_docs, ids=new_ids, metadatas=new_metas)

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

httpx: A Modern HTTP Client for Python

httpx is a next-generation HTTP client. Its interface is similar to requests, but it supports...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 330
๐Ÿ“

AI Agents: ReAct Loop and Autonomous Actions

A chatbot answers questions. An agent takes action: it calls tools, retrieves real data, and...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 335
๐Ÿ“

Typer: CLI Applications Without the Boilerplate

Typer builds CLIs from Python type annotations. No argparse, no manual parsing โ€” just decorators...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 368
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Neural Networks in Code: 5 AI Projects in Python with Claude Open course curriculum