
Chroma Tutorial: RAG के लिए Local Vector Database जो Restart के बाद भी Data रखे
Also available in English: Read this article in English
आप अपने documents को Chroma में embed करते हैं, script restart करते हैं, और collection ख़ाली मिलता है। या किसी document की गलती सुधारकर दोबारा चलाते हैं, फिर भी पुराना text लौटता है। दोनों Chroma का सामान्य व्यवहार हैं। यहाँ एक छोटा, persistent setup है जो इनसे बचाता है। हमने इसे Python में chromadb 1.5.9 के साथ test किया।
सही client चुनें
chromadb.Client() memory में चलता है, इसलिए process बंद होते ही data चला जाता है। chromadb.PersistentClient(path=...) data को disk पर save करता है और start पर फिर load करता है। JavaScript app के लिए chroma run --path ./db से local server चलाएँ और localhost:8000 से जुड़ें।
Documents रखें और query करें
pip install chromadb
import chromadb
client = chromadb.PersistentClient(path="./chroma_db")
docs = client.get_or_create_collection(
"support-docs",
configuration={"hnsw": {"space": "cosine"}},
)
docs.upsert(
ids=["refund-1", "office-1"],
documents=["Refunds are processed within 5 working days.",
"Our office is in Pune."],
metadatas=[{"topic": "billing"}, {"topic": "company"}],
)
hits = docs.query(query_texts=["How long does a refund take?"],
n_results=3, where={"topic": "billing"})
print(hits["documents"][0], hits["distances"][0])
यह code क्या करता है:
- get_or_create_collection अगली बार वही मौजूदा collection लौटाता है। Collection पहले से हो तो बाकी arguments ignore होते हैं, इसलिए settings पहले दिन ही तय करें।
- Embedding function नहीं दिया, इसलिए Chroma अपना default all-MiniLM-L6-v2 model इस्तेमाल करता है। हमारे test में पहली बार लगभग 79 MB की model file download हुई। नए versions embedding function को collection के साथ save करते हैं।
- space: cosine text embeddings के लिए ठीक है। Default
l2है। - where सिर्फ़ वे results रखता है जिनका metadata मेल खाता है।
n_resultsन दें तो query 10 results लौटाती है।
आम गड़बड़ियाँ ठीक करें
- बदला हुआ text नहीं बदलता:
add()पहले से मौजूद ID को चुपचाप ignore करता है।upsert()याupdate()इस्तेमाल करें। - Embeddings None दिखते हैं: ये default रूप से नहीं लौटते।
include=["embeddings", "documents", "metadatas", "distances"]जोड़ें। - Dimension error: आपने embedding model बदल दिया। नया collection बनाकर सब दोबारा embed करें।
- "ef or M is too small":
n_resultsघटाएँ या HNSW settings बढ़ाएँ। - Install पर SQLite error: Chroma को SQLite 3.35 से नया version चाहिए।
client.reset() सोच-समझकर ही चलाएँ: यह पूरा database मिटा देता है।
Server-side retrieval के लिए हमारी Qdrant hybrid search गाइड, साफ़ input के लिए Firecrawl Markdown गाइड, जवाब परखने के लिए golden questions पढ़ें, या Chroma टूल पेज देखें।
स्रोत
- Chroma Clients, Chroma docs
- Manage Collections, Chroma docs
- Adding Data and Update Data, Chroma docs
- Configure Collections, Chroma docs
- Query and Get, Chroma docs
- Troubleshooting, Chroma docs
- chroma README, GitHub
FAQ
PersistentClient data कहाँ रखता है?
उस folder में जो आप path में देते हैं। Path न दें तो default .chroma है।
add() ने मेरा document update क्यों नहीं किया?
add() पहले से मौजूद IDs को ignore करता है। जोड़ने या बदलने के लिए upsert() इस्तेमाल करें।
क्या embeddings के लिए API key चाहिए?
नहीं। Default all-MiniLM-L6-v2 model local रूप से चलता है।
क्या बाद में embedding model बदल सकते हैं?
नए model का dimension अलग हो तो पुराने collection में जोड़ना fail होगा। नया collection बनाकर दोबारा embed करें।