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

开始使用MongoDB LangChain 集成

注意

本教程使用 LangChain 的Python库。有关使用 JavaScript 库的教程,请参阅开始使用 LangChain JavaScript/ Typescript 集成。

您可以将MongoDB Vector Search 与 LangChain 集成,以构建LLM 应用程序并实现检索增强生成 (RAG)。本教程演示如何开始使用MongoDB Vector Search 和 LangChain 对数据执行语义搜索并构建RAG实施。具体来说,您执行以下操作:

  1. 设置环境。

  2. 在MongoDB中存储自定义数据。

  3. 对数据创建MongoDB Vector Search索引。

  4. 运行以下向量搜索查询:

    • 语义搜索。

    • 带分数的语义搜索。

    • 带元数据预过滤的语义搜索。

  5. 使用 MongoDB Vector Search 来回答有关数据的问题,从而实施RAG。

LangChain 是一个开源框架,它通过使用“链”简化了 LLM 应用程序的创建。链是特定于 LangChain 的组件,可以组合用于各种 AI 使用案例,包括 RAG。

By integrating MongoDB Vector Search with LangChain, you can use MongoDB as a vector database and use MongoDB Vector Search to implement RAG by retrieving semantically similar documents from your data. To learn more about RAG, see Retrieval-Augmented Generation (RAG) with MongoDB.

如要完成本教程,您必须具备以下条件:

  • 以下MongoDB 集群类型之一:

    • 运行 MongoDB 6.0.11 版本的 Atlas 集群,7.0.2或以上版本。确保您的 IP 地址 包含在 Atlas 项目的 访问列表 中。

    • 使用 Python 和 Docker 创建的本地 Atlas 部署。安装 atlas-local-lib-py (pip install atlas-local-lib-py),以编程方式创建和管理本地部署。要了解更多信息,请参阅 atlas-local-lib-py 存储库。

    • A MongoDB Community cluster with Search and Vector Search installed.

  • A Voyage AI API key. To create an API key, see Manage Voyage AI Model API Keys.

  • OpenAI API密钥。您必须拥有一个具有可用于API请求的积分的 OpenAI 帐户。要学习;了解有关注册 OpenAI 帐户的更多信息,请参阅 OpenAI API网站。

  • 运行交互式Python笔记本(例如 Colab)的环境。

为此教程设置环境。 通过保存扩展名为 .ipynb 的文件来创建交互式Python笔记本。 此 Notebook 允许您单独运行Python代码片段,并且您将使用它来运行本教程中的代码。

要设立笔记本环境,请执行以下操作:

1

运行以下命令:

pip install --quiet --upgrade langchain langchain-community langchain-core langchain-mongodb langchain-voyageai langchain-openai langchain-text-splitters pymongo pypdf

然后,运行以下代码以导入所需的包:

import os, pymongo, pprint
from langchain_community.document_loaders import PyPDFLoader
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_mongodb import MongoDBAtlasVectorSearch
from langchain_voyageai import VoyageAIEmbeddings
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain_text_splitters import RecursiveCharacterTextSplitter
from pymongo import MongoClient
from pymongo.operations import SearchIndexModel
2

运行以下代码,将占位符替换为以下值:

  • 您的 Voyage AI和 OpenAI API密钥。

  • MongoDB集群的连接字符串。

os.environ["VOYAGE_API_KEY"] = "<voyage-api-key>"
os.environ["OPENAI_API_KEY"] = "<openai-api-key>"
MONGODB_URI = "<connection-string>"

注意

将 <connection-string> 替换为您的 Atlas 集群或本地部署的连接字符串。

连接字符串应使用以下格式:

mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net

重要: 确保连接字符串包含数据库用户的凭证。要学习有关查找连接字符串的更多信息,请参阅通过客户端库连接到集群。

然后,将自定义数据加载到MongoDB中,并将MongoDB 集群实例化为向量数据库(也称为向量存储)。将以下代码片段复制并粘贴到笔记本中。

1

For this tutorial, you use a publicly accessible PDF document about a recent MongoDB earnings report as the data source for your vector store.

