@mastra/rag 2.1.1-alpha.0 → 2.1.2-alpha.0
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.
- package/CHANGELOG.md +18 -0
- package/dist/docs/SKILL.md +38 -0
- package/dist/docs/assets/SOURCE_MAP.json +6 -0
- package/dist/docs/references/docs-rag-chunking-and-embedding.md +183 -0
- package/dist/docs/references/docs-rag-graph-rag.md +215 -0
- package/dist/docs/references/docs-rag-overview.md +72 -0
- package/dist/docs/references/docs-rag-retrieval.md +515 -0
- package/dist/docs/references/reference-rag-chunk.md +221 -0
- package/dist/docs/references/reference-rag-database-config.md +261 -0
- package/dist/docs/references/reference-rag-document.md +114 -0
- package/dist/docs/references/reference-rag-extract-params.md +168 -0
- package/dist/docs/references/reference-rag-graph-rag.md +111 -0
- package/dist/docs/references/reference-rag-rerank.md +75 -0
- package/dist/docs/references/reference-rag-rerankWithScorer.md +80 -0
- package/dist/docs/references/reference-tools-document-chunker-tool.md +89 -0
- package/dist/docs/references/reference-tools-graph-rag-tool.md +182 -0
- package/dist/docs/references/reference-tools-vector-query-tool.md +459 -0
- package/dist/document/transformers/semantic-markdown.d.ts +6 -4
- package/dist/document/transformers/semantic-markdown.d.ts.map +1 -1
- package/dist/document/transformers/token.d.ts +5 -4
- package/dist/document/transformers/token.d.ts.map +1 -1
- package/dist/index.cjs +41 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +41 -26
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @mastra/rag
|
|
2
2
|
|
|
3
|
+
## 2.1.2-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Improved token-based chunking performance in `token` and `semantic-markdown` strategies. Markdown knowledge bases now chunk significantly faster with lower tokenization overhead. ([#13495](https://github.com/mastra-ai/mastra/pull/13495))
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`df170fd`](https://github.com/mastra-ai/mastra/commit/df170fd139b55f845bfd2de8488b16435bd3d0da), [`ae55343`](https://github.com/mastra-ai/mastra/commit/ae5534397fc006fd6eef3e4f80c235bcdc9289ef), [`c290cec`](https://github.com/mastra-ai/mastra/commit/c290cec5bf9107225de42942b56b487107aa9dce), [`f03e794`](https://github.com/mastra-ai/mastra/commit/f03e794630f812b56e95aad54f7b1993dc003add), [`aa4a5ae`](https://github.com/mastra-ai/mastra/commit/aa4a5aedb80d8d6837bab8cbb2e301215d1ba3e9), [`de3f584`](https://github.com/mastra-ai/mastra/commit/de3f58408752a8d80a295275c7f23fc306cf7f4f), [`d3fb010`](https://github.com/mastra-ai/mastra/commit/d3fb010c98f575f1c0614452667396e2653815f6), [`702ee1c`](https://github.com/mastra-ai/mastra/commit/702ee1c41be67cc532b4dbe89bcb62143508f6f0), [`f495051`](https://github.com/mastra-ai/mastra/commit/f495051eb6496a720f637fc85b6d69941c12554c), [`e622f1d`](https://github.com/mastra-ai/mastra/commit/e622f1d3ab346a8e6aca6d1fe2eac99bd961e50b), [`861f111`](https://github.com/mastra-ai/mastra/commit/861f11189211b20ddb70d8df81a6b901fc78d11e), [`00f43e8`](https://github.com/mastra-ai/mastra/commit/00f43e8e97a80c82b27d5bd30494f10a715a1df9), [`1b6f651`](https://github.com/mastra-ai/mastra/commit/1b6f65127d4a0d6c38d0a1055cb84527db529d6b), [`96a1702`](https://github.com/mastra-ai/mastra/commit/96a1702ce362c50dda20c8b4a228b4ad1a36a17a), [`cb9f921`](https://github.com/mastra-ai/mastra/commit/cb9f921320913975657abb1404855d8c510f7ac5), [`114e7c1`](https://github.com/mastra-ai/mastra/commit/114e7c146ac682925f0fb37376c1be70e5d6e6e5), [`1b6f651`](https://github.com/mastra-ai/mastra/commit/1b6f65127d4a0d6c38d0a1055cb84527db529d6b), [`72df4a8`](https://github.com/mastra-ai/mastra/commit/72df4a8f9bf1a20cfd3d9006a4fdb597ad56d10a)]:
|
|
10
|
+
- @mastra/core@1.8.0-alpha.0
|
|
11
|
+
|
|
12
|
+
## 2.1.1
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- dependencies updates: ([#13318](https://github.com/mastra-ai/mastra/pull/13318))
|
|
17
|
+
- Updated dependency [`zeroentropy@0.1.0-alpha.7` ↗︎](https://www.npmjs.com/package/zeroentropy/v/0.1.0) (from `0.1.0-alpha.6`, in `dependencies`)
|
|
18
|
+
- Updated dependencies [[`24284ff`](https://github.com/mastra-ai/mastra/commit/24284ffae306ddf0ab83273e13f033520839ef40), [`f5097cc`](https://github.com/mastra-ai/mastra/commit/f5097cc8a813c82c3378882c31178320cadeb655), [`71e237f`](https://github.com/mastra-ai/mastra/commit/71e237fa852a3ad9a50a3ddb3b5f3b20b9a8181c), [`13a291e`](https://github.com/mastra-ai/mastra/commit/13a291ebb9f9bca80befa0d9166b916bb348e8e9), [`397af5a`](https://github.com/mastra-ai/mastra/commit/397af5a69f34d4157f51a7c8da3f1ded1e1d611c), [`d4701f7`](https://github.com/mastra-ai/mastra/commit/d4701f7e24822b081b70f9c806c39411b1a712e7), [`2b40831`](https://github.com/mastra-ai/mastra/commit/2b40831dcca2275c9570ddf09b7f25ba3e8dc7fc), [`6184727`](https://github.com/mastra-ai/mastra/commit/6184727e812bf7a65cee209bacec3a2f5a16e923), [`0c338b8`](https://github.com/mastra-ai/mastra/commit/0c338b87362dcd95ff8191ca00df645b6953f534), [`6f6385b`](https://github.com/mastra-ai/mastra/commit/6f6385be5b33687cd21e71fc27e972e6928bb34c), [`14aba61`](https://github.com/mastra-ai/mastra/commit/14aba61b9cff76d72bc7ef6f3a83ae2c5d059193), [`dd9dd1c`](https://github.com/mastra-ai/mastra/commit/dd9dd1c9ae32ae79093f8c4adde1732ac6357233)]:
|
|
19
|
+
- @mastra/core@1.7.0
|
|
20
|
+
|
|
3
21
|
## 2.1.1-alpha.0
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mastra-rag
|
|
3
|
+
description: Documentation for @mastra/rag. Use when working with @mastra/rag APIs, configuration, or implementation.
|
|
4
|
+
metadata:
|
|
5
|
+
package: "@mastra/rag"
|
|
6
|
+
version: "2.1.2-alpha.0"
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## When to use
|
|
10
|
+
|
|
11
|
+
Use this skill whenever you are working with @mastra/rag to obtain the domain-specific knowledge.
|
|
12
|
+
|
|
13
|
+
## How to use
|
|
14
|
+
|
|
15
|
+
Read the individual reference documents for detailed explanations and code examples.
|
|
16
|
+
|
|
17
|
+
### Docs
|
|
18
|
+
|
|
19
|
+
- [Chunking and Embedding Documents](references/docs-rag-chunking-and-embedding.md) - Guide on chunking and embedding documents in Mastra for efficient processing and retrieval.
|
|
20
|
+
- [GraphRAG](references/docs-rag-graph-rag.md) - Guide on graph-based retrieval in Mastra's RAG systems for documents with complex relationships.
|
|
21
|
+
- [RAG (Retrieval-Augmented Generation) in Mastra](references/docs-rag-overview.md) - Overview of Retrieval-Augmented Generation (RAG) in Mastra, detailing its capabilities for enhancing LLM outputs with relevant context.
|
|
22
|
+
- [Retrieval, Semantic Search, Reranking](references/docs-rag-retrieval.md) - Guide on retrieval processes in Mastra's RAG systems, including semantic search, filtering, and re-ranking.
|
|
23
|
+
|
|
24
|
+
### Reference
|
|
25
|
+
|
|
26
|
+
- [Reference: .chunk()](references/reference-rag-chunk.md) - Documentation for the chunk function in Mastra, which splits documents into smaller segments using various strategies.
|
|
27
|
+
- [Reference: DatabaseConfig](references/reference-rag-database-config.md) - API reference for database-specific configuration types used with vector query tools in Mastra RAG systems.
|
|
28
|
+
- [Reference: MDocument](references/reference-rag-document.md) - Documentation for the MDocument class in Mastra, which handles document processing and chunking.
|
|
29
|
+
- [Reference: ExtractParams](references/reference-rag-extract-params.md) - Documentation for metadata extraction configuration in Mastra.
|
|
30
|
+
- [Reference: GraphRAG](references/reference-rag-graph-rag.md) - Documentation for the GraphRAG class in Mastra, which implements a graph-based approach to retrieval augmented generation.
|
|
31
|
+
- [Reference: rerank()](references/reference-rag-rerank.md) - Documentation for the rerank function in Mastra, which provides advanced reranking capabilities for vector search results.
|
|
32
|
+
- [Reference: rerankWithScorer()](references/reference-rag-rerankWithScorer.md) - Documentation for the rerank function in Mastra, which provides advanced reranking capabilities for vector search results.
|
|
33
|
+
- [Reference: createDocumentChunkerTool()](references/reference-tools-document-chunker-tool.md) - Documentation for the Document Chunker Tool in Mastra, which splits documents into smaller chunks for efficient processing and retrieval.
|
|
34
|
+
- [Reference: createGraphRAGTool()](references/reference-tools-graph-rag-tool.md) - Documentation for the GraphRAG Tool in Mastra, which enhances RAG by building a graph of semantic relationships between documents.
|
|
35
|
+
- [Reference: createVectorQueryTool()](references/reference-tools-vector-query-tool.md) - Documentation for the Vector Query Tool in Mastra, which facilitates semantic search over vector stores with filtering and reranking capabilities.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
Read [assets/SOURCE_MAP.json](assets/SOURCE_MAP.json) for source code references.
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# Chunking and Embedding Documents
|
|
2
|
+
|
|
3
|
+
Before processing, create a MDocument instance from your content. You can initialize it from various formats:
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
const docFromText = MDocument.fromText('Your plain text content...')
|
|
7
|
+
const docFromHTML = MDocument.fromHTML('<html>Your HTML content...</html>')
|
|
8
|
+
const docFromMarkdown = MDocument.fromMarkdown('# Your Markdown content...')
|
|
9
|
+
const docFromJSON = MDocument.fromJSON(`{ "key": "value" }`)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Step 1: Document Processing
|
|
13
|
+
|
|
14
|
+
Use `chunk` to split documents into manageable pieces. Mastra supports multiple chunking strategies optimized for different document types:
|
|
15
|
+
|
|
16
|
+
- `recursive`: Smart splitting based on content structure
|
|
17
|
+
- `character`: Simple character-based splits
|
|
18
|
+
- `token`: Token-aware splitting
|
|
19
|
+
- `markdown`: Markdown-aware splitting
|
|
20
|
+
- `semantic-markdown`: Markdown splitting based on related header families
|
|
21
|
+
- `html`: HTML structure-aware splitting
|
|
22
|
+
- `json`: JSON structure-aware splitting
|
|
23
|
+
- `latex`: LaTeX structure-aware splitting
|
|
24
|
+
- `sentence`: Sentence-aware splitting
|
|
25
|
+
|
|
26
|
+
> **Note:** Each strategy accepts different parameters optimized for its chunking approach.
|
|
27
|
+
|
|
28
|
+
Here's an example of how to use the `recursive` strategy:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
const chunks = await doc.chunk({
|
|
32
|
+
strategy: 'recursive',
|
|
33
|
+
maxSize: 512,
|
|
34
|
+
overlap: 50,
|
|
35
|
+
separators: ['\n'],
|
|
36
|
+
extract: {
|
|
37
|
+
metadata: true, // Optionally extract metadata
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
For text where preserving sentence structure is important, here's an example of how to use the `sentence` strategy:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const chunks = await doc.chunk({
|
|
46
|
+
strategy: 'sentence',
|
|
47
|
+
maxSize: 450,
|
|
48
|
+
minSize: 50,
|
|
49
|
+
overlap: 0,
|
|
50
|
+
sentenceEnders: ['.'],
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
For markdown documents where preserving the semantic relationships between sections is important, here's an example of how to use the `semantic-markdown` strategy:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
const chunks = await doc.chunk({
|
|
58
|
+
strategy: 'semantic-markdown',
|
|
59
|
+
joinThreshold: 500,
|
|
60
|
+
modelName: 'gpt-3.5-turbo',
|
|
61
|
+
})
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
> **Note:** Metadata extraction may use LLM calls, so ensure your API key is set.
|
|
65
|
+
|
|
66
|
+
We go deeper into chunking strategies in our [`chunk()` reference documentation](https://mastra.ai/reference/rag/chunk).
|
|
67
|
+
|
|
68
|
+
## Step 2: Embedding Generation
|
|
69
|
+
|
|
70
|
+
Transform chunks into embeddings using your preferred provider. Mastra supports embedding models through the model router.
|
|
71
|
+
|
|
72
|
+
### Using the Model Router
|
|
73
|
+
|
|
74
|
+
The simplest way is to use Mastra's model router with `provider/model` strings:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
78
|
+
import { embedMany } from 'ai'
|
|
79
|
+
|
|
80
|
+
const { embeddings } = await embedMany({
|
|
81
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
82
|
+
values: chunks.map(chunk => chunk.text),
|
|
83
|
+
})
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Mastra supports OpenAI and Google embedding models. For a complete list of supported embedding models, see the [embeddings reference](https://mastra.ai/reference/rag/embeddings).
|
|
87
|
+
|
|
88
|
+
The model router automatically handles API key detection from environment variables.
|
|
89
|
+
|
|
90
|
+
The embedding functions return vectors, arrays of numbers representing the semantic meaning of your text, ready for similarity searches in your vector database.
|
|
91
|
+
|
|
92
|
+
### Configuring Embedding Dimensions
|
|
93
|
+
|
|
94
|
+
Embedding models typically output vectors with a fixed number of dimensions (e.g., 1536 for OpenAI's `text-embedding-3-small`). Some models support reducing this dimensionality, which can help:
|
|
95
|
+
|
|
96
|
+
- Decrease storage requirements in vector databases
|
|
97
|
+
- Reduce computational costs for similarity searches
|
|
98
|
+
|
|
99
|
+
Here are some supported models:
|
|
100
|
+
|
|
101
|
+
OpenAI (text-embedding-3 models):
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
105
|
+
|
|
106
|
+
const { embeddings } = await embedMany({
|
|
107
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
108
|
+
options: {
|
|
109
|
+
dimensions: 256, // Only supported in text-embedding-3 and later
|
|
110
|
+
},
|
|
111
|
+
values: chunks.map(chunk => chunk.text),
|
|
112
|
+
})
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Google (text-embedding-001):
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
const { embeddings } = await embedMany({
|
|
119
|
+
model: google('gemini-embedding-001', {
|
|
120
|
+
outputDimensionality: 256, // Truncates excessive values from the end
|
|
121
|
+
}),
|
|
122
|
+
values: chunks.map(chunk => chunk.text),
|
|
123
|
+
})
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
> **Vector Database Compatibility:** When storing embeddings, the vector database index must be configured to match the output size of your embedding model. If the dimensions do not match, you may get errors or data corruption.
|
|
127
|
+
|
|
128
|
+
## Example: Complete Pipeline
|
|
129
|
+
|
|
130
|
+
Here's an example showing document processing and embedding generation with both providers:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
import { embedMany } from 'ai'
|
|
134
|
+
|
|
135
|
+
import { MDocument } from '@mastra/rag'
|
|
136
|
+
|
|
137
|
+
// Initialize document
|
|
138
|
+
const doc = MDocument.fromText(`
|
|
139
|
+
Climate change poses significant challenges to global agriculture.
|
|
140
|
+
Rising temperatures and changing precipitation patterns affect crop yields.
|
|
141
|
+
`)
|
|
142
|
+
|
|
143
|
+
// Create chunks
|
|
144
|
+
const chunks = await doc.chunk({
|
|
145
|
+
strategy: 'recursive',
|
|
146
|
+
maxSize: 256,
|
|
147
|
+
overlap: 50,
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
// Generate embeddings with OpenAI
|
|
151
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
152
|
+
|
|
153
|
+
const { embeddings } = await embedMany({
|
|
154
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
155
|
+
values: chunks.map(chunk => chunk.text),
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
// OR
|
|
159
|
+
|
|
160
|
+
// Generate embeddings with Cohere
|
|
161
|
+
const { embeddings } = await embedMany({
|
|
162
|
+
model: 'cohere/embed-english-v3.0',
|
|
163
|
+
values: chunks.map(chunk => chunk.text),
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
// Store embeddings in your vector database
|
|
167
|
+
await vectorStore.upsert({
|
|
168
|
+
indexName: 'embeddings',
|
|
169
|
+
vectors: embeddings,
|
|
170
|
+
})
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
##
|
|
174
|
+
|
|
175
|
+
For more examples of different chunking strategies and embedding configurations, see:
|
|
176
|
+
|
|
177
|
+
- [Chunk Reference](https://mastra.ai/reference/rag/chunk)
|
|
178
|
+
- [Embeddings Reference](https://mastra.ai/reference/rag/embeddings)
|
|
179
|
+
|
|
180
|
+
For more details on vector databases and embeddings, see:
|
|
181
|
+
|
|
182
|
+
- [Vector Databases](https://mastra.ai/docs/rag/vector-databases)
|
|
183
|
+
- [Embedding API Reference](https://mastra.ai/reference/rag/embeddings)
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# GraphRAG
|
|
2
|
+
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
## When to use GraphRAG
|
|
6
|
+
|
|
7
|
+
GraphRAG is particularly effective when:
|
|
8
|
+
|
|
9
|
+
- Information is spread across multiple documents
|
|
10
|
+
- Documents reference each other
|
|
11
|
+
- You need to traverse relationships to find complete answers
|
|
12
|
+
- Understanding connections between concepts is important
|
|
13
|
+
- Simple vector similarity misses important contextual relationships
|
|
14
|
+
|
|
15
|
+
For straightforward semantic search without relationship traversal, use [standard retrieval methods](https://mastra.ai/docs/rag/retrieval).
|
|
16
|
+
|
|
17
|
+
## How GraphRAG works
|
|
18
|
+
|
|
19
|
+
GraphRAG combines vector similarity with knowledge graph traversal:
|
|
20
|
+
|
|
21
|
+
1. Initial vector search retrieves relevant chunks based on semantic similarity
|
|
22
|
+
2. A knowledge graph is constructed from the retrieved chunks
|
|
23
|
+
3. The graph is traversed to find connected information
|
|
24
|
+
4. Results include both directly relevant chunks and related content
|
|
25
|
+
|
|
26
|
+
This process helps surface information that might not be semantically similar to the query but is contextually relevant through connections.
|
|
27
|
+
|
|
28
|
+
## Creating a graph query tool
|
|
29
|
+
|
|
30
|
+
The Graph Query Tool provides agents with the ability to perform graph-based retrieval:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { createGraphRAGTool } from '@mastra/rag'
|
|
34
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
35
|
+
|
|
36
|
+
const graphQueryTool = createGraphRAGTool({
|
|
37
|
+
vectorStoreName: 'pgVector',
|
|
38
|
+
indexName: 'embeddings',
|
|
39
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
40
|
+
graphOptions: {
|
|
41
|
+
threshold: 0.7,
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Configuration options
|
|
47
|
+
|
|
48
|
+
The `graphOptions` parameter controls how the knowledge graph is built and traversed:
|
|
49
|
+
|
|
50
|
+
- `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.
|
|
51
|
+
- `dimension`: Vector embedding dimension. Must match the embedding model's output dimension (e.g., 1536 for OpenAI's text-embedding-3-small).
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
const graphQueryTool = createGraphRAGTool({
|
|
55
|
+
vectorStoreName: 'pgVector',
|
|
56
|
+
indexName: 'embeddings',
|
|
57
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
58
|
+
graphOptions: {
|
|
59
|
+
dimension: 1536,
|
|
60
|
+
threshold: 0.7,
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Using GraphRAG with agents
|
|
66
|
+
|
|
67
|
+
Integrate the graph query tool with an agent to enable graph-based retrieval:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { Agent } from '@mastra/core/agent'
|
|
71
|
+
|
|
72
|
+
const ragAgent = new Agent({
|
|
73
|
+
id: 'rag-agent',
|
|
74
|
+
name: 'GraphRAG Agent',
|
|
75
|
+
instructions: `You are a helpful assistant that answers questions based on the provided context.
|
|
76
|
+
When answering questions, use the graph query tool to find relevant information and relationships.
|
|
77
|
+
Base your answers on the context provided by the tool, and clearly state if the context doesn't contain enough information.`,
|
|
78
|
+
model: 'openai/gpt-5.1',
|
|
79
|
+
tools: {
|
|
80
|
+
graphQueryTool,
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Document processing and storage
|
|
86
|
+
|
|
87
|
+
Before using graph-based retrieval, process documents into chunks and store their embeddings:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { MDocument } from '@mastra/rag'
|
|
91
|
+
import { embedMany } from 'ai'
|
|
92
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
93
|
+
|
|
94
|
+
// Create and chunk document
|
|
95
|
+
const doc = MDocument.fromText('Your document content here...')
|
|
96
|
+
|
|
97
|
+
const chunks = await doc.chunk({
|
|
98
|
+
strategy: 'recursive',
|
|
99
|
+
size: 512,
|
|
100
|
+
overlap: 50,
|
|
101
|
+
separator: '\n',
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
// Generate embeddings
|
|
105
|
+
const { embeddings } = await embedMany({
|
|
106
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
107
|
+
values: chunks.map(chunk => chunk.text),
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
// Store in vector database
|
|
111
|
+
const vectorStore = mastra.getVector('pgVector')
|
|
112
|
+
await vectorStore.createIndex({
|
|
113
|
+
indexName: 'embeddings',
|
|
114
|
+
dimension: 1536,
|
|
115
|
+
})
|
|
116
|
+
await vectorStore.upsert({
|
|
117
|
+
indexName: 'embeddings',
|
|
118
|
+
vectors: embeddings,
|
|
119
|
+
metadata: chunks?.map(chunk => ({ text: chunk.text })),
|
|
120
|
+
})
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Querying with GraphRAG
|
|
124
|
+
|
|
125
|
+
Once configured, the agent can perform graph-based queries:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const query = 'What are the effects of infrastructure changes on local businesses?'
|
|
129
|
+
const response = await ragAgent.generate(query)
|
|
130
|
+
console.log(response.text)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
The agent uses the graph query tool to:
|
|
134
|
+
|
|
135
|
+
1. Convert the query to an embedding
|
|
136
|
+
2. Find semantically similar chunks in the vector store
|
|
137
|
+
3. Build a knowledge graph from related chunks
|
|
138
|
+
4. Traverse the graph to find connected information
|
|
139
|
+
5. Return comprehensive context for generating the response
|
|
140
|
+
|
|
141
|
+
## Choosing the right threshold
|
|
142
|
+
|
|
143
|
+
The threshold parameter significantly impacts retrieval quality:
|
|
144
|
+
|
|
145
|
+
- **High threshold (0.8-0.9)**: Strict connections, fewer relationships, more precise but potentially incomplete results
|
|
146
|
+
- **Medium threshold (0.6-0.8)**: Balanced approach, good for most use cases
|
|
147
|
+
- **Low threshold (0.4-0.6)**: More connections, broader context, risk of including less relevant information
|
|
148
|
+
|
|
149
|
+
Start with 0.7 and adjust based on your specific use case:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
// Strict connections for precise answers
|
|
153
|
+
const strictGraphTool = createGraphRAGTool({
|
|
154
|
+
vectorStoreName: 'pgVector',
|
|
155
|
+
indexName: 'embeddings',
|
|
156
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
157
|
+
graphOptions: {
|
|
158
|
+
threshold: 0.85,
|
|
159
|
+
},
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
// Broader connections for exploratory queries
|
|
163
|
+
const broadGraphTool = createGraphRAGTool({
|
|
164
|
+
vectorStoreName: 'pgVector',
|
|
165
|
+
indexName: 'embeddings',
|
|
166
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
167
|
+
graphOptions: {
|
|
168
|
+
threshold: 0.5,
|
|
169
|
+
},
|
|
170
|
+
})
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Combining with other retrieval methods
|
|
174
|
+
|
|
175
|
+
GraphRAG can be used alongside other retrieval approaches:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
import { createVectorQueryTool } from '@mastra/rag'
|
|
179
|
+
|
|
180
|
+
const vectorQueryTool = createVectorQueryTool({
|
|
181
|
+
vectorStoreName: 'pgVector',
|
|
182
|
+
indexName: 'embeddings',
|
|
183
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
const graphQueryTool = createGraphRAGTool({
|
|
187
|
+
vectorStoreName: 'pgVector',
|
|
188
|
+
indexName: 'embeddings',
|
|
189
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
190
|
+
graphOptions: {
|
|
191
|
+
threshold: 0.7,
|
|
192
|
+
},
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
const agent = new Agent({
|
|
196
|
+
id: 'rag-agent',
|
|
197
|
+
name: 'RAG Agent',
|
|
198
|
+
instructions: `Use vector search for simple fact-finding queries.
|
|
199
|
+
Use graph search when you need to understand relationships or find connected information.`,
|
|
200
|
+
model: 'openai/gpt-5.1',
|
|
201
|
+
tools: {
|
|
202
|
+
vectorQueryTool,
|
|
203
|
+
graphQueryTool,
|
|
204
|
+
},
|
|
205
|
+
})
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
This gives the agent flexibility to choose the appropriate retrieval method based on the query.
|
|
209
|
+
|
|
210
|
+
## Reference
|
|
211
|
+
|
|
212
|
+
For detailed API documentation, see:
|
|
213
|
+
|
|
214
|
+
- [GraphRAG Class](https://mastra.ai/reference/rag/graph-rag)
|
|
215
|
+
- [createGraphRAGTool()](https://mastra.ai/reference/tools/graph-rag-tool)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# RAG (Retrieval-Augmented Generation) in Mastra
|
|
2
|
+
|
|
3
|
+
RAG in Mastra helps you enhance LLM outputs by incorporating relevant context from your own data sources, improving accuracy and grounding responses in real information.
|
|
4
|
+
|
|
5
|
+
Mastra's RAG system provides:
|
|
6
|
+
|
|
7
|
+
- Standardized APIs to process and embed documents
|
|
8
|
+
- Support for multiple vector stores
|
|
9
|
+
- Chunking and embedding strategies for optimal retrieval
|
|
10
|
+
- Observability for tracking embedding and retrieval performance
|
|
11
|
+
|
|
12
|
+
## Example
|
|
13
|
+
|
|
14
|
+
To implement RAG, you process your documents into chunks, create embeddings, store them in a vector database, and then retrieve relevant context at query time.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { embedMany } from 'ai'
|
|
18
|
+
import { PgVector } from '@mastra/pg'
|
|
19
|
+
import { MDocument } from '@mastra/rag'
|
|
20
|
+
import { z } from 'zod'
|
|
21
|
+
|
|
22
|
+
// 1. Initialize document
|
|
23
|
+
const doc = MDocument.fromText(`Your document text here...`)
|
|
24
|
+
|
|
25
|
+
// 2. Create chunks
|
|
26
|
+
const chunks = await doc.chunk({
|
|
27
|
+
strategy: 'recursive',
|
|
28
|
+
size: 512,
|
|
29
|
+
overlap: 50,
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
// 3. Generate embeddings; we need to pass the text of each chunk
|
|
33
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
34
|
+
|
|
35
|
+
const { embeddings } = await embedMany({
|
|
36
|
+
values: chunks.map(chunk => chunk.text),
|
|
37
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
// 4. Store in vector database
|
|
41
|
+
const pgVector = new PgVector({
|
|
42
|
+
id: 'pg-vector',
|
|
43
|
+
connectionString: process.env.POSTGRES_CONNECTION_STRING,
|
|
44
|
+
})
|
|
45
|
+
await pgVector.upsert({
|
|
46
|
+
indexName: 'embeddings',
|
|
47
|
+
vectors: embeddings,
|
|
48
|
+
}) // using an index name of 'embeddings'
|
|
49
|
+
|
|
50
|
+
// 5. Query similar chunks
|
|
51
|
+
const results = await pgVector.query({
|
|
52
|
+
indexName: 'embeddings',
|
|
53
|
+
queryVector: queryVector,
|
|
54
|
+
topK: 3,
|
|
55
|
+
}) // queryVector is the embedding of the query
|
|
56
|
+
|
|
57
|
+
console.log('Similar chunks:', results)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This example shows the essentials: initialize a document, create chunks, generate embeddings, store them, and query for similar content.
|
|
61
|
+
|
|
62
|
+
## Document Processing
|
|
63
|
+
|
|
64
|
+
The basic building block of RAG is document processing. Documents can be chunked using various strategies (recursive, sliding window, etc.) and enriched with metadata. See the [chunking and embedding doc](https://mastra.ai/docs/rag/chunking-and-embedding).
|
|
65
|
+
|
|
66
|
+
## Vector Storage
|
|
67
|
+
|
|
68
|
+
Mastra supports multiple vector stores for embedding persistence and similarity search, including pgvector, Pinecone, Qdrant, and MongoDB. See the [vector database doc](https://mastra.ai/docs/rag/vector-databases).
|
|
69
|
+
|
|
70
|
+
## More resources
|
|
71
|
+
|
|
72
|
+
- [Chain of Thought RAG Example](https://github.com/mastra-ai/mastra/tree/main/examples/basics/rag/cot-rag)
|