@mastra/rag 2.0.0-beta.3 → 2.0.0-beta.5

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.
Files changed (37) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/dist/docs/README.md +32 -0
  3. package/dist/docs/SKILL.md +33 -0
  4. package/dist/docs/SOURCE_MAP.json +6 -0
  5. package/dist/docs/rag/01-overview.md +74 -0
  6. package/dist/docs/rag/02-chunking-and-embedding.md +190 -0
  7. package/dist/docs/rag/03-retrieval.md +549 -0
  8. package/dist/docs/rag/04-graph-rag.md +217 -0
  9. package/dist/docs/rag/05-reference.md +854 -0
  10. package/dist/docs/tools/01-reference.md +684 -0
  11. package/dist/document/extractors/keywords.d.ts +2 -2
  12. package/dist/document/extractors/keywords.d.ts.map +1 -1
  13. package/dist/document/extractors/questions.d.ts +2 -2
  14. package/dist/document/extractors/questions.d.ts.map +1 -1
  15. package/dist/document/extractors/summary.d.ts.map +1 -1
  16. package/dist/document/extractors/title.d.ts +2 -2
  17. package/dist/document/extractors/title.d.ts.map +1 -1
  18. package/dist/document/extractors/types.d.ts +6 -6
  19. package/dist/document/extractors/types.d.ts.map +1 -1
  20. package/dist/index.cjs +130 -59
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.js +132 -61
  23. package/dist/index.js.map +1 -1
  24. package/dist/rerank/relevance/mastra-agent/index.d.ts +2 -2
  25. package/dist/rerank/relevance/mastra-agent/index.d.ts.map +1 -1
  26. package/dist/tools/graph-rag.d.ts.map +1 -1
  27. package/dist/tools/index.d.ts +1 -0
  28. package/dist/tools/index.d.ts.map +1 -1
  29. package/dist/tools/types.d.ts +171 -14
  30. package/dist/tools/types.d.ts.map +1 -1
  31. package/dist/tools/vector-query.d.ts.map +1 -1
  32. package/dist/utils/index.d.ts +1 -0
  33. package/dist/utils/index.d.ts.map +1 -1
  34. package/dist/utils/tool-helpers.d.ts +54 -0
  35. package/dist/utils/tool-helpers.d.ts.map +1 -0
  36. package/dist/utils/vector-search.d.ts.map +1 -1
  37. package/package.json +9 -9