要加载样本数据,请运行以下代码片段。 它执行以下操作:

  • 从指定的 URL 检索 PDF 并加载原始文本数据。

  • 使用文本拆分器将数据分割为较小的文档。

  • 指定数据块参数,它确定每个文档中的字符数以及两个连续文档之间应重叠的字符数。

# Load the PDF
loader = PyPDFLoader("https://investors.mongodb.com/node/13176/pdf")
data = loader.load()
# Split PDF into documents
text_splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=20)
docs = text_splitter.split_documents(data)
# Print the first document
docs[0]
Document(metadata={'producer': 'West Corporation using ABCpdf', 'creator': 'PyPDF', 'creationdate': '2025-03-05T21:06:26+00:00', 'title': 'MongoDB, Inc. Announces Fourth Quarter and Full Year Fiscal 2025 Financial Results', 'source': 'https://investors.mongodb.com/node/13176/pdf', 'total_pages': 9, 'page': 0, 'page_label': '1'}, page_content='MongoDB, Inc. Announces Fourth Quarter and Full Year Fiscal 2025 Financial Results\nMarch 5, 2025\nFourth Quarter Fiscal 2025 Total Revenue of $548.4 million, up 20% Year-over-Year')
2

运行以下代码以从示例文档中创建一个名为 vector_store 的向量存储实例。此代码段指定如下内容:

  • MongoDB 集群的连接字符串。

  • langchain_db.test 作为存储文档的MongoDB命名空间。

  • 来自 Voyage AI的 voyage-3-large 嵌入模型。将文本转换为 embedding字段的向量嵌入。

  • vector_index 作为用于查询向量存储的索引。

# Instantiate the vector store using your MongoDB connection string
vector_store = MongoDBAtlasVectorSearch.from_connection_string(
connection_string = MONGODB_URI,
namespace = "langchain_db.test",
embedding = VoyageAIEmbeddings(model="voyage-3-large"),
index_name = "vector_index"
)
# Add documents to the vector store
vector_store.add_documents(documents=docs)

运行示例代码后,如果您使用的是Atlas ,则可以导航到Atlas 用户界面中的 langchain_db.test命名空间来验证向量嵌入。

要在向量存储上启用向量搜索查询,请使用 LangChain 辅助工具方法或 PyMongo 驱动程序方法在 langchain_db.test 集合上创建 MongoDB 向量搜索索引。

在笔记本中,按照首选方法运行以下代码。此索引定义指定对以下字段进行索引:

  • embedding field as the vector type. The embedding field contains the embeddings created using Voyage AI's voyage-3-large embedding model. The index definition specifies 1024 vector dimensions and measures similarity using cosine.

  • page_label field as the filter type for pre-filtering data by the page number in the PDF.

# Use helper method to create the vector search index
vector_store.create_vector_search_index(
dimensions = 1024, # The number of vector dimensions to index
filters = [ "page_label" ]
)

The index should take about one minute to build. While it builds, the index is in an initial sync state. When it finishes building, you can start querying the data in your collection.

MongoDB构建索引后,即可对数据运行向量搜索查询。以下示例演示了可以对矢量化数据运行的各种查询。

提示

有关语义搜索方法的完整列表,请参阅API参考。

本部分演示如何使用MongoDB Vector Search 和 LangChain 在应用程序中实现RAG。现在您已经使用MongoDB Vector Search检索语义相似的文档,运行以下代码示例以提示法学硕士回答基于这些文档的问题。

此示例执行以下操作:

  • 将MongoDB Vector Search 实例化为检索器以查询类似文档,包括可选的k 参数以仅搜索10 最相关的文档。
  • 定义 LangChain 提示模板,指示 LLM 使用这些文档作为查询的上下文。LangChain 将这些文档传递给 {context} 输入变量,并将您的查询传递给 {question} 变量。

  • 构造一条指定以下内容的链:

    • MongoDB Vector Search 作为检索器,搜索要用作上下文的文档。

    • 您定义的提示模板。

    • OpenAI 的 gpt-4o 聊天模型用于生成情境感知响应。

  • 使用样本查询调用链。

  • 返回 LLM 的响应和作为上下文使用的文档。 产生的响应可能会有所不同。

