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

将 MongoDB 与 LangGraph.js 集成

您可以将MongoDB与LangGraph.js集成,以构建AI代理和高级RAG应用程序。本页概述了MongoDB LangGraph.js 集成,以及如何在 LangGraph 工作流程中使用MongoDB来实现代理状态持久化和检索。

注意

有关Python集成,请参阅 LangGraph。

LangGraph 是 LangChain 生态系统中的一个专用框架,旨在构建 AI 代理和复杂的多代理工作流程。图表是 LangGraph 的核心组件,代表了代理的工作流程。MongoDB LangGraph 集成支持以下功能:

  • MongoDB LangGraph Checkpointer:您可以在MongoDB中持久保存 LangGraph 代理的状态,从而提供短期记忆。

  • 检索工具:您可以使用 MongoDB LangChain 集成快速为您的 LangGraph 工作流创建检索工具。

通过将 LangGraph 应用程序与MongoDB集成,您可以将检索功能和代理内存整合到单个数据库中,从而简化架构并降低操作复杂性。

MongoDB LangGraph Checkpointer 允许您将代理的状态保留在MongoDB中,以实现短期记忆。此功能可为 LangGraph 代理启用人机交互、内存、时间旅行和容错功能。

要安装此组件的包:

npm install @langchain/langgraph-checkpoint-mongodb
import { MongoDBSaver } from "@langchain/langgraph-checkpoint-mongodb";import { MongoClient } from "mongodb"; // Connect to your MongoDB clusterconst client = new MongoClient("<connection-string>"); // Initialize the MongoDB checkpointerconst checkpointer = new MongoDBSaver(client); // Instantiate the graph with the checkpointerconst app = graph.compile(checkpointer);

您可以无缝地将检索器用作 LangGraph 工作流程中的工具,从MongoDB检索相关数据。

  1. 要使用MongoDB Vector Search 和 LangChain 创建基本检索工具,请执行以下操作:

    import { MongoDBAtlasVectorSearch } from "@langchain/mongodb";import { MongoClient } from "mongodb";import { VoyageAIEmbeddings } from "@langchain/community/embeddings/voyage";import { createRetrieverTool } from "langchain/tools/retriever"; // Instantiate the vector storeconst client = new MongoClient("<connection-string>");const collection = client.db("<databaseName>").collection("<collectionName>");const embeddingModel = new VoyageAIEmbeddings(); const vectorStore = new MongoDBAtlasVectorSearch(embeddingModel, { collection: collection, indexName: "vector_index", // Name of the index textKey: "text", // Name of the collection field containing the raw content embeddingKey: "embedding", // Name of the collection field containing the embedded text}); // Create a retriever toolconst retriever = vectorStore.asRetriever();const retrieverTool = createRetrieverTool( retriever, { name: "vector_search_retriever", // Tool name description: "Retrieve relevant documents from the collection" // Tool description },);
  2. 要在 LangGraph 中添加工具作为节点,请执行以下操作:

    1. 将工具转换为节点。

    2. 将节点添加到图中。

    import { StateGraph } from "@langchain/langgraph";import { ToolNode } from "@langchain/langgraph/prebuilt"; // Convert the retriever tool into a nodeconst retrieverNode = new ToolNode([retrieverTool]); // Define the graphconst workflow = new StateGraph(SomeGraphState) // Add the tool as a node in the graph .addNode("vector_search_retriever", retrieverNode); const graph = workflow.compile();
获得技能徽章

免费掌握“Gen AI”!

了解详情

给本页内容打分