Files
chroma/examples/basic_functionality/local_persistence.ipynb
Hammad Bashir 46de47945a SQLite Release PR (#808)
## Description of changes
Base PR to release sqlite refactor, which spans many stacked PRs.

Remaining
- [x] Merge this to main
- [x] Layered Persistent Index #761 
- [x] Remove old impls (In #781 )
- [x] Remove persist() API (In #787)
- [x] Add telemetry to SegmentAPI, it was not included. (#788)
- [x] New clients #805 
- [x] locking and soak tests for thread-safety 
- [x] Migration tool
- [x] Fix #739 
- [x] Fix metadata None vs empty
- [x] Fix persist directory (addressed in #761)
- [x] Leave files open in #761 (merge stacked PR)

Post Release
- [ ] Un xfail cross version tests once we cut the release
- [x] Documentation updates for new silent ADD failure.
- [x] Update all documentation for new API instantiation
- [x] Update all documentation for settings changes
- [ ] Update terraform deployment
- [ ] Update cloudformation deployment

---------

Co-authored-by: Luke VanderHart <luke@vanderhart.net>
Co-authored-by: Jeffrey Huber <jeff@trychroma.com>
Co-authored-by: Anton Troynikov <atroyn@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sebastian Sosa <37946988+CakeCrusher@users.noreply.github.com>
Co-authored-by: Russell Pollari <russell@sharpestminds.com>
Co-authored-by: russell-pollari <pollarir@mgail.com>
2023-07-17 14:21:34 -07:00

189 lines
5.2 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Local Peristence Demo\n",
"This notebook demonstrates how to configure Chroma to persist to disk, then load it back in. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import chromadb"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Create a new Chroma client with persistence enabled. \n",
"persist_directory = \"db\"\n",
"\n",
"client = chromadb.PersistentClient(path=persist_directory)\n",
"\n",
"# Create a new chroma collection\n",
"collection_name = \"peristed_collection\"\n",
"collection = client.get_or_create_collection(name=collection_name)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Add some data to the collection\n",
"collection.add(\n",
" embeddings=[\n",
" [1.1, 2.3, 3.2],\n",
" [4.5, 6.9, 4.4],\n",
" [1.1, 2.3, 3.2],\n",
" [4.5, 6.9, 4.4],\n",
" [1.1, 2.3, 3.2],\n",
" [4.5, 6.9, 4.4],\n",
" [1.1, 2.3, 3.2],\n",
" [4.5, 6.9, 4.4],\n",
" ],\n",
" metadatas=[\n",
" {\"uri\": \"img1.png\", \"style\": \"style1\"},\n",
" {\"uri\": \"img2.png\", \"style\": \"style2\"},\n",
" {\"uri\": \"img3.png\", \"style\": \"style1\"},\n",
" {\"uri\": \"img4.png\", \"style\": \"style1\"},\n",
" {\"uri\": \"img5.png\", \"style\": \"style1\"},\n",
" {\"uri\": \"img6.png\", \"style\": \"style1\"},\n",
" {\"uri\": \"img7.png\", \"style\": \"style1\"},\n",
" {\"uri\": \"img8.png\", \"style\": \"style1\"},\n",
" ],\n",
" documents=[\"doc1\", \"doc2\", \"doc3\", \"doc4\", \"doc5\", \"doc6\", \"doc7\", \"doc8\"],\n",
" ids=[\"id1\", \"id2\", \"id3\", \"id4\", \"id5\", \"id6\", \"id7\", \"id8\"],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Create a new client with the same settings\n",
"client = chromadb.PersistentClient(path=persist_directory)\n",
"\n",
"# Load the collection\n",
"collection = client.get_collection(collection_name)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'ids': [['id1']], 'distances': [[5.1159076593562386e-15]], 'metadatas': [[{'style': 'style1', 'uri': 'img1.png'}]], 'embeddings': None, 'documents': [['doc1']]}\n"
]
}
],
"source": [
"# Query the collection\n",
"results = collection.query(\n",
" query_embeddings=[[1.1, 2.3, 3.2]],\n",
" n_results=1\n",
")\n",
"\n",
"print(results)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ids': ['id1', 'id2', 'id3', 'id4', 'id5', 'id6', 'id7', 'id8'],\n",
" 'embeddings': [[1.100000023841858, 2.299999952316284, 3.200000047683716],\n",
" [4.5, 6.900000095367432, 4.400000095367432],\n",
" [1.100000023841858, 2.299999952316284, 3.200000047683716],\n",
" [4.5, 6.900000095367432, 4.400000095367432],\n",
" [1.100000023841858, 2.299999952316284, 3.200000047683716],\n",
" [4.5, 6.900000095367432, 4.400000095367432],\n",
" [1.100000023841858, 2.299999952316284, 3.200000047683716],\n",
" [4.5, 6.900000095367432, 4.400000095367432]],\n",
" 'metadatas': [{'style': 'style1', 'uri': 'img1.png'},\n",
" {'style': 'style2', 'uri': 'img2.png'},\n",
" {'style': 'style1', 'uri': 'img3.png'},\n",
" {'style': 'style1', 'uri': 'img4.png'},\n",
" {'style': 'style1', 'uri': 'img5.png'},\n",
" {'style': 'style1', 'uri': 'img6.png'},\n",
" {'style': 'style1', 'uri': 'img7.png'},\n",
" {'style': 'style1', 'uri': 'img8.png'}],\n",
" 'documents': ['doc1', 'doc2', 'doc3', 'doc4', 'doc5', 'doc6', 'doc7', 'doc8']}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"collection.get(include=[\"embeddings\", \"metadatas\", \"documents\"])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Clean up\n",
"! rm -rf db"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "chroma",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "88f09714c9334832bac29166716f9f6a879ee2a4ed4822c1d4120cb2393b58dd"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}