MongoDB を LangGraph.jsと統合することができますAI エージェントと高度な RAG アプリケーションを構築できます。このページでは、MongoDB LangGraph.js 統合の概要と、LangGraph ワークフローでの検索とエージェント状態の永続化の両方に MongoDB を使用する方法について説明します。
注意
Python統合については、 LgGraph を参照してください。
バックグラウンド
LangGraph は、LangChain エコシステム内でAIエージェントと複雑なマルチエージェントワークフローを構築するために設計された専用のフレームワークです。グラフは、エージェントのワークフローを表す LangGraph の主要コンポーネントです。MongoDB LangGraph 統合により、次の機能が有効になります。
検索ツール: MongoDB LangChain 統合により、LangGraph ワークフロー用の検索ツールを素早く作成できます。
MongoDB チェックポインター: MongoDB 内に LangGraph エージェントの状態を永続化し、会話メモリとフォールトトレランスを提供できます。
LangGraph アプリケーションを MongoDB と統合すると、検索機能とエージェント状態の永続性の両方を単一のデータベースに統合できるため、アーキテクチャを簡素化し、運用の複雑さを軽減できます。
検索ツール
レトリーバーを LangGraph ワークフローのツールとしてシームレスに使用して、Atlasから関連データを検索できます。
使用法
Atlas 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 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 }, ); LangGraph にツールをノードとして追加するには、次のように行います。
ツールをノードに変換します。
ノードをグラフに追加します。
import { StateGraph } from "@langchain/langgraph"; import { ToolNode } from "@langchain/langgraph/prebuilt"; // Convert the retriever tool into a node const 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();
MongoDB チェックポインター
MongoDB チェックポインターを使用すると、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);