@mastra/rag 0.1.24-alpha.0 → 0.2.0-alpha.1
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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +23 -0
- package/dist/_tsup-dts-rollup.d.cts +263 -5
- package/dist/_tsup-dts-rollup.d.ts +263 -5
- package/dist/index.cjs +92 -13
- package/dist/index.js +92 -13
- package/package.json +8 -5
- package/src/graph-rag/index.ts +1 -1
- package/src/rerank/index.ts +4 -3
- package/src/tools/graph-rag.ts +23 -3
- package/src/tools/vector-query.ts +28 -4
- package/src/utils/convert-sources.ts +43 -0
|
@@ -3,9 +3,13 @@ import type { EmbeddingModel } from 'ai';
|
|
|
3
3
|
import type { MastraLanguageModel } from '@mastra/core/agent';
|
|
4
4
|
import type { MastraVector } from '@mastra/core/vector';
|
|
5
5
|
import type { QueryResult } from '@mastra/core/vector';
|
|
6
|
+
import type { QueryResult as QueryResult_2 } from '@mastra/core';
|
|
6
7
|
import type { TiktokenEncoding } from 'js-tiktoken';
|
|
7
8
|
import type { TiktokenModel } from 'js-tiktoken';
|
|
9
|
+
import { Tool } from '@mastra/core/tools';
|
|
10
|
+
import { ToolExecutionContext } from '@mastra/core';
|
|
8
11
|
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
12
|
+
import { z } from 'zod';
|
|
9
13
|
|
|
10
14
|
/** @deprecated Import from @mastra/astra instead */
|
|
11
15
|
declare const ASTRA_PROMPT = "When querying Astra, you can ONLY use the operators listed below. Any other operators will be rejected.\nImportant: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.\nIf a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.\n\nBasic Comparison Operators:\n- $eq: Exact match (default when using field: value)\n Example: { \"category\": \"electronics\" }\n- $ne: Not equal\n Example: { \"category\": { \"$ne\": \"electronics\" } }\n- $gt: Greater than\n Example: { \"price\": { \"$gt\": 100 } }\n- $gte: Greater than or equal\n Example: { \"price\": { \"$gte\": 100 } }\n- $lt: Less than\n Example: { \"price\": { \"$lt\": 100 } }\n- $lte: Less than or equal\n Example: { \"price\": { \"$lte\": 100 } }\n\nArray Operators:\n- $in: Match any value in array\n Example: { \"category\": { \"$in\": [\"electronics\", \"books\"] } }\n- $nin: Does not match any value in array\n Example: { \"category\": { \"$nin\": [\"electronics\", \"books\"] } }\n- $all: Match all values in array\n Example: { \"tags\": { \"$all\": [\"premium\", \"sale\"] } }\n\nLogical Operators:\n- $and: Logical AND (can be implicit or explicit)\n Implicit Example: { \"price\": { \"$gt\": 100 }, \"category\": \"electronics\" }\n Explicit Example: { \"$and\": [{ \"price\": { \"$gt\": 100 } }, { \"category\": \"electronics\" }] }\n- $or: Logical OR\n Example: { \"$or\": [{ \"price\": { \"$lt\": 50 } }, { \"category\": \"books\" }] }\n- $not: Logical NOT\n Example: { \"$not\": { \"category\": \"electronics\" } }\n\nElement Operators:\n- $exists: Check if field exists\n Example: { \"rating\": { \"$exists\": true } }\n\nSpecial Operators:\n- $size: Array length check\n Example: { \"tags\": { \"$size\": 2 } }\n\nRestrictions:\n- Regex patterns are not supported\n- Only $and, $or, and $not logical operators are supported\n- Nested fields are supported using dot notation\n- Multiple conditions on the same field are supported with both implicit and explicit $and\n- Empty arrays in $in/$nin will return no results\n- A non-empty array is required for $all operator\n- Only logical operators ($and, $or, $not) can be used at the top level\n- All other operators must be used within a field condition\n Valid: { \"field\": { \"$gt\": 100 } }\n Valid: { \"$and\": [...] }\n Invalid: { \"$gt\": 100 }\n- Logical operators must contain field conditions, not direct operators\n Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n Invalid: { \"$and\": [{ \"$gt\": 100 }] }\n- $not operator:\n - Must be an object\n - Cannot be empty\n - Can be used at field level or top level\n - Valid: { \"$not\": { \"field\": \"value\" } }\n - Valid: { \"field\": { \"$not\": { \"$eq\": \"value\" } } }\n- Other logical operators ($and, $or):\n - Can only be used at top level or nested within other logical operators\n - Can not be used on a field level, or be nested inside a field\n - Can not be used inside an operator\n - Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n - Valid: { \"$or\": [{ \"$and\": [{ \"field\": { \"$gt\": 100 } }] }] }\n - Invalid: { \"field\": { \"$and\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$or\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$gt\": { \"$and\": [{...}] } } }\n\nExample Complex Query:\n{\n \"$and\": [\n { \"category\": { \"$in\": [\"electronics\", \"computers\"] } },\n { \"price\": { \"$gte\": 100, \"$lte\": 1000 } },\n { \"tags\": { \"$all\": [\"premium\"] } },\n { \"rating\": { \"$exists\": true, \"$gt\": 4 } },\n { \"$or\": [\n { \"stock\": { \"$gt\": 0 } },\n { \"preorder\": true }\n ]}\n ]\n}";
|
|
@@ -143,6 +147,19 @@ declare type ChunkStrategy = 'recursive' | 'character' | 'token' | 'markdown' |
|
|
|
143
147
|
export { ChunkStrategy }
|
|
144
148
|
export { ChunkStrategy as ChunkStrategy_alias_1 }
|
|
145
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Convert an array of source inputs (QueryResult, RankedNode, or RerankResult) to an array of sources.
|
|
152
|
+
* @param results Array of source inputs to convert.
|
|
153
|
+
* @returns Array of sources.
|
|
154
|
+
*/
|
|
155
|
+
export declare const convertToSources: (results: SourceInput[]) => {
|
|
156
|
+
id: string;
|
|
157
|
+
vector: number[];
|
|
158
|
+
score: number;
|
|
159
|
+
metadata: Record<string, any> | undefined;
|
|
160
|
+
document: string;
|
|
161
|
+
}[];
|
|
162
|
+
|
|
146
163
|
declare const createDocumentChunkerTool: ({ doc, params, }: {
|
|
147
164
|
doc: MDocument;
|
|
148
165
|
params?: ChunkParams;
|
|
@@ -151,11 +168,12 @@ export { createDocumentChunkerTool }
|
|
|
151
168
|
export { createDocumentChunkerTool as createDocumentChunkerTool_alias_1 }
|
|
152
169
|
export { createDocumentChunkerTool as createDocumentChunkerTool_alias_2 }
|
|
153
170
|
|
|
154
|
-
declare const createGraphRAGTool: ({ vectorStoreName, indexName, model, enableFilter, graphOptions, id, description, }: {
|
|
171
|
+
declare const createGraphRAGTool: ({ vectorStoreName, indexName, model, enableFilter, includeSources, graphOptions, id, description, }: {
|
|
155
172
|
vectorStoreName: string;
|
|
156
173
|
indexName: string;
|
|
157
174
|
model: EmbeddingModel<string>;
|
|
158
175
|
enableFilter?: boolean;
|
|
176
|
+
includeSources?: boolean;
|
|
159
177
|
graphOptions?: {
|
|
160
178
|
dimension?: number;
|
|
161
179
|
randomWalkSteps?: number;
|
|
@@ -164,20 +182,258 @@ declare const createGraphRAGTool: ({ vectorStoreName, indexName, model, enableFi
|
|
|
164
182
|
};
|
|
165
183
|
id?: string;
|
|
166
184
|
description?: string;
|
|
167
|
-
}) =>
|
|
185
|
+
}) => Tool<z.ZodObject<{
|
|
186
|
+
queryText: z.ZodString;
|
|
187
|
+
topK: z.ZodNumber;
|
|
188
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
189
|
+
queryText: z.ZodString;
|
|
190
|
+
topK: z.ZodNumber;
|
|
191
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
192
|
+
queryText: z.ZodString;
|
|
193
|
+
topK: z.ZodNumber;
|
|
194
|
+
}, z.ZodTypeAny, "passthrough">>, z.ZodObject<{
|
|
195
|
+
relevantContext: z.ZodAny;
|
|
196
|
+
sources: z.ZodArray<z.ZodObject<{
|
|
197
|
+
id: z.ZodString;
|
|
198
|
+
metadata: z.ZodAny;
|
|
199
|
+
vector: z.ZodArray<z.ZodNumber, "many">;
|
|
200
|
+
score: z.ZodNumber;
|
|
201
|
+
document: z.ZodString;
|
|
202
|
+
}, "strip", z.ZodTypeAny, {
|
|
203
|
+
id: string;
|
|
204
|
+
vector: number[];
|
|
205
|
+
score: number;
|
|
206
|
+
document: string;
|
|
207
|
+
metadata?: any;
|
|
208
|
+
}, {
|
|
209
|
+
id: string;
|
|
210
|
+
vector: number[];
|
|
211
|
+
score: number;
|
|
212
|
+
document: string;
|
|
213
|
+
metadata?: any;
|
|
214
|
+
}>, "many">;
|
|
215
|
+
}, "strip", z.ZodTypeAny, {
|
|
216
|
+
sources: {
|
|
217
|
+
id: string;
|
|
218
|
+
vector: number[];
|
|
219
|
+
score: number;
|
|
220
|
+
document: string;
|
|
221
|
+
metadata?: any;
|
|
222
|
+
}[];
|
|
223
|
+
relevantContext?: any;
|
|
224
|
+
}, {
|
|
225
|
+
sources: {
|
|
226
|
+
id: string;
|
|
227
|
+
vector: number[];
|
|
228
|
+
score: number;
|
|
229
|
+
document: string;
|
|
230
|
+
metadata?: any;
|
|
231
|
+
}[];
|
|
232
|
+
relevantContext?: any;
|
|
233
|
+
}>, ToolExecutionContext<z.ZodObject<{
|
|
234
|
+
queryText: z.ZodString;
|
|
235
|
+
topK: z.ZodNumber;
|
|
236
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
237
|
+
queryText: z.ZodString;
|
|
238
|
+
topK: z.ZodNumber;
|
|
239
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
240
|
+
queryText: z.ZodString;
|
|
241
|
+
topK: z.ZodNumber;
|
|
242
|
+
}, z.ZodTypeAny, "passthrough">>>> & {
|
|
243
|
+
inputSchema: z.ZodObject<{
|
|
244
|
+
queryText: z.ZodString;
|
|
245
|
+
topK: z.ZodNumber;
|
|
246
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
247
|
+
queryText: z.ZodString;
|
|
248
|
+
topK: z.ZodNumber;
|
|
249
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
250
|
+
queryText: z.ZodString;
|
|
251
|
+
topK: z.ZodNumber;
|
|
252
|
+
}, z.ZodTypeAny, "passthrough">>;
|
|
253
|
+
outputSchema: z.ZodObject<{
|
|
254
|
+
relevantContext: z.ZodAny;
|
|
255
|
+
sources: z.ZodArray<z.ZodObject<{
|
|
256
|
+
id: z.ZodString;
|
|
257
|
+
metadata: z.ZodAny;
|
|
258
|
+
vector: z.ZodArray<z.ZodNumber, "many">;
|
|
259
|
+
score: z.ZodNumber;
|
|
260
|
+
document: z.ZodString;
|
|
261
|
+
}, "strip", z.ZodTypeAny, {
|
|
262
|
+
id: string;
|
|
263
|
+
vector: number[];
|
|
264
|
+
score: number;
|
|
265
|
+
document: string;
|
|
266
|
+
metadata?: any;
|
|
267
|
+
}, {
|
|
268
|
+
id: string;
|
|
269
|
+
vector: number[];
|
|
270
|
+
score: number;
|
|
271
|
+
document: string;
|
|
272
|
+
metadata?: any;
|
|
273
|
+
}>, "many">;
|
|
274
|
+
}, "strip", z.ZodTypeAny, {
|
|
275
|
+
sources: {
|
|
276
|
+
id: string;
|
|
277
|
+
vector: number[];
|
|
278
|
+
score: number;
|
|
279
|
+
document: string;
|
|
280
|
+
metadata?: any;
|
|
281
|
+
}[];
|
|
282
|
+
relevantContext?: any;
|
|
283
|
+
}, {
|
|
284
|
+
sources: {
|
|
285
|
+
id: string;
|
|
286
|
+
vector: number[];
|
|
287
|
+
score: number;
|
|
288
|
+
document: string;
|
|
289
|
+
metadata?: any;
|
|
290
|
+
}[];
|
|
291
|
+
relevantContext?: any;
|
|
292
|
+
}>;
|
|
293
|
+
execute: (context: ToolExecutionContext<z.ZodObject<{
|
|
294
|
+
queryText: z.ZodString;
|
|
295
|
+
topK: z.ZodNumber;
|
|
296
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
297
|
+
queryText: z.ZodString;
|
|
298
|
+
topK: z.ZodNumber;
|
|
299
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
300
|
+
queryText: z.ZodString;
|
|
301
|
+
topK: z.ZodNumber;
|
|
302
|
+
}, z.ZodTypeAny, "passthrough">>>) => Promise<any>;
|
|
303
|
+
};
|
|
168
304
|
export { createGraphRAGTool }
|
|
169
305
|
export { createGraphRAGTool as createGraphRAGTool_alias_1 }
|
|
170
306
|
export { createGraphRAGTool as createGraphRAGTool_alias_2 }
|
|
171
307
|
|
|
172
|
-
declare const createVectorQueryTool: ({ vectorStoreName, indexName, model, enableFilter, reranker, id, description, }: {
|
|
308
|
+
declare const createVectorQueryTool: ({ vectorStoreName, indexName, model, enableFilter, includeVectors, includeSources, reranker, id, description, }: {
|
|
173
309
|
vectorStoreName: string;
|
|
174
310
|
indexName: string;
|
|
175
311
|
model: EmbeddingModel<string>;
|
|
176
312
|
enableFilter?: boolean;
|
|
313
|
+
includeVectors?: boolean;
|
|
314
|
+
includeSources?: boolean;
|
|
177
315
|
reranker?: RerankConfig;
|
|
178
316
|
id?: string;
|
|
179
317
|
description?: string;
|
|
180
|
-
}) =>
|
|
318
|
+
}) => Tool<z.ZodObject<{
|
|
319
|
+
queryText: z.ZodString;
|
|
320
|
+
topK: z.ZodNumber;
|
|
321
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
322
|
+
queryText: z.ZodString;
|
|
323
|
+
topK: z.ZodNumber;
|
|
324
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
325
|
+
queryText: z.ZodString;
|
|
326
|
+
topK: z.ZodNumber;
|
|
327
|
+
}, z.ZodTypeAny, "passthrough">>, z.ZodObject<{
|
|
328
|
+
relevantContext: z.ZodAny;
|
|
329
|
+
sources: z.ZodArray<z.ZodObject<{
|
|
330
|
+
id: z.ZodString;
|
|
331
|
+
metadata: z.ZodAny;
|
|
332
|
+
vector: z.ZodArray<z.ZodNumber, "many">;
|
|
333
|
+
score: z.ZodNumber;
|
|
334
|
+
document: z.ZodString;
|
|
335
|
+
}, "strip", z.ZodTypeAny, {
|
|
336
|
+
id: string;
|
|
337
|
+
vector: number[];
|
|
338
|
+
score: number;
|
|
339
|
+
document: string;
|
|
340
|
+
metadata?: any;
|
|
341
|
+
}, {
|
|
342
|
+
id: string;
|
|
343
|
+
vector: number[];
|
|
344
|
+
score: number;
|
|
345
|
+
document: string;
|
|
346
|
+
metadata?: any;
|
|
347
|
+
}>, "many">;
|
|
348
|
+
}, "strip", z.ZodTypeAny, {
|
|
349
|
+
sources: {
|
|
350
|
+
id: string;
|
|
351
|
+
vector: number[];
|
|
352
|
+
score: number;
|
|
353
|
+
document: string;
|
|
354
|
+
metadata?: any;
|
|
355
|
+
}[];
|
|
356
|
+
relevantContext?: any;
|
|
357
|
+
}, {
|
|
358
|
+
sources: {
|
|
359
|
+
id: string;
|
|
360
|
+
vector: number[];
|
|
361
|
+
score: number;
|
|
362
|
+
document: string;
|
|
363
|
+
metadata?: any;
|
|
364
|
+
}[];
|
|
365
|
+
relevantContext?: any;
|
|
366
|
+
}>, ToolExecutionContext<z.ZodObject<{
|
|
367
|
+
queryText: z.ZodString;
|
|
368
|
+
topK: z.ZodNumber;
|
|
369
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
370
|
+
queryText: z.ZodString;
|
|
371
|
+
topK: z.ZodNumber;
|
|
372
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
373
|
+
queryText: z.ZodString;
|
|
374
|
+
topK: z.ZodNumber;
|
|
375
|
+
}, z.ZodTypeAny, "passthrough">>>> & {
|
|
376
|
+
inputSchema: z.ZodObject<{
|
|
377
|
+
queryText: z.ZodString;
|
|
378
|
+
topK: z.ZodNumber;
|
|
379
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
380
|
+
queryText: z.ZodString;
|
|
381
|
+
topK: z.ZodNumber;
|
|
382
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
383
|
+
queryText: z.ZodString;
|
|
384
|
+
topK: z.ZodNumber;
|
|
385
|
+
}, z.ZodTypeAny, "passthrough">>;
|
|
386
|
+
outputSchema: z.ZodObject<{
|
|
387
|
+
relevantContext: z.ZodAny;
|
|
388
|
+
sources: z.ZodArray<z.ZodObject<{
|
|
389
|
+
id: z.ZodString;
|
|
390
|
+
metadata: z.ZodAny;
|
|
391
|
+
vector: z.ZodArray<z.ZodNumber, "many">;
|
|
392
|
+
score: z.ZodNumber;
|
|
393
|
+
document: z.ZodString;
|
|
394
|
+
}, "strip", z.ZodTypeAny, {
|
|
395
|
+
id: string;
|
|
396
|
+
vector: number[];
|
|
397
|
+
score: number;
|
|
398
|
+
document: string;
|
|
399
|
+
metadata?: any;
|
|
400
|
+
}, {
|
|
401
|
+
id: string;
|
|
402
|
+
vector: number[];
|
|
403
|
+
score: number;
|
|
404
|
+
document: string;
|
|
405
|
+
metadata?: any;
|
|
406
|
+
}>, "many">;
|
|
407
|
+
}, "strip", z.ZodTypeAny, {
|
|
408
|
+
sources: {
|
|
409
|
+
id: string;
|
|
410
|
+
vector: number[];
|
|
411
|
+
score: number;
|
|
412
|
+
document: string;
|
|
413
|
+
metadata?: any;
|
|
414
|
+
}[];
|
|
415
|
+
relevantContext?: any;
|
|
416
|
+
}, {
|
|
417
|
+
sources: {
|
|
418
|
+
id: string;
|
|
419
|
+
vector: number[];
|
|
420
|
+
score: number;
|
|
421
|
+
document: string;
|
|
422
|
+
metadata?: any;
|
|
423
|
+
}[];
|
|
424
|
+
relevantContext?: any;
|
|
425
|
+
}>;
|
|
426
|
+
execute: (context: ToolExecutionContext<z.ZodObject<{
|
|
427
|
+
queryText: z.ZodString;
|
|
428
|
+
topK: z.ZodNumber;
|
|
429
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
430
|
+
queryText: z.ZodString;
|
|
431
|
+
topK: z.ZodNumber;
|
|
432
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
433
|
+
queryText: z.ZodString;
|
|
434
|
+
topK: z.ZodNumber;
|
|
435
|
+
}, z.ZodTypeAny, "passthrough">>>) => Promise<any>;
|
|
436
|
+
};
|
|
181
437
|
export { createVectorQueryTool }
|
|
182
438
|
export { createVectorQueryTool as createVectorQueryTool_alias_1 }
|
|
183
439
|
export { createVectorQueryTool as createVectorQueryTool_alias_2 }
|
|
@@ -622,7 +878,7 @@ declare class QuestionsAnsweredExtractor extends BaseExtractor {
|
|
|
622
878
|
export { QuestionsAnsweredExtractor }
|
|
623
879
|
export { QuestionsAnsweredExtractor as QuestionsAnsweredExtractor_alias_1 }
|
|
624
880
|
|
|
625
|
-
declare interface RankedNode extends GraphNode {
|
|
881
|
+
export declare interface RankedNode extends GraphNode {
|
|
626
882
|
score: number;
|
|
627
883
|
}
|
|
628
884
|
|
|
@@ -796,6 +1052,8 @@ declare interface ScoringDetails {
|
|
|
796
1052
|
};
|
|
797
1053
|
}
|
|
798
1054
|
|
|
1055
|
+
declare type SourceInput = QueryResult_2 | RankedNode | RerankResult;
|
|
1056
|
+
|
|
799
1057
|
export declare function splitTextOnTokens({ text, tokenizer }: {
|
|
800
1058
|
text: string;
|
|
801
1059
|
tokenizer: Tokenizer;
|
package/dist/index.cjs
CHANGED
|
@@ -5,6 +5,7 @@ var zod = require('zod');
|
|
|
5
5
|
var nodeHtmlBetterParser = require('node-html-better-parser');
|
|
6
6
|
var jsTiktoken = require('js-tiktoken');
|
|
7
7
|
var relevance = require('@mastra/core/relevance');
|
|
8
|
+
var big_js = require('big.js');
|
|
8
9
|
var tools = require('@mastra/core/tools');
|
|
9
10
|
var ai = require('ai');
|
|
10
11
|
|
|
@@ -5950,9 +5951,9 @@ async function rerank(results, query, model, options) {
|
|
|
5950
5951
|
...DEFAULT_WEIGHTS,
|
|
5951
5952
|
...options.weights
|
|
5952
5953
|
};
|
|
5953
|
-
const
|
|
5954
|
-
if (
|
|
5955
|
-
throw new Error(
|
|
5954
|
+
const sum = Object.values(weights).reduce((acc, w) => acc.plus(w.toString()), new big_js.Big(0));
|
|
5955
|
+
if (!sum.eq(1)) {
|
|
5956
|
+
throw new Error(`Weights must add up to 1. Got ${sum} from ${weights}`);
|
|
5956
5957
|
}
|
|
5957
5958
|
const resultLength = results.length;
|
|
5958
5959
|
const queryAnalysis = queryEmbedding ? analyzeQueryEmbedding(queryEmbedding) : null;
|
|
@@ -6271,12 +6272,44 @@ var filterDescription = `JSON-formatted criteria to refine search results.
|
|
|
6271
6272
|
- IMPORTANT: Always ensure JSON is properly closed with matching brackets
|
|
6272
6273
|
- Multiple filters can be combined`;
|
|
6273
6274
|
|
|
6275
|
+
// src/utils/convert-sources.ts
|
|
6276
|
+
var convertToSources = (results) => {
|
|
6277
|
+
return results.map((result) => {
|
|
6278
|
+
if ("content" in result) {
|
|
6279
|
+
return {
|
|
6280
|
+
id: result.id,
|
|
6281
|
+
vector: result.embedding || [],
|
|
6282
|
+
score: result.score,
|
|
6283
|
+
metadata: result.metadata,
|
|
6284
|
+
document: result.content || ""
|
|
6285
|
+
};
|
|
6286
|
+
}
|
|
6287
|
+
if ("result" in result) {
|
|
6288
|
+
return {
|
|
6289
|
+
id: result.result.id,
|
|
6290
|
+
vector: result.result.vector || [],
|
|
6291
|
+
score: result.score,
|
|
6292
|
+
metadata: result.result.metadata,
|
|
6293
|
+
document: result.result.document || ""
|
|
6294
|
+
};
|
|
6295
|
+
}
|
|
6296
|
+
return {
|
|
6297
|
+
id: result.id,
|
|
6298
|
+
vector: result.vector || [],
|
|
6299
|
+
score: result.score,
|
|
6300
|
+
metadata: result.metadata,
|
|
6301
|
+
document: result.document || ""
|
|
6302
|
+
};
|
|
6303
|
+
});
|
|
6304
|
+
};
|
|
6305
|
+
|
|
6274
6306
|
// src/tools/graph-rag.ts
|
|
6275
6307
|
var createGraphRAGTool = ({
|
|
6276
6308
|
vectorStoreName,
|
|
6277
6309
|
indexName,
|
|
6278
6310
|
model,
|
|
6279
6311
|
enableFilter = false,
|
|
6312
|
+
includeSources = true,
|
|
6280
6313
|
graphOptions = {
|
|
6281
6314
|
dimension: 1536,
|
|
6282
6315
|
randomWalkSteps: 100,
|
|
@@ -6301,8 +6334,27 @@ var createGraphRAGTool = ({
|
|
|
6301
6334
|
return tools.createTool({
|
|
6302
6335
|
id: toolId,
|
|
6303
6336
|
inputSchema,
|
|
6337
|
+
// Output schema includes `sources`, which exposes the full set of retrieved chunks (QueryResult objects)
|
|
6338
|
+
// Each source contains all information needed to reference
|
|
6339
|
+
// the original document, chunk, and similarity score.
|
|
6304
6340
|
outputSchema: zod.z.object({
|
|
6305
|
-
|
|
6341
|
+
// Array of metadata or content for compatibility with prior usage
|
|
6342
|
+
relevantContext: zod.z.any(),
|
|
6343
|
+
// Array of full retrieval result objects
|
|
6344
|
+
sources: zod.z.array(
|
|
6345
|
+
zod.z.object({
|
|
6346
|
+
id: zod.z.string(),
|
|
6347
|
+
// Unique chunk/document identifier
|
|
6348
|
+
metadata: zod.z.any(),
|
|
6349
|
+
// All metadata fields (document ID, etc.)
|
|
6350
|
+
vector: zod.z.array(zod.z.number()),
|
|
6351
|
+
// Embedding vector (if available)
|
|
6352
|
+
score: zod.z.number(),
|
|
6353
|
+
// Similarity score for this retrieval
|
|
6354
|
+
document: zod.z.string()
|
|
6355
|
+
// Full chunk/document text (if available)
|
|
6356
|
+
})
|
|
6357
|
+
)
|
|
6306
6358
|
}),
|
|
6307
6359
|
description: toolDescription,
|
|
6308
6360
|
execute: async ({ context: { queryText, topK, filter }, mastra }) => {
|
|
@@ -6322,7 +6374,7 @@ var createGraphRAGTool = ({
|
|
|
6322
6374
|
if (logger) {
|
|
6323
6375
|
logger.error("Vector store not found", { vectorStoreName });
|
|
6324
6376
|
}
|
|
6325
|
-
return { relevantContext: [] };
|
|
6377
|
+
return { relevantContext: [], sources: [] };
|
|
6326
6378
|
}
|
|
6327
6379
|
let queryFilter = {};
|
|
6328
6380
|
if (enableFilter) {
|
|
@@ -6381,8 +6433,10 @@ var createGraphRAGTool = ({
|
|
|
6381
6433
|
if (logger) {
|
|
6382
6434
|
logger.debug("Returning relevant context chunks", { count: relevantChunks.length });
|
|
6383
6435
|
}
|
|
6436
|
+
const sources = includeSources ? convertToSources(rerankedResults) : [];
|
|
6384
6437
|
return {
|
|
6385
|
-
relevantContext: relevantChunks
|
|
6438
|
+
relevantContext: relevantChunks,
|
|
6439
|
+
sources
|
|
6386
6440
|
};
|
|
6387
6441
|
} catch (err) {
|
|
6388
6442
|
if (logger) {
|
|
@@ -6392,7 +6446,7 @@ var createGraphRAGTool = ({
|
|
|
6392
6446
|
errorStack: err instanceof Error ? err.stack : void 0
|
|
6393
6447
|
});
|
|
6394
6448
|
}
|
|
6395
|
-
return { relevantContext: [] };
|
|
6449
|
+
return { relevantContext: [], sources: [] };
|
|
6396
6450
|
}
|
|
6397
6451
|
}
|
|
6398
6452
|
});
|
|
@@ -6402,6 +6456,8 @@ var createVectorQueryTool = ({
|
|
|
6402
6456
|
indexName,
|
|
6403
6457
|
model,
|
|
6404
6458
|
enableFilter = false,
|
|
6459
|
+
includeVectors = false,
|
|
6460
|
+
includeSources = true,
|
|
6405
6461
|
reranker,
|
|
6406
6462
|
id,
|
|
6407
6463
|
description
|
|
@@ -6419,8 +6475,27 @@ var createVectorQueryTool = ({
|
|
|
6419
6475
|
return tools.createTool({
|
|
6420
6476
|
id: toolId,
|
|
6421
6477
|
inputSchema,
|
|
6478
|
+
// Output schema includes `sources`, which exposes the full set of retrieved chunks (QueryResult objects)
|
|
6479
|
+
// Each source contains all information needed to reference
|
|
6480
|
+
// the original document, chunk, and similarity score.
|
|
6422
6481
|
outputSchema: zod.z.object({
|
|
6423
|
-
|
|
6482
|
+
// Array of metadata or content for compatibility with prior usage
|
|
6483
|
+
relevantContext: zod.z.any(),
|
|
6484
|
+
// Array of full retrieval result objects
|
|
6485
|
+
sources: zod.z.array(
|
|
6486
|
+
zod.z.object({
|
|
6487
|
+
id: zod.z.string(),
|
|
6488
|
+
// Unique chunk/document identifier
|
|
6489
|
+
metadata: zod.z.any(),
|
|
6490
|
+
// All metadata fields (document ID, etc.)
|
|
6491
|
+
vector: zod.z.array(zod.z.number()),
|
|
6492
|
+
// Embedding vector (if available)
|
|
6493
|
+
score: zod.z.number(),
|
|
6494
|
+
// Similarity score for this retrieval
|
|
6495
|
+
document: zod.z.string()
|
|
6496
|
+
// Full chunk/document text (if available)
|
|
6497
|
+
})
|
|
6498
|
+
)
|
|
6424
6499
|
}),
|
|
6425
6500
|
description: toolDescription,
|
|
6426
6501
|
execute: async ({ context: { queryText, topK, filter }, mastra }) => {
|
|
@@ -6440,7 +6515,7 @@ var createVectorQueryTool = ({
|
|
|
6440
6515
|
if (logger) {
|
|
6441
6516
|
logger.error("Vector store not found", { vectorStoreName });
|
|
6442
6517
|
}
|
|
6443
|
-
return { relevantContext: [] };
|
|
6518
|
+
return { relevantContext: [], sources: [] };
|
|
6444
6519
|
}
|
|
6445
6520
|
let queryFilter = {};
|
|
6446
6521
|
if (enableFilter && filter) {
|
|
@@ -6464,7 +6539,8 @@ var createVectorQueryTool = ({
|
|
|
6464
6539
|
queryText,
|
|
6465
6540
|
model,
|
|
6466
6541
|
queryFilter: Object.keys(queryFilter || {}).length > 0 ? queryFilter : void 0,
|
|
6467
|
-
topK: topKValue
|
|
6542
|
+
topK: topKValue,
|
|
6543
|
+
includeVectors
|
|
6468
6544
|
});
|
|
6469
6545
|
if (logger) {
|
|
6470
6546
|
logger.debug("vectorQuerySearch returned results", { count: results.length });
|
|
@@ -6484,14 +6560,17 @@ var createVectorQueryTool = ({
|
|
|
6484
6560
|
if (logger) {
|
|
6485
6561
|
logger.debug("Returning reranked relevant context chunks", { count: relevantChunks2.length });
|
|
6486
6562
|
}
|
|
6487
|
-
|
|
6563
|
+
const sources2 = includeSources ? convertToSources(rerankedResults) : [];
|
|
6564
|
+
return { relevantContext: relevantChunks2, sources: sources2 };
|
|
6488
6565
|
}
|
|
6489
6566
|
const relevantChunks = results.map((result) => result?.metadata);
|
|
6490
6567
|
if (logger) {
|
|
6491
6568
|
logger.debug("Returning relevant context chunks", { count: relevantChunks.length });
|
|
6492
6569
|
}
|
|
6570
|
+
const sources = includeSources ? convertToSources(results) : [];
|
|
6493
6571
|
return {
|
|
6494
|
-
relevantContext: relevantChunks
|
|
6572
|
+
relevantContext: relevantChunks,
|
|
6573
|
+
sources
|
|
6495
6574
|
};
|
|
6496
6575
|
} catch (err) {
|
|
6497
6576
|
if (logger) {
|
|
@@ -6501,7 +6580,7 @@ var createVectorQueryTool = ({
|
|
|
6501
6580
|
errorStack: err instanceof Error ? err.stack : void 0
|
|
6502
6581
|
});
|
|
6503
6582
|
}
|
|
6504
|
-
return { relevantContext: [] };
|
|
6583
|
+
return { relevantContext: [], sources: [] };
|
|
6505
6584
|
}
|
|
6506
6585
|
}
|
|
6507
6586
|
});
|