🥞
Store and Load ChromaDB
Here you will learn how to store and load a ChromaDB in Databutton
First off, we are going to assume that you have created a ChromaDB through something like
VectorDB = Chroma.from_documents(
documents=doc_list,
embedding=OpenAIEmbeddings(openai_api_key=openai_api_key),
persist_directory=".tmp",
)
Then, to store (persist) that VectorDB in Databutton, you can use the following function
def chromadb_put(key, cdb, persist_directory=".chromadb"):
cdb.persist()
shutil.make_archive("chromadb_store", "zip", persist_directory)
with open("./chromadb_store.zip", "rb") as file:
db.storage.binary.put(f"chromadb_{str(key)}", file.read())
chromadb_put("MyChromaDB", index, persist_directory=".tmp")
To load it again, you can use
def chromadb_get(key, persist_directory=".chromadb"):
stream = db.storage.binary.get(f"chromadb_{str(key)}")
with open("chroma_stora.zip", "wb") as file:
file.write(stream)
shutil.unpack_archive("chroma_stora.zip", persist_directory)
index = Chroma(
persist_directory=persist_directory,
embedding_function=OpenAIEmbeddings(openai_api_key=openai_api_key),
)
return index
Note that a very annoying property of ChromaDB is that you need to specify the embeddings both when creating and loading.
Last modified 1mo ago