[Chroma](https://docs.trychroma.com/docs/overview/getting-started) is an open source [[vector database]]. ## installation ```bash uv add chromadb ``` ## quickstart This quick start processes a single document into an in-memory ChromaDB. ```python import chromadb import uuid from pathlib import Path client = chromadb.Client() collection = client.get_or_create_collection(name="my_collection") file_path = Path('<path_to_file>') with open(file_path, 'r', encoding='utf-8') as f: lines: list[str] = f.read().splitlines() collection.add( ids=[str(uuid.uuid4()) for _ in lines], documents=lines, metadatas=[{"line": line} for line in range(len(lines))] ) print(collection.peek()) # Print first 10 records # Get relevant records from query (not an answer!) results = collection.query( query_texts=[ "What is your first query?", "What is your second query?" ], n_results = 5 ) for i, query_results in enumerate(results["documents"]): print(f'\nQuery {i}') print("\n".join(query_results)) ``` ## client types ChromaDB clients can be either in-memory, persistent, client-server mode, or cloud-based. ```python # in-memory client = chromadb.Client() # Persistent on disk client = chromadb.PersistentClient(path="./chroma_data") # Client-server (localhost) client = chromadb.HTTPClient(host="localhost", port=8000) # Cloud (requires API key in .env file, see docs) client = chromadb.CloudClient() ``` ## chromaDB CLI ChromaDB comes with a [[command line interface|CLI]] to connect to a local ChromaDB server. ```bash uv run chromadb run ``` ## integrations ChromaDB is a popular vector store integrated with various [[LLM Engineering]] libraries. ### LangChain Chroma is embedded in [[LangChain]] for easy integration. ```python from langchain_chroma import Chroma vectorstore = Chroma.from_documents( documents=chunks, embedding=embeddings, persist_directory=db_name ) ``` ### LlamaIndex Chroma is integrated with [[LLamaIndex]] (see [example](https://docs.llamaindex.ai/en/stable/examples/vector_stores/ChromaIndexDemo/)). ## gitignore Add these lines to your `.gitignore' ```gitignore chroma_db/ *.chroma ```