MongoDB をLgGraph.js と統合して、 AIエージェントと高度な RAG アプリケーションを構築できます。このページでは、 MongoDB LgGraph.js 統合の概要と、LgGraph ワークフローでエージェント状態の永続性と取得にMongoDBを使用する方法について説明します。
注意
Python統合については、 LgGraph を参照してください。
バックグラウンド
LangGraph は、LangChain エコシステム内でAIエージェントと複雑なマルチエージェントワークフローを構築するために設計された専用のフレームワークです。グラフは、エージェントのワークフローを表す LangGraph の主要コンポーネントです。MongoDB LangGraph 統合により、次の機能が有効になります。
MongoDB LingGraph チェックポイント : MongoDBで LingGraph エージェントの状態を永続化し、短期間のメモリ を提供できます。
検索ツール: MongoDB LangChain 統合により、LangGraph ワークフロー用の検索ツールを素早く作成できます。
LgGraph アプリケーションをMongoDBと統合すると、取得機能とエージェントメモリの両方を 1 つのデータベースに統合 でき、アーキテクチャが簡素化され、運用の複雑さが軽減されます。
MongoDB LingGraph チェックポイント(短期間メモリ)
MongoDB LingGraph チェックポイントを使用すると、 MongoDBでエージェントの状態を永続化し、 短期間メモリを実装できます。この機能により、LingGraph エージェントのループ内、メモリ、時間移動、フォールトトレランスが可能になります。
このコンポーネントのパッケージをインストールするには:
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);検索ツール
LingGraph ワークフローのツールとしてリツールをシームレスに使用して、 MongoDBから関連データを取得できます。
使用法
MongoDB ベクトル検索と LgDB を使用して基本的な検索ツールを作成するには、次の手順に従います。
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 },);LangGraph にツールをノードとして追加するには、次のように行います。
ツールをノードに変換します。
ノードをグラフに追加します。
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();