# Instantiate MongoDB Vector Search as a retriever
retriever = vector_store.as_retriever(
search_type = "similarity",
search_kwargs = { "k": 10 }
)
# Define a prompt template
template = """
Use the following pieces of context to answer the question at the end.
{context}
Question: {question}
"""
prompt = PromptTemplate.from_template(template)
model = ChatOpenAI(model="gpt-4o")
# Construct a chain to answer questions on your data
chain = (
{ "context": retriever, "question": RunnablePassthrough()}
| prompt
| model
| StrOutputParser()
)
# Prompt the chain
question = "What was MongoDB's latest acquisition?"
answer = chain.invoke(question)
print("Question: " + question)
print("Answer: " + answer)
# Return source documents
documents = retriever.invoke(question)
print("\nSource documents:")
pprint.pprint(documents)
Question: What was MongoDB's latest acquisition?
Answer: MongoDB's latest acquisition was Voyage AI, a pioneer in state-of-the-art embedding and reranking models.
Source documents:
[Document(id='67f0259b8bb2babc06924409', metadata={'_id': '67f0259b8bb2babc06924409', ... 'page_label': '9'}, page_content='SOURCE MongoDB, Inc.'),
Document(id='67f0259b8bb2babc06924351', metadata={'_id': '67f0259b8bb2babc06924351', ... 'page_label': '2'}, page_content='Measures."\nFourth Quarter Fiscal 2025 and Recent Business Highlights\nMongoDB acquired Voyage AI, a pioneer in state-of-the-art embedding and reranking models that power next-generation'),
Document(id='67f0259b8bb2babc0692432f', metadata={'_id': '67f0259b8bb2babc0692432f', ... 'page_label': '1'}, page_content='MongoDB platform. In fiscal year 2026 we expect to see stable consumption growth in Atlas, our main growth driver," said Dev Ittycheria, President\nand Chief Executive Officer of MongoDB .'),
Document(id='67f0259b8bb2babc06924355', metadata={'_id': '67f0259b8bb2babc06924355', ... 'page_label': '2'}, page_content='conjunction with the acquisition of Voyage, MongoDB is announcing a stock buyback program of $200 million, to offset the\ndilutive impact of the acquisition consideration.'),
Document(id='67f0259b8bb2babc069243a6', metadata={'_id': '67f0259b8bb2babc069243a6', ... 'page_label': '4'}, page_content="MongoDB's unified, intelligent data platform was built to power the next generation of applications, and MongoDB is the most widely available, globally"),
Document(id='67f0259b8bb2babc06924329', metadata={'_id': '67f0259b8bb2babc06924329', ... 'page_label': '1'}, page_content='MongoDB, Inc. Announces Fourth Quarter and Full Year Fiscal 2025 Financial Results\nMarch 5, 2025\nFourth Quarter Fiscal 2025 Total Revenue of $548.4 million, up 20% Year-over-Year'),
Document(id='67f0259b8bb2babc069243a7', metadata={'_id': '67f0259b8bb2babc069243a7', ... 'page_label': '4'}, page_content='distributed database on the market. With integrated capabilities for operational data, search, real-time analytics, and AI-powered retrieval, MongoDB'),
Document(id='67f0259b8bb2babc069243a5', metadata={'_id': '67f0259b8bb2babc069243a5', ... 'page_label': '4'}, page_content="Headquartered in New York, MongoDB's mission is to empower innovators to create, transform, and disrupt industries with software and data."),
Document(id='67f0259b8bb2babc06924354', metadata={'_id': '67f0259b8bb2babc06924354', ... 'page_label': '2'}, page_content='data.\nMongoDB completed the redemption of 2026 Convertible Notes, eliminating all debt from the balance sheet. Additionally, in'),
Document(id='67f0259b8bb2babc069243a9', metadata={'_id': '67f0259b8bb2babc069243a9', ... 'page_label': '4'}, page_content='50,000 customers across almost every industry—including 70% of the Fortune 100—rely on MongoDB for their most important applications. To learn\nmore, visit mongodb.com .\nInvestor Relations')]

请观看本视频教程,以了解更多关于使用 LangChain 和 MongoDB 进行语义搜索和检索增强生成 (RAG) 的信息。

时长:8 分钟