createGraphRAGTool()
createGraphRAGTool() は、ドキュメント間の意味的な関係を表すグラフを構築して RAG を強化する Tool を作成します。内部では GraphRAG システムを使用し、直接的な類似性と接続関係の両方から関連コンテンツを見つけるグラフベースの取得機能を提供します。
使用例使用例への直接リンク
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
= false
メタデータに基づく結果のフィルタリングを有効にします。(作成時のみ設定可能ですが、Request Context でフィルターが指定されている場合は自動的に有効になります)
includeSources?:
boolean
= true
完全な取得オブジェクトを結果に含めます。(作成時に設定するか、実行時に上書き可能)
graphOptions?:
GraphOptions
= Default graph options
グラフベースの取得設定
GraphOptions
dimension?:
number
Embedding ベクトルの次元数
threshold?:
number
ノード間にエッジを作成する類似度のしきい値(0~1)
randomWalkSteps?:
number
グラフ探索におけるランダムウォークのステップ数。(作成時に設定するか、実行時に上書き可能)
restartProb?:
number
クエリノードからランダムウォークを再開する確率。(作成時に設定するか、実行時に上書き可能)
providerOptions?:
Record<string, Record<string, any>>
Embedding Model の Provider 固有オプション(例: outputDimensionality)。AI SDK EmbeddingModelV2 モデルでのみ機能します。V1 モデルでは、モデル自体の作成時にオプションを設定してください。
vectorStore?:
MastraVector | VectorStoreResolver
ベクトルストアの直接のインスタンス、または動的選択用の Resolver 関数。Request Context に基づいてベクトルストアを選択するマルチテナントアプリケーションでは、関数を使用します。指定すると
vectorStoreName は省略可能になります。戻り値戻り値への直接リンク
Tool は次の要素を持つオブジェクトを返します。
relevantContext:
string
グラフベースのランキングで取得した、最も関連性の高いドキュメントチャンクの結合テキスト
sources:
QueryResult[]
完全な取得結果オブジェクトの配列。各オブジェクトには、元のドキュメント、チャンク、類似度スコアを参照するために必要なすべての情報が含まれます。
QueryResult オブジェクトの構造queryresult-object-structureへの直接リンク
{
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 のデフォルト説明Tool のデフォルト説明への直接リンク
デフォルトの説明では、次の点に重点を置いています。
- ドキュメント間の関係の分析
- パターンとつながりの発見
- 複雑なクエリへの回答
高度な例高度な例への直接リンク
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
},
})
カスタム説明を使用する例カスタム説明を使用する例への直接リンク
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 の使用例: Request Context の使用への直接リンク
const graphTool = createGraphRAGTool({
vectorStoreName: 'pinecone',
indexName: 'docs',
model: 'openai/text-embedding-3-small ',
})
Request Context を使用する場合は、実行時に Request Context を介して必須パラメーターを指定します。
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 の詳細については、次を参照してください。
マルチテナントアプリケーション向けの動的ベクトルストアマルチテナントアプリケーション向けの動的ベクトルストアへの直接リンク
テナントごとにデータを分離するマルチテナントアプリケーションでは、静的なベクトルストアの代わりに Resolver 関数を渡せます。
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 - 動的ベクトルストアを参照してください。