@mastra/rag 2.0.0-beta.4 → 2.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,217 @@
1
+ > Guide on graph-based retrieval in Mastra
2
+
3
+ # GraphRAG
4
+
5
+ Graph-based retrieval enhances traditional vector search by following relationships between chunks of information. This approach is useful when information is spread across multiple documents or when documents reference each other.
6
+
7
+ ## When to use GraphRAG
8
+
9
+ GraphRAG is particularly effective when:
10
+
11
+ - Information is spread across multiple documents
12
+ - Documents reference each other
13
+ - You need to traverse relationships to find complete answers
14
+ - Understanding connections between concepts is important
15
+ - Simple vector similarity misses important contextual relationships
16
+
17
+ For straightforward semantic search without relationship traversal, use [standard retrieval methods](https://mastra.ai/docs/v1/rag/retrieval).
18
+
19
+ ## How GraphRAG works
20
+
21
+ GraphRAG combines vector similarity with knowledge graph traversal:
22
+
23
+ 1. Initial vector search retrieves relevant chunks based on semantic similarity
24
+ 2. A knowledge graph is constructed from the retrieved chunks
25
+ 3. The graph is traversed to find connected information
26
+ 4. Results include both directly relevant chunks and related content
27
+
28
+ This process helps surface information that might not be semantically similar to the query but is contextually relevant through connections.
29
+
30
+ ## Creating a graph query tool
31
+
32
+ The Graph Query Tool provides agents with the ability to perform graph-based retrieval:
33
+
34
+ ```ts
35
+ import { createGraphRAGTool } from "@mastra/rag";
36
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
37
+
38
+ const graphQueryTool = createGraphRAGTool({
39
+ vectorStoreName: "pgVector",
40
+ indexName: "embeddings",
41
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
42
+ graphOptions: {
43
+ threshold: 0.7,
44
+ },
45
+ });
46
+ ```
47
+
48
+ ### Configuration options
49
+
50
+ The `graphOptions` parameter controls how the knowledge graph is built and traversed:
51
+
52
+ - `threshold`: Similarity threshold (0-1) for determining which chunks are related. Higher values create sparser graphs with stronger connections; lower values create denser graphs with more potential relationships.
53
+ - `dimension`: Vector embedding dimension. Must match the embedding model's output dimension (e.g., 1536 for OpenAI's text-embedding-3-small).
54
+
55
+ ```ts
56
+ const graphQueryTool = createGraphRAGTool({
57
+ vectorStoreName: "pgVector",
58
+ indexName: "embeddings",
59
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
60
+ graphOptions: {
61
+ dimension: 1536,
62
+ threshold: 0.7,
63
+ },
64
+ });
65
+ ```
66
+
67
+ ## Using GraphRAG with agents
68
+
69
+ Integrate the graph query tool with an agent to enable graph-based retrieval:
70
+
71
+ ```ts
72
+ import { Agent } from "@mastra/core/agent";
73
+
74
+ const ragAgent = new Agent({
75
+ id: "rag-agent",
76
+ name: "GraphRAG Agent",
77
+ instructions: `You are a helpful assistant that answers questions based on the provided context.
78
+ When answering questions, use the graph query tool to find relevant information and relationships.
79
+ Base your answers on the context provided by the tool, and clearly state if the context doesn't contain enough information.`,
80
+ model: "openai/gpt-5.1",
81
+ tools: {
82
+ graphQueryTool,
83
+ },
84
+ });
85
+ ```
86
+
87
+ ## Document processing and storage
88
+
89
+ Before using graph-based retrieval, process documents into chunks and store their embeddings:
90
+
91
+ ```ts
92
+ import { MDocument } from "@mastra/rag";
93
+ import { embedMany } from "ai";
94
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
95
+
96
+ // Create and chunk document
97
+ const doc = MDocument.fromText("Your document content here...");
98
+
99
+ const chunks = await doc.chunk({
100
+ strategy: "recursive",
101
+ size: 512,
102
+ overlap: 50,
103
+ separator: "\n",
104
+ });
105
+
106
+ // Generate embeddings
107
+ const { embeddings } = await embedMany({
108
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
109
+ values: chunks.map((chunk) => chunk.text),
110
+ });
111
+
112
+ // Store in vector database
113
+ const vectorStore = mastra.getVector("pgVector");
114
+ await vectorStore.createIndex({
115
+ indexName: "embeddings",
116
+ dimension: 1536,
117
+ });
118
+ await vectorStore.upsert({
119
+ indexName: "embeddings",
120
+ vectors: embeddings,
121
+ metadata: chunks?.map((chunk) => ({ text: chunk.text })),
122
+ });
123
+ ```
124
+
125
+ ## Querying with GraphRAG
126
+
127
+ Once configured, the agent can perform graph-based queries:
128
+
129
+ ```ts
130
+ const query = "What are the effects of infrastructure changes on local businesses?";
131
+ const response = await ragAgent.generate(query);
132
+ console.log(response.text);
133
+ ```
134
+
135
+ The agent uses the graph query tool to:
136
+
137
+ 1. Convert the query to an embedding
138
+ 2. Find semantically similar chunks in the vector store
139
+ 3. Build a knowledge graph from related chunks
140
+ 4. Traverse the graph to find connected information
141
+ 5. Return comprehensive context for generating the response
142
+
143
+ ## Choosing the right threshold
144
+
145
+ The threshold parameter significantly impacts retrieval quality:
146
+
147
+ - **High threshold (0.8-0.9)**: Strict connections, fewer relationships, more precise but potentially incomplete results
148
+ - **Medium threshold (0.6-0.8)**: Balanced approach, good for most use cases
149
+ - **Low threshold (0.4-0.6)**: More connections, broader context, risk of including less relevant information
150
+
151
+ Start with 0.7 and adjust based on your specific use case:
152
+
153
+ ```ts
154
+ // Strict connections for precise answers
155
+ const strictGraphTool = createGraphRAGTool({
156
+ vectorStoreName: "pgVector",
157
+ indexName: "embeddings",
158
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
159
+ graphOptions: {
160
+ threshold: 0.85,
161
+ },
162
+ });
163
+
164
+ // Broader connections for exploratory queries
165
+ const broadGraphTool = createGraphRAGTool({
166
+ vectorStoreName: "pgVector",
167
+ indexName: "embeddings",
168
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
169
+ graphOptions: {
170
+ threshold: 0.5,
171
+ },
172
+ });
173
+ ```
174
+
175
+ ## Combining with other retrieval methods
176
+
177
+ GraphRAG can be used alongside other retrieval approaches:
178
+
179
+ ```ts
180
+ import { createVectorQueryTool } from "@mastra/rag";
181
+
182
+ const vectorQueryTool = createVectorQueryTool({
183
+ vectorStoreName: "pgVector",
184
+ indexName: "embeddings",
185
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
186
+ });
187
+
188
+ const graphQueryTool = createGraphRAGTool({
189
+ vectorStoreName: "pgVector",
190
+ indexName: "embeddings",
191
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
192
+ graphOptions: {
193
+ threshold: 0.7,
194
+ },
195
+ });
196
+
197
+ const agent = new Agent({
198
+ id: "rag-agent",
199
+ name: "RAG Agent",
200
+ instructions: `Use vector search for simple fact-finding queries.
201
+ Use graph search when you need to understand relationships or find connected information.`,
202
+ model: "openai/gpt-5.1",
203
+ tools: {
204
+ vectorQueryTool,
205
+ graphQueryTool,
206
+ },
207
+ });
208
+ ```
209
+
210
+ This gives the agent flexibility to choose the appropriate retrieval method based on the query.
211
+
212
+ ## Reference
213
+
214
+ For detailed API documentation, see:
215
+
216
+ - [GraphRAG Class](https://mastra.ai/reference/v1/rag/graph-rag)
217
+ - [createGraphRAGTool()](https://mastra.ai/reference/v1/tools/graph-rag-tool)