"Chroma is an open-source vector database that is similar to Milvus and can be used with Windows systems. Here is an example of code illustrating its use."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Installing the chromadb package\n",
"!pip install chromadb"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Importing the necessary module\n",
"from chromadb import PersistentClient"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Creating a database client stored in the \"ragdb\" folder, or loading it if it already exists\n",
"client = PersistentClient(path=\"./ragdb\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Creating or loading a collection in ChromaDB\n",
"# Adding documents with their metadata and unique identifiers\n",
"documents = [\n",
" \"The sun rises in the east and sets in the west.\",\n",
" \"Raindrops create soothing sounds as they hit the ground.\",\n",
" \"Stars twinkle brightly in the clear night sky.\",\n",
" \"The ocean waves crash gently against the shore.\",\n",
" \"Mountains stand tall and majestic, covered in snow.\",\n",
" \"Birds chirp melodiously during the early morning hours.\",\n",
" \"The forest is alive with the sounds of rustling leaves and wildlife.\",\n",
" \"A gentle breeze flows through the meadow, carrying the scent of flowers.\"\n",
"]\n",
"embeddings = [text_embedding(document) for document in documents]\n",
"ids = [f\"{i}\" for i in range(len(documents))]\n",
"\n",
"collection.add(\n",
" documents=documents,\n",
" embeddings=embeddings,\n",
" ids=ids\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Querying to find the documents most similar to a given phrase\n",
"query = \"What happens in the forest during the day?\"\n",
"# query = \"Describe how stars appear in a clear night sky.\"\n",
"\n",
"query_embedding = text_embedding(query)\n",
"\n",
"results = collection.query(\n",
" query_embeddings=[query_embedding],\n",
" n_results=2 # Number of desired similar results\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Displaying the results\n",
"for result in results['documents']:\n",
" print(\"Similar document:\", result)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "td_llm",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
# Chroma database
Chroma is an open-source vector database that is similar to Milvus and can be used with Windows systems. Here is an example of code illustrating its use.
%% Cell type:code id: tags:
``` python
# Installing the chromadb package
!pipinstallchromadb
```
%% Cell type:code id: tags:
``` python
# Importing the necessary module
fromchromadbimportPersistentClient
```
%% Cell type:code id: tags:
``` python
# Creating a database client stored in the "ragdb" folder, or loading it if it already exists