@mastra/rag 2.1.0 → 2.1.1-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 CHANGED
@@ -1,5 +1,14 @@
1
1
  # @mastra/rag
2
2
 
3
+ ## 2.1.1-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - dependencies updates: ([#13318](https://github.com/mastra-ai/mastra/pull/13318))
8
+ - 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`)
9
+ - 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), [`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)]:
10
+ - @mastra/core@1.7.0-alpha.0
11
+
3
12
  ## 2.1.0
4
13
 
5
14
  ### Minor Changes
@@ -50,6 +59,7 @@
50
59
  Previously, the Language enum defined PHP, but it was not supported in the `getSeparatorsForLanguage` method. This caused runtime errors when trying to use PHP for code chunking.
51
60
  This change adds proper separator definitions for PHP, ensuring that PHP defined in the Language enum is now fully supported. PHP has been configured with appropriate separators based on its syntax and common programming patterns (classes, functions, control structures, etc.).
52
61
  **Before:**
62
+
53
63
  ```typescript
54
64
  import { RecursiveCharacterTransformer, Language } from '@mastra/rag';
55
65
 
@@ -57,7 +67,9 @@
57
67
  const chunks = transformer.transform(phpCodeDocument);
58
68
  // Throws: "Language PHP is not supported!"
59
69
  ```
70
+
60
71
  **After:**
72
+
61
73
  ```typescript
62
74
  import { RecursiveCharacterTransformer, Language } from '@mastra/rag';
63
75
 
@@ -65,7 +77,9 @@
65
77
  const chunks = transformer.transform(phpCodeDocument);
66
78
  // Successfully chunks PHP code at namespace, class, function boundaries
67
79
  ```
80
+
68
81
  Fixes the issue where using `Language.PHP` would throw "Language PHP is not supported!" error.
