@mastra/rag 2.7.3-alpha.0 → 2.7.3-alpha.2
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/dist/docs/SKILL.md +1 -2
- package/dist/docs/assets/SOURCE_MAP.json +13 -13
- package/dist/docs/references/reference-rag-graph-rag.md +2 -2
- package/dist/docs/references/reference-rag-rerank.md +1 -1
- package/dist/docs/references/reference-rag-rerankWithScorer.md +1 -1
- package/dist/graph-rag/index.d.ts +3 -2
- package/dist/graph-rag/index.d.ts.map +1 -1
- package/dist/index.cjs +52 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +52 -28
- package/dist/index.js.map +1 -1
- package/dist/rerank/index.d.ts.map +1 -1
- package/dist/utils/tool-helpers.d.ts +3 -3
- package/dist/utils/tool-helpers.d.ts.map +1 -1
- package/package.json +3 -3
- package/dist/docs/references/reference-migrations-upgrade-to-v1-rag.md +0 -46
package/dist/docs/SKILL.md
CHANGED
|
@@ -3,7 +3,7 @@ name: mastra-rag
|
|
|
3
3
|
description: Documentation for @mastra/rag. Use when working with @mastra/rag APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/rag"
|
|
6
|
-
version: "2.7.3-alpha.
|
|
6
|
+
version: "2.7.3-alpha.2"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -20,7 +20,6 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
20
20
|
|
|
21
21
|
### Reference
|
|
22
22
|
|
|
23
|
-
- [RAG](references/reference-migrations-upgrade-to-v1-rag.md) - Migrate RAG code to Mastra v1 with renamed chunking parameters and narrowed option types, including updates to related document-processing APIs.
|
|
24
23
|
- [Reference: .chunk()](references/reference-rag-chunk.md) - The .chunk() function splits documents into smaller segments using strategies and options.
|
|
25
24
|
- [Chunking and embedding documents](references/reference-rag-chunking-and-embedding.md) - Chunk documents with MDocument, generate embeddings through the AI SDK, and prepare text and metadata for vector storage and RAG retrieval.
|
|
26
25
|
- [Reference: DatabaseConfig](references/reference-rag-database-config.md) - The DatabaseConfig type allows you to specify database-specific configurations when using vector query tools.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.7.3-alpha.
|
|
2
|
+
"version": "2.7.3-alpha.2",
|
|
3
3
|
"package": "@mastra/rag",
|
|
4
4
|
"exports": {
|
|
5
5
|
"CohereRelevanceScorer": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"GraphRAG": {
|
|
11
11
|
"types": "dist/graph-rag/index.d.ts",
|
|
12
12
|
"implementation": "dist/index.js",
|
|
13
|
-
"line":
|
|
13
|
+
"line": 6293
|
|
14
14
|
},
|
|
15
15
|
"Language": {
|
|
16
16
|
"types": "dist/document/types.d.ts",
|
|
@@ -35,57 +35,57 @@
|
|
|
35
35
|
"createBedrockKBTool": {
|
|
36
36
|
"types": "dist/tools/bedrock-knowledge-base.d.ts",
|
|
37
37
|
"implementation": "dist/index.js",
|
|
38
|
-
"line":
|
|
38
|
+
"line": 7138
|
|
39
39
|
},
|
|
40
40
|
"createDocumentChunkerTool": {
|
|
41
41
|
"types": "dist/tools/document-chunker.d.ts",
|
|
42
42
|
"implementation": "dist/index.js",
|
|
43
|
-
"line":
|
|
43
|
+
"line": 6529
|
|
44
44
|
},
|
|
45
45
|
"createGraphRAGTool": {
|
|
46
46
|
"types": "dist/tools/graph-rag.d.ts",
|
|
47
47
|
"implementation": "dist/index.js",
|
|
48
|
-
"line":
|
|
48
|
+
"line": 6844
|
|
49
49
|
},
|
|
50
50
|
"createVectorQueryTool": {
|
|
51
51
|
"types": "dist/tools/vector-query.d.ts",
|
|
52
52
|
"implementation": "dist/index.js",
|
|
53
|
-
"line":
|
|
53
|
+
"line": 6998
|
|
54
54
|
},
|
|
55
55
|
"defaultGraphRagDescription": {
|
|
56
56
|
"types": "dist/utils/default-settings.d.ts",
|
|
57
57
|
"implementation": "dist/index.js",
|
|
58
|
-
"line":
|
|
58
|
+
"line": 6676
|
|
59
59
|
},
|
|
60
60
|
"defaultVectorQueryDescription": {
|
|
61
61
|
"types": "dist/utils/default-settings.d.ts",
|
|
62
62
|
"implementation": "dist/index.js",
|
|
63
|
-
"line":
|
|
63
|
+
"line": 6675
|
|
64
64
|
},
|
|
65
65
|
"filterDescription": {
|
|
66
66
|
"types": "dist/utils/default-settings.d.ts",
|
|
67
67
|
"implementation": "dist/index.js",
|
|
68
|
-
"line":
|
|
68
|
+
"line": 6695
|
|
69
69
|
},
|
|
70
70
|
"queryTextDescription": {
|
|
71
71
|
"types": "dist/utils/default-settings.d.ts",
|
|
72
72
|
"implementation": "dist/index.js",
|
|
73
|
-
"line":
|
|
73
|
+
"line": 6677
|
|
74
74
|
},
|
|
75
75
|
"rerank": {
|
|
76
76
|
"types": "dist/rerank/index.d.ts",
|
|
77
77
|
"implementation": "dist/index.js",
|
|
78
|
-
"line":
|
|
78
|
+
"line": 6280
|
|
79
79
|
},
|
|
80
80
|
"rerankWithScorer": {
|
|
81
81
|
"types": "dist/rerank/index.d.ts",
|
|
82
82
|
"implementation": "dist/index.js",
|
|
83
|
-
"line":
|
|
83
|
+
"line": 6272
|
|
84
84
|
},
|
|
85
85
|
"topKDescription": {
|
|
86
86
|
"types": "dist/utils/default-settings.d.ts",
|
|
87
87
|
"implementation": "dist/index.js",
|
|
88
|
-
"line":
|
|
88
|
+
"line": 6685
|
|
89
89
|
}
|
|
90
90
|
},
|
|
91
91
|
"modules": {}
|
|
@@ -103,7 +103,7 @@ serialize(): GraphRAGSnapshot
|
|
|
103
103
|
|
|
104
104
|
Returns a `GraphRAGSnapshot` object containing:
|
|
105
105
|
|
|
106
|
-
**version** (`
|
|
106
|
+
**version** (`1`): Snapshot format version. Always 1 in the current format, so deserialize rejects snapshots with any other version
|
|
107
107
|
|
|
108
108
|
**dimension** (`number`): Dimension of the embedding vectors the graph was built with
|
|
109
109
|
|
|
@@ -127,7 +127,7 @@ static deserialize(snapshot: GraphRAGSnapshot): GraphRAG
|
|
|
127
127
|
|
|
128
128
|
**snapshot** (`GraphRAGSnapshot`): A snapshot previously returned by serialize
|
|
129
129
|
|
|
130
|
-
Throws if the snapshot version is unsupported, if a node embedding doesn't match the snapshot dimension, or if an edge references a node that isn't in the snapshot. A bad snapshot therefore fails at load time instead of during a later query.
|
|
130
|
+
Throws if the snapshot version is unsupported, if a node embedding doesn't match the snapshot dimension or contains non-finite values, or if an edge references a node that isn't in the snapshot. A bad snapshot therefore fails at load time instead of during a later query.
|
|
131
131
|
|
|
132
132
|
## Persisting a graph
|
|
133
133
|
|
|
@@ -43,7 +43,7 @@ const rerankedResults = await rerank(vectorSearchResults, 'How do I deploy to pr
|
|
|
43
43
|
|
|
44
44
|
**options** (`RerankerFunctionOptions`): Options for the reranking model
|
|
45
45
|
|
|
46
|
-
**options.weights** (`WeightConfig`): Weights for different scoring components (must add up to 1)
|
|
46
|
+
**options.weights** (`WeightConfig`): Weights for different scoring components (must add up to 1; each weight must be between 0 and 1)
|
|
47
47
|
|
|
48
48
|
**options.weights.semantic** (`number (default: 0.4)`): Weight for semantic relevance
|
|
49
49
|
|
|
@@ -47,7 +47,7 @@ const rerankedResults = await rerank({
|
|
|
47
47
|
|
|
48
48
|
**options** (`RerankerFunctionOptions`): Options for the reranking model
|
|
49
49
|
|
|
50
|
-
**options.weights** (`WeightConfig`): Weights for different scoring components (must add up to 1)
|
|
50
|
+
**options.weights** (`WeightConfig`): Weights for different scoring components (must add up to 1; each weight must be between 0 and 1)
|
|
51
51
|
|
|
52
52
|
**options.weights.semantic** (`number (default: 0.4)`): Weight for semantic relevance
|
|
53
53
|
|
|
@@ -64,8 +64,9 @@ export declare class GraphRAG {
|
|
|
64
64
|
/**
|
|
65
65
|
* Rebuild a GraphRAG instance from a snapshot produced by `serialize()`.
|
|
66
66
|
*
|
|
67
|
-
* @throws if the snapshot version is unsupported, a node
|
|
68
|
-
* match the snapshot dimension
|
|
67
|
+
* @throws if the snapshot version is unsupported, a node ID is duplicated,
|
|
68
|
+
* a node embedding does not match the snapshot dimension or contains nonfinite values,
|
|
69
|
+
* or an edge references an unknown node.
|
|
69
70
|
*/
|
|
70
71
|
static deserialize(snapshot: GraphRAGSnapshot): GraphRAG;
|
|
71
72
|
clear(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/graph-rag/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,KAAK,iBAAiB,GAAG,UAAU,CAAC;AACpC,KAAK,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAGzC,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,QAAQ,CAAC,EAAE,aAAa,CAAC;CAC1B;AAED,MAAM,WAAW,UAAW,SAAQ,SAAS;IAC3C,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,iBAAiB,CAAC;CACzB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC/B;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,CAAC,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,SAAS,EAAE,CAAC;IACnB,KAAK,EAAE,SAAS,EAAE,CAAC;CACpB;AAID,qBAAa,QAAQ;IACnB,OAAO,CAAC,KAAK,CAAyB;I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9 +6210,25 @@ async function executeRerank({ results, query, scorer, options }) {
|
|
|
6210
6210
|
...DEFAULT_WEIGHTS,
|
|
6211
6211
|
...options.weights
|
|
6212
6212
|
};
|
|
6213
|
+
for (const [name, weight] of Object.entries(weights)) if (!Number.isFinite(weight) || weight < 0 || weight > 1) {
|
|
6214
|
+
const err = /* @__PURE__ */ new Error(`Weights must be finite numbers between 0 and 1. Got ${name}: ${weight}`);
|
|
6215
|
+
rerankSpan?.error({
|
|
6216
|
+
error: err,
|
|
6217
|
+
endSpan: true
|
|
6218
|
+
});
|
|
6219
|
+
throw err;
|
|
6220
|
+
}
|
|
6213
6221
|
const sum = Object.values(weights).reduce((acc, w) => acc.plus(w.toString()), new big_js.Big(0));
|
|
6214
6222
|
if (!sum.eq(1)) {
|
|
6215
|
-
const err = /* @__PURE__ */ new Error(`Weights must add up to 1. Got ${sum} from ${weights}`);
|
|
6223
|
+
const err = /* @__PURE__ */ new Error(`Weights must add up to 1. Got ${sum} from ${JSON.stringify(weights)}`);
|
|
6224
|
+
rerankSpan?.error({
|
|
6225
|
+
error: err,
|
|
6226
|
+
endSpan: true
|
|
6227
|
+
});
|
|
6228
|
+
throw err;
|
|
6229
|
+
}
|
|
6230
|
+
if (!Number.isInteger(topK) || topK < 0) {
|
|
6231
|
+
const err = /* @__PURE__ */ new Error(`topK must be a nonnegative integer. Got ${topK}`);
|
|
6216
6232
|
rerankSpan?.error({
|
|
6217
6233
|
error: err,
|
|
6218
6234
|
endSpan: true
|
|
@@ -6290,12 +6306,17 @@ var GraphRAG = class GraphRAG {
|
|
|
6290
6306
|
addNode(node) {
|
|
6291
6307
|
if (!node.embedding) throw new Error("Node must have an embedding");
|
|
6292
6308
|
if (node.embedding.length !== this.dimension) throw new Error(`Embedding dimension must be ${this.dimension}`);
|
|
6293
|
-
|
|
6309
|
+
for (const component of node.embedding) if (!Number.isFinite(component)) throw new Error("Embedding must contain only finite numbers");
|
|
6310
|
+
this.nodes.set(node.id, {
|
|
6311
|
+
...node,
|
|
6312
|
+
embedding: [...node.embedding]
|
|
6313
|
+
});
|
|
6294
6314
|
}
|
|
6295
6315
|
addEdge(edge) {
|
|
6296
6316
|
if (!this.nodes.has(edge.source) || !this.nodes.has(edge.target)) throw new Error("Both source and target nodes must exist");
|
|
6317
|
+
if (!Number.isFinite(edge.weight)) throw new Error("Edge weight must be a finite number");
|
|
6297
6318
|
this.edges.push(edge);
|
|
6298
|
-
this.edges.push({
|
|
6319
|
+
if (edge.source !== edge.target) this.edges.push({
|
|
6299
6320
|
source: edge.target,
|
|
6300
6321
|
target: edge.source,
|
|
6301
6322
|
weight: edge.weight,
|
|
@@ -6303,7 +6324,10 @@ var GraphRAG = class GraphRAG {
|
|
|
6303
6324
|
});
|
|
6304
6325
|
}
|
|
6305
6326
|
getNodes() {
|
|
6306
|
-
return Array.from(this.nodes.values())
|
|
6327
|
+
return Array.from(this.nodes.values(), (node) => ({
|
|
6328
|
+
...node,
|
|
6329
|
+
...node.embedding ? { embedding: [...node.embedding] } : {}
|
|
6330
|
+
}));
|
|
6307
6331
|
}
|
|
6308
6332
|
getEdges() {
|
|
6309
6333
|
return this.edges;
|
|
@@ -6336,20 +6360,25 @@ var GraphRAG = class GraphRAG {
|
|
|
6336
6360
|
/**
|
|
6337
6361
|
* Rebuild a GraphRAG instance from a snapshot produced by `serialize()`.
|
|
6338
6362
|
*
|
|
6339
|
-
* @throws if the snapshot version is unsupported, a node
|
|
6340
|
-
* match the snapshot dimension
|
|
6363
|
+
* @throws if the snapshot version is unsupported, a node ID is duplicated,
|
|
6364
|
+
* a node embedding does not match the snapshot dimension or contains nonfinite values,
|
|
6365
|
+
* or an edge references an unknown node.
|
|
6341
6366
|
*/
|
|
6342
6367
|
static deserialize(snapshot) {
|
|
6343
6368
|
if (snapshot?.version !== GRAPH_RAG_SNAPSHOT_VERSION) throw new Error(`Unsupported GraphRAG snapshot version: ${snapshot?.version}`);
|
|
6344
6369
|
const graph = new GraphRAG(snapshot.dimension, snapshot.threshold);
|
|
6345
|
-
for (const node of snapshot.nodes ?? [])
|
|
6346
|
-
|
|
6347
|
-
|
|
6348
|
-
|
|
6349
|
-
|
|
6370
|
+
for (const node of snapshot.nodes ?? []) {
|
|
6371
|
+
if (graph.nodes.has(node.id)) throw new Error(`Duplicate node id in snapshot: ${node.id}`);
|
|
6372
|
+
graph.addNode({
|
|
6373
|
+
...node,
|
|
6374
|
+
...node.embedding ? { embedding: [...node.embedding] } : {},
|
|
6375
|
+
...node.metadata ? { metadata: structuredClone(node.metadata) } : {}
|
|
6376
|
+
});
|
|
6377
|
+
}
|
|
6350
6378
|
for (const edge of snapshot.edges ?? []) {
|
|
6351
6379
|
if (!graph.nodes.has(edge.source)) throw new Error(`Edge references unknown node: ${edge.source}`);
|
|
6352
6380
|
if (!graph.nodes.has(edge.target)) throw new Error(`Edge references unknown node: ${edge.target}`);
|
|
6381
|
+
if (!Number.isFinite(edge.weight)) throw new Error("Edge weight must be a finite number");
|
|
6353
6382
|
}
|
|
6354
6383
|
graph.edges = (snapshot.edges ?? []).map((edge) => ({ ...edge }));
|
|
6355
6384
|
return graph;
|
|
@@ -6391,22 +6420,21 @@ var GraphRAG = class GraphRAG {
|
|
|
6391
6420
|
createGraph(chunks, embeddings) {
|
|
6392
6421
|
if (!chunks?.length || !embeddings?.length) throw new Error("Chunks and embeddings arrays must not be empty");
|
|
6393
6422
|
if (chunks.length !== embeddings.length) throw new Error("Chunks and embeddings must have the same length");
|
|
6423
|
+
const graph = new GraphRAG(this.dimension, this.threshold);
|
|
6394
6424
|
chunks.forEach((chunk, index) => {
|
|
6395
|
-
|
|
6425
|
+
graph.addNode({
|
|
6396
6426
|
id: index.toString(),
|
|
6397
6427
|
content: chunk.text,
|
|
6398
6428
|
embedding: embeddings[index]?.vector,
|
|
6399
6429
|
metadata: { ...chunk.metadata }
|
|
6400
|
-
};
|
|
6401
|
-
this.addNode(node);
|
|
6402
|
-
this.nodes.set(node.id, node);
|
|
6430
|
+
});
|
|
6403
6431
|
});
|
|
6404
6432
|
for (let i = 0; i < chunks.length; i++) {
|
|
6405
6433
|
const firstEmbedding = embeddings[i]?.vector;
|
|
6406
6434
|
for (let j = i + 1; j < chunks.length; j++) {
|
|
6407
6435
|
const secondEmbedding = embeddings[j]?.vector;
|
|
6408
6436
|
const similarity = this.cosineSimilarity(firstEmbedding, secondEmbedding);
|
|
6409
|
-
if (similarity > this.threshold)
|
|
6437
|
+
if (similarity > this.threshold) graph.addEdge({
|
|
6410
6438
|
source: i.toString(),
|
|
6411
6439
|
target: j.toString(),
|
|
6412
6440
|
weight: similarity,
|
|
@@ -6414,6 +6442,8 @@ var GraphRAG = class GraphRAG {
|
|
|
6414
6442
|
});
|
|
6415
6443
|
}
|
|
6416
6444
|
}
|
|
6445
|
+
this.nodes = graph.nodes;
|
|
6446
|
+
this.edges = graph.edges;
|
|
6417
6447
|
}
|
|
6418
6448
|
selectWeightedNeighbor(neighbors) {
|
|
6419
6449
|
const totalWeight = neighbors.reduce((sum, n) => sum + n.weight, 0);
|
|
@@ -6457,9 +6487,10 @@ var GraphRAG = class GraphRAG {
|
|
|
6457
6487
|
*/
|
|
6458
6488
|
query({ query, topK = 10, randomWalkSteps = 100, restartProb = .15, filter }) {
|
|
6459
6489
|
if (!query || query.length !== this.dimension) throw new Error(`Query embedding must have dimension ${this.dimension}`);
|
|
6460
|
-
|
|
6461
|
-
if (
|
|
6462
|
-
if (
|
|
6490
|
+
for (const component of query) if (!Number.isFinite(component)) throw new Error("Query embedding must contain only finite numbers");
|
|
6491
|
+
if (!Number.isInteger(topK) || topK < 1) throw new Error("TopK must be a positive integer");
|
|
6492
|
+
if (!Number.isInteger(randomWalkSteps) || randomWalkSteps < 1) throw new Error("Random walk steps must be a positive integer");
|
|
6493
|
+
if (!Number.isFinite(restartProb) || restartProb <= 0 || restartProb >= 1) throw new Error("Restart probability must be between 0 and 1");
|
|
6463
6494
|
const filterEntries = Object.entries(filter ?? {});
|
|
6464
6495
|
const matchesFilter = (node) => filterEntries.length === 0 ? true : filterEntries.every(([key, value]) => node.metadata?.[key] === value);
|
|
6465
6496
|
const nodesToSearch = Array.from(this.nodes.values()).filter(matchesFilter);
|
|
@@ -6752,15 +6783,8 @@ async function resolveVectorStore(options, context, logger) {
|
|
|
6752
6783
|
if (mastra) return mastra.getVector(vectorStoreName);
|
|
6753
6784
|
}
|
|
6754
6785
|
function coerceTopK(topK, defaultValue = 10) {
|
|
6755
|
-
|
|
6756
|
-
|
|
6757
|
-
return defaultValue;
|
|
6758
|
-
}
|
|
6759
|
-
if (typeof topK === "string") {
|
|
6760
|
-
const parsed = Number(topK);
|
|
6761
|
-
if (Number.isFinite(parsed) && parsed > 0) return parsed;
|
|
6762
|
-
return defaultValue;
|
|
6763
|
-
}
|
|
6786
|
+
const parsed = typeof topK === "string" ? Number(topK) : topK;
|
|
6787
|
+
if (typeof parsed === "number" && Number.isFinite(parsed) && Math.floor(parsed) >= 1) return Math.floor(parsed);
|
|
6764
6788
|
return defaultValue;
|
|
6765
6789
|
}
|
|
6766
6790
|
function parseFilterValue(filter, logger) {
|