Files
wehub-resource-sync a0c8464e58
Build Package / build (ubuntu-latest) (push) Failing after 1s
CodeQL / Analyze (python) (push) Failing after 1s
Core Typecheck / core-typecheck (push) Failing after 1s
Linting / lint (push) Failing after 1s
llama-dev tests / test-llama-dev (push) Failing after 1s
Publish Sub-Package to PyPI if Needed / publish_subpackage_if_needed (push) Has been skipped
Sync Docs to Developer Hub / sync-docs (push) Failing after 0s
Build Package / build (windows-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:26:52 +08:00

185 lines
4.7 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/vector_stores/MongoDBAtlasVectorSearch.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"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://<username>:<password>@<host>?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": [
"<b>\n",
"Uber's revenue for 2021 was $17,455 million.</b>"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"response = index.as_query_engine().query(\"What was Uber's revenue?\")\n",
"display(Markdown(f\"<b>{response}</b>\"))"
]
},
{
"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
}