@mastra/rag 2.1.1 → 2.1.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.
@@ -0,0 +1,182 @@
1
+ # createGraphRAGTool()
2
+
3
+ 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.
4
+
5
+ ## Usage Example
6
+
7
+ ```typescript
8
+ import { createGraphRAGTool } from '@mastra/rag'
9
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
10
+
11
+ const graphTool = createGraphRAGTool({
12
+ vectorStoreName: 'pinecone',
13
+ indexName: 'docs',
14
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
15
+ graphOptions: {
16
+ dimension: 1536,
17
+ threshold: 0.7,
18
+ randomWalkSteps: 100,
19
+ restartProb: 0.15,
20
+ },
21
+ })
22
+ ```
23
+
24
+ ## Parameters
25
+
26
+ > **Note:** **Parameter Requirements:** Most fields can be set at creation as defaults. Some fields can be overridden at runtime via the request context or input. If a required field is missing from both creation and runtime, an error will be thrown. Note that `model`, `id`, and `description` can only be set at creation time.
27
+
28
+ **id?:** (`string`): Custom ID for the tool. By default: 'GraphRAG {vectorStoreName} {indexName} Tool'. (Set at creation only.)
29
+
30
+ **description?:** (`string`): Custom description for the tool. By default: 'Access and analyze relationships between information in the knowledge base to answer complex questions about connections and patterns.' (Set at creation only.)
31
+
32
+ **vectorStoreName:** (`string`): Name of the vector store to query. (Can be set at creation or overridden at runtime.)
33
+
34
+ **indexName:** (`string`): Name of the index within the vector store. (Can be set at creation or overridden at runtime.)
35
+
36
+ **model:** (`EmbeddingModel`): Embedding model to use for vector search. (Set at creation only.)
37
+
38
+ **enableFilter?:** (`boolean`): Enable filtering of results based on metadata. (Set at creation only, but will be automatically enabled if a filter is provided in the request context.) (Default: `false`)
39
+
40
+ **includeSources?:** (`boolean`): Include the full retrieval objects in the results. (Can be set at creation or overridden at runtime.) (Default: `true`)
41
+
42
+ **graphOptions?:** (`GraphOptions`): Configuration for the graph-based retrieval (Default: `Default graph options`)
43
+
44
+ **providerOptions?:** (`Record<string, Record<string, any>>`): Provider-specific options for the embedding model (e.g., outputDimensionality). \*\*Important\*\*: Only works with AI SDK EmbeddingModelV2 models. For V1 models, configure options when creating the model itself.
45
+
46
+ **vectorStore?:** (`MastraVector | VectorStoreResolver`): Direct vector store instance or a resolver function for dynamic selection. Use a function for multi-tenant applications where the vector store is selected based on request context. When provided, \`vectorStoreName\` becomes optional.
47
+
48
+ ### GraphOptions
49
+
50
+ **dimension?:** (`number`): Dimension of the embedding vectors (Default: `1536`)
51
+
52
+ **threshold?:** (`number`): Similarity threshold for creating edges between nodes (0-1) (Default: `0.7`)
53
+
54
+ **randomWalkSteps?:** (`number`): Number of steps in random walk for graph traversal. (Can be set at creation or overridden at runtime.) (Default: `100`)
55
+
56
+ **restartProb?:** (`number`): Probability of restarting random walk from query node. (Can be set at creation or overridden at runtime.) (Default: `0.15`)
57
+
58
+ ## Returns
59
+
60
+ The tool returns an object with:
61
+
62
+ **relevantContext:** (`string`): Combined text from the most relevant document chunks, retrieved using graph-based ranking
63
+
64
+ **sources:** (`QueryResult[]`): Array of full retrieval result objects. Each object contains all information needed to reference the original document, chunk, and similarity score.
65
+
66
+ ### QueryResult object structure
67
+
68
+ ```typescript
69
+ {
70
+ id: string; // Unique chunk/document identifier
71
+ metadata: any; // All metadata fields (document ID, etc.)
72
+ vector: number[]; // Embedding vector (if available)
73
+ score: number; // Similarity score for this retrieval
74
+ document: string; // Full chunk/document text (if available)
75
+ }
76
+ ```
77
+
78
+ ## Default Tool Description
79
+
80
+ The default description focuses on:
81
+
82
+ - Analyzing relationships between documents
83
+ - Finding patterns and connections
84
+ - Answering complex queries
85
+
86
+ ## Advanced Example
87
+
88
+ ```typescript
89
+ const graphTool = createGraphRAGTool({
90
+ vectorStoreName: 'pinecone',
91
+ indexName: 'docs',
92
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
93
+ graphOptions: {
94
+ dimension: 1536,
95
+ threshold: 0.8, // Higher similarity threshold
96
+ randomWalkSteps: 200, // More exploration steps
97
+ restartProb: 0.2, // Higher restart probability
98
+ },
99
+ })
100
+ ```
101
+
102
+ ## Example with Custom Description
103
+
104
+ ```typescript
105
+ const graphTool = createGraphRAGTool({
106
+ vectorStoreName: 'pinecone',
107
+ indexName: 'docs',
108
+ model: 'openai/text-embedding-3-small ',
109
+ description:
110
+ "Analyze document relationships to find complex patterns and connections in our company's historical data",
111
+ })
112
+ ```
113
+
114
+ This example shows how to customize the tool description for a specific use case while maintaining its core purpose of relationship analysis.
115
+
116
+ ## Example: Using Request Context
117
+
118
+ ```typescript
119
+ const graphTool = createGraphRAGTool({
120
+ vectorStoreName: 'pinecone',
121
+ indexName: 'docs',
122
+ model: 'openai/text-embedding-3-small ',
123
+ })
124
+ ```
125
+
126
+ When using request context, provide required parameters at execution time via the request context:
127
+
128
+ ```typescript
129
+ const requestContext = new RequestContext<{
130
+ vectorStoreName: string
131
+ indexName: string
132
+ topK: number
133
+ filter: any
134
+ }>()
135
+ requestContext.set('vectorStoreName', 'my-store')
136
+ requestContext.set('indexName', 'my-index')
137
+ requestContext.set('topK', 5)
138
+ requestContext.set('filter', { category: 'docs' })
139
+ requestContext.set('randomWalkSteps', 100)
140
+ requestContext.set('restartProb', 0.15)
141
+
142
+ const response = await agent.generate('Find documentation from the knowledge base.', {
143
+ requestContext,
144
+ })
145
+ ```
146
+
147
+ For more information on request context, please see:
148
+
149
+ - [Agent Request Context](https://mastra.ai/docs/server/request-context)
150
+ - [Request Context](https://mastra.ai/docs/server/request-context)
151
+
152
+ ## Dynamic Vector Store for Multi-Tenant Applications
153
+
154
+ For multi-tenant applications where each tenant has isolated data, you can pass a resolver function instead of a static vector store:
155
+
156
+ ```typescript
157
+ import { createGraphRAGTool, VectorStoreResolver } from '@mastra/rag'
158
+ import { PgVector } from '@mastra/pg'
159
+
160
+ const vectorStoreResolver: VectorStoreResolver = async ({ requestContext }) => {
161
+ const tenantId = requestContext?.get('tenantId')
162
+
163
+ return new PgVector({
164
+ id: `pg-vector-${tenantId}`,
165
+ connectionString: process.env.POSTGRES_CONNECTION_STRING!,
166
+ schemaName: `tenant_${tenantId}`,
167
+ })
168
+ }
169
+
170
+ const graphTool = createGraphRAGTool({
171
+ indexName: 'embeddings',
172
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
173
+ vectorStore: vectorStoreResolver,
174
+ })
175
+ ```
176
+
177
+ See [createVectorQueryTool - Dynamic Vector Store](https://mastra.ai/reference/tools/vector-query-tool) for more details.
178
+
179
+ ## Related
180
+
181
+ - [createVectorQueryTool](https://mastra.ai/reference/tools/vector-query-tool)
182
+ - [GraphRAG](https://mastra.ai/reference/rag/graph-rag)
@@ -0,0 +1,459 @@
1
+ # createVectorQueryTool()
2
+
3
+ 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.
4
+
5
+ ## Basic Usage
6
+
7
+ ```typescript
8
+ import { createVectorQueryTool } from '@mastra/rag'
9
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
10
+
11
+ const queryTool = createVectorQueryTool({
12
+ vectorStoreName: 'pinecone',
13
+ indexName: 'docs',
14
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
15
+ })
16
+ ```
17
+
18
+ ## Parameters
19
+
20
+ > **Note:** **Parameter Requirements:** Most fields can be set at creation as defaults. Some fields can be overridden at runtime via the request context or input. If a required field is missing from both creation and runtime, an error will be thrown. Note that `model`, `id`, and `description` can only be set at creation time.
21
+
22
+ **id?:** (`string`): Custom ID for the tool. By default: 'VectorQuery {vectorStoreName} {indexName} Tool'. (Set at creation only.)
23
+
24
+ **description?:** (`string`): Custom description for the tool. By default: 'Access the knowledge base to find information needed to answer user questions' (Set at creation only.)
25
+
26
+ **model:** (`EmbeddingModel`): Embedding model to use for vector search. (Set at creation only.)
27
+
28
+ **vectorStoreName:** (`string`): Name of the vector store to query. (Can be set at creation or overridden at runtime.)
29
+
30
+ **indexName:** (`string`): Name of the index within the vector store. (Can be set at creation or overridden at runtime.)
31
+
32
+ **enableFilter?:** (`boolean`): Enable filtering of results based on metadata. (Set at creation only, but will be automatically enabled if a filter is provided in the request context.) (Default: `false`)
33
+
34
+ **includeVectors?:** (`boolean`): Include the embedding vectors in the results. (Can be set at creation or overridden at runtime.) (Default: `false`)
35
+
36
+ **includeSources?:** (`boolean`): Include the full retrieval objects in the results. (Can be set at creation or overridden at runtime.) (Default: `true`)
37
+
38
+ **reranker?:** (`RerankConfig`): Options for reranking results. (Can be set at creation or overridden at runtime.)
39
+
40
+ **databaseConfig?:** (`DatabaseConfig`): Database-specific configuration options for optimizing queries. (Can be set at creation or overridden at runtime.)
41
+
42
+ **providerOptions?:** (`Record<string, Record<string, any>>`): Provider-specific options for the embedding model (e.g., outputDimensionality). \*\*Important\*\*: Only works with AI SDK EmbeddingModelV2 models. For V1 models, configure options when creating the model itself.
43
+
44
+ **vectorStore?:** (`MastraVector | VectorStoreResolver`): Direct vector store instance or a resolver function for dynamic selection. Use a function for multi-tenant applications where the vector store is selected based on request context. When provided, \`vectorStoreName\` becomes optional.
45
+
46
+ ### DatabaseConfig
47
+
48
+ 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.
49
+
50
+ **pinecone?:** (`PineconeConfig`): objectnamespace?:stringPinecone namespace for organizing vectorssparseVector?:{ indices: number\[]; values: number\[]; }Sparse vector for hybrid search
51
+
52
+ **pgvector?:** (`PgVectorConfig`): objectminScore?:numberMinimum similarity score threshold for resultsef?:numberHNSW search parameter - controls accuracy vs speed tradeoffprobes?:numberIVFFlat probe parameter - number of cells to visit during search
53
+
54
+ **chroma?:** (`ChromaConfig`): objectwhere?:Record\<string, any>Metadata filtering conditionswhereDocument?:Record\<string, any>Document content filtering conditions
55
+
56
+ ### RerankConfig
57
+
58
+ **model:** (`MastraLanguageModel`): Language model to use for reranking
59
+
60
+ **options?:** (`RerankerOptions`): objectweights?:WeightConfigWeights for scoring components (semantic: 0.4, vector: 0.4, position: 0.2)topK?:numberNumber of top results to return
61
+
62
+ ## Returns
63
+
64
+ The tool returns an object with:
65
+
66
+ **relevantContext:** (`string`): Combined text from the most relevant document chunks
67
+
68
+ **sources:** (`QueryResult[]`): Array of full retrieval result objects. Each object contains all information needed to reference the original document, chunk, and similarity score.
69
+
70
+ ### QueryResult object structure
71
+
72
+ ```typescript
73
+ {
74
+ id: string; // Unique chunk/document identifier
75
+ metadata: any; // All metadata fields (document ID, etc.)
76
+ vector: number[]; // Embedding vector (if available)
77
+ score: number; // Similarity score for this retrieval
78
+ document: string; // Full chunk/document text (if available)
79
+ }
80
+ ```
81
+
82
+ ## Default Tool Description
83
+
84
+ The default description focuses on:
85
+
86
+ - Finding relevant information in stored knowledge
87
+ - Answering user questions
88
+ - Retrieving factual content
89
+
90
+ ## Result Handling
91
+
92
+ 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.
93
+
94
+ ## Example with Filters
95
+
96
+ ```typescript
97
+ const queryTool = createVectorQueryTool({
98
+ vectorStoreName: 'pinecone',
99
+ indexName: 'docs',
100
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
101
+ enableFilter: true,
102
+ })
103
+ ```
104
+
105
+ With filtering enabled, the tool processes queries to construct metadata filters that combine with semantic search. The process works as follows:
106
+
107
+ 1. A user makes a query with specific filter requirements like "Find content where the 'version' field is greater than 2.0"
108
+ 2. The agent analyzes the query and constructs the appropriate filters:
109
+ ```typescript
110
+ {
111
+ "version": { "$gt": 2.0 }
112
+ }
113
+ ```
114
+
115
+ This agent-driven approach:
116
+
117
+ - Processes natural language queries into filter specifications
118
+ - Implements vector store-specific filter syntax
119
+ - Translates query terms to filter operators
120
+
121
+ For detailed filter syntax and store-specific capabilities, see the [Metadata Filters](https://mastra.ai/reference/rag/metadata-filters) documentation.
122
+
123
+ 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.
124
+
125
+ ## Example with Reranking
126
+
127
+ ```typescript
128
+ const queryTool = createVectorQueryTool({
129
+ vectorStoreName: 'milvus',
130
+ indexName: 'documentation',
131
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
132
+ reranker: {
133
+ model: 'openai/gpt-5.1',
134
+ options: {
135
+ weights: {
136
+ semantic: 0.5, // Semantic relevance weight
137
+ vector: 0.3, // Vector similarity weight
138
+ position: 0.2, // Original position weight
139
+ },
140
+ topK: 5,
141
+ },
142
+ },
143
+ })
144
+ ```
145
+
146
+ Reranking improves result quality by combining:
147
+
148
+ - Semantic relevance: Using LLM-based scoring of text similarity
149
+ - Vector similarity: Original vector distance scores
150
+ - Position bias: Consideration of original result ordering
151
+ - Query analysis: Adjustments based on query characteristics
152
+
153
+ The reranker processes the initial vector search results and returns a reordered list optimized for relevance.
154
+
155
+ ## Example with Custom Description
156
+
157
+ ```typescript
158
+ const queryTool = createVectorQueryTool({
159
+ vectorStoreName: 'pinecone',
160
+ indexName: 'docs',
161
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
162
+ description:
163
+ 'Search through document archives to find relevant information for answering questions about company policies and procedures',
164
+ })
165
+ ```
166
+
167
+ This example shows how to customize the tool description for a specific use case while maintaining its core purpose of information retrieval.
168
+
169
+ ## Database-Specific Configuration Examples
170
+
171
+ The `databaseConfig` parameter allows you to leverage unique features and optimizations specific to each vector database. These configurations are automatically applied during query execution.
172
+
173
+ **Pinecone**:
174
+
175
+ ### Pinecone Configuration
176
+
177
+ ```typescript
178
+ const pineconeQueryTool = createVectorQueryTool({
179
+ vectorStoreName: 'pinecone',
180
+ indexName: 'docs',
181
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
182
+ databaseConfig: {
183
+ pinecone: {
184
+ namespace: 'production', // Organize vectors by environment
185
+ sparseVector: {
186
+ // Enable hybrid search
187
+ indices: [0, 1, 2, 3],
188
+ values: [0.1, 0.2, 0.15, 0.05],
189
+ },
190
+ },
191
+ },
192
+ })
193
+ ```
194
+
195
+ **Pinecone Features:**
196
+
197
+ - **Namespace**: Isolate different data sets within the same index
198
+ - **Sparse Vector**: Combine dense and sparse embeddings for improved search quality
199
+ - **Use Cases**: Multi-tenant applications, hybrid semantic search
200
+
201
+ **pgVector**:
202
+
203
+ ### pgVector Configuration
204
+
205
+ ```typescript
206
+ const pgVectorQueryTool = createVectorQueryTool({
207
+ vectorStoreName: 'postgres',
208
+ indexName: 'embeddings',
209
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
210
+ databaseConfig: {
211
+ pgvector: {
212
+ minScore: 0.7, // Only return results above 70% similarity
213
+ ef: 200, // Higher value = better accuracy, slower search
214
+ probes: 10, // For IVFFlat: more probes = better recall
215
+ },
216
+ },
217
+ })
218
+ ```
219
+
220
+ **pgVector Features:**
221
+
222
+ - **minScore**: Filter out low-quality matches
223
+ - **ef (HNSW)**: Control accuracy vs speed for HNSW indexes
224
+ - **probes (IVFFlat)**: Control recall vs speed for IVFFlat indexes
225
+ - **Use Cases**: Performance tuning, quality filtering
226
+
227
+ **Chroma**:
228
+
229
+ ### Chroma Configuration
230
+
231
+ ```typescript
232
+ const chromaQueryTool = createVectorQueryTool({
233
+ vectorStoreName: 'chroma',
234
+ indexName: 'documents',
235
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
236
+ databaseConfig: {
237
+ chroma: {
238
+ where: {
239
+ // Metadata filtering
240
+ category: 'technical',
241
+ status: 'published',
242
+ },
243
+ whereDocument: {
244
+ // Document content filtering
245
+ $contains: 'API',
246
+ },
247
+ },
248
+ },
249
+ })
250
+ ```
251
+
252
+ **Chroma Features:**
253
+
254
+ - **where**: Filter by metadata fields
255
+ - **whereDocument**: Filter by document content
256
+ - **Use Cases**: Advanced filtering, content-based search
257
+
258
+ **Multiple Configs**:
259
+
260
+ ### Multiple Database Configurations
261
+
262
+ ```typescript
263
+ // Configure for multiple databases (useful for dynamic stores)
264
+ const multiDbQueryTool = createVectorQueryTool({
265
+ vectorStoreName: 'dynamic-store', // Will be set at runtime
266
+ indexName: 'docs',
267
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
268
+ databaseConfig: {
269
+ pinecone: {
270
+ namespace: 'default',
271
+ },
272
+ pgvector: {
273
+ minScore: 0.8,
274
+ ef: 150,
275
+ },
276
+ chroma: {
277
+ where: { type: 'documentation' },
278
+ },
279
+ },
280
+ })
281
+ ```
282
+
283
+ **Multi-Config Benefits:**
284
+
285
+ - Support multiple vector stores with one tool
286
+ - Database-specific optimizations are automatically applied
287
+ - Flexible deployment scenarios
288
+
289
+ ### Runtime Configuration Override
290
+
291
+ You can override database configurations at runtime to adapt to different scenarios:
292
+
293
+ ```typescript
294
+ import { RequestContext } from '@mastra/core/request-context'
295
+
296
+ const queryTool = createVectorQueryTool({
297
+ vectorStoreName: 'pinecone',
298
+ indexName: 'docs',
299
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
300
+ databaseConfig: {
301
+ pinecone: {
302
+ namespace: 'development',
303
+ },
304
+ },
305
+ })
306
+
307
+ // Override at runtime
308
+ const requestContext = new RequestContext()
309
+ requestContext.set('databaseConfig', {
310
+ pinecone: {
311
+ namespace: 'production', // Switch to production namespace
312
+ },
313
+ })
314
+
315
+ const response = await agent.generate('Find information about deployment', {
316
+ requestContext,
317
+ })
318
+ ```
319
+
320
+ This approach allows you to:
321
+
322
+ - Switch between environments (dev/staging/prod)
323
+ - Adjust performance parameters based on load
324
+ - Apply different filtering strategies per request
325
+
326
+ ## Example: Using Request Context
327
+
328
+ ```typescript
329
+ const queryTool = createVectorQueryTool({
330
+ vectorStoreName: 'pinecone',
331
+ indexName: 'docs',
332
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
333
+ })
334
+ ```
335
+
336
+ When using request context, provide required parameters at execution time via the request context:
337
+
338
+ ```typescript
339
+ const requestContext = new RequestContext<{
340
+ vectorStoreName: string
341
+ indexName: string
342
+ topK: number
343
+ filter: VectorFilter
344
+ databaseConfig: DatabaseConfig
345
+ }>()
346
+ requestContext.set('vectorStoreName', 'my-store')
347
+ requestContext.set('indexName', 'my-index')
348
+ requestContext.set('topK', 5)
349
+ requestContext.set('filter', { category: 'docs' })
350
+ requestContext.set('databaseConfig', {
351
+ pinecone: { namespace: 'runtime-namespace' },
352
+ })
353
+ requestContext.set('model', 'openai/text-embedding-3-small')
354
+
355
+ const response = await agent.generate('Find documentation from the knowledge base.', {
356
+ requestContext,
357
+ })
358
+ ```
359
+
360
+ For more information on request context, please see:
361
+
362
+ - [Agent Request Context](https://mastra.ai/docs/server/request-context)
363
+ - [Request Context](https://mastra.ai/docs/server/request-context)
364
+
365
+ ## Usage Without a Mastra Server
366
+
367
+ The tool can be used by itself to retrieve documents matching a query:
368
+
369
+ ```typescript
370
+ import { RequestContext } from '@mastra/core/request-context'
371
+ import { createVectorQueryTool } from '@mastra/rag'
372
+ import { PgVector } from '@mastra/pg'
373
+
374
+ const pgVector = new PgVector({
375
+ id: 'pg-vector',
376
+ connectionString: process.env.POSTGRES_CONNECTION_STRING!,
377
+ })
378
+
379
+ const vectorQueryTool = createVectorQueryTool({
380
+ vectorStoreName: 'pgVector', // optional since we're passing in a store
381
+ vectorStore: pgVector,
382
+ indexName: 'embeddings',
383
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
384
+ })
385
+
386
+ const requestContext = new RequestContext()
387
+ const queryResult = await vectorQueryTool.execute({ queryText: 'foo', topK: 1 }, { requestContext })
388
+
389
+ console.log(queryResult.sources)
390
+ ```
391
+
392
+ ## Dynamic Vector Store for Multi-Tenant Applications
393
+
394
+ 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:
395
+
396
+ ```typescript
397
+ import { createVectorQueryTool, VectorStoreResolver } from '@mastra/rag'
398
+ import { PgVector } from '@mastra/pg'
399
+
400
+ // Cache for tenant-specific vector stores
401
+ const vectorStoreCache = new Map<string, PgVector>()
402
+
403
+ // Resolver function that returns the correct vector store based on tenant
404
+ const vectorStoreResolver: VectorStoreResolver = async ({ requestContext }) => {
405
+ const tenantId = requestContext?.get('tenantId')
406
+
407
+ if (!tenantId) {
408
+ throw new Error('tenantId is required in request context')
409
+ }
410
+
411
+ // Return cached instance or create new one
412
+ if (!vectorStoreCache.has(tenantId)) {
413
+ vectorStoreCache.set(
414
+ tenantId,
415
+ new PgVector({
416
+ id: `pg-vector-${tenantId}`,
417
+ connectionString: process.env.POSTGRES_CONNECTION_STRING!,
418
+ schemaName: `tenant_${tenantId}`, // Each tenant has their own schema
419
+ }),
420
+ )
421
+ }
422
+
423
+ return vectorStoreCache.get(tenantId)!
424
+ }
425
+
426
+ const vectorQueryTool = createVectorQueryTool({
427
+ indexName: 'embeddings',
428
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
429
+ vectorStore: vectorStoreResolver, // Dynamic resolution!
430
+ })
431
+
432
+ // Usage with tenant context
433
+ const requestContext = new RequestContext()
434
+ requestContext.set('tenantId', 'acme-corp')
435
+
436
+ const result = await vectorQueryTool.execute(
437
+ { queryText: 'company policies', topK: 5 },
438
+ { requestContext },
439
+ )
440
+ ```
441
+
442
+ This pattern is similar to how `Agent.memory` supports dynamic configuration and enables:
443
+
444
+ - **Schema isolation**: Each tenant's data in separate PostgreSQL schemas
445
+ - **Database isolation**: Route to different database instances per tenant
446
+ - **Dynamic configuration**: Adjust vector store settings based on request context
447
+
448
+ ## Tool Details
449
+
450
+ The tool is created with:
451
+
452
+ - **ID**: `VectorQuery {vectorStoreName} {indexName} Tool`
453
+ - **Input Schema**: Requires queryText and filter objects
454
+ - **Output Schema**: Returns relevantContext string
455
+
456
+ ## Related
457
+
458
+ - [rerank()](https://mastra.ai/reference/rag/rerank)
459
+ - [createGraphRAGTool](https://mastra.ai/reference/tools/graph-rag-tool)
@@ -1,13 +1,15 @@
1
- import type { TiktokenModel, TiktokenEncoding } from 'js-tiktoken';
1
+ import type { TiktokenModel, TiktokenEncoding, Tiktoken } from 'js-tiktoken';
2
2
  import { Document } from '../schema/index.js';
3
3
  import type { SemanticMarkdownChunkOptions } from '../types.js';
4
4
  import { TextTransformer } from './text.js';
5
5
  export declare class SemanticMarkdownTransformer extends TextTransformer {
6
6
  private tokenizer;
7
7
  private joinThreshold;
8
- private allowedSpecial;
9
- private disallowedSpecial;
10
- constructor({ joinThreshold, encodingName, modelName, allowedSpecial, disallowedSpecial, ...baseOptions }?: SemanticMarkdownChunkOptions);
8
+ private allowedArray;
9
+ private disallowedArray;
10
+ constructor({ joinThreshold, encodingName, modelName, tokenizer: existingTokenizer, allowedSpecial, disallowedSpecial, ...baseOptions }?: SemanticMarkdownChunkOptions & {
11
+ tokenizer?: Tiktoken;
12
+ });
11
13
  private countTokens;
12
14
  private splitMarkdownByHeaders;
13
15
  private mergeSemanticSections;
@@ -1 +1 @@
1
- {"version":3,"file":"semantic-markdown.d.ts","sourceRoot":"","sources":["../../../src/document/transformers/semantic-markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAY,MAAM,aAAa,CAAC;AAE7E,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AAE7D,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AASzC,qBAAa,2BAA4B,SAAQ,eAAe;IAC9D,OAAO,CAAC,SAAS,CAAW;IAC5B,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,cAAc,CAAsB;IAC5C,OAAO,CAAC,iBAAiB,CAAsB;gBAEnC,EACV,aAAmB,EACnB,YAA4B,EAC5B,SAAS,EACT,cAA0B,EAC1B,iBAAyB,EACzB,GAAG,WAAW,EACf,GAAE,4BAAiC;IAcpC,OAAO,CAAC,WAAW;IAQnB,OAAO,CAAC,sBAAsB;IA2D9B,OAAO,CAAC,qBAAqB;IA+B7B,SAAS,CAAC,EAAE,IAAI,EAAE,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,EAAE;IAgB/C,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,QAAQ,EAAE;IAuB/E,kBAAkB,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,QAAQ,EAAE;IAYrD,MAAM,CAAC,YAAY,CAAC,EAClB,YAA4B,EAC5B,SAAS,EACT,OAAY,GACb,EAAE;QACD,YAAY,CAAC,EAAE,gBAAgB,CAAC;QAChC,SAAS,CAAC,EAAE,aAAa,CAAC;QAC1B,OAAO,CAAC,EAAE,4BAA4B,CAAC;KACxC,GAAG,2BAA2B;CA4BhC"}
1
+ {"version":3,"file":"semantic-markdown.d.ts","sourceRoot":"","sources":["../../../src/document/transformers/semantic-markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE7E,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AAE7D,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AASzC,qBAAa,2BAA4B,SAAQ,eAAe;IAC9D,OAAO,CAAC,SAAS,CAAW;IAC5B,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,YAAY,CAAmB;IACvC,OAAO,CAAC,eAAe,CAAmB;gBAE9B,EACV,aAAmB,EACnB,YAA4B,EAC5B,SAAS,EACT,SAAS,EAAE,iBAAiB,EAC5B,cAA0B,EAC1B,iBAAyB,EACzB,GAAG,WAAW,EACf,GAAE,4BAA4B,GAAG;QAAE,SAAS,CAAC,EAAE,QAAQ,CAAA;KAAO;IAkB/D,OAAO,CAAC,WAAW;IAKnB,OAAO,CAAC,sBAAsB;IA2D9B,OAAO,CAAC,qBAAqB;IAqC7B,SAAS,CAAC,EAAE,IAAI,EAAE,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,EAAE;IAgB/C,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,QAAQ,EAAE;IAuB/E,kBAAkB,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,QAAQ,EAAE;IAYrD,MAAM,CAAC,YAAY,CAAC,EAClB,YAA4B,EAC5B,SAAS,EACT,OAAY,GACb,EAAE;QACD,YAAY,CAAC,EAAE,gBAAgB,CAAC;QAChC,SAAS,CAAC,EAAE,aAAa,CAAC;QAC1B,OAAO,CAAC,EAAE,4BAA4B,CAAC;KACxC,GAAG,2BAA2B;CA6BhC"}
@@ -1,4 +1,4 @@
1
- import type { TiktokenModel, TiktokenEncoding } from 'js-tiktoken';
1
+ import type { TiktokenModel, TiktokenEncoding, Tiktoken } from 'js-tiktoken';
2
2
  import type { TokenChunkOptions } from '../types.js';
3
3
  import { TextTransformer } from './text.js';
4
4
  interface Tokenizer {
@@ -13,11 +13,12 @@ export declare function splitTextOnTokens({ text, tokenizer }: {
13
13
  }): string[];
14
14
  export declare class TokenTransformer extends TextTransformer {
15
15
  private tokenizer;
16
- private allowedSpecial;
17
- private disallowedSpecial;
18
- constructor({ encodingName, modelName, allowedSpecial, disallowedSpecial, options, }: {
16
+ private allowedArray;
17
+ private disallowedArray;
18
+ constructor({ encodingName, modelName, tokenizer: existingTokenizer, allowedSpecial, disallowedSpecial, options, }: {
19
19
  encodingName?: TiktokenEncoding;
20
20
  modelName?: TiktokenModel;
21
+ tokenizer?: Tiktoken;
21
22
  allowedSpecial?: Set<string> | 'all';
22
23
  disallowedSpecial?: Set<string> | 'all';
23
24
  options: TokenChunkOptions;