您可以将MongoDB与 LangChain 集成,以构建生成式人工智能和 RAG 应用程序。本页概述了 LangChain MongoDB Python集成以及您可以在应用程序中使用的不同组件。
注意
有关组件和方法的完整列表,请参阅API参考。
有关 JavaScript 集成,请参阅 LangChain JS/TS。
安装和设置
要将MongoDB Vector Search 与 LangChain 结合使用,您必须首先安装 langchain-mongodb包:
pip install langchain-mongodb
向量存储
MongoDBAtlasVectorSearch 是一个向量存储,允许您在MongoDB中的集合中存储和检索向量检索。您可以使用此组件存储数据中的嵌入,并使用MongoDB Vector Search 进行检索。
此组件需要一个MongoDB Vector Search 索引。
使用
Atlas支持两种嵌入模式:
手动嵌入:使用您指定的嵌入模型在客户端生成嵌入向量。
Parameter | 必要性 | 说明 |
|---|---|---|
| 必需 | |
| 必需 | 指定用于存储向量嵌入的 MongoDB 命名空间。 例如: |
| 必需 | 要使用的嵌入模型。您可以使用 LangChain 支持的任何嵌入模型或 |
| Optional | MongoDB Vector Search索引的名称。默认为 |
| Optional | 包含文档文本内容的字段名称。默认值为 |
| Optional | 存储嵌入向量的字段名称。默认值为 |
| Optional | 要使用的相似性函数。接受的值为 |
| Optional | 向量维度数。如果设立此值并且集合上没有向量搜索索引,则MongoDB会为您创建索引。 |
| Optional | 用于确定是否在向量索引不存在时自动创建向量索引的标志。默认值为 |
| Optional | 等待自动创建的向量搜索索引准备就绪的超时时间(以秒为单位)。 |
| Optional | 用于配置向量搜索索引的附加选项字典。 |
| Optional | 传递给向量存储的附加参数,例如 LangChain 特定的参数。 |
Retrievers
LangChain 检索器 是用于从向量存储中获取相关文档的组件。您可以使用 LangChain 的内置检索器或以下MongoDB检索器从MongoDB查询和检索数据。
Vector Search Retriever
将MongoDB实例化为向量存储后,您可以使用向量存储实例作为检索器,通过MongoDB Vector Search查询数据。
使用
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch from langchain_voyageai import VoyageAIEmbeddings # Instantiate the vector store vector_store = MongoDBAtlasVectorSearch.from_connection_string( connection_string="<connection-string>", # MongoDB cluster URI namespace="<database-name>.<collection-name>", # Database and collection name embedding=VoyageAIEmbeddings(model="voyage-3-large"), # Embedding model to use index_name="vector_index", # Name of the vector search index ) # Use the vector store as a retriever retriever = vector_store.as_retriever() # Define your query query = "some search query" # Print results documents = retriever.invoke(query) for doc in documents: print(doc)
全文检索器
MongoDBAtlasFullTextSearchRetriever 是使用MongoDB Search 执行全文搜索的检索器。具体来说,它使用 Lucene 的标准 BM25 算法。
使用
from langchain_mongodb.retrievers.full_text_search import ( MongoDBAtlasFullTextSearchRetriever, ) from pymongo import MongoClient # Connect to your MongoDB cluster client = MongoClient("<connection-string>") collection = client["<database-name>"]["<collection-name>"] # Initialize the retriever retriever = MongoDBAtlasFullTextSearchRetriever( collection=collection, # MongoDB Collection in Atlas search_field="<field-name>", # Name of the field to search search_index_name="<index-name>", # Name of the search index ) # Define your query query = "some search query" # Print results documents = retriever.invoke(query) for doc in documents: print(doc)
注意
混合搜索检索器
MongoDBAtlasHybridSearchRetriever 是使用倒数排名融合 (RRF)算法将向量搜索和全文搜索结果相结合的检索器。要学习;了解详情,请参阅如何执行混合搜索。
此检索器需要现有的向量存储、 MongoDB Vector Search Index 和MongoDB Search Index。
使用
from langchain_mongodb.retrievers.hybrid_search import ( MongoDBAtlasHybridSearchRetriever, ) from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch from langchain_voyageai import VoyageAIEmbeddings # Instantiate the vector store vector_store = MongoDBAtlasVectorSearch.from_connection_string( connection_string="<connection-string>", # MongoDB cluster URI namespace="<database-name>.<collection-name>", # Database and collection name embedding=VoyageAIEmbeddings(model="voyage-3-large"), # Embedding model to use index_name="vector_index", # Name of the vector search index ) # Initialize the retriever retriever = MongoDBAtlasHybridSearchRetriever( vectorstore=vector_store, # Vector store instance search_index_name="<index-name>", # Name of the MongoDB Search index top_k=5, # Number of documents to return fulltext_penalty=60.0, # Penalty for full-text search vector_penalty=60.0, # Penalty for vector search ) # Define your query query = "some search query" # Print results documents = retriever.invoke(query) for doc in documents: print(doc)
Parent Document Retriever
MongoDBAtlasParentDocumentRetriever 是一个检索器,它首先查询较小的数据块,然后将较大的父文档返回给 LLM。此类检索称为 父文档检索。父文档检索可以通过允许对较小的数据块进行更精细的搜索,同时为LLM提供父文档的完整上下文,从而改善您的 RAG 代理和应用程序的响应。
此检索器将父文档和子文档存储在单个 MongoDB 集合中,这支持通过仅计算和索引子文档的嵌入来实现高效检索。
在后台,该检索器会创建如下内容:
MongoDBAtlasVectorSearch 的一个实例,用于处理对子文档的向量搜索查询。
MongoDBDocStore 的一个实例,用于处理父文档的存储和检索。
使用
将 text_key 设置为 page_content,以便向量存储和父文档存储对文档文本使用相同的字段名称。如果没有此参数,检索器会将父文档写入一个字段并从另一字段读取,且查询会失败并返回 KeyError: 'text'。
from langchain_mongodb.retrievers import MongoDBAtlasParentDocumentRetriever from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_voyageai import VoyageAIEmbeddings retriever = MongoDBAtlasParentDocumentRetriever.from_connection_string( connection_string="<connection-string>", # MongoDB cluster URI embedding_model=VoyageAIEmbeddings( # Embedding model to use model="voyage-3-large" ), child_splitter=RecursiveCharacterTextSplitter(), # Text splitter to use database_name="<database-name>", # Database to store the collection collection_name="<collection-name>", # Collection to store the collection text_key="page_content", # Match the key the parent document store uses # Additional vector store or parent class arguments... ) # Define your query query = "some search query" # Print results documents = retriever.invoke(query) for doc in documents: print(doc)
自查询检索器
MongoDBAtlasSelfQueryRetriever 是一个查询自身的检索器。检索器使用 LLM进程搜索查询,以确定可能的元数据筛选器,使用筛选器形成结构化向量搜索查询,然后运行该查询以检索最相关的文档。
示例,对于类似这样的查询:“What aretrunk movie from after 2010 with ratings 以上 8?”,检索器可以识别针对 genre、year 和 rating 字段的筛选器,并使用这些筛选器用于检索与查询匹配的文档的筛选器。
此检索器需要现有的向量存储和MongoDB Vector Search Index。
使用
from langchain_mongodb.retrievers import MongoDBAtlasSelfQueryRetriever from langchain_mongodb import MongoDBAtlasVectorSearch from langchain_classic.chains.query_constructor.schema import AttributeInfo from langchain_voyageai import VoyageAIEmbeddings from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-4o", temperature=0) vector_store = MongoDBAtlasVectorSearch.from_connection_string( connection_string="<connection-string>", namespace="langchain_db.movies", embedding=VoyageAIEmbeddings(model="voyage-3-large"), index_name="vector_index", ) # Given an existing vector store with movies data, define metadata describing the data metadata_field_info = [ AttributeInfo( name="genre", description="The genre of the movie. One of ['science fiction', 'comedy', 'drama', 'thriller', 'romance', 'animated']", type="string", ), AttributeInfo( name="year", description="The year the movie was released", type="integer", ), AttributeInfo( name="rating", description="A 1-10 rating for the movie", type="float" ), ] # Create the retriever from the VectorStore, an LLM and info about the documents retriever = MongoDBAtlasSelfQueryRetriever.from_llm( llm=llm, vectorstore=vector_store, metadata_field_info=metadata_field_info, document_contents="Descriptions of movies", enable_limit=True, ) # This example results in the following composite filter sent to $vectorSearch: # {'filter': {'$and': [{'year': {'$lt': 1960}}, {'rating': {'$gt': 8}}]}} documents = retriever.invoke("Movies made before 1960 that are rated higher than 8") print(documents)
GraphRAG
GraphRAG 是传统 RAG 的一种替代方法,它将数据结构化为实体及其关系的知识图表,而不是向量嵌入。基于向量的 RAG 会查找在语义上与查询相似的文档,而 GraphRAG 则会查找与查询相关的实体,并遍历图表中的关系以检索相关信息。
这种方法尤其适用于回答基于关系的问题,例如“公司 A 和公司 B 之间有什么联系?”或“谁是 X 先生/女士的经理?”。
MongoDBGraphStore 是 LangChain MongoDB集成中的一个组件,允许您通过在MongoDB集合中存储实体(节点)及其关系(边)来实现GraphRAG。该组件将每个实体存储为一个文档,其中包含引用集合中其他文档的关系字段。它使用$graphLookup 聚合阶段执行查询。
使用
from langchain_mongodb.graphrag import MongoDBGraphStore from langchain_openai import ChatOpenAI from langchain_core.documents import Document # Initialize the graph store graph_store = MongoDBGraphStore( connection_string="<connection-string>", # MongoDB cluster URI database_name="<database-name>", # Database to store the graph collection_name="<collection-name>", # Collection to store the graph entity_extraction_model=ChatOpenAI( # LLM to extract entities model="gpt-4o", temperature=0 ), # Other optional parameters... ) # Add documents to the graph docs = [ Document( page_content=( "MongoDB is a document database. " "Dev Ittycheria is the CEO of MongoDB." ) ), Document(page_content="MongoDB Atlas is the cloud platform offered by MongoDB."), ] graph_store.add_documents(docs) # Query the graph query = "Who is the CEO of MongoDB?" answer = graph_store.chat_response(query) print(answer.content)
LLM 缓存
缓存用于存储类似或重复查询的重复响应以避免重新计算,从而优化 LLM 性能。 MongoDB为 LangChain 应用程序提供以下缓存。
MongoDB 缓存
MongoDBCache 允许您在MongoDB集合中存储基本缓存。
使用
from langchain_mongodb import MongoDBCache from langchain_core.globals import set_llm_cache set_llm_cache( MongoDBCache( connection_string="<connection-string>", # MongoDB cluster URI database_name="langchain_db", # Database to store the cache collection_name="cache", # Collection to store the cache ) )
语义缓存
语义缓存是一种更高级的缓存形式,它根据用户输入和缓存结果之间的语义相似性检索缓存的提示。
MongoDBAtlasSemanticCache 是一个语义缓存,它使用MongoDB Vector Search 来检索缓存的提示。此组件需要MongoDB Vector Search索引。
使用
from langchain_mongodb import MongoDBAtlasSemanticCache from langchain_core.globals import set_llm_cache from langchain_voyageai import VoyageAIEmbeddings set_llm_cache( MongoDBAtlasSemanticCache( embedding=VoyageAIEmbeddings(model="voyage-3-large"), # Embedding model connection_string="<connection-string>", # MongoDB cluster URI database_name="langchain_db", # Database to store the cache collection_name="semantic_cache", # Collection to store the cache ) )
DeepAgents 虚拟文件系统
LangChain DeepAgents 是一种代理工具,专为长时间运行、多步骤的任务而设计。它负责规划、上下文管理以及将工作委派给子代理。该工具支持可交换的后端协议,允许您更改代理文件的实际位置。 langchain-mongodb-deepagents-vfs包是该协议的实施: Amazon S3 保存文件,嵌入提供商(AWS Bedrock 或 OpenAI)计算嵌入, MongoDB Atlas保存数据段和嵌入,MongoFilesystemBackend 类路由每个文件操作到正确的处理程序。
当您的代理需要搜索S3 中的设立现有文件时,请使用此包。 grep 作为单个MongoDB聚合运行,该聚合使用倒数排名融合 (RRF)算法将全文搜索结果和向量搜索结果相结合,因此它可以进行扩展,而无需将每个文件加载到代理中以进行逐个过滤。 glob 和 ls 直接处理文件名和目录查找。当代理调用 read、write、edit、upload_files 或 download_files 时,这些调用会直接转到您的文件所在的 S3。后端的观察程序会自动选取其他工具添加的文件并对其进行索引。
在安装此包之前,请确保您拥有:
MongoDB Atlas连接字符串
AWS凭证(
AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY、AWS_DEFAULT_REGION),其中 IAM 策略授予:s3:GetObject、s3:PutObject、s3:ListBucket和s3:DeleteObject(在您的 S3 存储桶上)bedrock:InvokeModel位于与AWS_DEFAULT_REGION位于同一地区的amazon.titan-embed-text-v2:0上,如果使用默认基岩提供商程序则需要
您选择的嵌入提供商:Bedrock(默认,使用上面的 AWS凭证)或 OpenAI(设立
EMBEDDING_PROVIDER=openai并提供OPENAI_API_KEY)
要安装此包,请确定是否希望MongoDB使用 AWS Bedrock 还是 OpenAI 生成搜索嵌入,然后运行匹配命令:
pip install "langchain-mongodb-deepagents-vfs[bedrock]"
pip install "langchain-mongodb-deepagents-vfs[openai]"
使用
使用您的 S3 存储桶名称和Atlas连接字符串实例化 MongoFilesystemBackend。以下示例将两个文件写入 S3,然后演示每种搜索方法:
grep搜索文件内容glob按模式匹配文件路径ls列出目录内容
from langchain_mongodb_deepagents_vfs import MongoFilesystemBackend # Instantiate the backend backend = MongoFilesystemBackend( s3_bucket_name="<bucket-name>", # S3 bucket that stores your files mongodb_connection_string="<connection-string>", # MongoDB Atlas connection string ) # Write two files to S3: one .txt, one .md, so glob can demonstrate # filtering by extension backend.write("mongodb_vfs/docs/notes.txt", "Our authentication flow uses OAuth 2.0.") backend.write("mongodb_vfs/docs/overview.md", "This directory contains onboarding docs.") # Search for files that mention "authentication flow" # Newly written files can take a few seconds to become searchable result = backend.grep("authentication flow", path="mongodb_vfs/docs/") print("grep matches:") for match in result.matches or []: print(match["path"], match["line"], match["text"]) # Find files that match a glob pattern result = backend.glob("*.txt", path="mongodb_vfs/docs/") print("glob matches:", result.matches) # List the contents of a directory result = backend.ls("mongodb_vfs/docs/") print("ls entries:", result.entries) print("init_errors:", backend.init_errors)
默认下,后端会将每个操作限制为存储桶中的 mongodb_vfs/ 前缀。将不同的 s3_prefix 值传递给 MongoFilesystemBackend 以更改此设置,或将 s3_prefix="" 值传递给整个存储桶访问权限。
注意
要学习;了解如何将此后端连接到 DeepAgents代理,请参阅 DeepAgents 快速入门。
MongoDB 助手工具包
MongoDB 助手工具包 是一个工具集合,您可以将其传递给 LangGraph React助手 以便它可以与MongoDB资源交互。
可用工具
名称 | 说明 |
|---|---|
| 一种用于查询MongoDB 数据库的工具。 |
| 一种用于获取MongoDB 数据库元数据的工具。 |
| 用于获取MongoDB数据库集合名称的工具。 |
| 调用 LLM 来检查数据库查询是否正确的工具。 |
使用
from langchain_openai import ChatOpenAI from langgraph.prebuilt import create_react_agent from langchain_mongodb.agent_toolkit import ( MONGODB_AGENT_SYSTEM_PROMPT, MongoDBDatabase, MongoDBDatabaseToolkit, ) db_wrapper = MongoDBDatabase.from_connection_string( "<connection-string>", database="<database-name>" ) llm = ChatOpenAI(model="gpt-4o-mini", timeout=60) toolkit = MongoDBDatabaseToolkit(db=db_wrapper, llm=llm) system_message = MONGODB_AGENT_SYSTEM_PROMPT.format(top_k=5) test_query = "Which country's customers spent the most?" agent = create_react_agent(llm, toolkit.get_tools(), prompt=system_message) agent.step_timeout = 60 events = agent.stream( {"messages": [("user", test_query)]}, stream_mode="values", ) messages = [] for event in events: messages.extend(event["messages"]) print(messages[-1].content)
注意
文档加载器
文档加载器是帮助您为 LangChain 应用程序加载数据的工具。
MongoDBLoader 是一个文档加载器,可从 MongoDB 数据库返回文档列表。
使用
from langchain_mongodb.loaders import MongoDBLoader loader = MongoDBLoader.from_connection_string( connection_string="<connection-string>", # MongoDB cluster URI db_name="langchain_db", # Database that contains the collection collection_name="documents", # Collection to load documents from filter_criteria={"category": "ai"}, # Optional document to specify a filter field_names=["title", "summary"], # Optional list of fields to include metadata_names=["category"], # Optional metadata fields to extract ) docs = loader.load()
注意
聊天记录
MongoDBChatMessageHistory 是一个允许您在MongoDB 数据库中存储和管理聊天消息历史记录的组件。它可以保存与唯一会话标识符相关的用户和AI生成的消息。将此组件用于随时间追踪交互的应用程序,例如聊天机器人。
使用
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory chat_message_history = MongoDBChatMessageHistory( session_id="<session-id>", # Unique session identifier connection_string="<connection-string>", # MongoDB cluster URI database_name="langchain_db", # Database to store the chat history collection_name="chat_history", # Collection to store the chat history ) chat_message_history.add_user_message("Hello") chat_message_history.add_ai_message("Hi")
print(chat_message_history.messages)
[HumanMessage(content='Hello', additional_kwargs={}, response_metadata={}), AIMessage(content='Hi', additional_kwargs={}, response_metadata={}, tool_calls=[], invalid_tool_calls=[])]
存储
您可以使用以下自定义数据存储来管理和存储在 MongoDB 中的数据。
文档存储
MongoDBDocStore 是一个自定义键值存储,使用 MongoDB 存储和管理文档。您可以执行 CRUD 操作,就像在任何其他 MongoDB 集合上执行一样。
使用
from langchain_mongodb.docstores import MongoDBDocStore # Replace with your MongoDB connection string and namespace connection_string = "<connection-string>" namespace = "<database-name>.<collection-name>" # Initialize the MongoDBDocStore docstore = MongoDBDocStore.from_connection_string(connection_string, namespace)
注意
二进制存储
MongoDBByteStore 是一个自定义数据存储,它使用MongoDB存储和管理二进制数据,特别是以字节表示的数据。 您可以使用键值对执行CRUD操作,其中键是字符串,值是字节序列。
使用
from langchain_community.storage.mongodb import MongoDBByteStore # Instantiate the MongoDBByteStore mongodb_store = MongoDBByteStore( connection_string="<connection-string>", # MongoDB cluster URI db_name="langchain_db", # Name of the database collection_name="byte_store", # Name of the collection ) # Set values for keys mongodb_store.mset([("key1", b"hello"), ("key2", b"world")]) # Get values for keys values = mongodb_store.mget(["key1", "key2"]) print(values) # Iterate over keys for key in mongodb_store.yield_keys(): print(key) # Delete keys mongodb_store.mdelete(["key1", "key2"])
[b'hello', b'world'] key1 key2
注意
其他资源
要学习;了解如何将MongoDB与 LangGraph 集成,请参阅将MongoDB与 LangGraph 集成。
有关交互式Python笔记本,请参阅Docs Notebooks 存储库和生成式AI使用案例存储库。