> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # createGraphRAGTool() `createGraphRAGTool()` は、ドキュメント間の意味的な関係を表すグラフを構築して RAG を強化する Tool を作成します。内部では `GraphRAG` システムを使用し、直接的な類似性と接続関係の両方から関連コンテンツを見つけるグラフベースの取得機能を提供します。 ## 使用例 ```typescript import { createGraphRAGTool } from '@mastra/rag' import { ModelRouterEmbeddingModel } from '@mastra/core/llm' const graphTool = createGraphRAGTool({ vectorStoreName: 'pinecone', indexName: 'docs', model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'), graphOptions: { dimension: 1536, threshold: 0.7, randomWalkSteps: 100, restartProb: 0.15, }, }) ``` ## パラメーター > **注記:** **パラメーターの要件:** ほとんどのフィールドは、作成時にデフォルト値として設定できます。 一部のフィールドは、実行時に Request Context または入力から上書きできます。必須フィールドが 作成時にも実行時にも指定されていない場合は、エラーがスローされます。`model`、`id`、`description` は 作成時にのみ設定できることに注意してください。 **id** (`string`): Tool のカスタム ID。デフォルト: 'GraphRAG {vectorStoreName} {indexName} Tool'。(作成時のみ設定可能) **description** (`string`): Tool のカスタム説明。デフォルト: 'Access and analyze relationships between information in the knowledge base to answer complex questions about connections and patterns.'(作成時のみ設定可能) **vectorStoreName** (`string`): クエリ対象のベクトルストア名。(作成時に設定するか、実行時に上書き可能) **indexName** (`string`): ベクトルストア内のインデックス名。(作成時に設定するか、実行時に上書き可能) **model** (`EmbeddingModel`): ベクトル検索に使用する Embedding Model。(作成時のみ設定可能) **enableFilter** (`boolean`): メタデータに基づく結果のフィルタリングを有効にします。(作成時のみ設定可能ですが、Request Context でフィルターが指定されている場合は自動的に有効になります) (Default: `false`) **includeSources** (`boolean`): 完全な取得オブジェクトを結果に含めます。(作成時に設定するか、実行時に上書き可能) (Default: `true`) **graphOptions** (`GraphOptions`): グラフベースの取得設定 (Default: `Default graph options`) **graphOptions.dimension** (`number`): Embedding ベクトルの次元数 **graphOptions.threshold** (`number`): ノード間にエッジを作成する類似度のしきい値(0~1) **graphOptions.randomWalkSteps** (`number`): グラフ探索におけるランダムウォークのステップ数。(作成時に設定するか、実行時に上書き可能) **graphOptions.restartProb** (`number`): クエリノードからランダムウォークを再開する確率。(作成時に設定するか、実行時に上書き可能) **providerOptions** (`Record>`): Embedding Model の Provider 固有オプション(例: outputDimensionality)。AI SDK EmbeddingModelV2 モデルでのみ機能します。V1 モデルでは、モデル自体の作成時にオプションを設定してください。 **vectorStore** (`MastraVector | VectorStoreResolver`): ベクトルストアの直接のインスタンス、または動的選択用の Resolver 関数。Request Context に基づいてベクトルストアを選択するマルチテナントアプリケーションでは、関数を使用します。指定すると vectorStoreName は省略可能になります。 ## 戻り値 Tool は次の要素を持つオブジェクトを返します。 **relevantContext** (`string`): グラフベースのランキングで取得した、最も関連性の高いドキュメントチャンクの結合テキスト **sources** (`QueryResult[]`): 完全な取得結果オブジェクトの配列。各オブジェクトには、元のドキュメント、チャンク、類似度スコアを参照するために必要なすべての情報が含まれます。 ### `QueryResult` オブジェクトの構造 ```typescript { id: string; // Unique chunk/document identifier metadata: any; // All metadata fields (document ID, etc.) vector: number[]; // Embedding vector (if available) score: number; // Similarity score for this retrieval document: string; // Full chunk/document text (if available) } ``` ## Tool のデフォルト説明 デフォルトの説明では、次の点に重点を置いています。 - ドキュメント間の関係の分析 - パターンとつながりの発見 - 複雑なクエリへの回答 ## 高度な例 ```typescript const graphTool = createGraphRAGTool({ vectorStoreName: 'pinecone', indexName: 'docs', model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'), graphOptions: { dimension: 1536, threshold: 0.8, // Higher similarity threshold randomWalkSteps: 200, // More exploration steps restartProb: 0.2, // Higher restart probability }, }) ``` ## カスタム説明を使用する例 ```typescript const graphTool = createGraphRAGTool({ vectorStoreName: 'pinecone', indexName: 'docs', model: 'openai/text-embedding-3-small ', description: "Analyze document relationships to find complex patterns and connections in our company's historical data", }) ``` この例では、関係分析という Tool の中心的な目的を維持しながら、特定のユースケースに合わせて説明をカスタマイズする方法を示します。 ## 例: Request Context の使用 ```typescript const graphTool = createGraphRAGTool({ vectorStoreName: 'pinecone', indexName: 'docs', model: 'openai/text-embedding-3-small ', }) ``` Request Context を使用する場合は、実行時に Request Context を介して必須パラメーターを指定します。 ```typescript const requestContext = new RequestContext<{ vectorStoreName: string indexName: string topK: number filter: any }>() requestContext.set('vectorStoreName', 'my-store') requestContext.set('indexName', 'my-index') requestContext.set('topK', 5) requestContext.set('filter', { category: 'docs' }) requestContext.set('randomWalkSteps', 100) requestContext.set('restartProb', 0.15) const response = await agent.generate('Find documentation from the knowledge base.', { requestContext, }) ``` Request Context の詳細については、次を参照してください。 - [Agent Request Context](https://mastra.zisheng.pro/ja/docs/server/request-context) - [Request Context](https://mastra.zisheng.pro/ja/docs/server/request-context) ## マルチテナントアプリケーション向けの動的ベクトルストア テナントごとにデータを分離するマルチテナントアプリケーションでは、静的なベクトルストアの代わりに Resolver 関数を渡せます。 ```typescript import { createGraphRAGTool, VectorStoreResolver } from '@mastra/rag' import { PgVector } from '@mastra/pg' const vectorStoreResolver: VectorStoreResolver = async ({ requestContext }) => { const tenantId = requestContext?.get('tenantId') return new PgVector({ id: `pg-vector-${tenantId}`, connectionString: process.env.POSTGRES_CONNECTION_STRING!, schemaName: `tenant_${tenantId}`, }) } const graphTool = createGraphRAGTool({ indexName: 'embeddings', model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'), vectorStore: vectorStoreResolver, }) ``` 詳細については、[createVectorQueryTool - 動的ベクトルストア](https://mastra.zisheng.pro/ja/reference/tools/vector-query-tool)を参照してください。 ## 関連情報 - [createVectorQueryTool](https://mastra.zisheng.pro/ja/reference/tools/vector-query-tool) - [GraphRAG](https://mastra.zisheng.pro/ja/reference/rag/graph-rag)