Docs 菜单
Docs 主页
/
MongoDB 阿特拉斯
/ /

开始使用 LangChain JS/TS 集成

在此页面上

  • 背景
  • 先决条件
  • 设置环境
  • 使用 Atlas 作为向量存储
  • 创建 Atlas Vector Search 索引
  • 运行向量搜索查询
  • 回答有关数据的问题
  • 后续步骤

注意

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

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

  1. 设置环境。

  2. 在 Atlas 上存储自定义数据。

  3. 在您的数据上创建一个 Atlas Vector Search 索引。

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

    • 语义搜索。

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

    • 最大边际相关性 (MMR) Atlas Search 。

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

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

通过将 Atlas Vector Search 与 LangChain 集成,您可以将 Atlas 用作向量数据库,并使用 Atlas Vector Search 从数据中检索语义相似的文档来实施RAG 。要了解有关RAG的更多信息,请参阅使用 Atlas Vector Search 进行检索增强生成 (RAG)。

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

  • 运行 MongoDB 6.0.11 版本的 Atlas 集群, 7.0.2 或更高版本(包括RC )。

  • OpenAI API 密钥。 您必须拥有一个 OpenAI 付费帐户,并有可用于 API 请求的信用。

  • 用于运行 Node.js 项目的终端和代码编辑器。

  • npm 和 Node.js 已安装。

您必须首先为本教程设置环境。 要设置环境,请完成以下步骤。

1

在终端中运行以下命令,创建名为 langchain-mongodb的新目录并初始化项目:

mkdir langchain-mongodb
cd langchain-mongodb
npm init -y
2

运行以下命令:

npm install langchain @langchain/community @langchain/mongodb @langchain/openai pdf-parse fs
3

配置项目以使用 ES 模块 "type": "module"package.json将 添加到 文件,然后保存。

{
"type": "module",
// other fields...
}
4

在项目中,创建一个名为get-started.js的文件,然后将以下代码复制并粘贴到该文件中。 在整个教程中,您将向此文件添加代码。

此初始代码片段导入本教程所需的包,定义环境变量,并建立与 Atlas 集群的连接。

import { formatDocumentsAsString } from "langchain/util/document";
import { MongoClient } from "mongodb";
import { MongoDBAtlasVectorSearch } from "@langchain/mongodb";
import { OpenAIEmbeddings, ChatOpenAI } from "@langchain/openai";
import { PDFLoader } from "@langchain/community/document_loaders/fs/pdf";
import { PromptTemplate } from "@langchain/core/prompts";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { RunnableSequence, RunnablePassthrough } from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
import * as fs from 'fs';
process.env.OPENAI_API_KEY = "<api-key>";
process.env.ATLAS_CONNECTION_STRING = "<connection-string>";
const client = new MongoClient(process.env.ATLAS_CONNECTION_STRING);
5

要完成环境设置,请将<api-key> 中的 和<connection-string> 占位符值替换为您的get-started.js OpenAIAPI 密钥和 集群的 SRV 连接string Atlas。连接string应使用以下格式:

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

在本部分中,您将定义一个异步函数以将自定义数据加载到 Atlas 中,并将 Atlas 实例化为向量数据库(也称为 向量存储 ) 。将以下代码添加到get-started.js文件中。

注意

在本教程中,您将使用可公开访问的 PDF 文档,标题为“ MongoDB Atlas 最佳实践 ” 作为向量存储的数据源。本文档介绍了管理 Atlas 部署的各种建议和核心概念。

此代码执行以下操作:

  • 通过指定以下参数来配置 Atlas 集合:

    • langchain_db.test 作为 Atlas 集合来存储文档。

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

    • text 作为包含原始文本内容的字段的名称。

    • embedding 作为包含向量嵌入的字段的名称。

  • 通过执行以下操作来准备自定义数据:

    • 从指定 URL 检索原始数据并将其另存为 PDF。

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

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

  • 通过调用MongoDBAtlasVectorSearch.fromDocuments方法从样本文档创建向量存储。 此方法指定以下参数:

    • 要存储在向量数据库中的示例文档。

    • OpenAI 的嵌入模型用于将文本转换为embedding字段的向量嵌入。

    • 您的 Atlas 配置。

