createGraphRAGTool()
The createGraphRAGTool()
creates a tool that enhances RAG by building a graph of semantic relationships between documents. It uses the GraphRAG
system under the hood to provide graph-based retrieval, finding relevant content through both direct similarity and connected relationships.
Usage Example
import { createGraphRAGTool } from "@mastra/rag";
const graphTool = createGraphRAGTool({
vectorStoreName: "pinecone",
indexName: "docs",
options: {
provider: "OPEN_AI",
model: "text-embedding-ada-002",
maxRetries: 3
},
topK: 5,
graphOptions: {
dimension: 1536,
threshold: 0.7,
randomWalkSteps: 100,
restartProb: 0.15
}
});
Parameters
vectorStoreName:
string
Name of the vector store to query
indexName:
string
Name of the index within the vector store
options:
EmbeddingOptions
Configuration for embedding generation
topK?:
number
Maximum number of results to retrieve
graphOptions?:
GraphOptions
Configuration for the graph-based retrieval
GraphOptions
dimension?:
number
Dimension of the embedding vectors
threshold?:
number
Similarity threshold for creating edges between nodes (0-1)
randomWalkSteps?:
number
Number of steps in random walk for graph traversal
restartProb?:
number
Probability of restarting random walk from query node
Returns
The tool returns an object with:
relevantContext:
string
Combined text from the most relevant document chunks, retrieved using graph-based ranking
Advanced Example
const graphTool = createGraphRAGTool({
vectorStoreName: "pinecone",
indexName: "docs",
options: {
provider: "OPEN_AI",
model: "text-embedding-ada-002",
maxRetries: 3
},
topK: 5,
graphOptions: {
dimension: 1536,
threshold: 0.8, // Higher similarity threshold
randomWalkSteps: 200, // More exploration steps
restartProb: 0.2 // Higher restart probability
}
});