@mastra/rag 1.3.5 → 1.3.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 +18 -0
- package/dist/graph-rag/index.d.ts +13 -2
- package/dist/graph-rag/index.d.ts.map +1 -1
- package/dist/index.cjs +23 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +23 -5
- package/dist/index.js.map +1 -1
- package/dist/utils/convert-sources.d.ts +3 -1
- package/dist/utils/convert-sources.d.ts.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @mastra/rag
|
|
2
2
|
|
|
3
|
+
## 1.3.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Add metadata filtering support a GraphRag (#10352) ([#10403](https://github.com/mastra-ai/mastra/pull/10403))
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`5657314`](https://github.com/mastra-ai/mastra/commit/5657314a1f9d49019bb53f357fa48f75a69247ca), [`e5aca78`](https://github.com/mastra-ai/mastra/commit/e5aca78bb7f263bb8b470bedae81efe9805d7544), [`33a607a`](https://github.com/mastra-ai/mastra/commit/33a607a1f716c2029d4a1ff1603dd756129a33b3), [`cc10fc1`](https://github.com/mastra-ai/mastra/commit/cc10fc192d9f527c71a23cc9def10d8718935ee1), [`1f7ee84`](https://github.com/mastra-ai/mastra/commit/1f7ee841a643ef12d90392125881f06fdf877293), [`e7d5149`](https://github.com/mastra-ai/mastra/commit/e7d514995260b63b2108308e85c64de37dcd0f71), [`f195082`](https://github.com/mastra-ai/mastra/commit/f1950822a2425d5ccae78c5d010e02ddb027a869), [`d9986dd`](https://github.com/mastra-ai/mastra/commit/d9986dd3513f7ca3244a8e599a440ccf4d8bc28b), [`a45b0f0`](https://github.com/mastra-ai/mastra/commit/a45b0f0cd19eab1fe4deceae3abf029442c22f74), [`f6e8eb3`](https://github.com/mastra-ai/mastra/commit/f6e8eb3dac53b70b06e906b2818b1d2a5b0486d7), [`ce57a2b`](https://github.com/mastra-ai/mastra/commit/ce57a2b62fd0d5f6532e4ecd1ba9ba93ac9b95fc), [`3236f35`](https://github.com/mastra-ai/mastra/commit/3236f352ae13cc8552c2965164e97bd125dae48d), [`ce57a2b`](https://github.com/mastra-ai/mastra/commit/ce57a2b62fd0d5f6532e4ecd1ba9ba93ac9b95fc), [`0230321`](https://github.com/mastra-ai/mastra/commit/02303217870bedea0ef009bea9a952f24ed38aaf), [`7b541f4`](https://github.com/mastra-ai/mastra/commit/7b541f49eda6f5a87b738198edbd136927599475), [`0eea842`](https://github.com/mastra-ai/mastra/commit/0eea8423cbdd37f2111593c6f7d2efcde4b7e4ce), [`63ae8a2`](https://github.com/mastra-ai/mastra/commit/63ae8a22c0c09bbb8b9779f5f38934cd75f616af), [`bf810c5`](https://github.com/mastra-ai/mastra/commit/bf810c5c561bd8ef221c0f6bd84e69770b9a38cc), [`ac7ef07`](https://github.com/mastra-ai/mastra/commit/ac7ef07633caee89707142171d2873c888ffef85), [`522f0b4`](https://github.com/mastra-ai/mastra/commit/522f0b45330719858794eabffffde4f343f55549), [`bf810c5`](https://github.com/mastra-ai/mastra/commit/bf810c5c561bd8ef221c0f6bd84e69770b9a38cc), [`8b51d55`](https://github.com/mastra-ai/mastra/commit/8b51d55bae531edf7e383958d7ecee04df31f5d5), [`2131ac5`](https://github.com/mastra-ai/mastra/commit/2131ac571d5065f0a656c57494bca98691bb7609)]:
|
|
10
|
+
- @mastra/core@0.24.6
|
|
11
|
+
|
|
12
|
+
## 1.3.6-alpha.0
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Add metadata filtering support a GraphRag (#10352) ([#10403](https://github.com/mastra-ai/mastra/pull/10403))
|
|
17
|
+
|
|
18
|
+
- Updated dependencies [[`5657314`](https://github.com/mastra-ai/mastra/commit/5657314a1f9d49019bb53f357fa48f75a69247ca), [`e5aca78`](https://github.com/mastra-ai/mastra/commit/e5aca78bb7f263bb8b470bedae81efe9805d7544), [`33a607a`](https://github.com/mastra-ai/mastra/commit/33a607a1f716c2029d4a1ff1603dd756129a33b3), [`cc10fc1`](https://github.com/mastra-ai/mastra/commit/cc10fc192d9f527c71a23cc9def10d8718935ee1), [`1f7ee84`](https://github.com/mastra-ai/mastra/commit/1f7ee841a643ef12d90392125881f06fdf877293), [`e7d5149`](https://github.com/mastra-ai/mastra/commit/e7d514995260b63b2108308e85c64de37dcd0f71), [`f195082`](https://github.com/mastra-ai/mastra/commit/f1950822a2425d5ccae78c5d010e02ddb027a869), [`d9986dd`](https://github.com/mastra-ai/mastra/commit/d9986dd3513f7ca3244a8e599a440ccf4d8bc28b), [`a45b0f0`](https://github.com/mastra-ai/mastra/commit/a45b0f0cd19eab1fe4deceae3abf029442c22f74), [`f6e8eb3`](https://github.com/mastra-ai/mastra/commit/f6e8eb3dac53b70b06e906b2818b1d2a5b0486d7), [`ce57a2b`](https://github.com/mastra-ai/mastra/commit/ce57a2b62fd0d5f6532e4ecd1ba9ba93ac9b95fc), [`3236f35`](https://github.com/mastra-ai/mastra/commit/3236f352ae13cc8552c2965164e97bd125dae48d), [`ce57a2b`](https://github.com/mastra-ai/mastra/commit/ce57a2b62fd0d5f6532e4ecd1ba9ba93ac9b95fc), [`0230321`](https://github.com/mastra-ai/mastra/commit/02303217870bedea0ef009bea9a952f24ed38aaf), [`7b541f4`](https://github.com/mastra-ai/mastra/commit/7b541f49eda6f5a87b738198edbd136927599475), [`0eea842`](https://github.com/mastra-ai/mastra/commit/0eea8423cbdd37f2111593c6f7d2efcde4b7e4ce), [`63ae8a2`](https://github.com/mastra-ai/mastra/commit/63ae8a22c0c09bbb8b9779f5f38934cd75f616af), [`bf810c5`](https://github.com/mastra-ai/mastra/commit/bf810c5c561bd8ef221c0f6bd84e69770b9a38cc), [`ac7ef07`](https://github.com/mastra-ai/mastra/commit/ac7ef07633caee89707142171d2873c888ffef85), [`522f0b4`](https://github.com/mastra-ai/mastra/commit/522f0b45330719858794eabffffde4f343f55549), [`bf810c5`](https://github.com/mastra-ai/mastra/commit/bf810c5c561bd8ef221c0f6bd84e69770b9a38cc), [`8b51d55`](https://github.com/mastra-ai/mastra/commit/8b51d55bae531edf7e383958d7ecee04df31f5d5), [`2131ac5`](https://github.com/mastra-ai/mastra/commit/2131ac571d5065f0a656c57494bca98691bb7609)]:
|
|
19
|
+
- @mastra/core@0.24.6-alpha.0
|
|
20
|
+
|
|
3
21
|
## 1.3.5
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
* - Improve graph traversal and querying using types
|
|
7
7
|
*/
|
|
8
8
|
type SupportedEdgeType = 'semantic';
|
|
9
|
+
type GraphMetadata = Record<string, any>;
|
|
9
10
|
export interface GraphNode {
|
|
10
11
|
id: string;
|
|
11
12
|
content: string;
|
|
12
13
|
embedding?: number[];
|
|
13
|
-
metadata?:
|
|
14
|
+
metadata?: GraphMetadata;
|
|
14
15
|
}
|
|
15
16
|
export interface RankedNode extends GraphNode {
|
|
16
17
|
score: number;
|
|
@@ -46,11 +47,21 @@ export declare class GraphRAG {
|
|
|
46
47
|
createGraph(chunks: GraphChunk[], embeddings: GraphEmbedding[]): void;
|
|
47
48
|
private selectWeightedNeighbor;
|
|
48
49
|
private randomWalkWithRestart;
|
|
49
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Query the graph with a dense embedding and optional metadata filter.
|
|
52
|
+
*
|
|
53
|
+
* @param query - The embedding vector to query.
|
|
54
|
+
* @param topK - Number of top results to return.
|
|
55
|
+
* @param randomWalkSteps - Steps for random walk reranking.
|
|
56
|
+
* @param restartProb - Restart probability for random walk.
|
|
57
|
+
* @param filter - Optional strict metadata filter. All key-value pairs must match exactly.
|
|
58
|
+
*/
|
|
59
|
+
query({ query, topK, randomWalkSteps, restartProb, filter, }: {
|
|
50
60
|
query: number[];
|
|
51
61
|
topK?: number;
|
|
52
62
|
randomWalkSteps?: number;
|
|
53
63
|
restartProb?: number;
|
|
64
|
+
filter?: Partial<GraphMetadata>;
|
|
54
65
|
}): RankedNode[];
|
|
55
66
|
}
|
|
56
67
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/graph-rag/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;
|
|
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,qBAAa,QAAQ;IACnB,OAAO,CAAC,KAAK,CAAyB;IACtC,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,SAAS,CAAS;gBAEd,SAAS,GAAE,MAAa,EAAE,SAAS,GAAE,MAAY;IAQ7D,OAAO,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI;IAW9B,OAAO,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI;IAe9B,QAAQ,IAAI,SAAS,EAAE;IAKvB,QAAQ,IAAI,SAAS,EAAE;IAIvB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE;IAIzC,KAAK,IAAI,IAAI;IAKb,iBAAiB,CAAC,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI;IASvD,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,gBAAgB;IAgCxB,WAAW,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,UAAU,EAAE,cAAc,EAAE;IAuC9D,OAAO,CAAC,sBAAsB;IAoB9B,OAAO,CAAC,qBAAqB;IA2C7B;;;;;;;;OAQG;IAEH,KAAK,CAAC,EACJ,KAAK,EACL,IAAS,EACT,eAAqB,EACrB,WAAkB,EAClB,MAAM,GACP,EAAE;QACD,KAAK,EAAE,MAAM,EAAE,CAAC;QAChB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,MAAM,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;KACjC,GAAG,UAAU,EAAE;CA+DjB"}
|
package/dist/index.cjs
CHANGED
|
@@ -6814,7 +6814,7 @@ var GraphRAG = class {
|
|
|
6814
6814
|
return neighbors[neighbors.length - 1]?.id;
|
|
6815
6815
|
}
|
|
6816
6816
|
// Perform random walk with restart
|
|
6817
|
-
randomWalkWithRestart(startNodeId, steps, restartProb) {
|
|
6817
|
+
randomWalkWithRestart(startNodeId, steps, restartProb, allowedNodeIds) {
|
|
6818
6818
|
const visits = /* @__PURE__ */ new Map();
|
|
6819
6819
|
let currentNodeId = startNodeId;
|
|
6820
6820
|
for (let step = 0; step < steps; step++) {
|
|
@@ -6823,7 +6823,10 @@ var GraphRAG = class {
|
|
|
6823
6823
|
currentNodeId = startNodeId;
|
|
6824
6824
|
continue;
|
|
6825
6825
|
}
|
|
6826
|
-
|
|
6826
|
+
let neighbors = this.getNeighbors(currentNodeId);
|
|
6827
|
+
if (allowedNodeIds) {
|
|
6828
|
+
neighbors = neighbors.filter((n) => allowedNodeIds.has(n.id));
|
|
6829
|
+
}
|
|
6827
6830
|
if (neighbors.length === 0) {
|
|
6828
6831
|
currentNodeId = startNodeId;
|
|
6829
6832
|
continue;
|
|
@@ -6837,12 +6840,22 @@ var GraphRAG = class {
|
|
|
6837
6840
|
}
|
|
6838
6841
|
return normalizedVisits;
|
|
6839
6842
|
}
|
|
6843
|
+
/**
|
|
6844
|
+
* Query the graph with a dense embedding and optional metadata filter.
|
|
6845
|
+
*
|
|
6846
|
+
* @param query - The embedding vector to query.
|
|
6847
|
+
* @param topK - Number of top results to return.
|
|
6848
|
+
* @param randomWalkSteps - Steps for random walk reranking.
|
|
6849
|
+
* @param restartProb - Restart probability for random walk.
|
|
6850
|
+
* @param filter - Optional strict metadata filter. All key-value pairs must match exactly.
|
|
6851
|
+
*/
|
|
6840
6852
|
// Retrieve relevant nodes using hybrid approach
|
|
6841
6853
|
query({
|
|
6842
6854
|
query,
|
|
6843
6855
|
topK = 10,
|
|
6844
6856
|
randomWalkSteps = 100,
|
|
6845
|
-
restartProb = 0.15
|
|
6857
|
+
restartProb = 0.15,
|
|
6858
|
+
filter
|
|
6846
6859
|
}) {
|
|
6847
6860
|
if (!query || query.length !== this.dimension) {
|
|
6848
6861
|
throw new Error(`Query embedding must have dimension ${this.dimension}`);
|
|
@@ -6856,15 +6869,20 @@ var GraphRAG = class {
|
|
|
6856
6869
|
if (restartProb <= 0 || restartProb >= 1) {
|
|
6857
6870
|
throw new Error("Restart probability must be between 0 and 1");
|
|
6858
6871
|
}
|
|
6859
|
-
const
|
|
6872
|
+
const filterEntries = Object.entries(filter ?? {});
|
|
6873
|
+
const matchesFilter = (node) => filterEntries.length === 0 ? true : filterEntries.every(([key, value]) => node.metadata?.[key] === value);
|
|
6874
|
+
const nodesToSearch = Array.from(this.nodes.values()).filter(matchesFilter);
|
|
6875
|
+
const similarities = nodesToSearch.map((node) => ({
|
|
6860
6876
|
node,
|
|
6861
6877
|
similarity: this.cosineSimilarity(query, node.embedding)
|
|
6862
6878
|
}));
|
|
6863
6879
|
similarities.sort((a, b) => b.similarity - a.similarity);
|
|
6864
6880
|
const topNodes = similarities.slice(0, topK);
|
|
6881
|
+
const useFilter = filterEntries.length > 0;
|
|
6882
|
+
const allowedNodeIds = useFilter ? new Set(nodesToSearch.map((n) => n.id)) : void 0;
|
|
6865
6883
|
const rerankedNodes = /* @__PURE__ */ new Map();
|
|
6866
6884
|
for (const { node, similarity } of topNodes) {
|
|
6867
|
-
const walkScores = this.randomWalkWithRestart(node.id, randomWalkSteps, restartProb);
|
|
6885
|
+
const walkScores = this.randomWalkWithRestart(node.id, randomWalkSteps, restartProb, allowedNodeIds);
|
|
6868
6886
|
for (const [nodeId, walkScore] of walkScores) {
|
|
6869
6887
|
const node2 = this.nodes.get(nodeId);
|
|
6870
6888
|
const existingScore = rerankedNodes.get(nodeId)?.score || 0;
|