@@ -0,0 +1,684 @@
1
+ # Tools API Reference
2
+
3
+ > API reference for tools - 3 entries
4
+
5
+
6
+ ---
7
+
8
+ ## Reference: createDocumentChunkerTool()
9
+
10
+ > Documentation for the Document Chunker Tool in Mastra, which splits documents into smaller chunks for efficient processing and retrieval.
11
+
12
+ The `createDocumentChunkerTool()` function creates a tool for splitting documents into smaller chunks for efficient processing and retrieval. It supports different chunking strategies and configurable parameters.
13
+
14
+ ## Basic Usage
15
+
16
+ ```typescript
17
+ import { createDocumentChunkerTool, MDocument } from "@mastra/rag";
18
+
19
+ const document = new MDocument({
20
+ text: "Your document content here...",
21
+ metadata: { source: "user-manual" },
22
+ });
23
+
24
+ const chunker = createDocumentChunkerTool({
25
+ doc: document,
26
+ params: {
27
+ strategy: "recursive",
28
+ size: 512,
29
+ overlap: 50,
30
+ separator: "\n",
31
+ },
32
+ });
33
+
34
+ const { chunks } = await chunker.execute();
35
+ ```
36
+
37
+ ## Parameters
38
+
39
+ ### ChunkParams
40
+
41
+ ## Returns
42
+
43
+ ## Example with Custom Parameters
44
+
45
+ ```typescript
46
+ const technicalDoc = new MDocument({
47
+ text: longDocumentContent,
48
+ metadata: {
49
+ type: "technical",
50
+ version: "1.0",
51
+ },
52
+ });
53
+
54
+ const chunker = createDocumentChunkerTool({
55
+ doc: technicalDoc,
56
+ params: {
57
+ strategy: "recursive",
58
+ size: 1024, // Larger chunks
59
+ overlap: 100, // More overlap
60
+ separator: "\n\n", // Split on double newlines
61
+ },
62
+ });
63
+
64
+ const { chunks } = await chunker.execute();
65
+
66
+ // Process the chunks
67
+ chunks.forEach((chunk, index) => {
68
+ console.log(`Chunk ${index + 1} length: ${chunk.content.length}`);
69
+ });
70
+ ```
71
+
72
+ ## Tool Details
73
+
74
+ The chunker is created as a Mastra tool with the following properties:
75
+
76
+ - **Tool ID**: `Document Chunker {strategy} {size}`
77
+ - **Description**: `Chunks document using {strategy} strategy with size {size} and {overlap} overlap`
78
+ - **Input Schema**: Empty object (no additional inputs required)
79
+ - **Output Schema**: Object containing the chunks array
80
+
81
+ ## Related
82
+
83
+ - [MDocument](../rag/document)
84
+ - [createVectorQueryTool](./vector-query-tool)
85
+
86
+ ---
87
+
88
+ ## Reference: createGraphRAGTool()
89
+
90
+ > Documentation for the GraphRAG Tool in Mastra, which enhances RAG by building a graph of semantic relationships between documents.
91
+
92
+ The `createGraphRAGTool()` creates a tool that enhances RAG by building a graph of semantic relationships between documents. It uses the `GraphRAG` system under the hood to provide graph-based retrieval, finding relevant content through both direct similarity and connected relationships.
93
+
94
+ ## Usage Example
95
+
96
+ ```typescript
97
+ import { createGraphRAGTool } from "@mastra/rag";
98
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
99
+
100
+ const graphTool = createGraphRAGTool({
101
+ vectorStoreName: "pinecone",
102
+ indexName: "docs",
103
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
104
+ graphOptions: {
105
+ dimension: 1536,
106
+ threshold: 0.7,
107
+ randomWalkSteps: 100,
108
+ restartProb: 0.15,
109
+ },
110
+ });
111
+ ```
112
+
113
+ ## Parameters
114
+
115
+ > **Note:**
116
+
117
+ **Parameter Requirements:** Most fields can be set at creation as defaults.
118
+ Some fields can be overridden at runtime via the request context or input. If
119
+ a required field is missing from both creation and runtime, an error will be
120
+ thrown. Note that `model`, `id`, and `description` can only be set at creation
121
+ time.
122
+
123
+ ### GraphOptions
124
+
125
+ ## Returns
126
+
127
+ The tool returns an object with:
128
+
129
+ ### QueryResult object structure
130
+
131
+ ```typescript
132
+ {
133
+ id: string; // Unique chunk/document identifier
134
+ metadata: any; // All metadata fields (document ID, etc.)
135
+ vector: number[]; // Embedding vector (if available)
136
+ score: number; // Similarity score for this retrieval
137
+ document: string; // Full chunk/document text (if available)
138
+ }
139
+ ```
140
+
141
+ ## Default Tool Description
142
+
143
+ The default description focuses on:
144
+
145
+ - Analyzing relationships between documents
146
+ - Finding patterns and connections
147
+ - Answering complex queries
148
+
149
+ ## Advanced Example
150
+
151
+ ```typescript
152
+ const graphTool = createGraphRAGTool({
153
+ vectorStoreName: "pinecone",
154
+ indexName: "docs",
155
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
156
+ graphOptions: {
157
+ dimension: 1536,
158
+ threshold: 0.8, // Higher similarity threshold
159
+ randomWalkSteps: 200, // More exploration steps
160
+ restartProb: 0.2, // Higher restart probability
161
+ },
162
+ });
163
+ ```
164
+
165
+ ## Example with Custom Description
166
+
167
+ ```typescript
168
+ const graphTool = createGraphRAGTool({
169
+ vectorStoreName: "pinecone",
170
+ indexName: "docs",
171
+ model: "openai/text-embedding-3-small ",
172
+ description:
173
+ "Analyze document relationships to find complex patterns and connections in our company's historical data",
174
+ });
175
+ ```
176
+
177
+ This example shows how to customize the tool description for a specific use case while maintaining its core purpose of relationship analysis.
178
+
179
+ ## Example: Using Request Context
180
+
181
+ ```typescript
182
+ const graphTool = createGraphRAGTool({
183
+ vectorStoreName: "pinecone",
184
+ indexName: "docs",
185
+ model: "openai/text-embedding-3-small ",
186
+ });
187
+ ```
188
+
189
+ When using request context, provide required parameters at execution time via the request context:
190
+
191
+ ```typescript
192
+ const requestContext = new RequestContext<{
193
+ vectorStoreName: string;
194
+ indexName: string;
195
+ topK: number;
196
+ filter: any;
197
+ }>();
198
+ requestContext.set("vectorStoreName", "my-store");
199
+ requestContext.set("indexName", "my-index");
200
+ requestContext.set("topK", 5);
201
+ requestContext.set("filter", { category: "docs" });
202
+ requestContext.set("randomWalkSteps", 100);
203
+ requestContext.set("restartProb", 0.15);
204
+
205
+ const response = await agent.generate(
206
+ "Find documentation from the knowledge base.",
207
+ {
208
+ requestContext,
209
+ },
210
+ );
211
+ ```
212
+
213
+ For more information on request context, please see:
214
+
215
+ - [Agent Request Context](https://mastra.ai/docs/v1/server/request-context)
216
+ - [Request Context](https://mastra.ai/docs/v1/server/request-context#accessing-values-with-tools)
217
+
218
+ ## Dynamic Vector Store for Multi-Tenant Applications
219
+
220
+ For multi-tenant applications where each tenant has isolated data, you can pass a resolver function instead of a static vector store:
221
+
222
+ ```typescript
223
+ import { createGraphRAGTool, VectorStoreResolver } from "@mastra/rag";
224
+ import { PgVector } from "@mastra/pg";
225
+
226
+ const vectorStoreResolver: VectorStoreResolver = async ({ requestContext }) => {
227
+ const tenantId = requestContext?.get("tenantId");
228
+
229
+ return new PgVector({
230
+ id: `pg-vector-${tenantId}`,
231
+ connectionString: process.env.POSTGRES_CONNECTION_STRING!,
232
+ schemaName: `tenant_${tenantId}`,
233
+ });
234
+ };
235
+
236
+ const graphTool = createGraphRAGTool({
237
+ indexName: "embeddings",
238
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
239
+ vectorStore: vectorStoreResolver,
240
+ });
241
+ ```
242
+
243
+ See [createVectorQueryTool - Dynamic Vector Store](./vector-query-tool#dynamic-vector-store-for-multi-tenant-applications) for more details.
244
+
245
+ ## Related
246
+
247
+ - [createVectorQueryTool](./vector-query-tool)
248
+ - [GraphRAG](../rag/graph-rag)
249
+
250
+ ---
251
+
252
+ ## Reference: createVectorQueryTool()
253
+
254
+ > Documentation for the Vector Query Tool in Mastra, which facilitates semantic search over vector stores with filtering and reranking capabilities.
255
+
256
+ The `createVectorQueryTool()` function creates a tool for semantic search over vector stores. It supports filtering, reranking, database-specific configurations, and integrates with various vector store backends.
257
+
258
+ ## Basic Usage
259
+
260
+ ```typescript
261
+ import { createVectorQueryTool } from "@mastra/rag";
262
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
263
+
264
+ const queryTool = createVectorQueryTool({
265
+ vectorStoreName: "pinecone",
266
+ indexName: "docs",
267
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
268
+ });
269
+ ```
270
+
271
+ ## Parameters
272
+
273
+ > **Note:**
274
+
275
+ **Parameter Requirements:** Most fields can be set at creation as defaults.
276
+ Some fields can be overridden at runtime via the request context or input. If
277
+ a required field is missing from both creation and runtime, an error will be
278
+ thrown. Note that `model`, `id`, and `description` can only be set at creation
279
+ time.
280
+
281
+ ### DatabaseConfig
282
+
283
+ The `DatabaseConfig` type allows you to specify database-specific configurations that are automatically applied to query operations. This enables you to take advantage of unique features and optimizations offered by different vector stores.
284
+
285
+ ### RerankConfig
286
+
287
+ ## Returns
288
+
289
+ The tool returns an object with:
290
+
291
+ ### QueryResult object structure
292
+
293
+ ```typescript
294
+ {
295
+ id: string; // Unique chunk/document identifier
296
+ metadata: any; // All metadata fields (document ID, etc.)
297
+ vector: number[]; // Embedding vector (if available)
298
+ score: number; // Similarity score for this retrieval
299
+ document: string; // Full chunk/document text (if available)
300
+ }
301
+ ```
302
+
303
+ ## Default Tool Description
304
+
305
+ The default description focuses on:
306
+
307
+ - Finding relevant information in stored knowledge
308
+ - Answering user questions
309
+ - Retrieving factual content
310
+
311
+ ## Result Handling
312
+
313
+ The tool determines the number of results to return based on the user's query, with a default of 10 results. This can be adjusted based on the query requirements.
314
+
315
+ ## Example with Filters
316
+
317
+ ```typescript
318
+ const queryTool = createVectorQueryTool({
319
+ vectorStoreName: "pinecone",
320
+ indexName: "docs",
321
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
322
+ enableFilter: true,
323
+ });
324
+ ```
325
+
326
+ With filtering enabled, the tool processes queries to construct metadata filters that combine with semantic search. The process works as follows:
327
+
328
+ 1. A user makes a query with specific filter requirements like "Find content where the 'version' field is greater than 2.0"
329
+ 2. The agent analyzes the query and constructs the appropriate filters:
330
+ ```typescript
331
+ {
332
+ "version": { "$gt": 2.0 }
333
+ }
334
+ ```
335
+
336
+ This agent-driven approach:
337
+
338
+ - Processes natural language queries into filter specifications
339
+ - Implements vector store-specific filter syntax
340
+ - Translates query terms to filter operators
341
+
342
+ For detailed filter syntax and store-specific capabilities, see the [Metadata Filters](../rag/metadata-filters) documentation.
343
+
344
+ For an example of how agent-driven filtering works, see the [Agent-Driven Metadata Filtering](https://github.com/mastra-ai/mastra/tree/main/examples/basics/rag/filter-rag) example.
345
+
346
+ ## Example with Reranking
347
+
348
+ ```typescript
349
+ const queryTool = createVectorQueryTool({
350
+ vectorStoreName: "milvus",
351
+ indexName: "documentation",
352
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
353
+ reranker: {
354
+ model: "openai/gpt-5.1",
355
+ options: {
356
+ weights: {
357
+ semantic: 0.5, // Semantic relevance weight
358
+ vector: 0.3, // Vector similarity weight
359
+ position: 0.2, // Original position weight
360
+ },
361
+ topK: 5,
362
+ },
363
+ },
364
+ });
365
+ ```
366
+
367
+ Reranking improves result quality by combining:
368
+
369
+ - Semantic relevance: Using LLM-based scoring of text similarity
370
+ - Vector similarity: Original vector distance scores
371
+ - Position bias: Consideration of original result ordering
372
+ - Query analysis: Adjustments based on query characteristics
373
+
374
+ The reranker processes the initial vector search results and returns a reordered list optimized for relevance.
375
+
376
+ ## Example with Custom Description
377
+
378
+ ```typescript
379
+ const queryTool = createVectorQueryTool({
380
+ vectorStoreName: "pinecone",
381
+ indexName: "docs",
382
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
383
+ description:
384
+ "Search through document archives to find relevant information for answering questions about company policies and procedures",
385
+ });
386
+ ```
387
+
388
+ This example shows how to customize the tool description for a specific use case while maintaining its core purpose of information retrieval.
389
+
390
+ ## Database-Specific Configuration Examples
391
+
392
+ The `databaseConfig` parameter allows you to leverage unique features and optimizations specific to each vector database. These configurations are automatically applied during query execution.
393
+
394
+ **pinecone:**
395
+
396
+ ### Pinecone Configuration
397
+
398
+ ```typescript
399
+ const pineconeQueryTool = createVectorQueryTool({
400
+ vectorStoreName: "pinecone",
401
+ indexName: "docs",
402
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
403
+ databaseConfig: {
404
+ pinecone: {
405
+ namespace: "production", // Organize vectors by environment
406
+ sparseVector: { // Enable hybrid search
407
+ indices: [0, 1, 2, 3],
408
+ values: [0.1, 0.2, 0.15, 0.05]
409
+ }
410
+ }
411
+ }
412
+ });
413
+ ```
414
+
415
+ **Pinecone Features:**
416
+ - **Namespace**: Isolate different data sets within the same index
417
+ - **Sparse Vector**: Combine dense and sparse embeddings for improved search quality
418
+ - **Use Cases**: Multi-tenant applications, hybrid semantic search
419
+
420
+
421
+
422
+ **pgvector:**
423
+
424
+ ### pgVector Configuration
425
+
426
+ ```typescript
427
+ const pgVectorQueryTool = createVectorQueryTool({
428
+ vectorStoreName: "postgres",
429
+ indexName: "embeddings",
430
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
431
+ databaseConfig: {
432
+ pgvector: {
433
+ minScore: 0.7, // Only return results above 70% similarity
434
+ ef: 200, // Higher value = better accuracy, slower search
435
+ probes: 10 // For IVFFlat: more probes = better recall
436
+ }
437
+ }
438
+ });
439
+ ```
440
+
441
+ **pgVector Features:**
442
+ - **minScore**: Filter out low-quality matches
443
+ - **ef (HNSW)**: Control accuracy vs speed for HNSW indexes
444
+ - **probes (IVFFlat)**: Control recall vs speed for IVFFlat indexes
445
+ - **Use Cases**: Performance tuning, quality filtering
446
+
447
+
448
+
449
+ **chroma:**
450
+
451
+ ### Chroma Configuration
452
+
453
+ ```typescript
454
+ const chromaQueryTool = createVectorQueryTool({
455
+ vectorStoreName: "chroma",
456
+ indexName: "documents",
457
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
458
+ databaseConfig: {
459
+ chroma: {
460
+ where: { // Metadata filtering
461
+ "category": "technical",
462
+ "status": "published"
463
+ },
464
+ whereDocument: { // Document content filtering
465
+ "$contains": "API"
466
+ }
467
+ }
468
+ }
469
+ });
470
+ ```
471
+
472
+ **Chroma Features:**
473
+ - **where**: Filter by metadata fields
474
+ - **whereDocument**: Filter by document content
475
+ - **Use Cases**: Advanced filtering, content-based search
476
+
477
+
478
+
479
+ **multiple-configs:**
480
+
481
+ ### Multiple Database Configurations
482
+
483
+ ```typescript
484
+ // Configure for multiple databases (useful for dynamic stores)
485
+ const multiDbQueryTool = createVectorQueryTool({
486
+ vectorStoreName: "dynamic-store", // Will be set at runtime
487
+ indexName: "docs",
488
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
489
+ databaseConfig: {
490
+ pinecone: {
491
+ namespace: "default"
492
+ },
493
+ pgvector: {
494
+ minScore: 0.8,
495
+ ef: 150
496
+ },
497
+ chroma: {
498
+ where: { "type": "documentation" }
499
+ }
500
+ }
501
+ });
502
+ ```
503
+
504
+ **Multi-Config Benefits:**
505
+ - Support multiple vector stores with one tool
506
+ - Database-specific optimizations are automatically applied
507
+ - Flexible deployment scenarios
508
+
509
+
510
+
511
+ ### Runtime Configuration Override
512
+
513
+ You can override database configurations at runtime to adapt to different scenarios:
514
+
515
+ ```typescript
516
+ import { RequestContext } from "@mastra/core/request-context";
517
+
518
+ const queryTool = createVectorQueryTool({
519
+ vectorStoreName: "pinecone",
520
+ indexName: "docs",
521
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
522
+ databaseConfig: {
523
+ pinecone: {
524
+ namespace: "development",
525
+ },
526
+ },
527
+ });
528
+
529
+ // Override at runtime
530
+ const requestContext = new RequestContext();
531
+ requestContext.set("databaseConfig", {
532
+ pinecone: {
533
+ namespace: "production", // Switch to production namespace
534
+ },
535
+ });
536
+
537
+ const response = await agent.generate("Find information about deployment", {
538
+ requestContext,
539
+ });
540
+ ```
541
+
542
+ This approach allows you to:
543
+
544
+ - Switch between environments (dev/staging/prod)
545
+ - Adjust performance parameters based on load
546
+ - Apply different filtering strategies per request
547
+
548
+ ## Example: Using Request Context
549
+
550
+ ```typescript
551
+ const queryTool = createVectorQueryTool({
552
+ vectorStoreName: "pinecone",
553
+ indexName: "docs",
554
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
555
+ });
556
+ ```
557
+
558
+ When using request context, provide required parameters at execution time via the request context:
559
+
560
+ ```typescript
561
+ const requestContext = new RequestContext<{
562
+ vectorStoreName: string;
563
+ indexName: string;
564
+ topK: number;
565
+ filter: VectorFilter;
566
+ databaseConfig: DatabaseConfig;
567
+ }>();
568
+ requestContext.set("vectorStoreName", "my-store");
569
+ requestContext.set("indexName", "my-index");
570
+ requestContext.set("topK", 5);
571
+ requestContext.set("filter", { category: "docs" });
572
+ requestContext.set("databaseConfig", {
573
+ pinecone: { namespace: "runtime-namespace" },
574
+ });
575
+ requestContext.set("model", "openai/text-embedding-3-small");
576
+
577
+ const response = await agent.generate(
578
+ "Find documentation from the knowledge base.",
579
+ {
580
+ requestContext,
581
+ },
582
+ );
583
+ ```
584
+
585
+ For more information on request context, please see:
586
+
587
+ - [Agent Request Context](https://mastra.ai/docs/v1/server/request-context)
588
+ - [Request Context](https://mastra.ai/docs/v1/server/request-context#accessing-values-with-tools)
589
+
590
+ ## Usage Without a Mastra Server
591
+
592
+ The tool can be used by itself to retrieve documents matching a query:
593
+
594
+ ```typescript title="src/index.ts"
595
+ import { RequestContext } from "@mastra/core/request-context";
596
+ import { createVectorQueryTool } from "@mastra/rag";
597
+ import { PgVector } from "@mastra/pg";
598
+
599
+ const pgVector = new PgVector({
600
+ id: 'pg-vector',
601
+ connectionString: process.env.POSTGRES_CONNECTION_STRING!,
602
+ });
603
+
604
+ const vectorQueryTool = createVectorQueryTool({
605
+ vectorStoreName: "pgVector", // optional since we're passing in a store
606
+ vectorStore: pgVector,
607
+ indexName: "embeddings",
608
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
609
+ });
610
+
611
+ const requestContext = new RequestContext();
612
+ const queryResult = await vectorQueryTool.execute(
613
+ { queryText: "foo", topK: 1 },
614
+ { requestContext }
615
+ );
616
+
617
+ console.log(queryResult.sources);
618
+ ```
619
+
620
+ ## Dynamic Vector Store for Multi-Tenant Applications
621
+
622
+ For multi-tenant applications where each tenant has isolated data (e.g., separate PostgreSQL schemas), you can pass a resolver function instead of a static vector store instance. The function receives the request context and can return the appropriate vector store for the current tenant:
623
+
624
+ ```typescript title="src/index.ts"
625
+ import { createVectorQueryTool, VectorStoreResolver } from "@mastra/rag";
626
+ import { PgVector } from "@mastra/pg";
627
+
628
+ // Cache for tenant-specific vector stores
629
+ const vectorStoreCache = new Map<string, PgVector>();
630
+
631
+ // Resolver function that returns the correct vector store based on tenant
632
+ const vectorStoreResolver: VectorStoreResolver = async ({ requestContext }) => {
633
+ const tenantId = requestContext?.get("tenantId");
634
+
635
+ if (!tenantId) {
636
+ throw new Error("tenantId is required in request context");
637
+ }
638
+
639
+ // Return cached instance or create new one
640
+ if (!vectorStoreCache.has(tenantId)) {
641
+ vectorStoreCache.set(tenantId, new PgVector({
642
+ id: `pg-vector-${tenantId}`,
643
+ connectionString: process.env.POSTGRES_CONNECTION_STRING!,
644
+ schemaName: `tenant_${tenantId}`, // Each tenant has their own schema
645
+ }));
646
+ }
647
+
648
+ return vectorStoreCache.get(tenantId)!;
649
+ };
650
+
651
+ const vectorQueryTool = createVectorQueryTool({
652
+ indexName: "embeddings",
653
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
654
+ vectorStore: vectorStoreResolver, // Dynamic resolution!
655
+ });
656
+
657
+ // Usage with tenant context
658
+ const requestContext = new RequestContext();
659
+ requestContext.set("tenantId", "acme-corp");
660
+
661
+ const result = await vectorQueryTool.execute(
662
+ { queryText: "company policies", topK: 5 },
663
+ { requestContext }
664
+ );
665
+ ```
666
+
667
+ This pattern is similar to how `Agent.memory` supports dynamic configuration and enables:
668
+
669
+ - **Schema isolation**: Each tenant's data in separate PostgreSQL schemas
670
+ - **Database isolation**: Route to different database instances per tenant
671
+ - **Dynamic configuration**: Adjust vector store settings based on request context
672
+
673
+ ## Tool Details
674
+
675
+ The tool is created with:
676
+
677
+ - **ID**: `VectorQuery {vectorStoreName} {indexName} Tool`
678
+ - **Input Schema**: Requires queryText and filter objects
679
+ - **Output Schema**: Returns relevantContext string
680
+
681
+ ## Related
682
+
683
+ - [rerank()](../rag/rerank)
684
+ - [createGraphRAGTool](./graph-rag-tool)
@@ -1,4 +1,4 @@
1
- import type { MastraLanguageModel } from '@mastra/core/agent';
1
+ import type { MastraLanguageModel, MastraLegacyLanguageModel } from '@mastra/core/agent';
2
2
  import type { KeywordExtractPrompt } from '../prompts/index.js';
3
3
  import type { BaseNode } from '../schema/index.js';
4
4
  import { BaseExtractor } from './base.js';
@@ -13,7 +13,7 @@ type ExtractKeyword = {
13
13
  * Extract keywords from a list of nodes.
14
14
  */
15
15
  export declare class KeywordExtractor extends BaseExtractor {
16
- llm: MastraLanguageModel;
16
+ llm: MastraLanguageModel | MastraLegacyLanguageModel;
17
17
  keywords: number;
18
18
  promptTemplate: KeywordExtractPrompt;
19
19
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"keywords.d.ts","sourceRoot":"","sources":["../../../src/document/extractors/keywords.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAE9D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AACvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEvC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD,KAAK,cAAc,GAAG;IACpB;;OAEG;IACH,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,aAAa;IACjD,GAAG,EAAE,mBAAmB,CAAC;IACzB,QAAQ,EAAE,MAAM,CAAK;IACrB,cAAc,EAAE,oBAAoB,CAAC;IAErC;;;;;;OAMG;gBACS,OAAO,CAAC,EAAE,kBAAkB;IAexC;;;;OAIG;IACH;;;OAGG;IACG,wBAAwB,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,cAAc,CAAC;IAoDvE;;;;OAIG;IACH;;;;OAIG;IACG,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;CAKjE"}
1
+ {"version":3,"file":"keywords.d.ts","sourceRoot":"","sources":["../../../src/document/extractors/keywords.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAEzF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AACvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEvC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD,KAAK,cAAc,GAAG;IACpB;;OAEG;IACH,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,aAAa;IACjD,GAAG,EAAE,mBAAmB,GAAG,yBAAyB,CAAC;IACrD,QAAQ,EAAE,MAAM,CAAK;IACrB,cAAc,EAAE,oBAAoB,CAAC;IAErC;;;;;;OAMG;gBACS,OAAO,CAAC,EAAE,kBAAkB;IAexC;;;;OAIG;IACH;;;OAGG;IACG,wBAAwB,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,cAAc,CAAC;IAoDvE;;;;OAIG;IACH;;;;OAIG;IACG,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;CAKjE"}