async function run() {
try {
// Configure your Atlas collection
const database = client.db("langchain_db");
const collection = database.collection("test");
const dbConfig = {
collection: collection,
indexName: "vector_index", // The name of the Atlas search index to use.
textKey: "text", // Field name for the raw text content. Defaults to "text".
embeddingKey: "embedding", // Field name for the vector embeddings. Defaults to "embedding".
};
// Ensure that the collection is empty
const count = await collection.countDocuments();
if (count > 0) {
await collection.deleteMany({});
}
// Save online PDF as a file
const rawData = await fetch("https://query.prod.cms.rt.microsoft.com/cms/api/am/binary/RE4HkJP");
const pdfBuffer = await rawData.arrayBuffer();
const pdfData = Buffer.from(pdfBuffer);
fs.writeFileSync("atlas_best_practices.pdf", pdfData);
// Load and split the sample data
const loader = new PDFLoader(`atlas_best_practices.pdf`);
const data = await loader.load();
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 200,
chunkOverlap: 20,
});
const docs = await textSplitter.splitDocuments(data);
// Instantiate Atlas as a vector store
const vectorStore = await MongoDBAtlasVectorSearch.fromDocuments(docs, new OpenAIEmbeddings(), dbConfig);
} finally {
// Ensure that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);

保存文件,然后运行以下命令将数据加载到 Atlas。

node get-started.js

提示

运行get-started.js 后,您可以导航到集群中的collection集合, 在 Atlas UI langchain_db.test中 查看向量嵌入。

注意

要创建 Atlas Vector Search 索引,您必须对 Atlas 项目具有Project Data Access Admin或更高访问权限。

要在向量存储上启用向量搜索查询,请在langchain_db.test集合上创建 Atlas Vector Search 索引。

将以下代码添加到您在get-started.js文件中定义的异步函数中。此代码创建一个vectorSearch类型的索引,指定对以下字段进行索引:

  • embedding 字段作为向量类型。 embedding字段包含使用 OpenAI 的text-embedding-ada-002嵌入模型创建的嵌入。 索引定义指定了1536向量维度,并使用cosine来衡量相似性。

  • loc.pageNumber 字段作为筛选器类型,用于按 PDF 中的页码对数据进行预筛选。

此代码还使用await 函数来确保搜索索引在使用前已同步到数据。

1// Ensure index does not already exist, then create your Atlas Vector Search index
2const indexes = await collection.listSearchIndexes("vector_index").toArray();
3if(indexes.length === 0){
4
5 // Define your Atlas Vector Search Index
6 const index = {
7 name: "vector_index",
8 type: "vectorSearch",
9 definition: {
10 "fields": [
11 {
12 "type": "vector",
13 "numDimensions": 1536,
14 "path": "embedding",
15 "similarity": "cosine"
16 },
17 {
18 "type": "filter",
19 "path": "loc.pageNumber"
20 }
21 ]
22 }
23 }
24
25 // Run the helper method
26 const result = await collection.createSearchIndex(index);
27 console.log(result);
28
29 // Wait for Atlas to sync index
30 console.log("Waiting for initial sync...");
31 await new Promise(resolve => setTimeout(() => {
32 resolve();
33 }, 10000));
34}

保存文件,然后运行以下命令以创建 Atlas Vector Search 索引。

node get-started.js

本部分演示了可以对矢量化数据运行的各种查询。 创建索引后,将以下代码添加到异步函数中,以对数据运行向量Atlas Search查询。

注意

如果您在查询数据时遇到不准确的结果,则索引的同步时间可能比预期长。 增加setTimeout函数中的数字,以便为初始同步留出更多时间。

1

以下代码使用similaritySearch方法对stringMongoDB Atlas security执行基本语义搜索。它返回仅包含fieldspageContentpageNumber的按相关性排名的文档列表。

// Basic semantic search
const basicOutput = await vectorStore.similaritySearch("MongoDB Atlas security");
const basicResults = basicOutput.map((results => ({
pageContent: results.pageContent,
pageNumber: results.metadata.loc.pageNumber,
})))
console.log("Semantic Search Results:")
console.log(basicResults)
2
node get-started.js
...
Semantic Search Results:
[
{
pageContent: 'MongoDB Atlas features extensive capabilities to defend,\n' +
'detect, and control access to MongoDB, offering among\n' +
'the most complete security controls of any modern\n' +
'database:',
pageNumber: 18
},
{
pageContent: 'Atlas provides encryption of data at rest with encrypted\n' +
'storage volumes.\n' +
'Optionally, Atlas users can configure an additional layer of\n' +
'encryption on their data at rest using the MongoDB',
pageNumber: 19
},
{
pageContent: 'automatically enabled.\n' +
'Review thesecurity section of the MongoDB Atlas\n' +
'documentationto learn more about each of the security\n' +
'features discussed below.\n' +
'IP Whitelisting',
pageNumber: 18
},
{
pageContent: '16Security\n' +
'17Business Intelligence with MongoDB Atlas\n' +
'18Considerations for Proofs of Concept\n' +
'18MongoDB Stitch: Serverless Platform from MongoDB\n' +
'19We Can Help\n' +
'19Resources',
pageNumber: 2
}
]

您可以使用MQL匹配表达式对数据进行预过滤,该表达式将索引字段与布尔值、数字值或字符串值进行比较。您必须将要筛选的任何元数据字段作为filter类型进行索引。要了解更多信息,请参阅如何为向量搜索的字段编制索引。

注意

在为本教程创建索引时,您已指定loc.pageNumber字段作为筛选器。

1

以下代码使用similaritySearch方法对字符串MongoDB Atlas security执行语义搜索。它指定了以下参数:

  • 要以3形式返回的文档数。

  • 针对loc.pageNumber字段的预筛选器,使用$eq操作符仅匹配出现在17页上的文档。

它返回按相关性排名的文档列表,其中仅包含pageContentpageNumber字段。

// Semantic search with metadata filter
const filteredOutput = await vectorStore.similaritySearch("MongoDB Atlas security", 3, {
preFilter: {
"loc.pageNumber": {"$eq": 17 },
}
});
const filteredResults = filteredOutput.map((results => ({
pageContent: results.pageContent,
pageNumber: results.metadata.loc.pageNumber,
})))
console.log("Semantic Search with Filtering Results:")
console.log(filteredResults)
2
node get-started.js
...
Semantic Search with Filter Results:
[
{
pageContent: 'BSON database dumps produced bymongodump.\n' +
'In the vast majority of cases, MongoDB Atlas backups\n' +
'delivers the simplest, safest, and most efficient backup',
pageNumber: 17
},
{
pageContent: 'Monitoring Solutions\n' +
'The MongoDB Atlas API provides integration with external\n' +
'management frameworks through programmatic access to\n' +
'automation features and alerts.\n' +
'APM Integration',
pageNumber: 17
},
{
pageContent: 'MongoDB Atlas backups are maintained continuously, just\n' +
'a few seconds behind the operational system. If the\n' +
'MongoDB cluster experiences a failure, the most recent',
pageNumber: 17
}
]

您还可以根据最大边际相关性 (MMR) 执行语义搜索,最大边际相关性是针对多样性而优化的语义相关性度量。

1

以下代码使用maxMarginalRelevanceSearch方法搜索字符串MongoDB Atlas security 。它还指定一个定义以下可选参数的对象:

  • k 将返回的文档数量限制为3

  • fetchK 在将文档传递给MMR算法之前仅获取10文档。

它返回按相关性排名的文档列表,其中仅包含pageContentpageNumber字段。

// Max Marginal Relevance search
const mmrOutput = await vectorStore.maxMarginalRelevanceSearch("MongoDB Atlas security", {
k: 3,
fetchK: 10,
});
const mmrResults = mmrOutput.map((results => ({
pageContent: results.pageContent,
pageNumber: results.metadata.loc.pageNumber,
})))
console.log("Max Marginal Relevance Search Results:")
console.log(mmrResults)
2
node get-started.js
...
Max Marginal Relevance Search Results:
[
{
pageContent: 'MongoDB Atlas features extensive capabilities to defend,\n' +
'detect, and control access to MongoDB, offering among\n' +
'the most complete security controls of any modern\n' +
'database:',
pageNumber: 18
},
{
pageContent: 'automatically enabled.\n' +
'Review thesecurity section of the MongoDB Atlas\n' +
'documentationto learn more about each of the security\n' +
'features discussed below.\n' +
'IP Whitelisting',
pageNumber: 18
},
{
pageContent: '16Security\n' +
'17Business Intelligence with MongoDB Atlas\n' +
'18Considerations for Proofs of Concept\n' +
'18MongoDB Stitch: Serverless Platform from MongoDB\n' +
'19We Can Help\n' +
'19Resources',
pageNumber: 2
}
]

提示

另请参阅:

有关更多信息,请参阅 API 参考文档。

本部分演示使用 Atlas Vector Search 和 LangChain 的两种不同RAG实施。 现在您已经使用Atlas Vector Search检索语义相似的文档,使用以下代码示例提示LLM回答针对Atlas Vector Search返回的文档的问题。

1

此代码执行以下操作:

  • 定义 LangChain 提示模板 以指示 法学硕士 使用这些文档作为查询的上下文。 LangChain 将这些文档传递给{context} 输入变量,并将您的查询传递给{question} 变量。

  • 构建一条 使用 OpenAI 的聊天模型根据提示生成上下文感知响应。

  • 使用有关 Atlas 安全建议的示例查询提示链。

  • 返回LLM的响应以及用作上下文的文档。

// Implement RAG to answer questions on your data
const retriever = vectorStore.asRetriever();
const prompt =
PromptTemplate.fromTemplate(`Answer the question based on the following context:
{context}
Question: {question}`);
const model = new ChatOpenAI({});
const chain = RunnableSequence.from([
{
context: retriever.pipe(formatDocumentsAsString),
question: new RunnablePassthrough(),
},
prompt,
model,
new StringOutputParser(),
]);
// Prompt the LLM
const question = "How can I secure my MongoDB Atlas cluster?";
const answer = await chain.invoke(question);
console.log("Question: " + question);
console.log("Answer: " + answer);
// Return source documents
const retrievedResults = await retriever.getRelevantDocuments(question)
const documents = retrievedResults.map((documents => ({
pageContent: documents.pageContent,
pageNumber: documents.metadata.loc.pageNumber,
})))
console.log("\nSource documents:\n" + JSON.stringify(documents, 1, 2))
2

保存文件后,运行以下命令。 生成的响应可能会有所不同。

node get-started.js
...
Question: How can I secure my MongoDB Atlas cluster?
Answer: You can secure your MongoDB Atlas cluster by taking
advantage of extensive capabilities to defend, detect, and control
access to MongoDB. You can also enable encryption of data at rest
with encrypted storage volumes and configure an additional layer of
encryption on your data. Additionally, you can set up global clusters
on Amazon Web Services, Microsoft Azure, and Google Cloud Platform
with just a few clicks in the MongoDB Atlas UI.
Source documents:
[
{
"pageContent": "MongoDB Atlas features extensive capabilities to defend,\ndetect, and control access to MongoDB, offering among\nthe most complete security controls of any modern\ndatabase:",
"pageNumber": 18
},
{
"pageContent": "throughput is required, it is recommended to either\nupgrade the Atlas cluster or take advantage of MongoDB's\nauto-shardingto distribute read operations across multiple\nprimary members.",
"pageNumber": 14
},
{
"pageContent": "Atlas provides encryption of data at rest with encrypted\nstorage volumes.\nOptionally, Atlas users can configure an additional layer of\nencryption on their data at rest using the MongoDB",
"pageNumber": 19
},
{
"pageContent": "You can set up global clusters — available on Amazon Web\nServices, Microsoft Azure, and Google Cloud Platform —\nwith just a few clicks in the MongoDB Atlas UI. MongoDB",
"pageNumber": 13
}
]
1

此代码执行以下操作:

  • 将 Atlas Vector Search 实例化为 检索器 来查询语义相似的文档。它还指定了以下可选参数:

    • searchTypemmr ,指定 Atlas Vector Search 根据最大边际相关性 (MMR) 检索文档。

    • filterlog.pageNumbers字段上添加预过滤器,以仅包含出现在17页上的文档。

    • 以下MMR特定参数:

      • fetchK 在将文档传递给MMR算法之前仅获取20文档。

      • lambda,一个介于01之间的值,用于确定结果之间的多样性程度,其中0表示最大多样性, 1表示最小多样性。

  • 定义 LangChain 提示模板 以指示 法学硕士 使用这些文档作为查询的上下文。 LangChain 将这些文档传递给{context} 输入变量,并将您的查询传递给{question} 变量。

  • 构建一条 使用 OpenAI 的聊天模型根据提示生成上下文感知响应。

  • 使用有关 Atlas 安全建议的示例查询提示链。

  • 返回LLM的响应以及用作上下文的文档。

// Implement RAG to answer questions on your data
const retriever = await vectorStore.asRetriever({
searchType: "mmr", // Defaults to "similarity
filter: { preFilter: { "loc.pageNumber": { "$eq": 17 } } },
searchKwargs: {
fetchK: 20,
lambda: 0.1,
},
});
const prompt =
PromptTemplate.fromTemplate(`Answer the question based on the following context:
{context}
Question: {question}`);
const model = new ChatOpenAI({});
const chain = RunnableSequence.from([
{
context: retriever.pipe(formatDocumentsAsString),
question: new RunnablePassthrough(),
},
prompt,
model,
new StringOutputParser(),
]);
// Prompt the LLM
const question = "How can I secure my MongoDB Atlas cluster?";
const answer = await chain.invoke(question);
console.log("Question: " + question);
console.log("Answer: " + answer);
// Return source documents
const retrievedResults = await retriever.getRelevantDocuments(question)
const documents = retrievedResults.map((documents => ({
pageContent: documents.pageContent,
pageNumber: documents.metadata.loc.pageNumber,
})))
console.log("\nSource documents:\n" + JSON.stringify(documents, 1, 2))
2

保存文件后,运行以下命令。 生成的响应可能会有所不同。

node get-started.js
...
Question: How can I secure my MongoDB Atlas cluster?
Answer: To secure your MongoDB Atlas cluster, you can take the following measures:
1. Enable authentication and use strong, unique passwords for all users.
2. Utilize encryption in transit and at rest to protect data both while in motion and at rest.
3. Configure network security by whitelisting IP addresses that can access your cluster.
4. Enable role-based access control to limit what actions users can perform within the cluster.
5. Monitor and audit your cluster for suspicious activity using logging and alerting features.
6. Keep your cluster up to date with the latest patches and updates to prevent vulnerabilities.
7. Implement backups and disaster recovery plans to ensure you can recover your data in case of data loss.
Source documents:
[
{
"pageContent": "BSON database dumps produced bymongodump.\nIn the vast majority of cases, MongoDB Atlas backups\ndelivers the simplest, safest, and most efficient backup",
"pageNumber": 17
},
{
"pageContent": "APM Integration\nMany operations teams use Application Performance\nMonitoring (APM) platforms to gain global oversight of\n15",
"pageNumber": 17
},
{
"pageContent": "performance SLA.\nIf in the course of a deployment it is determined that a new\nshard key should be used, it will be necessary to reload the\ndata with a new shard key because designation and values",
"pageNumber": 17
},
{
"pageContent": "to the database.\nReplication Lag\nReplication lag is the amount of time it takes a write\noperation on the primary replica set member to replicate to",
"pageNumber": 17
}
]

MongoDB 还提供以下开发者资源:

后退

LangChain

来年

LlamaIndex