@mastra/rag 2.7.3-alpha.1 → 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.
@@ -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.1"
6
+ version: "2.7.3-alpha.2"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.7.3-alpha.1",
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": 6285
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": 7115
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": 6506
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": 6821
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": 6975
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": 6653
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": 6652
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": 6672
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": 6654
73
+ "line": 6677
74
74
  },
75
75
  "rerank": {
76
76
  "types": "dist/rerank/index.d.ts",
77
77
  "implementation": "dist/index.js",
78
- "line": 6272
78
+ "line": 6280
79
79
  },
80
80
  "rerankWithScorer": {
81
81
  "types": "dist/rerank/index.d.ts",
82
82
  "implementation": "dist/index.js",
83
- "line": 6264
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": 6662
88
+ "line": 6685
89
89
  }
90
90
  },
91
91
  "modules": {}
@@ -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 embedding does not
68
- * match the snapshot dimension, or an edge references an unknown node.
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,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AAM,CAAC;QACzB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,MAAM,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;KACjC,GAAG,UAAU,EAAE,CAmEf;CACF"}
package/dist/index.cjs CHANGED
@@ -6210,9 +6210,17 @@ 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)}`);
6216
6224
  rerankSpan?.error({
6217
6225
  error: err,
6218
6226
  endSpan: true
@@ -6298,12 +6306,17 @@ var GraphRAG = class GraphRAG {
6298
6306
  addNode(node) {
6299
6307
  if (!node.embedding) throw new Error("Node must have an embedding");
6300
6308
  if (node.embedding.length !== this.dimension) throw new Error(`Embedding dimension must be ${this.dimension}`);
6301
- this.nodes.set(node.id, node);
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
+ });
6302
6314
  }
6303
6315
  addEdge(edge) {
6304
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");
6305
6318
  this.edges.push(edge);
6306
- this.edges.push({
6319
+ if (edge.source !== edge.target) this.edges.push({
6307
6320
  source: edge.target,
6308
6321
  target: edge.source,
6309
6322
  weight: edge.weight,
@@ -6311,7 +6324,10 @@ var GraphRAG = class GraphRAG {
6311
6324
  });
6312
6325
  }
6313
6326
  getNodes() {
6314
- return Array.from(this.nodes.values());
6327
+ return Array.from(this.nodes.values(), (node) => ({
6328
+ ...node,
6329
+ ...node.embedding ? { embedding: [...node.embedding] } : {}
6330
+ }));
6315
6331
  }
6316
6332
  getEdges() {
6317
6333
  return this.edges;
@@ -6344,20 +6360,25 @@ var GraphRAG = class GraphRAG {
6344
6360
  /**
6345
6361
  * Rebuild a GraphRAG instance from a snapshot produced by `serialize()`.
6346
6362
  *
6347
- * @throws if the snapshot version is unsupported, a node embedding does not
6348
- * match the snapshot dimension, or an edge references an unknown node.
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.
6349
6366
  */
6350
6367
  static deserialize(snapshot) {
6351
6368
  if (snapshot?.version !== GRAPH_RAG_SNAPSHOT_VERSION) throw new Error(`Unsupported GraphRAG snapshot version: ${snapshot?.version}`);
6352
6369
  const graph = new GraphRAG(snapshot.dimension, snapshot.threshold);
6353
- for (const node of snapshot.nodes ?? []) graph.addNode({
6354
- ...node,
6355
- ...node.embedding ? { embedding: [...node.embedding] } : {},
6356
- ...node.metadata ? { metadata: structuredClone(node.metadata) } : {}
6357
- });
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
+ }
6358
6378
  for (const edge of snapshot.edges ?? []) {
6359
6379
  if (!graph.nodes.has(edge.source)) throw new Error(`Edge references unknown node: ${edge.source}`);
6360
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");
6361
6382
  }
6362
6383
  graph.edges = (snapshot.edges ?? []).map((edge) => ({ ...edge }));
6363
6384
  return graph;
@@ -6399,22 +6420,21 @@ var GraphRAG = class GraphRAG {
6399
6420
  createGraph(chunks, embeddings) {
6400
6421
  if (!chunks?.length || !embeddings?.length) throw new Error("Chunks and embeddings arrays must not be empty");
6401
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);
6402
6424
  chunks.forEach((chunk, index) => {
6403
- const node = {
6425
+ graph.addNode({
6404
6426
  id: index.toString(),
6405
6427
  content: chunk.text,
6406
6428
  embedding: embeddings[index]?.vector,
6407
6429
  metadata: { ...chunk.metadata }
6408
- };
6409
- this.addNode(node);
6410
- this.nodes.set(node.id, node);
6430
+ });
6411
6431
  });
6412
6432
  for (let i = 0; i < chunks.length; i++) {
6413
6433
  const firstEmbedding = embeddings[i]?.vector;
6414
6434
  for (let j = i + 1; j < chunks.length; j++) {
6415
6435
  const secondEmbedding = embeddings[j]?.vector;
6416
6436
  const similarity = this.cosineSimilarity(firstEmbedding, secondEmbedding);
6417
- if (similarity > this.threshold) this.addEdge({
6437
+ if (similarity > this.threshold) graph.addEdge({
6418
6438
  source: i.toString(),
6419
6439
  target: j.toString(),
6420
6440
  weight: similarity,
@@ -6422,6 +6442,8 @@ var GraphRAG = class GraphRAG {
6422
6442
  });
6423
6443
  }
6424
6444
  }
6445
+ this.nodes = graph.nodes;
6446
+ this.edges = graph.edges;
6425
6447
  }
6426
6448
  selectWeightedNeighbor(neighbors) {
6427
6449
  const totalWeight = neighbors.reduce((sum, n) => sum + n.weight, 0);
@@ -6465,9 +6487,10 @@ var GraphRAG = class GraphRAG {
6465
6487
  */
6466
6488
  query({ query, topK = 10, randomWalkSteps = 100, restartProb = .15, filter }) {
6467
6489
  if (!query || query.length !== this.dimension) throw new Error(`Query embedding must have dimension ${this.dimension}`);
6468
- if (topK < 1) throw new Error("TopK must be greater than 0");
6469
- if (randomWalkSteps < 1) throw new Error("Random walk steps must be greater than 0");
6470
- if (restartProb <= 0 || restartProb >= 1) throw new Error("Restart probability must be between 0 and 1");
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");
6471
6494
  const filterEntries = Object.entries(filter ?? {});
6472
6495
  const matchesFilter = (node) => filterEntries.length === 0 ? true : filterEntries.every(([key, value]) => node.metadata?.[key] === value);
6473
6496
  const nodesToSearch = Array.from(this.nodes.values()).filter(matchesFilter);