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

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 CHANGED
@@ -1,5 +1,43 @@
1
1
  # @mastra/rag
2
2
 
3
+ ## 2.0.0-beta.6
4
+
5
+ ### Patch Changes
6
+
7
+ - Remove unnecessary `ai` package peer dependency to enable compatibility with AI SDK v6. The rag package doesn't directly use the ai package, so this peer dependency was unnecessarily constraining version compatibility. ([#11724](https://github.com/mastra-ai/mastra/pull/11724))
8
+
9
+ - Updated dependencies [[`08766f1`](https://github.com/mastra-ai/mastra/commit/08766f15e13ac0692fde2a8bd366c2e16e4321df), [`ae8baf7`](https://github.com/mastra-ai/mastra/commit/ae8baf7d8adcb0ff9dac11880400452bc49b33ff), [`cfabdd4`](https://github.com/mastra-ai/mastra/commit/cfabdd4aae7a726b706942d6836eeca110fb6267), [`a0e437f`](https://github.com/mastra-ai/mastra/commit/a0e437fac561b28ee719e0302d72b2f9b4c138f0), [`bec5efd`](https://github.com/mastra-ai/mastra/commit/bec5efde96653ccae6604e68c696d1bc6c1a0bf5), [`9eedf7d`](https://github.com/mastra-ai/mastra/commit/9eedf7de1d6e0022a2f4e5e9e6fe1ec468f9b43c)]:
10
+ - @mastra/core@1.0.0-beta.21
11
+
12
+ ## 2.0.0-beta.5
13
+
14
+ ### Minor Changes
15
+
16
+ - Add dynamic vectorStore resolver support for multi-tenant applications ([#11542](https://github.com/mastra-ai/mastra/pull/11542))
17
+
18
+ The vectorStore option in createVectorQueryTool and createGraphRAGTool now accepts a resolver function in addition to static instances. This enables multi-tenant setups where each tenant has isolated data in separate PostgreSQL schemas.
19
+
20
+ Also improves providerOptions type safety by using MastraEmbeddingOptions types instead of a generic Record type.
21
+
22
+ ### Patch Changes
23
+
24
+ - dependencies updates: ([#10133](https://github.com/mastra-ai/mastra/pull/10133))
25
+ - Updated dependency [`js-tiktoken@^1.0.21` ↗︎](https://www.npmjs.com/package/js-tiktoken/v/1.0.21) (from `^1.0.20`, in `dependencies`)
26
+
27
+ - Add embedded documentation support for Mastra packages ([#11472](https://github.com/mastra-ai/mastra/pull/11472))
28
+
29
+ Mastra packages now include embedded documentation in the published npm package under `dist/docs/`. This enables coding agents and AI assistants to understand and use the framework by reading documentation directly from `node_modules`.
30
+
31
+ Each package includes:
32
+ - **SKILL.md** - Entry point explaining the package's purpose and capabilities
33
+ - **SOURCE_MAP.json** - Machine-readable index mapping exports to types and implementation files
34
+ - **Topic folders** - Conceptual documentation organized by feature area
35
+
36
+ Documentation is driven by the `packages` frontmatter field in MDX files, which maps docs to their corresponding packages. CI validation ensures all docs include this field.
37
+
38
+ - Updated dependencies [[`d2d3e22`](https://github.com/mastra-ai/mastra/commit/d2d3e22a419ee243f8812a84e3453dd44365ecb0), [`bc72b52`](https://github.com/mastra-ai/mastra/commit/bc72b529ee4478fe89ecd85a8be47ce0127b82a0), [`05b8bee`](https://github.com/mastra-ai/mastra/commit/05b8bee9e50e6c2a4a2bf210eca25ee212ca24fa), [`c042bd0`](https://github.com/mastra-ai/mastra/commit/c042bd0b743e0e86199d0cb83344ca7690e34a9c), [`940a2b2`](https://github.com/mastra-ai/mastra/commit/940a2b27480626ed7e74f55806dcd2181c1dd0c2), [`e0941c3`](https://github.com/mastra-ai/mastra/commit/e0941c3d7fc75695d5d258e7008fd5d6e650800c), [`0c0580a`](https://github.com/mastra-ai/mastra/commit/0c0580a42f697cd2a7d5973f25bfe7da9055038a), [`28f5f89`](https://github.com/mastra-ai/mastra/commit/28f5f89705f2409921e3c45178796c0e0d0bbb64), [`e601b27`](https://github.com/mastra-ai/mastra/commit/e601b272c70f3a5ecca610373aa6223012704892), [`3d3366f`](https://github.com/mastra-ai/mastra/commit/3d3366f31683e7137d126a3a57174a222c5801fb), [`5a4953f`](https://github.com/mastra-ai/mastra/commit/5a4953f7d25bb15ca31ed16038092a39cb3f98b3), [`eb9e522`](https://github.com/mastra-ai/mastra/commit/eb9e522ce3070a405e5b949b7bf5609ca51d7fe2), [`20e6f19`](https://github.com/mastra-ai/mastra/commit/20e6f1971d51d3ff6dd7accad8aaaae826d540ed), [`4f0b3c6`](https://github.com/mastra-ai/mastra/commit/4f0b3c66f196c06448487f680ccbb614d281e2f7), [`74c4f22`](https://github.com/mastra-ai/mastra/commit/74c4f22ed4c71e72598eacc346ba95cdbc00294f), [`81b6a8f`](https://github.com/mastra-ai/mastra/commit/81b6a8ff79f49a7549d15d66624ac1a0b8f5f971), [`e4d366a`](https://github.com/mastra-ai/mastra/commit/e4d366aeb500371dd4210d6aa8361a4c21d87034), [`a4f010b`](https://github.com/mastra-ai/mastra/commit/a4f010b22e4355a5fdee70a1fe0f6e4a692cc29e), [`73b0bb3`](https://github.com/mastra-ai/mastra/commit/73b0bb394dba7c9482eb467a97ab283dbc0ef4db), [`5627a8c`](https://github.com/mastra-ai/mastra/commit/5627a8c6dc11fe3711b3fa7a6ffd6eb34100a306), [`3ff45d1`](https://github.com/mastra-ai/mastra/commit/3ff45d10e0c80c5335a957ab563da72feb623520), [`251df45`](https://github.com/mastra-ai/mastra/commit/251df4531407dfa46d805feb40ff3fb49769f455), [`f894d14`](https://github.com/mastra-ai/mastra/commit/f894d148946629af7b1f452d65a9cf864cec3765), [`c2b9547`](https://github.com/mastra-ai/mastra/commit/c2b9547bf435f56339f23625a743b2147ab1c7a6), [`580b592`](https://github.com/mastra-ai/mastra/commit/580b5927afc82fe460dfdf9a38a902511b6b7e7f), [`58e3931`](https://github.com/mastra-ai/mastra/commit/58e3931af9baa5921688566210f00fb0c10479fa), [`08bb631`](https://github.com/mastra-ai/mastra/commit/08bb631ae2b14684b2678e3549d0b399a6f0561e), [`4fba91b`](https://github.com/mastra-ai/mastra/commit/4fba91bec7c95911dc28e369437596b152b04cd0), [`12b0cc4`](https://github.com/mastra-ai/mastra/commit/12b0cc4077d886b1a552637dedb70a7ade93528c)]:
39
+ - @mastra/core@1.0.0-beta.20
40
+
3
41
  ## 2.0.0-beta.4
4
42
 
5
43
  ### Patch Changes
@@ -0,0 +1,32 @@
1
+ # @mastra/rag Documentation
2
+
3
+ > Embedded documentation for coding agents
4
+
5
+ ## Quick Start
6
+
7
+ ```bash
8
+ # Read the skill overview
9
+ cat docs/SKILL.md
10
+
11
+ # Get the source map
12
+ cat docs/SOURCE_MAP.json
13
+
14
+ # Read topic documentation
15
+ cat docs/<topic>/01-overview.md
16
+ ```
17
+
18
+ ## Structure
19
+
20
+ ```
21
+ docs/
22
+ ├── SKILL.md # Entry point
23
+ ├── README.md # This file
24
+ ├── SOURCE_MAP.json # Export index
25
+ ├── rag/ (11 files)
26
+ ├── tools/ (3 files)
27
+ ```
28
+
29
+ ## Version
30
+
31
+ Package: @mastra/rag
32
+ Version: 2.0.0-beta.6
@@ -0,0 +1,33 @@
1
+ ---
2
+ name: mastra-rag-docs
3
+ description: Documentation for @mastra/rag. Includes links to type definitions and readable implementation code in dist/.
4
+ ---
5
+
6
+ # @mastra/rag Documentation
7
+
8
+ > **Version**: 2.0.0-beta.6
9
+ > **Package**: @mastra/rag
10
+
11
+ ## Quick Navigation
12
+
13
+ Use SOURCE_MAP.json to find any export:
14
+
15
+ ```bash
16
+ cat docs/SOURCE_MAP.json
17
+ ```
18
+
19
+ Each export maps to:
20
+ - **types**: `.d.ts` file with JSDoc and API signatures
21
+ - **implementation**: `.js` chunk file with readable source
22
+ - **docs**: Conceptual documentation in `docs/`
23
+
24
+ ## Top Exports
25
+
26
+
27
+
28
+ See SOURCE_MAP.json for the complete list.
29
+
30
+ ## Available Topics
31
+
32
+ - [Rag](rag/) - 11 file(s)
33
+ - [Tools](tools/) - 3 file(s)
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": "2.0.0-beta.6",
3
+ "package": "@mastra/rag",
4
+ "exports": {},
5
+ "modules": {}
6
+ }
@@ -0,0 +1,74 @@
1
+ > Overview of Retrieval-Augmented Generation (RAG) in Mastra, detailing its capabilities for enhancing LLM outputs with relevant context.
2
+
3
+ # RAG (Retrieval-Augmented Generation) in Mastra
4
+
5
+ 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.
6
+
7
+ Mastra's RAG system provides:
8
+
9
+ - Standardized APIs to process and embed documents
10
+ - Support for multiple vector stores
11
+ - Chunking and embedding strategies for optimal retrieval
12
+ - Observability for tracking embedding and retrieval performance
13
+
14
+ ## Example
15
+
16
+ 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.
17
+
18
+ ```ts
19
+ import { embedMany } from "ai";
20
+ import { PgVector } from "@mastra/pg";
21
+ import { MDocument } from "@mastra/rag";
22
+ import { z } from "zod";
23
+
24
+ // 1. Initialize document
25
+ const doc = MDocument.fromText(`Your document text here...`);
26
+
27
+ // 2. Create chunks
28
+ const chunks = await doc.chunk({
29
+ strategy: "recursive",
30
+ size: 512,
31
+ overlap: 50,
32
+ });
33
+
34
+ // 3. Generate embeddings; we need to pass the text of each chunk
35
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
36
+
37
+ const { embeddings } = await embedMany({
38
+ values: chunks.map((chunk) => chunk.text),
39
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small")
40
+ });
41
+
42
+ // 4. Store in vector database
43
+ const pgVector = new PgVector({
44
+ id: 'pg-vector',
45
+ connectionString: process.env.POSTGRES_CONNECTION_STRING,
46
+ });
47
+ await pgVector.upsert({
48
+ indexName: "embeddings",
49
+ vectors: embeddings,
50
+ }); // using an index name of 'embeddings'
51
+
52
+ // 5. Query similar chunks
53
+ const results = await pgVector.query({
54
+ indexName: "embeddings",
55
+ queryVector: queryVector,
56
+ topK: 3,
57
+ }); // queryVector is the embedding of the query
58
+
59
+ console.log("Similar chunks:", results);
60
+ ```
61
+
62
+ This example shows the essentials: initialize a document, create chunks, generate embeddings, store them, and query for similar content.
63
+
64
+ ## Document Processing
65
+
66
+ 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](./chunking-and-embedding).
67
+
68
+ ## Vector Storage
69
+
70
+ Mastra supports multiple vector stores for embedding persistence and similarity search, including pgvector, Pinecone, Qdrant, and MongoDB. See the [vector database doc](./vector-databases).
71
+
72
+ ## More resources
73
+
74
+ - [Chain of Thought RAG Example](https://github.com/mastra-ai/mastra/tree/main/examples/basics/rag/cot-rag)
@@ -0,0 +1,190 @@
1
+ > Guide on chunking and embedding documents in Mastra for efficient processing and retrieval.
2
+
3
+ # Chunking and Embedding Documents
4
+
5
+ Before processing, create a MDocument instance from your content. You can initialize it from various formats:
6
+
7
+ ```ts
8
+ const docFromText = MDocument.fromText("Your plain text content...");
9
+ const docFromHTML = MDocument.fromHTML("<html>Your HTML content...</html>");
10
+ const docFromMarkdown = MDocument.fromMarkdown("# Your Markdown content...");
11
+ const docFromJSON = MDocument.fromJSON(`{ "key": "value" }`);
12
+ ```
13
+
14
+ ## Step 1: Document Processing
15
+
16
+ Use `chunk` to split documents into manageable pieces. Mastra supports multiple chunking strategies optimized for different document types:
17
+
18
+ - `recursive`: Smart splitting based on content structure
19
+ - `character`: Simple character-based splits
20
+ - `token`: Token-aware splitting
21
+ - `markdown`: Markdown-aware splitting
22
+ - `semantic-markdown`: Markdown splitting based on related header families
23
+ - `html`: HTML structure-aware splitting
24
+ - `json`: JSON structure-aware splitting
25
+ - `latex`: LaTeX structure-aware splitting
26
+ - `sentence`: Sentence-aware splitting
27
+
28
+ > **Note:**
29
+ Each strategy accepts different parameters optimized for its chunking approach.
30
+
31
+ Here's an example of how to use the `recursive` strategy:
32
+
33
+ ```ts
34
+ const chunks = await doc.chunk({
35
+ strategy: "recursive",
36
+ maxSize: 512,
37
+ overlap: 50,
38
+ separators: ["\n"],
39
+ extract: {
40
+ metadata: true, // Optionally extract metadata
41
+ },
42
+ });
43
+ ```
44
+
45
+ For text where preserving sentence structure is important, here's an example of how to use the `sentence` strategy:
46
+
47
+ ```ts
48
+ const chunks = await doc.chunk({
49
+ strategy: "sentence",
50
+ maxSize: 450,
51
+ minSize: 50,
52
+ overlap: 0,
53
+ sentenceEnders: ["."],
54
+ keepSeparator: true,
55
+ });
56
+ ```
57
+
58
+ For markdown documents where preserving the semantic relationships between sections is important, here's an example of how to use the `semantic-markdown` strategy:
59
+
60
+ ```ts
61
+ const chunks = await doc.chunk({
62
+ strategy: "semantic-markdown",
63
+ joinThreshold: 500,
64
+ modelName: "gpt-3.5-turbo",
65
+ });
66
+ ```
67
+
68
+ > **Note:**
69
+ Metadata extraction may use LLM calls, so ensure your API key is set.
70
+
71
+ We go deeper into chunking strategies in our [`chunk()` reference documentation](https://mastra.ai/reference/v1/rag/chunk).
72
+
73
+ ## Step 2: Embedding Generation
74
+
75
+ Transform chunks into embeddings using your preferred provider. Mastra supports embedding models through the model router.
76
+
77
+ ### Using the Model Router
78
+
79
+ The simplest way is to use Mastra's model router with `provider/model` strings:
80
+
81
+ ```ts
82
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
83
+ import { embedMany } from "ai";
84
+
85
+ const { embeddings } = await embedMany({
86
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
87
+ values: chunks.map((chunk) => chunk.text),
88
+ });
89
+ ```
90
+
91
+ Mastra supports OpenAI and Google embedding models. For a complete list of supported embedding models, see the [embeddings reference](https://mastra.ai/reference/v1/rag/embeddings).
92
+
93
+ The model router automatically handles API key detection from environment variables.
94
+
95
+ The embedding functions return vectors, arrays of numbers representing the semantic meaning of your text, ready for similarity searches in your vector database.
96
+
97
+ ### Configuring Embedding Dimensions
98
+
99
+ Embedding models typically output vectors with a fixed number of dimensions (e.g., 1536 for OpenAI's `text-embedding-3-small`).
100
+ Some models support reducing this dimensionality, which can help:
101
+
102
+ - Decrease storage requirements in vector databases
103
+ - Reduce computational costs for similarity searches
104
+
105
+ Here are some supported models:
106
+
107
+ OpenAI (text-embedding-3 models):
108
+
109
+ ```ts
110
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
111
+
112
+ const { embeddings } = await embedMany({
113
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
114
+ options: {
115
+ dimensions: 256, // Only supported in text-embedding-3 and later
116
+ },
117
+ values: chunks.map((chunk) => chunk.text),
118
+ });
119
+ ```
120
+
121
+ Google (text-embedding-001):
122
+
123
+ ```ts
124
+ const { embeddings } = await embedMany({
125
+ model: "google/gemini-embedding-001", {
126
+ outputDimensionality: 256, // Truncates excessive values from the end
127
+ }),
128
+ values: chunks.map((chunk) => chunk.text),
129
+ });
130
+ ```
131
+
132
+ important[Vector Database Compatibility]
133
+ 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.
134
+
135
+ ## Example: Complete Pipeline
136
+
137
+ Here's an example showing document processing and embedding generation with both providers:
138
+
139
+ ```ts
140
+ import { embedMany } from "ai";
141
+
142
+ import { MDocument } from "@mastra/rag";
143
+
144
+ // Initialize document
145
+ const doc = MDocument.fromText(`
146
+ Climate change poses significant challenges to global agriculture.
147
+ Rising temperatures and changing precipitation patterns affect crop yields.
148
+ `);
149
+
150
+ // Create chunks
151
+ const chunks = await doc.chunk({
152
+ strategy: "recursive",
153
+ maxSize: 256,
154
+ overlap: 50,
155
+ });
156
+
157
+ // Generate embeddings with OpenAI
158
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
159
+
160
+ const { embeddings } = await embedMany({
161
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
162
+ values: chunks.map((chunk) => chunk.text),
163
+ });
164
+
165
+ // OR
166
+
167
+ // Generate embeddings with Cohere
168
+ const { embeddings } = await embedMany({
169
+ model: "cohere/embed-english-v3.0",
170
+ values: chunks.map((chunk) => chunk.text),
171
+ });
172
+
173
+ // Store embeddings in your vector database
174
+ await vectorStore.upsert({
175
+ indexName: "embeddings",
176
+ vectors: embeddings,
177
+ });
178
+ ```
179
+
180
+ ##
181
+
182
+ For more examples of different chunking strategies and embedding configurations, see:
183
+
184
+ - [Chunk Reference](https://mastra.ai/reference/v1/rag/chunk)
185
+ - [Embeddings Reference](https://mastra.ai/reference/v1/rag/embeddings)
186
+
187
+ For more details on vector databases and embeddings, see:
188
+
189
+ - [Vector Databases](./vector-databases)
190
+ - [Embedding API Reference](https://mastra.ai/reference/v1/rag/embeddings)