1. 案例目标本示例展示了如何使用LlamaIndex与MongoDB Atlas向量搜索功能实现文档检索和查询。主要目标包括演示如何连接MongoDB Atlas数据库展示如何创建向量搜索索引展示如何将文档加载到MongoDB Atlas向量存储中演示如何执行向量搜索查询展示如何从向量存储中删除文档2. 技术栈与核心依赖LlamaIndexMongoDB AtlasPyMongo核心依赖包llama-index-vector-stores-mongodb- LlamaIndex的MongoDB向量存储集成llama-index- LlamaIndex核心库pymongo- MongoDB Python客户端3. 环境配置3.1 安装依赖%pip install llama-index-vector-stores-mongodb!pip install llama-index3.2 配置MongoDB Atlas连接import pymongo from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch from llama_index.core import VectorStoreIndex from llama_index.core import StorageContext from llama_index.core import SimpleDirectoryReader # MongoDB Atlas连接URI mongo_uri ( mongodbsrv://:?retryWritestruewmajority ) mongodb_client pymongo.MongoClient(mongo_uri)注意在使用MongoDB Atlas之前需要创建一个Atlas搜索索引。请参考MongoDB文档创建Atlas向量搜索索引4. 案例实现4.1 下载和准备数据下载Uber 2021年10-K报告作为示例数据# 创建数据目录 !mkdir -p data/10k/ # 下载Uber 2021年10-K报告 !wget https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10k/uber_2021.pdf -O data/10k/uber_2021.pdf4.2 创建向量存储和索引创建MongoDB Atlas向量搜索存储并设置向量搜索索引# 创建MongoDB Atlas向量搜索存储 store MongoDBAtlasVectorSearch(mongodb_client) # 创建向量搜索索引 store.create_vector_search_index( dimensions1536, pathembedding, similaritycosine ) # 创建存储上下文 storage_context StorageContext.from_defaults(vector_storestore)4.3 加载文档并构建索引使用SimpleDirectoryReader加载PDF文档并构建向量索引# 加载Uber 2021年10-K报告 uber_docs SimpleDirectoryReader( input_files[./data/10k/uber_2021.pdf] ).load_data() # 创建向量索引 index VectorStoreIndex.from_documents( uber_docs, storage_contextstorage_context )4.4 执行查询使用查询引擎执行查询# 查询Uber的收入 response index.as_query_engine().query(What was Ubers revenue?) display(Markdown(f{response}))4.5 文档管理展示如何检查和删除向量存储中的文档from llama_index.core import Response # 检查初始文档数量 print(store._collection.count_documents({})) # 获取参考文档ID typed_response ( response if isinstance(response, Response) else response.get_response() ) ref_doc_id typed_response.source_nodes[0].node.ref_doc_id # 检查特定参考文档的节点数量 print(store._collection.count_documents({metadata.ref_doc_id: ref_doc_id})) # 删除文档 if ref_doc_id: store.delete(ref_doc_id) print(store._collection.count_documents({}))5. 案例效果本示例展示了MongoDB Atlas向量搜索的基本功能和效果向量索引创建成功创建了1536维的向量索引使用余弦相似度文档加载成功将Uber 2021年10-K报告加载到向量存储中查询功能成功查询到Uber 2021年的收入为174.55亿美元文档管理成功展示了文档计数和删除功能MongoDB Atlas向量搜索的优势与MongoDB生态系统无缝集成支持混合查询向量搜索和传统数据库查询提供可扩展的云原生解决方案支持多种相似度计算方法6. 案例实现思路本案例的实现思路如下环境准备安装必要的依赖包配置MongoDB Atlas连接数据准备下载并准备示例文档Uber 2021年10-K报告向量存储配置创建MongoDB Atlas向量搜索存储设置向量索引索引构建加载文档并构建向量索引查询执行使用查询引擎执行向量搜索查询文档管理展示文档计数和删除功能关键技术点使用MongoDB Atlas作为向量存储后端创建向量搜索索引时指定维度、路径和相似度度量通过ref_doc_id管理文档的多个节点使用delete方法按ref_doc_id删除整个文档7. 扩展建议元数据过滤实现基于元数据的过滤查询功能混合搜索结合向量搜索和传统MongoDB查询实现混合搜索批量操作优化批量文档添加和删除的性能索引优化针对特定应用场景优化向量索引参数多租户支持实现多租户环境下的数据隔离实时更新实现向量索引的实时更新功能性能监控添加查询性能监控和分析功能8. 总结本示例详细介绍了如何使用LlamaIndex与MongoDB Atlas向量搜索功能实现文档检索和查询。MongoDB Atlas向量搜索提供了一种将向量搜索功能集成到现有MongoDB应用中的简便方法特别适合已经使用MongoDB作为主数据库的应用场景。通过本示例我们学习了如何连接MongoDB Atlas并创建向量搜索索引如何将文档加载到MongoDB Atlas向量存储中如何执行向量搜索查询如何管理向量存储中的文档这些技术可以应用于各种需要向量搜索功能的场景如文档检索系统、推荐引擎、语义搜索等为用户提供更智能、更相关的搜索体验。