对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

将MongoDB与 LangChain 集成

您可以将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 进行检索。

This component requires an MongoDB Vector Search Index.

Atlas支持两种嵌入模式:

  • 手动嵌入:使用您指定的嵌入模型在客户端生成嵌入向量。

  • Automated embedding: MongoDB embeds text on the server's side without needing to generate them manually. To learn more, see Automated Embedding.

实例化向量存储的最快方法是使用MongoDB 集群或本地部署的连接字符串:

from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch
from langchain_voyageai import VoyageAIEmbeddings
# Instantiate the vector store using your MongoDB connection string
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
# Other optional parameters...
)

LangChain 检索器 是用于从向量存储中获取相关文档的组件。您可以使用 LangChain 的内置检索器或以下MongoDB检索器从MongoDB查询和检索数据。

After instantiating MongoDB as a vector store, you can use the vector store instance as a retriever to query your data using 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算法。

This retriever requires an MongoDB Search Index.

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 is a retriever that combines vector search and full-text search results by using the Reciprocal Rank Fusion (RRF) algorithm. To learn more, see How to Perform Hybrid Search.

This retriever requires an existing vector store, MongoDB Vector Search Index, and 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)

MongoDBAtlasParentDocumentRetriever 是一个检索器,它首先查询较小的数据块,然后将较大的父文档返回给 LLM。此类检索称为 父文档检索。父文档检索可以通过允许对较小的数据块进行更精细的搜索,同时为LLM提供父文档的完整上下文,从而改善您的 RAG 代理和应用程序的响应。

此检索器将父文档和子文档存储在单个 MongoDB 集合中,这支持通过仅计算和索引子文档的嵌入来实现高效检索。

在后台,该检索器会创建如下内容:

将 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 字段的筛选器,并使用这些筛选器用于检索与查询匹配的文档的筛选器。

This retriever requires an existing vector store and 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 是传统 RAG 的一种替代方法,它将数据结构化为实体及其关系的知识图表,而不是向量嵌入。基于向量的 RAG 会查找在语义上与查询相似的文档,而 GraphRAG 则会查找与查询相关的实体,并遍历图表中的关系以检索相关信息。

这种方法尤其适用于回答基于关系的问题,例如“公司 A 和公司 B 之间有什么联系?”或“谁是 X 先生/女士的经理?”。

MongoDBGraphStore is a component in the LangChain MongoDB integration that allows you to implement GraphRAG by storing entities (nodes) and their relationships (edges) in a MongoDB collection. This component stores each entity as a document with relationship fields that reference other documents in your collection. It executes queries using the $graphLookup aggregation stage.

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 性能。MongoDB为 LangChain 应用程序提供以下缓存。

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 is a semantic cache that uses MongoDB Vector Search to retrieve the cached prompts. This component requires an MongoDB Vector Search index.

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
)
)

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 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 助手工具包 是一个工具集合,您可以将其传递给 LangGraph React助手 以便它可以与MongoDB资源交互。

名称
说明

MongoDBDatabaseToolkit

一种用于查询MongoDB 数据库的工具。

InfoMongoDBDatabaseTool

一种用于获取MongoDB 数据库元数据的工具。

ListMongoDBDatabaseTool

用于获取MongoDB数据库集合名称的工具。

QueryMongoDBCheckerTool

调用 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使用案例存储库。