{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# MongoDB Atlas Vector Store"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index-vector-stores-mongodb"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install llama-index"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Provide URI to constructor, or use environment variable\n",
"import pymongo\n",
"from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch\n",
"from llama_index.core import VectorStoreIndex\n",
"from llama_index.core import StorageContext\n",
"from llama_index.core import SimpleDirectoryReader"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Download Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!mkdir -p 'data/10k/'\n",
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10k/uber_2021.pdf' -O 'data/10k/uber_2021.pdf'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# mongo_uri = os.environ[\"MONGO_URI\"]\n",
"mongo_uri = (\n",
" \"mongodb+srv://:@?retryWrites=true&w=majority\"\n",
")\n",
"mongodb_client = pymongo.MongoClient(mongo_uri)\n",
"async_mongodb_client = pymongo.AsyncMongoClient(mongo_uri)\n",
"\n",
"store = MongoDBAtlasVectorSearch(\n",
" mongodb_client=mongodb_client, async_mongodb_client=async_mongodb_client\n",
")\n",
"store.create_vector_search_index(\n",
" dimensions=1536, path=\"embedding\", similarity=\"cosine\"\n",
")\n",
"storage_context = StorageContext.from_defaults(vector_store=store)\n",
"uber_docs = SimpleDirectoryReader(\n",
" input_files=[\"./data/10k/uber_2021.pdf\"]\n",
").load_data()\n",
"index = VectorStoreIndex.from_documents(\n",
" uber_docs, storage_context=storage_context\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"Uber's revenue for 2021 was $17,455 million."
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"response = index.as_query_engine().query(\"What was Uber's revenue?\")\n",
"display(Markdown(f\"{response}\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4454\n",
"1\n",
"4453\n"
]
}
],
"source": [
"from llama_index.core import Response\n",
"\n",
"# Initial size\n",
"\n",
"print(store._collection.count_documents({}))\n",
"# Get a ref_doc_id\n",
"typed_response = (\n",
" response if isinstance(response, Response) else response.get_response()\n",
")\n",
"ref_doc_id = typed_response.source_nodes[0].node.ref_doc_id\n",
"print(store._collection.count_documents({\"metadata.ref_doc_id\": ref_doc_id}))\n",
"# Test store delete\n",
"if ref_doc_id:\n",
" store.delete(ref_doc_id)\n",
" print(store._collection.count_documents({}))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: For MongoDB Atlas, you have to create an Atlas Search Index.\n",
"\n",
"[MongoDB Docs | Create an Atlas Vector Search Index](https://www.mongodb.com/docs/atlas/atlas-vector-search/create-index/)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "py38",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}