82
+
69
83
  - Updated dependencies [[`90fc0e5`](https://github.com/mastra-ai/mastra/commit/90fc0e5717cb280c2d4acf4f0410b510bb4c0a72), [`1cf5d2e`](https://github.com/mastra-ai/mastra/commit/1cf5d2ea1b085be23e34fb506c80c80a4e6d9c2b), [`b99ceac`](https://github.com/mastra-ai/mastra/commit/b99ceace2c830dbdef47c8692d56a91954aefea2), [`deea43e`](https://github.com/mastra-ai/mastra/commit/deea43eb1366d03a864c5e597d16a48592b9893f), [`833ae96`](https://github.com/mastra-ai/mastra/commit/833ae96c3e34370e58a1e979571c41f39a720592), [`943772b`](https://github.com/mastra-ai/mastra/commit/943772b4378f625f0f4e19ea2b7c392bd8e71786), [`b5c711b`](https://github.com/mastra-ai/mastra/commit/b5c711b281dd1fb81a399a766bc9f86c55efc13e), [`3efbe5a`](https://github.com/mastra-ai/mastra/commit/3efbe5ae20864c4f3143457f4f3ee7dc2fa5ca76), [`1e49e7a`](https://github.com/mastra-ai/mastra/commit/1e49e7ab5f173582154cb26b29d424de67d09aef), [`751eaab`](https://github.com/mastra-ai/mastra/commit/751eaab4e0d3820a94e4c3d39a2ff2663ded3d91), [`69d8156`](https://github.com/mastra-ai/mastra/commit/69d81568bcf062557c24471ce26812446bec465d), [`60d9d89`](https://github.com/mastra-ai/mastra/commit/60d9d899e44b35bc43f1bcd967a74e0ce010b1af), [`5c544c8`](https://github.com/mastra-ai/mastra/commit/5c544c8d12b08ab40d64d8f37b3c4215bee95b87), [`771ad96`](https://github.com/mastra-ai/mastra/commit/771ad962441996b5c43549391a3e6a02c6ddedc2), [`2b0936b`](https://github.com/mastra-ai/mastra/commit/2b0936b0c9a43eeed9bef63e614d7e02ee803f7e), [`3b04f30`](https://github.com/mastra-ai/mastra/commit/3b04f3010604f3cdfc8a0674731700ad66471cee), [`97e26de`](https://github.com/mastra-ai/mastra/commit/97e26deaebd9836647a67b96423281d66421ca07), [`ac9ec66`](https://github.com/mastra-ai/mastra/commit/ac9ec6672779b2e6d4344e415481d1a6a7d4911a), [`10523f4`](https://github.com/mastra-ai/mastra/commit/10523f4882d9b874b40ce6e3715f66dbcd4947d2), [`cb72d20`](https://github.com/mastra-ai/mastra/commit/cb72d2069d7339bda8a0e76d4f35615debb07b84), [`42856b1`](https://github.com/mastra-ai/mastra/commit/42856b1c8aeea6371c9ee77ae2f5f5fe34400933), [`66f33ff`](https://github.com/mastra-ai/mastra/commit/66f33ff68620018513e499c394411d1d39b3aa5c), [`ab3c190`](https://github.com/mastra-ai/mastra/commit/ab3c1901980a99910ca9b96a7090c22e24060113), [`d4f06c8`](https://github.com/mastra-ai/mastra/commit/d4f06c85ffa5bb0da38fb82ebf3b040cc6b4ec4e), [`0350626`](https://github.com/mastra-ai/mastra/commit/03506267ec41b67add80d994c0c0fcce93bbc75f), [`bc9fa00`](https://github.com/mastra-ai/mastra/commit/bc9fa00859c5c4a796d53a0a5cae46ab4a3072e4), [`f46a478`](https://github.com/mastra-ai/mastra/commit/f46a4782f595949c696569e891f81c8d26338508), [`90fc0e5`](https://github.com/mastra-ai/mastra/commit/90fc0e5717cb280c2d4acf4f0410b510bb4c0a72), [`f05a3a5`](https://github.com/mastra-ai/mastra/commit/f05a3a5cf2b9a9c2d40c09cb8c762a4b6cd5d565), [`a291da9`](https://github.com/mastra-ai/mastra/commit/a291da9363efd92dafd8775dccb4f2d0511ece7a), [`c5d71da`](https://github.com/mastra-ai/mastra/commit/c5d71da1c680ce5640b1a7f8ca0e024a4ab1cfed), [`07042f9`](https://github.com/mastra-ai/mastra/commit/07042f9f89080f38b8f72713ba1c972d5b1905b8), [`0423442`](https://github.com/mastra-ai/mastra/commit/0423442b7be2dfacba95890bea8f4a810db4d603)]:
70
84
  - @mastra/core@1.1.0
71
85
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/rag",
3
- "version": "2.1.0",
3
+ "version": "2.1.1-alpha.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -30,7 +30,7 @@
30
30
  "js-tiktoken": "^1.0.21",
31
31
  "node-html-better-parser": "^1.5.8",
32
32
  "pathe": "^2.0.3",
33
- "zeroentropy": "0.1.0-alpha.6"
33
+ "zeroentropy": "0.1.0-alpha.7"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@mastra/core": ">=1.0.0-0 <2.0.0-0",
@@ -41,19 +41,19 @@
41
41
  "@ai-sdk/openai": "^1.3.24",
42
42
  "@types/big.js": "^6.2.2",
43
43
  "@types/node": "22.19.7",
44
- "@vitest/coverage-v8": "4.0.12",
45
- "@vitest/ui": "4.0.12",
44
+ "@vitest/coverage-v8": "4.0.18",
45
+ "@vitest/ui": "4.0.18",
46
46
  "ai": "^4.3.19",
47
47
  "dotenv": "^17.2.3",
48
48
  "eslint": "^9.37.0",
49
49
  "tsup": "^8.5.1",
50
50
  "typescript": "^5.9.3",
51
- "vitest": "4.0.16",
51
+ "vitest": "4.0.18",
52
52
  "zod": "^3.25.76",
53
- "@internal/lint": "0.0.56",
54
- "@internal/ai-sdk-v4": "0.0.3",
55
- "@mastra/core": "1.1.0",
56
- "@internal/types-builder": "0.0.31"
53
+ "@internal/lint": "0.0.61",
54
+ "@internal/types-builder": "0.0.36",
55
+ "@mastra/core": "1.7.0-alpha.0",
56
+ "@internal/ai-sdk-v4": "0.0.8"
57
57
  },
58
58
  "keywords": [
59
59
  "rag",
@@ -85,7 +85,6 @@
85
85
  },
86
86
  "scripts": {
87
87
  "build:lib": "tsup --silent --config tsup.config.ts",
88
- "build:docs": "pnpx tsx ../../scripts/generate-package-docs.ts packages/rag",
89
88
  "build:watch": "pnpm build:lib --watch",
90
89
  "vitest": "vitest",
91
90
  "test": "vitest run",
@@ -1,32 +0,0 @@
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.1.0
@@ -1,33 +0,0 @@
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.1.0
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)
@@ -1,6 +0,0 @@
1
- {
2
- "version": "2.1.0",
3
- "package": "@mastra/rag",
4
- "exports": {},
5
- "modules": {}
6
- }
@@ -1,74 +0,0 @@
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)
@@ -1,189 +0,0 @@
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
- });
55
- ```
56
-
57
- For markdown documents where preserving the semantic relationships between sections is important, here's an example of how to use the `semantic-markdown` strategy:
58
-
59
- ```ts
60
- const chunks = await doc.chunk({
61
- strategy: "semantic-markdown",
62
- joinThreshold: 500,
63
- modelName: "gpt-3.5-turbo",
64
- });
65
- ```
66
-
67
- > **Note:**
68
- Metadata extraction may use LLM calls, so ensure your API key is set.
69
-
70
- We go deeper into chunking strategies in our [`chunk()` reference documentation](https://mastra.ai/reference/rag/chunk).
71
-
72
- ## Step 2: Embedding Generation
73
-
74
- Transform chunks into embeddings using your preferred provider. Mastra supports embedding models through the model router.
75
-
76
- ### Using the Model Router
77
-
78
- The simplest way is to use Mastra's model router with `provider/model` strings:
79
-
80
- ```ts
81
- import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
82
- import { embedMany } from "ai";
83
-
84
- const { embeddings } = await embedMany({
85
- model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
86
- values: chunks.map((chunk) => chunk.text),
87
- });
88
- ```
89
-
90
- 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).
91
-
92
- The model router automatically handles API key detection from environment variables.
93
-
94
- The embedding functions return vectors, arrays of numbers representing the semantic meaning of your text, ready for similarity searches in your vector database.
95
-
96
- ### Configuring Embedding Dimensions
97
-
98
- Embedding models typically output vectors with a fixed number of dimensions (e.g., 1536 for OpenAI's `text-embedding-3-small`).
99
- Some models support reducing this dimensionality, which can help:
100
-
101
- - Decrease storage requirements in vector databases
102
- - Reduce computational costs for similarity searches
103
-
104
- Here are some supported models:
105
-
106
- OpenAI (text-embedding-3 models):
107
-
108
- ```ts
109
- import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
110
-
111
- const { embeddings } = await embedMany({
112
- model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
113
- options: {
114
- dimensions: 256, // Only supported in text-embedding-3 and later
115
- },
116
- values: chunks.map((chunk) => chunk.text),
117
- });
118
- ```
119
-
120
- Google (text-embedding-001):
121
-
122
- ```ts
123
- const { embeddings } = await embedMany({
124
- model: "google/gemini-embedding-001", {
125
- outputDimensionality: 256, // Truncates excessive values from the end
126
- }),
127
- values: chunks.map((chunk) => chunk.text),
128
- });
129
- ```
130
-
131
- important[Vector Database Compatibility]
132
- 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.
133
-
134
- ## Example: Complete Pipeline
135
-
136
- Here's an example showing document processing and embedding generation with both providers:
137
-
138
- ```ts
139
- import { embedMany } from "ai";
140
-
141
- import { MDocument } from "@mastra/rag";
142
-
143
- // Initialize document
144
- const doc = MDocument.fromText(`
145
- Climate change poses significant challenges to global agriculture.
146
- Rising temperatures and changing precipitation patterns affect crop yields.
147
- `);
148
-
149
- // Create chunks
150
- const chunks = await doc.chunk({
151
- strategy: "recursive",
152
- maxSize: 256,
153
- overlap: 50,
154
- });
155
-
156
- // Generate embeddings with OpenAI
157
- import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
158
-
159
- const { embeddings } = await embedMany({
160
- model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
161
- values: chunks.map((chunk) => chunk.text),
162
- });
163
-
164
- // OR
165
-
166
- // Generate embeddings with Cohere
167
- const { embeddings } = await embedMany({
168
- model: "cohere/embed-english-v3.0",
169
- values: chunks.map((chunk) => chunk.text),
170
- });
171
-
172
- // Store embeddings in your vector database
173
- await vectorStore.upsert({
174
- indexName: "embeddings",
175
- vectors: embeddings,
176
- });
177
- ```
178
-
179
- ##
180
-
181
- For more examples of different chunking strategies and embedding configurations, see:
182
-
183
- - [Chunk Reference](https://mastra.ai/reference/rag/chunk)
184
- - [Embeddings Reference](https://mastra.ai/reference/rag/embeddings)
185
-
186
- For more details on vector databases and embeddings, see:
187
-
188
- - [Vector Databases](./vector-databases)
189
- - [Embedding API Reference](https://mastra.ai/reference/rag/embeddings)