@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 +38 -0
- package/dist/docs/README.md +32 -0
- package/dist/docs/SKILL.md +33 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/rag/01-overview.md +74 -0
- package/dist/docs/rag/02-chunking-and-embedding.md +190 -0
- package/dist/docs/rag/03-retrieval.md +549 -0
- package/dist/docs/rag/04-graph-rag.md +217 -0
- package/dist/docs/rag/05-reference.md +854 -0
- package/dist/docs/tools/01-reference.md +684 -0
- package/dist/index.cjs +116 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +116 -52
- package/dist/index.js.map +1 -1
- package/dist/tools/graph-rag.d.ts.map +1 -1
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/types.d.ts +171 -14
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/vector-query.d.ts.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/tool-helpers.d.ts +54 -0
- package/dist/utils/tool-helpers.d.ts.map +1 -0
- package/dist/utils/vector-search.d.ts.map +1 -1
- package/package.json +8 -8
|
@@ -0,0 +1,854 @@
|
|
|
1
|
+
# Rag API Reference
|
|
2
|
+
|
|
3
|
+
> API reference for rag - 7 entries
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Reference: GraphRAG
|
|
9
|
+
|
|
10
|
+
> Documentation for the GraphRAG class in Mastra, which implements a graph-based approach to retrieval augmented generation.
|
|
11
|
+
|
|
12
|
+
The `GraphRAG` class implements a graph-based approach to retrieval augmented generation. It creates a knowledge graph from document chunks where nodes represent documents and edges represent semantic relationships, enabling both direct similarity matching and discovery of related content through graph traversal.
|
|
13
|
+
|
|
14
|
+
## Basic Usage
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { GraphRAG } from "@mastra/rag";
|
|
18
|
+
|
|
19
|
+
const graphRag = new GraphRAG({
|
|
20
|
+
dimension: 1536,
|
|
21
|
+
threshold: 0.7,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Create the graph from chunks and embeddings
|
|
25
|
+
graphRag.createGraph(documentChunks, embeddings);
|
|
26
|
+
|
|
27
|
+
// Query the graph with embedding
|
|
28
|
+
const results = await graphRag.query({
|
|
29
|
+
query: queryEmbedding,
|
|
30
|
+
topK: 10,
|
|
31
|
+
randomWalkSteps: 100,
|
|
32
|
+
restartProb: 0.15,
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Constructor Parameters
|
|
37
|
+
|
|
38
|
+
## Methods
|
|
39
|
+
|
|
40
|
+
### createGraph
|
|
41
|
+
|
|
42
|
+
Creates a knowledge graph from document chunks and their embeddings.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
createGraph(chunks: GraphChunk[], embeddings: GraphEmbedding[]): void
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### Parameters
|
|
49
|
+
|
|
50
|
+
### query
|
|
51
|
+
|
|
52
|
+
Performs a graph-based search combining vector similarity and graph traversal.
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
query({
|
|
56
|
+
query,
|
|
57
|
+
topK = 10,
|
|
58
|
+
randomWalkSteps = 100,
|
|
59
|
+
restartProb = 0.15
|
|
60
|
+
}: {
|
|
61
|
+
query: number[];
|
|
62
|
+
topK?: number;
|
|
63
|
+
randomWalkSteps?: number;
|
|
64
|
+
restartProb?: number;
|
|
65
|
+
}): RankedNode[]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### Parameters
|
|
69
|
+
|
|
70
|
+
#### Returns
|
|
71
|
+
|
|
72
|
+
Returns an array of `RankedNode` objects, where each node contains:
|
|
73
|
+
|
|
74
|
+
## Advanced Example
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const graphRag = new GraphRAG({
|
|
78
|
+
dimension: 1536,
|
|
79
|
+
threshold: 0.8, // Stricter similarity threshold
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Create graph from chunks and embeddings
|
|
83
|
+
graphRag.createGraph(documentChunks, embeddings);
|
|
84
|
+
|
|
85
|
+
// Query with custom parameters
|
|
86
|
+
const results = await graphRag.query({
|
|
87
|
+
query: queryEmbedding,
|
|
88
|
+
topK: 5,
|
|
89
|
+
randomWalkSteps: 200,
|
|
90
|
+
restartProb: 0.2,
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Related
|
|
95
|
+
|
|
96
|
+
- [createGraphRAGTool](../tools/graph-rag-tool)
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Reference: Reference: .chunk()
|
|
101
|
+
|
|
102
|
+
> Documentation for the chunk function in Mastra, which splits documents into smaller segments using various strategies.
|
|
103
|
+
|
|
104
|
+
The `.chunk()` function splits documents into smaller segments using various strategies and options.
|
|
105
|
+
|
|
106
|
+
## Example
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { MDocument } from "@mastra/rag";
|
|
110
|
+
|
|
111
|
+
const doc = MDocument.fromMarkdown(`
|
|
112
|
+
# Introduction
|
|
113
|
+
This is a sample document that we want to split into chunks.
|
|
114
|
+
|
|
115
|
+
## Section 1
|
|
116
|
+
Here is the first section with some content.
|
|
117
|
+
|
|
118
|
+
## Section 2
|
|
119
|
+
Here is another section with different content.
|
|
120
|
+
`);
|
|
121
|
+
|
|
122
|
+
// Basic chunking with defaults
|
|
123
|
+
const chunks = await doc.chunk();
|
|
124
|
+
|
|
125
|
+
// Markdown-specific chunking with header extraction
|
|
126
|
+
const chunksWithMetadata = await doc.chunk({
|
|
127
|
+
strategy: "markdown",
|
|
128
|
+
headers: [
|
|
129
|
+
["#", "title"],
|
|
130
|
+
["##", "section"],
|
|
131
|
+
],
|
|
132
|
+
extract: {
|
|
133
|
+
summary: true, // Extract summaries with default settings
|
|
134
|
+
keywords: true, // Extract keywords with default settings
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Parameters
|
|
140
|
+
|
|
141
|
+
The following parameters are available for all chunking strategies.
|
|
142
|
+
**Important:** Each strategy will only utilize a subset of these parameters relevant to its specific use case.
|
|
143
|
+
|
|
144
|
+
See [ExtractParams reference](https://mastra.ai/reference/v1/rag/extract-params) for details on the `extract` parameter.
|
|
145
|
+
|
|
146
|
+
## Strategy-Specific Options
|
|
147
|
+
|
|
148
|
+
Strategy-specific options are passed as top-level parameters alongside the strategy parameter. For example:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
// Character strategy example
|
|
152
|
+
const chunks = await doc.chunk({
|
|
153
|
+
strategy: "character",
|
|
154
|
+
separator: ".", // Character-specific option
|
|
155
|
+
isSeparatorRegex: false, // Character-specific option
|
|
156
|
+
maxSize: 300, // general option
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Recursive strategy example
|
|
160
|
+
const chunks = await doc.chunk({
|
|
161
|
+
strategy: "recursive",
|
|
162
|
+
separators: ["\n\n", "\n", " "], // Recursive-specific option
|
|
163
|
+
language: "markdown", // Recursive-specific option
|
|
164
|
+
maxSize: 500, // general option
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// Sentence strategy example
|
|
168
|
+
const chunks = await doc.chunk({
|
|
169
|
+
strategy: "sentence",
|
|
170
|
+
maxSize: 450, // Required for sentence strategy
|
|
171
|
+
minSize: 50, // Sentence-specific option
|
|
172
|
+
sentenceEnders: ["."], // Sentence-specific option
|
|
173
|
+
fallbackToCharacters: false, // Sentence-specific option
|
|
174
|
+
keepSeparator: true, // general option
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// HTML strategy example
|
|
178
|
+
const chunks = await doc.chunk({
|
|
179
|
+
strategy: "html",
|
|
180
|
+
headers: [
|
|
181
|
+
["h1", "title"],
|
|
182
|
+
["h2", "subtitle"],
|
|
183
|
+
], // HTML-specific option
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// Markdown strategy example
|
|
187
|
+
const chunks = await doc.chunk({
|
|
188
|
+
strategy: "markdown",
|
|
189
|
+
headers: [
|
|
190
|
+
["#", "title"],
|
|
191
|
+
["##", "section"],
|
|
192
|
+
], // Markdown-specific option
|
|
193
|
+
stripHeaders: true, // Markdown-specific option
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// Semantic Markdown strategy example
|
|
197
|
+
const chunks = await doc.chunk({
|
|
198
|
+
strategy: "semantic-markdown",
|
|
199
|
+
joinThreshold: 500, // Semantic Markdown-specific option
|
|
200
|
+
modelName: "gpt-3.5-turbo", // Semantic Markdown-specific option
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Token strategy example
|
|
204
|
+
const chunks = await doc.chunk({
|
|
205
|
+
strategy: "token",
|
|
206
|
+
encodingName: "gpt2", // Token-specific option
|
|
207
|
+
modelName: "gpt-3.5-turbo", // Token-specific option
|
|
208
|
+
maxSize: 1000, // general option
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
The options documented below are passed directly at the top level of the configuration object, not nested within a separate options object.
|
|
213
|
+
|
|
214
|
+
### Character
|
|
215
|
+
|
|
216
|
+
### Recursive
|
|
217
|
+
|
|
218
|
+
### Sentence
|
|
219
|
+
|
|
220
|
+
### HTML
|
|
221
|
+
|
|
222
|
+
**Important:** When using the HTML strategy, all general options are ignored. Use `headers` for header-based splitting or `sections` for section-based splitting. If used together, `sections` will be ignored.
|
|
223
|
+
|
|
224
|
+
### Markdown
|
|
225
|
+
|
|
226
|
+
**Important:** When using the `headers` option, the markdown strategy ignores all general options and content is split based on the markdown header structure. To use size-based chunking with markdown, omit the `headers` parameter.
|
|
227
|
+
|
|
228
|
+
### Semantic Markdown
|
|
229
|
+
|
|
230
|
+
### Token
|
|
231
|
+
|
|
232
|
+
### JSON
|
|
233
|
+
|
|
234
|
+
### Latex
|
|
235
|
+
|
|
236
|
+
The Latex strategy uses only the general chunking options listed above. It provides LaTeX-aware splitting optimized for mathematical and academic documents.
|
|
237
|
+
|
|
238
|
+
## Return Value
|
|
239
|
+
|
|
240
|
+
Returns a `MDocument` instance containing the chunked documents. Each chunk includes:
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
interface DocumentNode {
|
|
244
|
+
text: string;
|
|
245
|
+
metadata: Record<string, any>;
|
|
246
|
+
embedding?: number[];
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## Reference: DatabaseConfig
|
|
253
|
+
|
|
254
|
+
> API reference for database-specific configuration types used with vector query tools in Mastra RAG systems.
|
|
255
|
+
|
|
256
|
+
The `DatabaseConfig` type allows you to specify database-specific configurations when using vector query tools. These configurations enable you to leverage unique features and optimizations offered by different vector stores.
|
|
257
|
+
|
|
258
|
+
## Type Definition
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
export type DatabaseConfig = {
|
|
262
|
+
pinecone?: PineconeConfig;
|
|
263
|
+
pgvector?: PgVectorConfig;
|
|
264
|
+
chroma?: ChromaConfig;
|
|
265
|
+
[key: string]: any; // Extensible for future databases
|
|
266
|
+
};
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Database-Specific Types
|
|
270
|
+
|
|
271
|
+
### PineconeConfig
|
|
272
|
+
|
|
273
|
+
Configuration options specific to Pinecone vector store.
|
|
274
|
+
|
|
275
|
+
**Use Cases:**
|
|
276
|
+
|
|
277
|
+
- Multi-tenant applications (separate namespaces per tenant)
|
|
278
|
+
- Environment isolation (dev/staging/prod namespaces)
|
|
279
|
+
- Hybrid search combining semantic and keyword matching
|
|
280
|
+
|
|
281
|
+
### PgVectorConfig
|
|
282
|
+
|
|
283
|
+
Configuration options specific to PostgreSQL with pgvector extension.
|
|
284
|
+
|
|
285
|
+
**Performance Guidelines:**
|
|
286
|
+
|
|
287
|
+
- **ef**: Start with 2-4x your topK value, increase for better accuracy
|
|
288
|
+
- **probes**: Start with 1-10, increase for better recall
|
|
289
|
+
- **minScore**: Use values between 0.5-0.9 depending on your quality requirements
|
|
290
|
+
|
|
291
|
+
**Use Cases:**
|
|
292
|
+
|
|
293
|
+
- Performance optimization for high-load scenarios
|
|
294
|
+
- Quality filtering to remove irrelevant results
|
|
295
|
+
- Fine-tuning search accuracy vs speed tradeoffs
|
|
296
|
+
|
|
297
|
+
### ChromaConfig
|
|
298
|
+
|
|
299
|
+
Configuration options specific to Chroma vector store.
|
|
300
|
+
|
|
301
|
+
**Filter Syntax Examples:**
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
// Simple equality
|
|
305
|
+
where: { "category": "technical" }
|
|
306
|
+
|
|
307
|
+
// Operators
|
|
308
|
+
where: { "price": { "$gt": 100 } }
|
|
309
|
+
|
|
310
|
+
// Multiple conditions
|
|
311
|
+
where: {
|
|
312
|
+
"category": "electronics",
|
|
313
|
+
"inStock": true
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Document content filtering
|
|
317
|
+
whereDocument: { "$contains": "API documentation" }
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
**Use Cases:**
|
|
321
|
+
|
|
322
|
+
- Advanced metadata filtering
|
|
323
|
+
- Content-based document filtering
|
|
324
|
+
- Complex query combinations
|
|
325
|
+
|
|
326
|
+
## Usage Examples
|
|
327
|
+
|
|
328
|
+
**basic-usage:**
|
|
329
|
+
|
|
330
|
+
### Basic Database Configuration
|
|
331
|
+
|
|
332
|
+
```typescript
|
|
333
|
+
import { createVectorQueryTool } from '@mastra/rag';
|
|
334
|
+
|
|
335
|
+
const vectorTool = createVectorQueryTool({
|
|
336
|
+
vectorStoreName: 'pinecone',
|
|
337
|
+
indexName: 'documents',
|
|
338
|
+
model: embedModel,
|
|
339
|
+
databaseConfig: {
|
|
340
|
+
pinecone: {
|
|
341
|
+
namespace: 'production'
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
**runtime-override:**
|
|
350
|
+
|
|
351
|
+
### Runtime Configuration Override
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
import { RequestContext } from '@mastra/core/request-context';
|
|
355
|
+
|
|
356
|
+
// Initial configuration
|
|
357
|
+
const vectorTool = createVectorQueryTool({
|
|
358
|
+
vectorStoreName: 'pinecone',
|
|
359
|
+
indexName: 'documents',
|
|
360
|
+
model: embedModel,
|
|
361
|
+
databaseConfig: {
|
|
362
|
+
pinecone: {
|
|
363
|
+
namespace: 'development'
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
// Override at runtime
|
|
369
|
+
const requestContext = new RequestContext();
|
|
370
|
+
requestContext.set('databaseConfig', {
|
|
371
|
+
pinecone: {
|
|
372
|
+
namespace: 'production'
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
await vectorTool.execute({
|
|
377
|
+
context: { queryText: 'search query' },
|
|
378
|
+
mastra,
|
|
379
|
+
requestContext
|
|
380
|
+
});
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
**multi-database:**
|
|
386
|
+
|
|
387
|
+
### Multi-Database Configuration
|
|
388
|
+
|
|
389
|
+
```typescript
|
|
390
|
+
const vectorTool = createVectorQueryTool({
|
|
391
|
+
vectorStoreName: 'dynamic', // Will be determined at runtime
|
|
392
|
+
indexName: 'documents',
|
|
393
|
+
model: embedModel,
|
|
394
|
+
databaseConfig: {
|
|
395
|
+
pinecone: {
|
|
396
|
+
namespace: 'default'
|
|
397
|
+
},
|
|
398
|
+
pgvector: {
|
|
399
|
+
minScore: 0.8,
|
|
400
|
+
ef: 150
|
|
401
|
+
},
|
|
402
|
+
chroma: {
|
|
403
|
+
where: { 'type': 'documentation' }
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
> **Note:**
|
|
410
|
+
|
|
411
|
+
**Multi-Database Support**: When you configure multiple databases, only the configuration matching the actual vector store being used will be applied.
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
**performance-tuning:**
|
|
416
|
+
|
|
417
|
+
### Performance Tuning
|
|
418
|
+
|
|
419
|
+
```typescript
|
|
420
|
+
// High accuracy configuration
|
|
421
|
+
const highAccuracyTool = createVectorQueryTool({
|
|
422
|
+
vectorStoreName: 'postgres',
|
|
423
|
+
indexName: 'embeddings',
|
|
424
|
+
model: embedModel,
|
|
425
|
+
databaseConfig: {
|
|
426
|
+
pgvector: {
|
|
427
|
+
ef: 400, // High accuracy
|
|
428
|
+
probes: 20, // High recall
|
|
429
|
+
minScore: 0.85 // High quality threshold
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
// High speed configuration
|
|
435
|
+
const highSpeedTool = createVectorQueryTool({
|
|
436
|
+
vectorStoreName: 'postgres',
|
|
437
|
+
indexName: 'embeddings',
|
|
438
|
+
model: embedModel,
|
|
439
|
+
databaseConfig: {
|
|
440
|
+
pgvector: {
|
|
441
|
+
ef: 50, // Lower accuracy, faster
|
|
442
|
+
probes: 3, // Lower recall, faster
|
|
443
|
+
minScore: 0.6 // Lower quality threshold
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
## Extensibility
|
|
452
|
+
|
|
453
|
+
The `DatabaseConfig` type is designed to be extensible. To add support for a new vector database:
|
|
454
|
+
|
|
455
|
+
```typescript
|
|
456
|
+
// 1. Define the configuration interface
|
|
457
|
+
export interface NewDatabaseConfig {
|
|
458
|
+
customParam1?: string;
|
|
459
|
+
customParam2?: number;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// 2. Extend DatabaseConfig type
|
|
463
|
+
export type DatabaseConfig = {
|
|
464
|
+
pinecone?: PineconeConfig;
|
|
465
|
+
pgvector?: PgVectorConfig;
|
|
466
|
+
chroma?: ChromaConfig;
|
|
467
|
+
newdatabase?: NewDatabaseConfig;
|
|
468
|
+
[key: string]: any;
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
// 3. Use in vector query tool
|
|
472
|
+
const vectorTool = createVectorQueryTool({
|
|
473
|
+
vectorStoreName: "newdatabase",
|
|
474
|
+
indexName: "documents",
|
|
475
|
+
model: embedModel,
|
|
476
|
+
databaseConfig: {
|
|
477
|
+
newdatabase: {
|
|
478
|
+
customParam1: "value",
|
|
479
|
+
customParam2: 42,
|
|
480
|
+
},
|
|
481
|
+
},
|
|
482
|
+
});
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
## Best Practices
|
|
486
|
+
|
|
487
|
+
1. **Environment Configuration**: Use different namespaces or configurations for different environments
|
|
488
|
+
2. **Performance Tuning**: Start with default values and adjust based on your specific needs
|
|
489
|
+
3. **Quality Filtering**: Use minScore to filter out low-quality results
|
|
490
|
+
4. **Runtime Flexibility**: Override configurations at runtime for dynamic scenarios
|
|
491
|
+
5. **Documentation**: Document your specific configuration choices for team members
|
|
492
|
+
|
|
493
|
+
## Migration Guide
|
|
494
|
+
|
|
495
|
+
Existing vector query tools continue to work without changes. To add database configurations:
|
|
496
|
+
|
|
497
|
+
```diff
|
|
498
|
+
const vectorTool = createVectorQueryTool({
|
|
499
|
+
vectorStoreName: 'pinecone',
|
|
500
|
+
indexName: 'documents',
|
|
501
|
+
model: embedModel,
|
|
502
|
+
+ databaseConfig: {
|
|
503
|
+
+ pinecone: {
|
|
504
|
+
+ namespace: 'production'
|
|
505
|
+
+ }
|
|
506
|
+
+ }
|
|
507
|
+
});
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
## Related
|
|
511
|
+
|
|
512
|
+
- [createVectorQueryTool()](https://mastra.ai/reference/v1/tools/vector-query-tool)
|
|
513
|
+
- [Hybrid Vector Search](https://mastra.ai/docs/v1/rag/retrieval#metadata-filtering)
|
|
514
|
+
- [Metadata Filters](https://mastra.ai/reference/v1/rag/metadata-filters)
|
|
515
|
+
|
|
516
|
+
---
|
|
517
|
+
|
|
518
|
+
## Reference: MDocument
|
|
519
|
+
|
|
520
|
+
> Documentation for the MDocument class in Mastra, which handles document processing and chunking.
|
|
521
|
+
|
|
522
|
+
The MDocument class processes documents for RAG applications. The main methods are `.chunk()` and `.extractMetadata()`.
|
|
523
|
+
|
|
524
|
+
## Constructor
|
|
525
|
+
|
|
526
|
+
## Static Methods
|
|
527
|
+
|
|
528
|
+
### fromText()
|
|
529
|
+
|
|
530
|
+
Creates a document from plain text content.
|
|
531
|
+
|
|
532
|
+
```typescript
|
|
533
|
+
static fromText(text: string, metadata?: Record<string, any>): MDocument
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
### fromHTML()
|
|
537
|
+
|
|
538
|
+
Creates a document from HTML content.
|
|
539
|
+
|
|
540
|
+
```typescript
|
|
541
|
+
static fromHTML(html: string, metadata?: Record<string, any>): MDocument
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
### fromMarkdown()
|
|
545
|
+
|
|
546
|
+
Creates a document from Markdown content.
|
|
547
|
+
|
|
548
|
+
```typescript
|
|
549
|
+
static fromMarkdown(markdown: string, metadata?: Record<string, any>): MDocument
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
### fromJSON()
|
|
553
|
+
|
|
554
|
+
Creates a document from JSON content.
|
|
555
|
+
|
|
556
|
+
```typescript
|
|
557
|
+
static fromJSON(json: string, metadata?: Record<string, any>): MDocument
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
## Instance Methods
|
|
561
|
+
|
|
562
|
+
### chunk()
|
|
563
|
+
|
|
564
|
+
Splits document into chunks and optionally extracts metadata.
|
|
565
|
+
|
|
566
|
+
```typescript
|
|
567
|
+
async chunk(params?: ChunkParams): Promise<Chunk[]>
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
See [chunk() reference](./chunk) for detailed options.
|
|
571
|
+
|
|
572
|
+
### getDocs()
|
|
573
|
+
|
|
574
|
+
Returns array of processed document chunks.
|
|
575
|
+
|
|
576
|
+
```typescript
|
|
577
|
+
getDocs(): Chunk[]
|
|
578
|
+
```
|
|
579
|
+
|
|
580
|
+
### getText()
|
|
581
|
+
|
|
582
|
+
Returns array of text strings from chunks.
|
|
583
|
+
|
|
584
|
+
```typescript
|
|
585
|
+
getText(): string[]
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
### getMetadata()
|
|
589
|
+
|
|
590
|
+
Returns array of metadata objects from chunks.
|
|
591
|
+
|
|
592
|
+
```typescript
|
|
593
|
+
getMetadata(): Record<string, any>[]
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
### extractMetadata()
|
|
597
|
+
|
|
598
|
+
Extracts metadata using specified extractors. See [ExtractParams reference](./extract-params) for details.
|
|
599
|
+
|
|
600
|
+
```typescript
|
|
601
|
+
async extractMetadata(params: ExtractParams): Promise<MDocument>
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
## Examples
|
|
605
|
+
|
|
606
|
+
```typescript
|
|
607
|
+
import { MDocument } from "@mastra/rag";
|
|
608
|
+
|
|
609
|
+
// Create document from text
|
|
610
|
+
const doc = MDocument.fromText("Your content here");
|
|
611
|
+
|
|
612
|
+
// Split into chunks with metadata extraction
|
|
613
|
+
const chunks = await doc.chunk({
|
|
614
|
+
strategy: "markdown",
|
|
615
|
+
headers: [
|
|
616
|
+
["#", "title"],
|
|
617
|
+
["##", "section"],
|
|
618
|
+
],
|
|
619
|
+
extract: {
|
|
620
|
+
summary: true, // Extract summaries with default settings
|
|
621
|
+
keywords: true, // Extract keywords with default settings
|
|
622
|
+
},
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
// Get processed chunks
|
|
626
|
+
const docs = doc.getDocs();
|
|
627
|
+
const texts = doc.getText();
|
|
628
|
+
const metadata = doc.getMetadata();
|
|
629
|
+
```
|
|
630
|
+
|
|
631
|
+
---
|
|
632
|
+
|
|
633
|
+
## Reference: ExtractParams
|
|
634
|
+
|
|
635
|
+
> Documentation for metadata extraction configuration in Mastra.
|
|
636
|
+
|
|
637
|
+
ExtractParams configures metadata extraction from document chunks using LLM analysis.
|
|
638
|
+
|
|
639
|
+
## Example
|
|
640
|
+
|
|
641
|
+
```typescript
|
|
642
|
+
import { MDocument } from "@mastra/rag";
|
|
643
|
+
|
|
644
|
+
const doc = MDocument.fromText(text);
|
|
645
|
+
const chunks = await doc.chunk({
|
|
646
|
+
extract: {
|
|
647
|
+
title: true, // Extract titles using default settings
|
|
648
|
+
summary: true, // Generate summaries using default settings
|
|
649
|
+
keywords: true, // Extract keywords using default settings
|
|
650
|
+
},
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
// Example output:
|
|
654
|
+
// chunks[0].metadata = {
|
|
655
|
+
// documentTitle: "AI Systems Overview",
|
|
656
|
+
// sectionSummary: "Overview of artificial intelligence concepts and applications",
|
|
657
|
+
// excerptKeywords: "KEYWORDS: AI, machine learning, algorithms"
|
|
658
|
+
// }
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
## Parameters
|
|
662
|
+
|
|
663
|
+
The `extract` parameter accepts the following fields:
|
|
664
|
+
|
|
665
|
+
## Extractor Arguments
|
|
666
|
+
|
|
667
|
+
### TitleExtractorsArgs
|
|
668
|
+
|
|
669
|
+
### SummaryExtractArgs
|
|
670
|
+
|
|
671
|
+
### QuestionAnswerExtractArgs
|
|
672
|
+
|
|
673
|
+
### KeywordExtractArgs
|
|
674
|
+
|
|
675
|
+
## Advanced Example
|
|
676
|
+
|
|
677
|
+
```typescript
|
|
678
|
+
import { MDocument } from "@mastra/rag";
|
|
679
|
+
|
|
680
|
+
const doc = MDocument.fromText(text);
|
|
681
|
+
const chunks = await doc.chunk({
|
|
682
|
+
extract: {
|
|
683
|
+
// Title extraction with custom settings
|
|
684
|
+
title: {
|
|
685
|
+
nodes: 2, // Extract 2 title nodes
|
|
686
|
+
nodeTemplate: "Generate a title for this: {context}",
|
|
687
|
+
combineTemplate: "Combine these titles: {context}",
|
|
688
|
+
},
|
|
689
|
+
|
|
690
|
+
// Summary extraction with custom settings
|
|
691
|
+
summary: {
|
|
692
|
+
summaries: ["self"], // Generate summaries for current chunk
|
|
693
|
+
promptTemplate: "Summarize this: {context}",
|
|
694
|
+
},
|
|
695
|
+
|
|
696
|
+
// Question generation with custom settings
|
|
697
|
+
questions: {
|
|
698
|
+
questions: 3, // Generate 3 questions
|
|
699
|
+
promptTemplate: "Generate {numQuestions} questions about: {context}",
|
|
700
|
+
embeddingOnly: false,
|
|
701
|
+
},
|
|
702
|
+
|
|
703
|
+
// Keyword extraction with custom settings
|
|
704
|
+
keywords: {
|
|
705
|
+
keywords: 5, // Extract 5 keywords
|
|
706
|
+
promptTemplate: "Extract {maxKeywords} key terms from: {context}",
|
|
707
|
+
},
|
|
708
|
+
},
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
// Example output:
|
|
712
|
+
// chunks[0].metadata = {
|
|
713
|
+
// documentTitle: "AI in Modern Computing",
|
|
714
|
+
// sectionSummary: "Overview of AI concepts and their applications in computing",
|
|
715
|
+
// questionsThisExcerptCanAnswer: "1. What is machine learning?\n2. How do neural networks work?",
|
|
716
|
+
// excerptKeywords: "1. Machine learning\n2. Neural networks\n3. Training data"
|
|
717
|
+
// }
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
## Document Grouping for Title Extraction
|
|
721
|
+
|
|
722
|
+
When using the `TitleExtractor`, you can group multiple chunks together for title extraction by specifying a shared `docId` in the `metadata` field of each chunk. All chunks with the same `docId` will receive the same extracted title. If no `docId` is set, each chunk is treated as its own document for title extraction.
|
|
723
|
+
|
|
724
|
+
**Example:**
|
|
725
|
+
|
|
726
|
+
```ts
|
|
727
|
+
import { MDocument } from "@mastra/rag";
|
|
728
|
+
|
|
729
|
+
const doc = new MDocument({
|
|
730
|
+
docs: [
|
|
731
|
+
{ text: "chunk 1", metadata: { docId: "docA" } },
|
|
732
|
+
{ text: "chunk 2", metadata: { docId: "docA" } },
|
|
733
|
+
{ text: "chunk 3", metadata: { docId: "docB" } },
|
|
734
|
+
],
|
|
735
|
+
type: "text",
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
await doc.extractMetadata({ title: true });
|
|
739
|
+
// The first two chunks will share a title, while the third chunk will be assigned a separate title.
|
|
740
|
+
```
|
|
741
|
+
|
|
742
|
+
---
|
|
743
|
+
|
|
744
|
+
## Reference: rerank()
|
|
745
|
+
|
|
746
|
+
> Documentation for the rerank function in Mastra, which provides advanced reranking capabilities for vector search results.
|
|
747
|
+
|
|
748
|
+
The `rerank()` function provides advanced reranking capabilities for vector search results by combining semantic relevance, vector similarity, and position-based scoring.
|
|
749
|
+
|
|
750
|
+
```typescript
|
|
751
|
+
function rerank(
|
|
752
|
+
results: QueryResult[],
|
|
753
|
+
query: string,
|
|
754
|
+
modelConfig: ModelConfig,
|
|
755
|
+
options?: RerankerFunctionOptions,
|
|
756
|
+
): Promise<RerankResult[]>;
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
## Usage Example
|
|
760
|
+
|
|
761
|
+
```typescript
|
|
762
|
+
import { rerank } from "@mastra/rag";
|
|
763
|
+
|
|
764
|
+
const model = "openai/gpt-5.1";
|
|
765
|
+
|
|
766
|
+
const rerankedResults = await rerank(
|
|
767
|
+
vectorSearchResults,
|
|
768
|
+
"How do I deploy to production?",
|
|
769
|
+
model,
|
|
770
|
+
{
|
|
771
|
+
weights: {
|
|
772
|
+
semantic: 0.5,
|
|
773
|
+
vector: 0.3,
|
|
774
|
+
position: 0.2,
|
|
775
|
+
},
|
|
776
|
+
topK: 3,
|
|
777
|
+
},
|
|
778
|
+
);
|
|
779
|
+
```
|
|
780
|
+
|
|
781
|
+
## Parameters
|
|
782
|
+
|
|
783
|
+
The rerank function accepts any LanguageModel from the Vercel AI SDK. When using the Cohere model `rerank-v3.5`, it will automatically use Cohere's reranking capabilities.
|
|
784
|
+
|
|
785
|
+
> **Note:** For semantic scoring to work properly during re-ranking, each result must include the text content in its `metadata.text` field.
|
|
786
|
+
|
|
787
|
+
### RerankerFunctionOptions
|
|
788
|
+
|
|
789
|
+
## Returns
|
|
790
|
+
|
|
791
|
+
The function returns an array of `RerankResult` objects:
|
|
792
|
+
|
|
793
|
+
### ScoringDetails
|
|
794
|
+
|
|
795
|
+
## Related
|
|
796
|
+
|
|
797
|
+
- [createVectorQueryTool](../tools/vector-query-tool)
|
|
798
|
+
|
|
799
|
+
---
|
|
800
|
+
|
|
801
|
+
## Reference: rerankWithScorer()
|
|
802
|
+
|
|
803
|
+
> Documentation for the rerank function in Mastra, which provides advanced reranking capabilities for vector search results.
|
|
804
|
+
|
|
805
|
+
The `rerankWithScorer()` function provides advanced reranking capabilities for vector search results by combining semantic relevance, vector similarity, and position-based scoring.
|
|
806
|
+
|
|
807
|
+
```typescript
|
|
808
|
+
function rerankWithScorer({
|
|
809
|
+
results: QueryResult[],
|
|
810
|
+
query: string,
|
|
811
|
+
scorer: RelevanceScoreProvider,
|
|
812
|
+
options?: RerankerFunctionOptions,
|
|
813
|
+
}): Promise<RerankResult[]>;
|
|
814
|
+
```
|
|
815
|
+
|
|
816
|
+
## Usage Example
|
|
817
|
+
|
|
818
|
+
```typescript
|
|
819
|
+
import { rerankWithScorer as rerank, CohereRelevanceScorer } from "@mastra/rag";
|
|
820
|
+
|
|
821
|
+
const scorer = new CohereRelevanceScorer("rerank-v3.5");
|
|
822
|
+
|
|
823
|
+
const rerankedResults = await rerank({
|
|
824
|
+
results: vectorSearchResults,
|
|
825
|
+
query: "How do I deploy to production?",
|
|
826
|
+
scorer,
|
|
827
|
+
options: {
|
|
828
|
+
weights: {
|
|
829
|
+
semantic: 0.5,
|
|
830
|
+
vector: 0.3,
|
|
831
|
+
position: 0.2,
|
|
832
|
+
},
|
|
833
|
+
topK: 3,
|
|
834
|
+
},
|
|
835
|
+
});
|
|
836
|
+
```
|
|
837
|
+
|
|
838
|
+
## Parameters
|
|
839
|
+
|
|
840
|
+
The `rerankWithScorer` function accepts any `RelevanceScoreProvider` from @mastra/rag.
|
|
841
|
+
|
|
842
|
+
> **Note:** For semantic scoring to work properly during re-ranking, each result must include the text content in its `metadata.text` field.
|
|
843
|
+
|
|
844
|
+
### RerankerFunctionOptions
|
|
845
|
+
|
|
846
|
+
## Returns
|
|
847
|
+
|
|
848
|
+
The function returns an array of `RerankResult` objects:
|
|
849
|
+
|
|
850
|
+
### ScoringDetails
|
|
851
|
+
|
|
852
|
+
## Related
|
|
853
|
+
|
|
854
|
+
- [createVectorQueryTool](../tools/vector-query-tool)
|