开始使用 LlamaIndex 集成
您可以将 Atlas Vector Search 与 LlamaIndex 集成 在您的 LLM 申请中实施检索增强生成 (RAG)。本教程演示如何开始将Atlas Vector Search与 LlamaIndex 结合使用,对数据执行语义Atlas Search并构建RAG实施。 具体来说,您执行以下操作:
设置环境。
在 Atlas 上存储自定义数据。
在您的数据上创建一个 Atlas Vector Search 索引。
运行以下向量搜索查询:
语义搜索。
带元数据预过滤的语义搜索。
使用 Atlas Vector Search 来回答有关数据的问题,从而实施RAG 。
背景
LlamaIndex 是一个开源框架,旨在简化将自定义数据源连接到LLM的方式。 它提供了多种工具(例如数据连接器、索引和查询引擎)来帮助您为RAG应用程序加载和准备向量嵌入。
通过将 Atlas Vector Search 与 LlaMaindex 集成,您可以将 Atlas 用作矢量数据库,并使用 Atlas Vector Search 通过从数据中检索语义相似的文档来实现 RAG。要了解有关 RAG 的更多信息,请参阅 使用 Atlas Vector Search 进行检索增强生成 (RAG)。
先决条件
如要完成本教程,您必须具备以下条件:
设置环境
为此教程设置环境。通过保存扩展名为 .ipynb
的文件来创建交互式Python笔记本。此 Notebook 允许您单独运行Python代码片段,并且您将使用它来运行本教程中的代码。
要设立笔记本环境,请执行以下操作:
安装并导入依赖项。
运行以下命令:
pip install --quiet --upgrade llama-index llama-index-vector-stores-mongodb llama-index-embeddings-openai pymongo
然后,运行以下代码以导入所需的包:
import getpass, os, pymongo, pprint from pymongo.operations import SearchIndexModel from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext from llama_index.core.settings import Settings from llama_index.core.retrievers import VectorIndexRetriever from llama_index.core.vector_stores import MetadataFilter, MetadataFilters, ExactMatchFilter, FilterOperator from llama_index.core.query_engine import RetrieverQueryEngine from llama_index.embeddings.openai import OpenAIEmbedding from llama_index.llms.openai import OpenAI from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch
定义环境变量。
运行以下代码并在出现提示时提供以下内容:
您的 OpenAI API 密钥。
您的Atlas集群的SRV连接string 。
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") ATLAS_CONNECTION_STRING = getpass.getpass("MongoDB Atlas SRV Connection String:")
注意
连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net
配置 LlamaIndex 设置。
运行以下代码以配置特定于 LlamaIndex 的设置。 这些设置指定以下内容:
OpenAI 作为应用程序使用的法学硕士,用于回答有关数据的问题。
text-embedding-ada-002
作为应用程序用于从数据生成向量嵌入的嵌入模型。数据段大小和重叠 自定义 LlamaIndex 如何对数据进行存储分区。
Settings.llm = OpenAI() Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002") Settings.chunk_size = 100 Settings.chunk_overlap = 10
使用 Atlas 作为向量存储
然后,将自定义数据加载到 Atlas 并将 Atlas 实例化为向量数据库(也称为 向量存储 ) 。将以下代码片段复制并粘贴到笔记本中。
加载示例数据。
在本教程中,您将使用可公开访问的 PDF 文档,标题为“ MongoDB Atlas 最佳实践 ” 作为向量存储的数据源。本文档介绍了管理 Atlas 部署的各种建议和核心概念。
要加载样本数据,请运行以下代码片段。 它执行以下操作:
创建一个名为
data
的新目录。从指定 URL 检索 PDF 并将其作为文件保存在目录中。
使用
SimpleDirectoryReader
数据connector 从文件中提取原始文本和元数据。它还将数据格式化为文档。
# Load the sample data !mkdir -p 'data/' !wget 'https://query.prod.cms.rt.microsoft.com/cms/api/am/binary/RE4HkJP' -O 'data/atlas_best_practices.pdf' sample_data = SimpleDirectoryReader(input_files=["./data/atlas_best_practices.pdf"]).load_data() # Print the first document sample_data[0]
Document(id_='e9893be3-e1a3-4249-9355-e4f42539f508', embedding=None, metadata={'page_label': '1', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-20', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-20'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={}, text='Mong oDB Atlas Best P racticesJanuary 20 19A MongoD B White P aper\n', start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n')
实例化向量存储。
运行以下代码,使用MongoDBAtlasVectorSearch
方法创建名为atlas_vector_store
的向量存储,该方法指定以下内容:
与 Atlas 集群的连接。
llamaindex_db.test
作为用于存储文档的 Atlas 数据库和集合。vector_index
作为用于查询向量存储的索引。
然后,将向量存储保存到 存储上下文 ,这是一个 LlamaIndex 容器对象,用于准备数据以进行存储。
# Connect to your Atlas cluster mongo_client = pymongo.MongoClient(ATLAS_CONNECTION_STRING) # Instantiate the vector store atlas_vector_store = MongoDBAtlasVectorSearch( mongo_client, db_name = "llamaindex_db", collection_name = "test", vector_index_name = "vector_index" ) vector_store_context = StorageContext.from_defaults(vector_store=atlas_vector_store)
将数据存储为向量嵌入。
加载数据并将 Atlas 实例化为向量存储后,从数据生成向量嵌入并将其存储在 Atlas 中。 为此,必须构建 向量存储索引 。此类索引是 LlamaIndex 数据结构,可拆分、嵌入数据,然后将数据存储在向量存储中。
以下代码使用VectorStoreIndex.from_documents
方法对样本数据构建向量存储索引。 它将样本数据转换为向量嵌入,并根据向量存储的存储上下文指定,将这些嵌入作为文档存储在您的llamaindex_db.test
Atlas 集群的 collection 中。
注意
此方法使用您在设置环境时配置的嵌入模型和数据段设置。
vector_store_index = VectorStoreIndex.from_documents( sample_data, storage_context=vector_store_context, show_progress=True )
提示
运行示例代码后,您可以导航到您的集群中的langchain_db.test
collection,在 Atlas 用户界面中查看向量嵌入。
创建 Atlas Vector Search 索引
注意
要创建 Atlas Vector Search 索引,您必须对 Atlas 项目具有Project Data Access Admin
或更高访问权限。
要在向量存储上启用向量搜索查询,请在llamaindex_db.test
集合上创建 Atlas Vector Search 索引。
在您的笔记本中,运行以下代码以创建 vectorSearch 类型的索引,该索引指定对以下字段进行索引:
embedding
字段作为向量类型。embedding
字段包含使用 OpenAI 的text-embedding-ada-002
嵌入模型创建的嵌入。 索引定义指定了1536
向量维度,并使用cosine
来衡量相似性。metadata.page_label
字段作为筛选器类型,用于按 PDF 中的页码对数据进行预筛选。
# Specify the collection for which to create the index collection = mongo_client["llamaindex_db"]["test"] # Create your index model, then create the search index search_index_model = SearchIndexModel( definition={ "fields": [ { "type": "vector", "path": "embedding", "numDimensions": 1536, "similarity": "cosine" }, { "type": "filter", "path": "metadata.page_label" } ] }, name="vector_index", type="vectorSearch", ) collection.create_search_index(model=search_index_model)
构建索引大约需要一分钟时间。在建立索引时,索引处于初始同步状态。 构建完成后,您可以开始查询集合中的数据。
运行向量搜索查询
Atlas 构建索引后,返回笔记本并对数据运行向量搜索查询。 以下示例演示了可以对矢量化数据运行的不同查询。
此示例对字符串MongoDB Atlas security
执行基本语义搜索,并返回按相关性分数排名的文档列表。 它还指定了以下内容:
Atlas Vector Search作为 检索器 执行语义Atlas Search 。
similarity_top_k
参数仅返回三个最相关的文档。
retriever = vector_store_index.as_retriever(similarity_top_k=3) nodes = retriever.retrieve("MongoDB Atlas security") for node in nodes: print(node)
Node ID: 8a743e7c-4d28-4f7c-9c64-1033523a767d Text: MongoD B Atlas provides: •Security f eatures to protect access to your data •Built in replication for always-on availability , tolerating complete data center failure •Backups and point in time recovery to protect against data corruption •Fine-grained monitoring to let you know when to scale. Score: 0.935 Node ID: 5904c51b-ac96-4a2f-818e-35c85af4b624 Text: MongoD B Atlas f eatures e xtensive capabilities to def end, detect, and control access to MongoD B, off ering among the most complete security controls of any modern database: •User Rights Management.User Rights Management. Control access to sensitive data using industry standard mec hanisms for authentication and authorization at the database ... Score: 0.932 Node ID: cb71a615-2f69-47b3-87e7-3373ff476fd6 Text: Protect data in motion over the network and at rest in persistent storage To ensure a secure system right out of the b ox, authentication and I P Address whitelisting are automatically enabled. Review the security section of the MongoD B Atlas documentation to learn more ab out eac h of the security features discussed below . Score: 0.930
您还可以使用匹配表达式对数据进行预过滤,该表达式将索引字段与布尔值、数字值或字符串值进行比较。您必须将要过滤的元数据字段索引为 filter
类型。要了解更多信息,请参阅如何为向量搜索建立字段索引。
注意
在为本教程创建索引时,您已指定metadata.page_label
字段作为筛选器。
此示例对字符串 MongoDB Atlas security
执行语义搜索,并返回按相关性分数排序的文档列表。它还指定了以下内容:
Atlas Vector Search作为 检索器 执行语义Atlas Search 。
similarity_top_k
参数仅返回三个最相关的文档。metadata.page_label
字段上的过滤器,以便 Atlas Vector Search 仅搜索出现在第 17 页上的文档。
# Specify metadata filters metadata_filters = MetadataFilters( filters=[ExactMatchFilter(key="metadata.page_label", value="17")] ) retriever = vector_store_index.as_retriever(similarity_top_k=3, filters=metadata_filters) nodes = retriever.retrieve("MongoDB Atlas security") for node in nodes: print(node)
Node ID: bd82d311-e70b-4d00-aab9-56b84ad16e3d Text: Integrating MongoD B with External Monitoring S olutions The MongoD B Atlas AP I provides integration with e xternal management frameworks through programmatic access to automation f eatures and alerts. APM Integration Many operations teams use Application P erformance Monitoring (AP M) platforms to gain global oversight of 15 Score: 0.911 Node ID: c24f0bdd-d84e-4214-aceb-aa2cbd362819 Text: If the MongoD B cluster e xperiences a failure, the most recentbackup is only moments behind, minimizing e xposure to data loss. In additional, MongoD B Atlas includes queryable bac kups, which allows you to perform queries against e xisting snapshots to more easily restore data at the document/ object level. Queryable bac kups allow you to acco... Score: 0.911 Node ID: 642f08a3-f9b7-427b-81ce-00c1574eea01 Text: In the vast majority of cases, MongoD B Atlas bac kups delivers the simplest, saf est, and most efficient bac kup solution. mongodump is useful when data needs to be exported to another system, when a local bac kup is needed, or when just a subset of the data needs to be backed up. Score: 0.909
回答有关数据的问题
本部分演示如何使用 Atlas Vector Search 和 LlamaIndex 在应用程序中实施RAG 。 现在您已了解如何运行矢量Atlas Search 查询来检索语义相似的文档,运行以下代码以使用Atlas Vector Search 来检索文档,并使用 LlamaIndex 查询引擎 然后根据这些文档回答问题。
此示例执行以下操作:
将 Atlas Vector Search 实例化为向量索引检索器 ,这是一种用于向量存储的特殊检索器。该检索器包含
similarity_top_k
参数,因此 Atlas Vector Search 只能检索 5 个最相关的文档。
实例化
RetrieverQueryEngine
查询引擎以回答有关数据的问题。 出现提示时,查询引擎执行以下操作:使用 Atlas Vector Search 作为检索器,根据提示查询语义相似的文档。
调用您在设置环境时指定的 LLM ,用于根据检索到的文档生成情境感知响应。
向 LLM 提示有关 Atlas 安全建议的示例查询。
返回 LLM 的响应和作为上下文使用的文档。产生的响应可能会有所不同。
# Instantiate Atlas Vector Search as a retriever vector_store_retriever = VectorIndexRetriever(index=vector_store_index, similarity_top_k=5) # Pass the retriever into the query engine query_engine = RetrieverQueryEngine(retriever=vector_store_retriever) # Prompt the LLM response = query_engine.query('How can I secure my MongoDB Atlas cluster?') print(response) print("\nSource documents: ") pprint.pprint(response.source_nodes)
You can secure your MongoDB Atlas cluster by utilizing security features such as authentication, IP address whitelisting, encryption for data in motion and at rest, user rights management, and encryption. Additionally, you can set up global clusters on various cloud platforms with just a few clicks in the MongoDB Atlas UI to ensure data is written to and read from different regions. Source documents: [NodeWithScore(node=TextNode(id_='56884a56-6bcb-4890-9bdc-7d8eb9980b42', embedding=None, metadata={'page_label': '3', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='79ee3a70-7d3d-4dda-b2b4-8da9299ac639', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '3', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='4acc6a58693d749a7f3ddd92063755de00ab9bc8c11be03fd05814bc9c3d2e47'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='9c4f4242-e8c0-493d-b32d-21b900138210', node_type=<ObjectType.TEXT: '1'>, metadata={'page_label': '3', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='6d12532c110420f9131f63bc1f676796103ea2b8078dfdab3809eaff9c4bde21'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='6554d774-108c-4602-8ce8-5aca08802b5a', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='ce37b9f7382f86f97316d5dd346f645175e4a392afabb11d6a13c2dce81395e5')}, text='MongoD B\nAtlas provides:\n•Security f eatures to protect access to your data\n•Built in replication for always-on availability , tolerating\ncomplete data center failure\n•Backups and point in time recovery to protect against\ndata corruption\n•Fine-grained monitoring to let you know when to scale.', start_char_idx=386, end_char_idx=679, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.9282928109169006), NodeWithScore(node=TextNode(id_='5ac63468-529e-4f74-a263-2dc15183f793', embedding=None, metadata={'page_label': '13', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='ae95f83a-15f8-46bd-9603-ed14792b2f18', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '13', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='07a7475af2413b7ad4a3010191462eca9d1691e29d8194389de7a7333ed2d67b'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='6d77733c-8532-43a9-a38d-c1da51a5a51b', node_type=<ObjectType.TEXT: '1'>, metadata={'page_label': '13', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='19ac3815d50ad3ba71f5119f9ebacc1c84742b7a215e014be2dbf46cf6f38cb6'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='99d8cf63-fecf-452b-aa2a-a5f6eec2933d', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='86b4419256e9d788383ea6a8cd30d4f37461f9f23e41c1e33ca9cd268dc12884')}, text='You can set up global clusters — available on Amazon W eb\nServices, Microsoft Azure, and Google Cloud Platform —\nwith just a f ew clic ks in the MongoD B Atlas U I. MongoD B\nAtlas takes care of the deployment and management of\ninfrastructure and database resources required to ensure\nthat data is written to and read from diff erent regions.', start_char_idx=498, end_char_idx=839, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.9278459548950195), NodeWithScore(node=TextNode(id_='71589aef-f5e3-43de-b711-9b9e6e1c9f42', embedding=None, metadata={'page_label': '18', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='fdfddc80-aa07-4411-8b5d-f8e02c53551e', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '18', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='8289ead3efad9fc0ffb10c1051f14a8a6357692c1ab8cc34841116591a3f4f01'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='ce3ad309-f8b0-4211-b4eb-db82afb18b8e', node_type=<ObjectType.TEXT: '1'>, metadata={'page_label': '18', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='8ddc31be6d74789b9a6fd9451bccb1d258bfc27cb60d443527eaad9de0d742ec'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='053bee76-40c8-42c7-b19c-3ec97a2eefab', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='5393211ed6e59c3ee8e1b2fc9e2529f403ee7241ee477da7c20242440a203976')}, text='Protect data in motion over the network\nand at rest in persistent storage\nTo ensure a secure system right out of the b ox,\nauthentication and I P Address whitelisting are\nautomatically enabled.\nReview the security section of the MongoD B Atlas\ndocumentation to learn more ab out eac h of the security\nfeatures discussed below .', start_char_idx=1852, end_char_idx=2179, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.9274715781211853), NodeWithScore(node=TextNode(id_='c2f91ce0-f310-43a4-b473-e8feb8b2dcca', embedding=None, metadata={'page_label': '11', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='8be9cdd6-0d45-4e03-994c-d103aac018a4', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '11', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='7dcc304caa6d650f0d8a1709dfbdeb8bd5e96bd62ea37e09d44c61eff1ec3a82'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='b2952038-2966-4eb8-a590-38a47bf2d2ff', node_type=<ObjectType.TEXT: '1'>, metadata={'page_label': '11', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='73dd5fb0c39eff5917f7ef8ebf2baed63463d720c147133bd1a030c71c0cfd22'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='3d175c9d-f332-44fd-ace6-17c676683e8e', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='2b982087b4e8a9600ae02c1dc31be7e4ab9b10d27d923654bd3de8e3fd134fae')}, text='Eac h node must be configured\nwith sufficient storage for the full data set, or for the subset\nto be stored in a single shard. T he storage speed and size\ncan be set when pic king the MongoD B Atlas instance\nduring cluster creation or reconfiguration.\nData volumes for customers deploying on A WS, Azure, and\nGCP are always encrypted.', start_char_idx=299, end_char_idx=633, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.9221477508544922), NodeWithScore(node=TextNode(id_='ce3ad309-f8b0-4211-b4eb-db82afb18b8e', embedding=None, metadata={'page_label': '18', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='fdfddc80-aa07-4411-8b5d-f8e02c53551e', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '18', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='8289ead3efad9fc0ffb10c1051f14a8a6357692c1ab8cc34841116591a3f4f01'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='d84004f0-4170-48c4-b9f7-69b76db64652', node_type=<ObjectType.TEXT: '1'>, metadata={'page_label': '18', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='13f060ab7a04314bd0b814dd83f9334e1014c43be94f4913bd7387d0f0521a66'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='71589aef-f5e3-43de-b711-9b9e6e1c9f42', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='23826d53a8be4492a2e267e08e3481b309ef43c249148758610e5cc17354467f')}, text='MongoD B Atlas f eatures e xtensive capabilities to def end,\ndetect, and control access to MongoD B, off ering among\nthe most complete security controls of any modern\ndatabase:\n•User Rights Management.User Rights Management. Control access to sensitive\ndata using industry standard mec hanisms for\nauthentication and authorization at the database level•Encryption.Encryption.', start_char_idx=1476, end_char_idx=1851, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.9206620454788208)]
此示例执行以下操作:
在
metadata.page_label
字段上定义元数据筛选器,以便 Atlas Vector Search 仅搜索出现在17页面上的文档。将 Atlas Vector Search 实例化为 向量索引检索器 ,一种用于向量存储的特定类型的检索器。它包括您定义的元数据筛选器和
similarity_top_k
参数,以便 Atlas Vector Search 从17页仅检索5最相关的文档。
实例化
RetrieverQueryEngine
查询引擎以回答有关数据的问题。 出现提示时,查询引擎执行以下操作:使用 Atlas Vector Search 作为检索器,根据提示查询语义相似的文档。
调用您在设置环境时指定的 LLM ,用于根据检索到的文档生成情境感知响应。
向 LLM 提示有关 Atlas 安全建议的示例查询。
返回 LLM 的响应和作为上下文使用的文档。产生的响应可能会有所不同。
# Specify metadata filters metadata_filters = MetadataFilters( filters=[ExactMatchFilter(key="metadata.page_label", value="17")] ) # Instantiate Atlas Vector Search as a retriever vector_store_retriever = VectorIndexRetriever(index=vector_store_index, filters=metadata_filters, similarity_top_k=5) # Pass the retriever into the query engine query_engine = RetrieverQueryEngine(retriever=vector_store_retriever) # Prompt the LLM response = query_engine.query('How can I secure my MongoDB Atlas cluster?') print(response) print("\nSource documents: ") pprint.pprint(response.source_nodes)
Regular backups are essential for securing your MongoDB Atlas cluster. By ensuring that backups are maintained continuously and are just a few seconds behind the operational system, you can minimize exposure to data loss in case of a failure. Additionally, utilizing queryable backups allows you to easily restore data at the document/object level. Integrating external monitoring solutions through the MongoDB Atlas API can also enhance security by providing access to automation features and alerts. Source documents: [NodeWithScore(node=TextNode(id_='72afbd12-441c-4390-843d-cc11609a7855', embedding=None, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='45d87295-3d74-41bb-812f-789b72b4f8ba', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='8e56ef0d706096509e6793e2406c4f5fd0bd020c077a0e7713dd5f3b595f7915'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='552250ae-a55b-4d6d-b326-6d736e5423c8', node_type=<ObjectType.TEXT: '1'>, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='19f3143232ce10c30ee4d9f44012bf3b672ecba3240742d00c921149d9c73016'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='936e940e-2063-4649-8a9a-20090a87aa0a', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='4751bacb2f79e8e61b00828e28cee72a221c5b33bbbec942d431220b2446e507')}, text='If the\nMongoD B cluster e xperiences a failure, the most recentbackup is only moments behind, minimizing e xposure to\ndata loss.\nIn additional, MongoD B Atlas includes queryable bac kups,\nwhich allows you to perform queries against e xisting\nsnapshots to more easily restore data at the document/\nobject level. Queryable bac kups allow you to accomplish\nthe following with less', start_char_idx=1987, end_char_idx=2364, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.913266658782959), NodeWithScore(node=TextNode(id_='552250ae-a55b-4d6d-b326-6d736e5423c8', embedding=None, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='45d87295-3d74-41bb-812f-789b72b4f8ba', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='8e56ef0d706096509e6793e2406c4f5fd0bd020c077a0e7713dd5f3b595f7915'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='a72f111d-1bb9-4173-a713-8bfce8cd2ad5', node_type=<ObjectType.TEXT: '1'>, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='5da4ac9abb19e20a0b14481751a7d4a80f46f8968f804f1d3f4f04fb351886a3'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='72afbd12-441c-4390-843d-cc11609a7855', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='5c99659b2505c1de0600c65fc65cc19c97321a3b9607107d0cac342c5ec9887a')}, text='T aking regular bac kups off ers\nother advantages, as well. T he bac kups can be used to\nseed new environments for development, staging, or QA\nwithout impacting production systems.\nMongoD B Atlas bac kups are maintained continuously , just\na few seconds behind the operational system.', start_char_idx=1702, end_char_idx=1986, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.9097342491149902), NodeWithScore(node=TextNode(id_='70fc2c34-1338-4f29-8fc6-7b8551ea2c39', embedding=None, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='45d87295-3d74-41bb-812f-789b72b4f8ba', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='8e56ef0d706096509e6793e2406c4f5fd0bd020c077a0e7713dd5f3b595f7915'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='834d6586-9bee-4dd8-bf94-2306f1c21f8a', node_type=<ObjectType.TEXT: '1'>, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='86fda9a7b7edce18f333bcbe91c28a9bdb0469957545b6e8cc7fc8e22228c820'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='be001832-41ee-46d2-bd29-4c8650129598', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='e0b09755cc3fad7edc84d2ad9e4b44c098e137c3efea14dd680e55b72c80ffe4')}, text='In the vast majority of cases, MongoD B Atlas bac kups\ndelivers the simplest, saf est, and most efficient bac kup\nsolution. mongodump is useful when data needs to be\nexported to another system, when a local bac kup is\nneeded, or when just a subset of the data needs to be\nbacked up.', start_char_idx=3104, end_char_idx=3386, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.9047020673751831), NodeWithScore(node=TextNode(id_='be001832-41ee-46d2-bd29-4c8650129598', embedding=None, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='45d87295-3d74-41bb-812f-789b72b4f8ba', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='8e56ef0d706096509e6793e2406c4f5fd0bd020c077a0e7713dd5f3b595f7915'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='70fc2c34-1338-4f29-8fc6-7b8551ea2c39', node_type=<ObjectType.TEXT: '1'>, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='53fad6c5333cc41a5246f204a317696c4cb97420363910170f3ae25ef253c1da'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='e3ed474b-1ada-4e15-9f48-db37535bbdd6', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='4eb5d83c88741d63c76679251b2402ff084d33ffd9619f3dd74e5fc0dffc87e2')}, text='Integrating MongoD B with External\nMonitoring S olutions\nThe MongoD B Atlas AP I provides integration with e xternal\nmanagement frameworks through programmatic access to\nautomation f eatures and alerts.\nAPM Integration\nMany operations teams use Application P erformance\nMonitoring (AP M) platforms to gain global oversight of\n15', start_char_idx=3387, end_char_idx=3715, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.9037604331970215), NodeWithScore(node=TextNode(id_='fd4d3ed9-a0d2-4663-9e0b-aee2faea2b4f', embedding=None, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='45d87295-3d74-41bb-812f-789b72b4f8ba', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '17', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='8e56ef0d706096509e6793e2406c4f5fd0bd020c077a0e7713dd5f3b595f7915'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='a53c9dbc-25ec-49cf-bd3c-04c2758dd681', node_type=<ObjectType.TEXT: '1'>, metadata={'page_label': '16', 'file_name': 'atlas_best_practices.pdf', 'file_path': 'data/atlas_best_practices.pdf', 'file_type': 'application/pdf', 'file_size': 512653, 'creation_date': '2024-02-21', 'last_modified_date': '2020-10-27', 'last_accessed_date': '2024-02-21'}, hash='ce8e610852c742743e0674dd6fc05126cc18138fa224e28fc0cc72c0319d087a'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='a07617d5-8090-47b4-92f8-f3bbe38cff54', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='9ad371a88420c2c0ace630858035b13b82f589042b0de31afc364bbe89d0d9ce')}, text='example, a poorly selected shard key can result in uneven\ndata distribution. In this case, most if not all of the queries\nwill be directed to the single mongodthat is managing the\ndata. F urthermore, MongoD B may be attempting to\nredistribute the documents to ac hieve a more ideal balance\nacross the servers.', start_char_idx=0, end_char_idx=309, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.9037080407142639)]
后续步骤
要探索 LlamaIndex 适用于 RAG 应用程序的完整工具库(包括数据连接器、索引和查询引擎),请参阅 LlamaHub。
要扩展本教程中的应用程序以进行来回对话,请参阅 聊天引擎。
MongoDB 还提供以下开发者资源: