Back to Chroma

Local Peristence Demo

examples/basic_functionality/local_persistence.ipynb

1.5.91.7 KB
Original Source

Local Peristence Demo

This notebook demonstrates how to configure Chroma to persist to disk, then load it back in.

python
import chromadb
python
# Create a new Chroma client with persistence enabled. 
persist_directory = "db"

client = chromadb.PersistentClient(path=persist_directory)

# Create a new chroma collection
collection_name = "peristed_collection"
collection = client.get_or_create_collection(name=collection_name)
python
# Add some data to the collection
collection.add(
    embeddings=[
        [1.1, 2.3, 3.2],
        [4.5, 6.9, 4.4],
        [1.1, 2.3, 3.2],
        [4.5, 6.9, 4.4],
        [1.1, 2.3, 3.2],
        [4.5, 6.9, 4.4],
        [1.1, 2.3, 3.2],
        [4.5, 6.9, 4.4],
    ],
    metadatas=[
        {"uri": "img1.png", "style": "style1"},
        {"uri": "img2.png", "style": "style2"},
        {"uri": "img3.png", "style": "style1"},
        {"uri": "img4.png", "style": "style1"},
        {"uri": "img5.png", "style": "style1"},
        {"uri": "img6.png", "style": "style1"},
        {"uri": "img7.png", "style": "style1"},
        {"uri": "img8.png", "style": "style1"},
    ],
    documents=["doc1", "doc2", "doc3", "doc4", "doc5", "doc6", "doc7", "doc8"],
    ids=["id1", "id2", "id3", "id4", "id5", "id6", "id7", "id8"],
)
python
# Create a new client with the same settings
client = chromadb.PersistentClient(path=persist_directory)

# Load the collection
collection = client.get_collection(collection_name)
python
# Query the collection
results = collection.query(
    query_embeddings=[[1.1, 2.3, 3.2]],
    n_results=1
)

print(results)
python
collection.get(include=["embeddings", "metadatas", "documents"])
python
# Clean up
! rm -rf db