Chroma
Chroma is the AI-native open-source embedding database for storing and searching vector embeddings
Alternative To
- • Pinecone
- • Weaviate Cloud
- • Qdrant
Difficulty Level
For experienced users. Complex setup and configuration required.
Overview
Chroma is an AI-native open-source embedding database designed to make it easy to build AI applications with vector search capabilities. It allows you to store, index, and query high-dimensional vector embeddings to enable semantic search, retrieval-augmented generation (RAG), recommendation systems, and other AI applications.
System Requirements
- CPU: 2+ cores (4+ recommended for production)
- RAM: 4GB+ (8GB+ recommended for larger collections)
- GPU: Not required
- Storage: 1GB+ (SSD recommended for better performance)
- Operating System: Linux, macOS, Windows, or Docker-compatible platforms
Installation Guide
Prerequisites
- Basic knowledge of Python and command line interfaces
- Python 3.7+ installed
- pip (Python package manager)
- Docker (optional, for containerized setup)
Option 1: Python Package Installation
The simplest way to install Chroma is via pip:
pip install chromadb
Once installed, you can use Chroma in-memory for quick prototyping:
import chromadb
# Create a client
client = chromadb.Client()
# Create a collection
collection = client.create_collection("my_collection")
# Add documents to the collection
collection.add(
documents=["This is document 1", "This is document 2"],
metadatas=[{"source": "source1"}, {"source": "source2"}],
ids=["doc1", "doc2"]
)
# Query the collection
results = collection.query(
query_texts=["document"],
n_results=2
)
Option 2: Docker Installation (Recommended for Production)
Pull and run the Chroma Docker image:
docker pull chromadb/chroma docker run -p 8000:8000 -v $(pwd)/chroma_data:/chroma/chroma_data chromadb/chromaOr using Docker Compose:
Create a
docker-compose.ymlfile:version: "3.9" services: chroma: image: chromadb/chroma volumes: - ./chroma_data:/chroma/chroma_data ports: - 8000:8000Then run:
docker-compose up -dConnect to the Chroma server using the client:
import chromadb # Connect to the Chroma server client = chromadb.HttpClient(host="localhost", port=8000) # Create a collection collection = client.create_collection("my_collection")
Note: For detailed installation instructions specific to your operating system and environment, please refer to the official documentation on the project’s GitHub repository.
Practical Exercise: Building a Simple RAG Application with Chroma
Let’s create a simple retrieval-augmented generation (RAG) application using Chroma and OpenAI.
Step 1: Install Required Packages
pip install chromadb openai langchain
Step 2: Create and Populate a Vector Database
import chromadb
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import TextLoader
import os
# Set your OpenAI API key
os.environ["OPENAI_API_KEY"] = "your-api-key"
# Initialize the embeddings model
embeddings = OpenAIEmbeddings()
# Load a sample document
loader = TextLoader("path/to/your/document.txt")
documents = loader.load()
# Split the document into chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
chunks = text_splitter.split_documents(documents)
# Create a Chroma vector store
vectordb = Chroma.from_documents(documents=chunks, embedding=embeddings, persist_directory="./chroma_db")
vectordb.persist()
print(f"Created vector database with {len(chunks)} document chunks")
Step 3: Implement a Simple Query System
from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI
# Load the persisted vector store
vectordb = Chroma(persist_directory="./chroma_db", embedding_function=embeddings)
# Create a retrieval chain
llm = ChatOpenAI(model_name="gpt-3.5-turbo")
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectordb.as_retriever(search_kwargs={"k": 3})
)
# Ask questions
while True:
query = input("\nEnter your question (or 'quit' to exit): ")
if query.lower() == "quit":
break
# Get answer from the retrieval chain
answer = qa_chain.run(query)
print(f"\nAnswer: {answer}")
Step 4: Advanced Features to Explore
Once you’re comfortable with the basics, try exploring some more advanced features:
- Use different embedding models (Sentence Transformers, Cohere, etc.)
- Implement metadata filtering in your queries
- Experiment with different collection settings and distance metrics
- Integrate with different LLMs (Anthropic, Llama, etc.)
- Build a web interface using Streamlit or Gradio
- Optimize for performance with larger document collections
Resources
Official Documentation
The official documentation provides comprehensive guides, API references, and examples:
GitHub Repository
The GitHub repository contains the source code and contribution guidelines:
Community Support
Get help and connect with other Chroma users through these channels:
Tutorials and Examples
Learn how to use Chroma with these resources:
Suggested Projects
You might also be interested in these similar projects:
Qdrant is a high-performance vector similarity search engine and vector database written in Rust, designed for production-ready AI applications
Open-source Firebase alternative with vector database capabilities for AI applications