Docs 菜单
Docs 主页
/
Atlas
/ /

将 MongoDB 与 LangGraph 集成

在此页面上

  • 背景
  • 检索工具
  • MongoDB 检查点程序

您可以将 MongoDB 与 LangGraph 集成,以构建 AI 代理和高级 RAG 应用程序。本页提供了 MongoDB LangGraph 集成的概述,并介绍了如何在 LangGraph 工作流程中使用 MongoDB 进行数据检索和代理状态持久化。

您可以将 MongoDB 与 LangGraph 集成,以构建 AI 代理和高级 RAG 应用程序。本页提供了 MongoDB LangGraph 集成的概述,并介绍了如何在 LangGraph 工作流程中使用 MongoDB 进行数据检索和代理状态持久化。

开始体验

➤ 使用 Select your language(选择您的语言)下拉菜单设置此页面上示例的语言。


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

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

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

  • MongoDB Checkpointer:您可以在 MongoDB 中持久化 LangGraph 代理的状态,从而提供会话记忆和容错能力。

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

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

您可以在 LangGraph 工作流程中无缝地使用 LangChain 检索器作为工具,从 Atlas 中检索相关数据。

LangChain MongoDB集成原生支持全文搜索、向量搜索、混合搜索和父文档检索。有关检索方法的完整列表,请参阅 LangChain MongoDB检索器。

  1. 要使用 Atlas Vector Search 和 LangChain 创建一个基本的检索工具:

    import { MongoDBAtlasVectorSearch } from "@langchain/mongodb";
    import { MongoClient } from "mongodb";
    import { VoyageEmbeddings } from "@langchain/community/embeddings/voyage";
    import { createRetrieverTool } from "langchain/tools/retriever";
    # Instantiate the vector store
    const 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 tool
    const 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 node
    retrieverNode = new ToolNode([retrieverTool]);
    // Define the graph
    const workflow = new StateGraph(SomeGraphState)
    // Add the tool as a node in the graph
    .addNode("vector_search_retriever", retrieverNode);
    const graph = workflow.compile();

注意

  1. 要使用 Atlas Vector Search 和 LangChain 创建一个基本的检索工具:

    from langchain.tools.retriever import create_retriever_tool
    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>", # Atlas cluster or local deployment URI
    namespace = "<database-name>.<collection-name>", # Database and collection name
    embedding = VoyageAIEmbeddings(), # Embedding model to use
    index_name = "vector_index", # Name of the vector search index
    # Other optional parameters...
    )
    # Create a retrieval tool
    retriever = vector_store.as_retriever()
    retriever_tool = create_retriever_tool(
    retriever,
    "vector_search_retriever", # Tool name
    "Retrieve relevant documents from the collection" # Tool description
    )
  2. 要在 LangGraph 中添加工具作为节点,请执行以下操作:

    1. 将工具转换为节点。

    2. 将节点添加到图中。

    from langgraph.graph import StateGraph
    from langgraph.prebuilt import ToolNode
    # Define the graph
    workflow = StateGraph()
    # Convert the retriever tool into a node
    retriever_node = ToolNode([retriever_tool])
    # Add the tool as a node in the graph
    workflow.add_node("vector_search_retriever", retriever_node)
    graph = workflow.compile()

MongoDB Checkpointer 允许您在 MongoDB 中持久化代理的状态。此功能为您的 LangGraph 代理实现人工干预循环、记忆存储、时间回溯和容错机制。

MongoDB Checkpointer 允许您在 MongoDB 中持久化代理的状态。此功能为您的 LangGraph 代理实现人工干预循环、记忆存储、时间回溯和容错机制。

import { MongoDBSaver } from "@langchain/langgraph-checkpoint-mongodb";
import { MongoClient } from "mongodb";
// Connect to your Atlas cluster or local Atlas deployment
const client = new MongoClient("<connection-string>");
// Initialize the MongoDB checkpointer
const checkpointer = new MongoDBSaver(client);
// Instantiate the graph with the checkpointer
const app = graph.compile(checkpointer);
from langgraph.checkpoint.mongodb import MongoDBSaver
from pymongo import MongoClient
# Connect to your Atlas cluster or local Atlas deployment
client = MongoClient("<connection-string>")
# Initialize the MongoDB checkpointer
checkpointer = MongoDBSaver(client)
# Instantiate the graph with the checkpointer
app = graph.compile(checkpointer=checkpointer)

后退

LangChain JS/TS