ChromaDB is an open-source vector database designed to store embeddings and perform similarity search.
The basic flow looks like this:
Document↓Embedding↓ChromaDB↓Similarity Search↓Relevant Documents
ChromaDB stores documents together with their embeddings, IDs, and optional metadata. When we search, the query is converted into an embedding and ChromaDB finds documents with similar vectors.
This makes ChromaDB useful for semantic search, RAG, recommendation systems, and AI applications.
Install ChromaDB with:
pip install chromadb
Then create a client and collection:
import chromadbclient = chromadb.Client()collection = client.create_collection(name="documents")
A collection is simply a place where we store related documents and their embeddings.
Let’s add a few documents:
collection.add(documents=["React is a JavaScript library for building user interfaces.","FastAPI is a Python web framework.","PostgreSQL is a relational database."],ids=["doc-1","doc-2","doc-3"])
ChromaDB generates embeddings for these documents using the configured embedding function.
Conceptually, each document contains:
doc-1├── document├── embedding└── metadata
The embedding is what allows ChromaDB to compare the meaning of different pieces of text.
Now let’s search for:
results = collection.query(query_texts=["What can I use to build a Python API?"],n_results=2)print(results["documents"])
The query goes through the same embedding process:
"What can I use to build a Python API?"↓Embedding↓ChromaDB↓Similarity Search↓"FastAPI is a Python web framework."
Notice that the query doesn’t contain the word FastAPI. ChromaDB can still find the document because their meanings are similar.
If a document changes, we can use update():
collection.update(ids=["doc-2"],documents=["FastAPI is a modern Python web framework for building APIs."])
To remove a document:
collection.delete(ids=["doc-2"])