@mastra/pg 1.6.0 → 1.6.1-alpha.0

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.
@@ -1,459 +0,0 @@
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: { // Enable hybrid search
186
- indices: [0, 1, 2, 3],
187
- values: [0.1, 0.2, 0.15, 0.05]
188
- }
189
- }
190
- }
191
- });
192
- ```
193
-
194
- **Pinecone Features:**
195
-
196
- - **Namespace**: Isolate different data sets within the same index
197
- - **Sparse Vector**: Combine dense and sparse embeddings for improved search quality
198
- - **Use Cases**: Multi-tenant applications, hybrid semantic search
199
-
200
- **pgVector**:
201
-
202
- ### pgVector Configuration
203
-
204
- ```typescript
205
- const pgVectorQueryTool = createVectorQueryTool({
206
- vectorStoreName: "postgres",
207
- indexName: "embeddings",
208
- model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
209
- databaseConfig: {
210
- pgvector: {
211
- minScore: 0.7, // Only return results above 70% similarity
212
- ef: 200, // Higher value = better accuracy, slower search
213
- probes: 10 // For IVFFlat: more probes = better recall
214
- }
215
- }
216
- });
217
- ```
218
-
219
- **pgVector Features:**
220
-
221
- - **minScore**: Filter out low-quality matches
222
- - **ef (HNSW)**: Control accuracy vs speed for HNSW indexes
223
- - **probes (IVFFlat)**: Control recall vs speed for IVFFlat indexes
224
- - **Use Cases**: Performance tuning, quality filtering
225
-
226
- **Chroma**:
227
-
228
- ### Chroma Configuration
229
-
230
- ```typescript
231
- const chromaQueryTool = createVectorQueryTool({
232
- vectorStoreName: "chroma",
233
- indexName: "documents",
234
- model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
235
- databaseConfig: {
236
- chroma: {
237
- where: { // Metadata filtering
238
- "category": "technical",
239
- "status": "published"
240
- },
241
- whereDocument: { // Document content filtering
242
- "$contains": "API"
243
- }
244
- }
245
- }
246
- });
247
- ```
248
-
249
- **Chroma Features:**
250
-
251
- - **where**: Filter by metadata fields
252
- - **whereDocument**: Filter by document content
253
- - **Use Cases**: Advanced filtering, content-based search
254
-
255
- **Multiple Configs**:
256
-
257
- ### Multiple Database Configurations
258
-
259
- ```typescript
260
- // Configure for multiple databases (useful for dynamic stores)
261
- const multiDbQueryTool = createVectorQueryTool({
262
- vectorStoreName: "dynamic-store", // Will be set at runtime
263
- indexName: "docs",
264
- model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
265
- databaseConfig: {
266
- pinecone: {
267
- namespace: "default"
268
- },
269
- pgvector: {
270
- minScore: 0.8,
271
- ef: 150
272
- },
273
- chroma: {
274
- where: { "type": "documentation" }
275
- }
276
- }
277
- });
278
- ```
279
-
280
- **Multi-Config Benefits:**
281
-
282
- - Support multiple vector stores with one tool
283
- - Database-specific optimizations are automatically applied
284
- - Flexible deployment scenarios
285
-
286
- ### Runtime Configuration Override
287
-
288
- You can override database configurations at runtime to adapt to different scenarios:
289
-
290
- ```typescript
291
- import { RequestContext } from "@mastra/core/request-context";
292
-
293
- const queryTool = createVectorQueryTool({
294
- vectorStoreName: "pinecone",
295
- indexName: "docs",
296
- model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
297
- databaseConfig: {
298
- pinecone: {
299
- namespace: "development",
300
- },
301
- },
302
- });
303
-
304
- // Override at runtime
305
- const requestContext = new RequestContext();
306
- requestContext.set("databaseConfig", {
307
- pinecone: {
308
- namespace: "production", // Switch to production namespace
309
- },
310
- });
311
-
312
- const response = await agent.generate("Find information about deployment", {
313
- requestContext,
314
- });
315
- ```
316
-
317
- This approach allows you to:
318
-
319
- - Switch between environments (dev/staging/prod)
320
- - Adjust performance parameters based on load
321
- - Apply different filtering strategies per request
322
-
323
- ## Example: Using Request Context
324
-
325
- ```typescript
326
- const queryTool = createVectorQueryTool({
327
- vectorStoreName: "pinecone",
328
- indexName: "docs",
329
- model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
330
- });
331
- ```
332
-
333
- When using request context, provide required parameters at execution time via the request context:
334
-
335
- ```typescript
336
- const requestContext = new RequestContext<{
337
- vectorStoreName: string;
338
- indexName: string;
339
- topK: number;
340
- filter: VectorFilter;
341
- databaseConfig: DatabaseConfig;
342
- }>();
343
- requestContext.set("vectorStoreName", "my-store");
344
- requestContext.set("indexName", "my-index");
345
- requestContext.set("topK", 5);
346
- requestContext.set("filter", { category: "docs" });
347
- requestContext.set("databaseConfig", {
348
- pinecone: { namespace: "runtime-namespace" },
349
- });
350
- requestContext.set("model", "openai/text-embedding-3-small");
351
-
352
- const response = await agent.generate(
353
- "Find documentation from the knowledge base.",
354
- {
355
- requestContext,
356
- },
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(
388
- { queryText: "foo", topK: 1 },
389
- { requestContext }
390
- );
391
-
392
- console.log(queryResult.sources);
393
- ```
394
-
395
- ## Dynamic Vector Store for Multi-Tenant Applications
396
-
397
- 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:
398
-
399
- ```typescript
400
- import { createVectorQueryTool, VectorStoreResolver } from "@mastra/rag";
401
- import { PgVector } from "@mastra/pg";
402
-
403
- // Cache for tenant-specific vector stores
404
- const vectorStoreCache = new Map<string, PgVector>();
405
-
406
- // Resolver function that returns the correct vector store based on tenant
407
- const vectorStoreResolver: VectorStoreResolver = async ({ requestContext }) => {
408
- const tenantId = requestContext?.get("tenantId");
409
-
410
- if (!tenantId) {
411
- throw new Error("tenantId is required in request context");
412
- }
413
-
414
- // Return cached instance or create new one
415
- if (!vectorStoreCache.has(tenantId)) {
416
- vectorStoreCache.set(tenantId, new PgVector({
417
- id: `pg-vector-${tenantId}`,
418
- connectionString: process.env.POSTGRES_CONNECTION_STRING!,
419
- schemaName: `tenant_${tenantId}`, // Each tenant has their own schema
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)