您可以将MongoDB与 LangChain 集成,以构建生成式人工智能和 RAG 应用程序。本页概述了 LangChain MongoDB Python集成以及您可以在应用程序中使用的不同组件。
开始体验安装和设置
要将MongoDB Vector Search 与 LangChain 结合使用,您必须首先安装 langchain-mongodb包:
pip install langchain-mongodb向量存储
MongoDBAtlasVectorSearch 是一个向量存储,允许您在MongoDB中的集合中存储和检索向量检索。您可以使用此组件存储数据中的嵌入,并使用MongoDB Vector Search 进行检索。
此组件需要一个MongoDB 向量搜索 索引。
使用
Atlas支持两种嵌入模式:
手动嵌入:使用您指定的嵌入模型在客户端生成嵌入向量。
自动嵌入 : MongoDB在服务器端嵌入文本,无需手动生成。要学习;了解更多信息,请参阅 自动嵌入。要学习;了解更多信息,请参阅自动嵌入。
实例化向量存储的最快方法是使用MongoDB 集群或本地部署的连接字符串:
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearchfrom langchain_voyageai import VoyageAIEmbeddings
# Instantiate the vector store using your MongoDB connection stringvector_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 # Other optional parameters...)要使用自动嵌入,请将 AutoEmbeddings实例传递给 embedding 参数。这使MongoDB能够自动生成和托管嵌入向量。
通过自动嵌入:
无需客户端嵌入计算
原始文本直接发送到MongoDB
嵌入向量在服务器端生成
embedding_key字段未存储在文档中
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearchfrom langchain_mongodb.embeddings import AutoEmbeddingsfrom langchain_core.documents import Document
# Some documents to embeddocs = [ Document(page_content="foo", metadata={"baz": "bar"}), Document(page_content="thud", metadata={"bar": "baz"}),]
# Instantiate the vector store with Automated Embeddingvector_store = MongoDBAtlasVectorSearch.from_connection_string( connection_string="<connection-string>", # MongoDB cluster URI namespace="<database-name>.<collection-name>", # Database and collection name embedding=AutoEmbeddings(model="voyage-4"), # Enable Automated Embedding index_name="vector_index", # Name of the vector search index # Other optional parameters...)
# Add documents - text is embedded server-sidevector_store.add_documents(documents=docs)
# Search - queries are embedded server-sideresults = vector_store.similarity_search("search query")该集成还支持其他方法来实例化矢量存储:
使用 MongoDB 客户端:
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearchfrom langchain_voyageai import VoyageAIEmbeddingsfrom pymongo import MongoClient # Connect to your MongoDB clusterclient = MongoClient("<connection-string>")collection = client["<database-name>"]["<collection-name>"] # Instantiate the vector storevector_store = MongoDBAtlasVectorSearch( collection=collection, # Collection to store embeddings embedding=VoyageAIEmbeddings(model="voyage-3-large"), # Embedding model to use index_name="vector_index", # Name of the vector search index # Other optional parameters...)从您创建的文档中:
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearchfrom langchain_voyageai import VoyageAIEmbeddingsfrom langchain_core.documents import Documentfrom pymongo import MongoClient # Some documents to embeddocument_1 = Document(page_content="foo", metadata={"baz": "bar"})document_2 = Document(page_content="thud", metadata={"bar": "baz"})docs = [document_1, document_2] # Connect to your MongoDB clusterclient = MongoClient("<connection-string>")collection = client["<database-name>"]["<collection-name>"] # Create the vector store from documentsvector_store = MongoDBAtlasVectorSearch.from_documents( documents=docs, # List of documents to embed embedding=VoyageAIEmbeddings(model="voyage-3-large"), # Embedding model to use collection=collection, # Collection to store embeddings index_name="vector_index", # Name of the vector search index)
Retrievers
LangChain 检索器 是用于从向量存储中获取相关文档的组件。您可以使用 LangChain 的内置检索器或以下MongoDB检索器从MongoDB查询和检索数据。
Vector Search Retriever
将MongoDB实例化为向量存储后,您可以使用向量存储实例作为检索器,通过MongoDB 向量搜索查询数据。
使用
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearchfrom langchain_voyageai import VoyageAIEmbeddings
# Instantiate the vector storevector_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 retrieverretriever = vector_store.as_retriever()
# Define your queryquery = "some search query"
# Print resultsdocuments = retriever.invoke(query)for doc in documents: print(doc)全文检索器
MongoDBAtlasFullTextSearchRetriever 是使用 MongoDB Search 进行全文搜索的检索器。具体来说,它使用 Lucene 的标准 BM25 算法。
此检索器需要 MongoDB Search 索引。
使用
from langchain_mongodb.retrievers.full_text_search import ( MongoDBAtlasFullTextSearchRetriever,)from pymongo import MongoClient
# Connect to your MongoDB clusterclient = MongoClient("<connection-string>")collection = client["<database-name>"]["<collection-name>"]
# Initialize the retrieverretriever = 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 queryquery = "some search query"
# Print resultsdocuments = retriever.invoke(query)for doc in documents: print(doc)注意
混合搜索检索器
MongoDBAtlasHybridSearchRetriever 是使用倒数排名融合 (RRF) 算法将向量搜索和全文搜索结果相结合的检索器。如要了解更多信息,请参阅如何执行混合搜索。
此检索器需要现有的向量存储、MongoDB 向量搜索索引 和MongoDB 搜索索引。
使用
from langchain_mongodb.retrievers.hybrid_search import ( MongoDBAtlasHybridSearchRetriever,)from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearchfrom langchain_voyageai import VoyageAIEmbeddings
# Instantiate the vector storevector_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 retrieverretriever = 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 queryquery = "some search query"
# Print resultsdocuments = 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 MongoDBAtlasParentDocumentRetrieverfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom 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 queryquery = "some search query"
# Print resultsdocuments = 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 索引。
使用
from langchain_mongodb.retrievers import MongoDBAtlasSelfQueryRetrieverfrom langchain_mongodb import MongoDBAtlasVectorSearchfrom langchain_classic.chains.query_constructor.schema import AttributeInfofrom langchain_voyageai import VoyageAIEmbeddingsfrom 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 datametadata_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 documentsretriever = 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 MongoDBGraphStorefrom langchain_openai import ChatOpenAIfrom langchain_core.documents import Document
# Initialize the graph storegraph_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 graphdocs = [ 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 graphquery = "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 MongoDBCachefrom 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 向量搜索 来检索缓存的提示。此组件需要MongoDB 向量搜索索引。
使用
from langchain_mongodb import MongoDBAtlasSemanticCachefrom langchain_core.globals import set_llm_cachefrom 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 存储文件,嵌入提供商(Amazon Web Services Bedrock 或 OpenAI)计算嵌入,MongoDB Atlas 存储数据块和嵌入,而 MongoFilesystemBackend 类将每个文件操作路由到正确的处理程序。
在代理需要搜索 S3 中大量现有文件时,使用此包。grep 作为单个 MongoDB 聚合运行,使用倒数排名融合 (RRF) 算法结合全文搜索和向量搜索结果。因此,它可以扩展,而无需将每个文件加载到代理中以逐一过滤器。glob 和 ls 直接处理文件名和目录查找。当代理调用 read、write、edit、upload_files 或 download_files 时,这些调用将直接转到 S3,您的文件存储在这里。后端的监视器会自动检测到其他工具添加的文件并对其进行索引。
安装包之前,请确保您已具备:
一个 MongoDB Atlas 连接字符串
Amazon Web Services 凭证(
AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY、AWS_DEFAULT_REGION),其 IAM 策略授予以下权限:s3:GetObject,s3:PutObject、s3:ListBucket和s3:DeleteObject在您的 S3 bucket 上bedrock:InvokeModel在amazon.titan-embed-text-v2:0上,与AWS_DEFAULT_REGION处于同一区域,如果使用默认 Bedrock 提供商,则需要此操作。
您的嵌入提供商选择:Bedrock(默认,使用上述 Amazon Web Services 凭证)或 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 backendbackend = 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 extensionbackend.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 searchableresult = 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 patternresult = backend.glob("*.txt", path="mongodb_vfs/docs/")print("glob matches:", result.matches)
# List the contents of a directoryresult = 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 ChatOpenAIfrom langgraph.prebuilt import create_react_agentfrom 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 = 60events = 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 namespaceconnection_string = "<connection-string>"namespace = "<database-name>.<collection-name>"
# Initialize the MongoDBDocStoredocstore = MongoDBDocStore.from_connection_string(connection_string, namespace)注意
二进制存储
MongoDBByteStore 是一个自定义数据存储,它使用MongoDB存储和管理二进制数据,特别是以字节表示的数据。 您可以使用键值对执行CRUD操作,其中键是字符串,值是字节序列。
使用
from langchain_community.storage.mongodb import MongoDBByteStore
# Instantiate the MongoDBByteStoremongodb_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 keysmongodb_store.mset([("key1", b"hello"), ("key2", b"world")])
# Get values for keysvalues = mongodb_store.mget(["key1", "key2"])print(values)
# Iterate over keysfor key in mongodb_store.yield_keys(): print(key)
# Delete keysmongodb_store.mdelete(["key1", "key2"])[b'hello', b'world']key1key2注意
其他资源
要学习;了解如何将MongoDB与 LangGraph 集成,请参阅将MongoDB与 LangGraph 集成。
有关交互式Python笔记本,请参阅Docs Notebooks 存储库和生成式AI使用案例存储库。