@mastra/pg 1.0.0-beta.11 → 1.0.0-beta.12

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,440 @@
1
+ # Tools API Reference
2
+
3
+ > API reference for tools - 1 entries
4
+
5
+
6
+ ---
7
+
8
+ ## Reference: createVectorQueryTool()
9
+
10
+ > Documentation for the Vector Query Tool in Mastra, which facilitates semantic search over vector stores with filtering and reranking capabilities.
11
+
12
+ 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.
13
+
14
+ ## Basic Usage
15
+
16
+ ```typescript
17
+ import { createVectorQueryTool } from "@mastra/rag";
18
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
19
+
20
+ const queryTool = createVectorQueryTool({
21
+ vectorStoreName: "pinecone",
22
+ indexName: "docs",
23
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
24
+ });
25
+ ```
26
+
27
+ ## Parameters
28
+
29
+ > **Note:**
30
+
31
+ **Parameter Requirements:** Most fields can be set at creation as defaults.
32
+ Some fields can be overridden at runtime via the request context or input. If
33
+ a required field is missing from both creation and runtime, an error will be
34
+ thrown. Note that `model`, `id`, and `description` can only be set at creation
35
+ time.
36
+
37
+ ### DatabaseConfig
38
+
39
+ 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.
40
+
41
+ ### RerankConfig
42
+
43
+ ## Returns
44
+
45
+ The tool returns an object with:
46
+
47
+ ### QueryResult object structure
48
+
49
+ ```typescript
50
+ {
51
+ id: string; // Unique chunk/document identifier
52
+ metadata: any; // All metadata fields (document ID, etc.)
53
+ vector: number[]; // Embedding vector (if available)
54
+ score: number; // Similarity score for this retrieval
55
+ document: string; // Full chunk/document text (if available)
56
+ }
57
+ ```
58
+
59
+ ## Default Tool Description
60
+
61
+ The default description focuses on:
62
+
63
+ - Finding relevant information in stored knowledge
64
+ - Answering user questions
65
+ - Retrieving factual content
66
+
67
+ ## Result Handling
68
+
69
+ 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.
70
+
71
+ ## Example with Filters
72
+
73
+ ```typescript
74
+ const queryTool = createVectorQueryTool({
75
+ vectorStoreName: "pinecone",
76
+ indexName: "docs",
77
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
78
+ enableFilter: true,
79
+ });
80
+ ```
81
+
82
+ With filtering enabled, the tool processes queries to construct metadata filters that combine with semantic search. The process works as follows:
83
+
84
+ 1. A user makes a query with specific filter requirements like "Find content where the 'version' field is greater than 2.0"
85
+ 2. The agent analyzes the query and constructs the appropriate filters:
86
+ ```typescript
87
+ {
88
+ "version": { "$gt": 2.0 }
89
+ }
90
+ ```
91
+
92
+ This agent-driven approach:
93
+
94
+ - Processes natural language queries into filter specifications
95
+ - Implements vector store-specific filter syntax
96
+ - Translates query terms to filter operators
97
+
98
+ For detailed filter syntax and store-specific capabilities, see the [Metadata Filters](../rag/metadata-filters) documentation.
99
+
100
+ 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.
101
+
102
+ ## Example with Reranking
103
+
104
+ ```typescript
105
+ const queryTool = createVectorQueryTool({
106
+ vectorStoreName: "milvus",
107
+ indexName: "documentation",
108
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
109
+ reranker: {
110
+ model: "openai/gpt-5.1",
111
+ options: {
112
+ weights: {
113
+ semantic: 0.5, // Semantic relevance weight
114
+ vector: 0.3, // Vector similarity weight
115
+ position: 0.2, // Original position weight
116
+ },
117
+ topK: 5,
118
+ },
119
+ },
120
+ });
121
+ ```
122
+
123
+ Reranking improves result quality by combining:
124
+
125
+ - Semantic relevance: Using LLM-based scoring of text similarity
126
+ - Vector similarity: Original vector distance scores
127
+ - Position bias: Consideration of original result ordering
128
+ - Query analysis: Adjustments based on query characteristics
129
+
130
+ The reranker processes the initial vector search results and returns a reordered list optimized for relevance.
131
+
132
+ ## Example with Custom Description
133
+
134
+ ```typescript
135
+ const queryTool = createVectorQueryTool({
136
+ vectorStoreName: "pinecone",
137
+ indexName: "docs",
138
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
139
+ description:
140
+ "Search through document archives to find relevant information for answering questions about company policies and procedures",
141
+ });
142
+ ```
143
+
144
+ This example shows how to customize the tool description for a specific use case while maintaining its core purpose of information retrieval.
145
+
146
+ ## Database-Specific Configuration Examples
147
+
148
+ The `databaseConfig` parameter allows you to leverage unique features and optimizations specific to each vector database. These configurations are automatically applied during query execution.
149
+
150
+ **pinecone:**
151
+
152
+ ### Pinecone Configuration
153
+
154
+ ```typescript
155
+ const pineconeQueryTool = createVectorQueryTool({
156
+ vectorStoreName: "pinecone",
157
+ indexName: "docs",
158
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
159
+ databaseConfig: {
160
+ pinecone: {
161
+ namespace: "production", // Organize vectors by environment
162
+ sparseVector: { // Enable hybrid search
163
+ indices: [0, 1, 2, 3],
164
+ values: [0.1, 0.2, 0.15, 0.05]
165
+ }
166
+ }
167
+ }
168
+ });
169
+ ```
170
+
171
+ **Pinecone Features:**
172
+ - **Namespace**: Isolate different data sets within the same index
173
+ - **Sparse Vector**: Combine dense and sparse embeddings for improved search quality
174
+ - **Use Cases**: Multi-tenant applications, hybrid semantic search
175
+
176
+
177
+
178
+ **pgvector:**
179
+
180
+ ### pgVector Configuration
181
+
182
+ ```typescript
183
+ const pgVectorQueryTool = createVectorQueryTool({
184
+ vectorStoreName: "postgres",
185
+ indexName: "embeddings",
186
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
187
+ databaseConfig: {
188
+ pgvector: {
189
+ minScore: 0.7, // Only return results above 70% similarity
190
+ ef: 200, // Higher value = better accuracy, slower search
191
+ probes: 10 // For IVFFlat: more probes = better recall
192
+ }
193
+ }
194
+ });
195
+ ```
196
+
197
+ **pgVector Features:**
198
+ - **minScore**: Filter out low-quality matches
199
+ - **ef (HNSW)**: Control accuracy vs speed for HNSW indexes
200
+ - **probes (IVFFlat)**: Control recall vs speed for IVFFlat indexes
201
+ - **Use Cases**: Performance tuning, quality filtering
202
+
203
+
204
+
205
+ **chroma:**
206
+
207
+ ### Chroma Configuration
208
+
209
+ ```typescript
210
+ const chromaQueryTool = createVectorQueryTool({
211
+ vectorStoreName: "chroma",
212
+ indexName: "documents",
213
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
214
+ databaseConfig: {
215
+ chroma: {
216
+ where: { // Metadata filtering
217
+ "category": "technical",
218
+ "status": "published"
219
+ },
220
+ whereDocument: { // Document content filtering
221
+ "$contains": "API"
222
+ }
223
+ }
224
+ }
225
+ });
226
+ ```
227
+
228
+ **Chroma Features:**
229
+ - **where**: Filter by metadata fields
230
+ - **whereDocument**: Filter by document content
231
+ - **Use Cases**: Advanced filtering, content-based search
232
+
233
+
234
+
235
+ **multiple-configs:**
236
+
237
+ ### Multiple Database Configurations
238
+
239
+ ```typescript
240
+ // Configure for multiple databases (useful for dynamic stores)
241
+ const multiDbQueryTool = createVectorQueryTool({
242
+ vectorStoreName: "dynamic-store", // Will be set at runtime
243
+ indexName: "docs",
244
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
245
+ databaseConfig: {
246
+ pinecone: {
247
+ namespace: "default"
248
+ },
249
+ pgvector: {
250
+ minScore: 0.8,
251
+ ef: 150
252
+ },
253
+ chroma: {
254
+ where: { "type": "documentation" }
255
+ }
256
+ }
257
+ });
258
+ ```
259
+
260
+ **Multi-Config Benefits:**
261
+ - Support multiple vector stores with one tool
262
+ - Database-specific optimizations are automatically applied
263
+ - Flexible deployment scenarios
264
+
265
+
266
+
267
+ ### Runtime Configuration Override
268
+
269
+ You can override database configurations at runtime to adapt to different scenarios:
270
+
271
+ ```typescript
272
+ import { RequestContext } from "@mastra/core/request-context";
273
+
274
+ const queryTool = createVectorQueryTool({
275
+ vectorStoreName: "pinecone",
276
+ indexName: "docs",
277
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
278
+ databaseConfig: {
279
+ pinecone: {
280
+ namespace: "development",
281
+ },
282
+ },
283
+ });
284
+
285
+ // Override at runtime
286
+ const requestContext = new RequestContext();
287
+ requestContext.set("databaseConfig", {
288
+ pinecone: {
289
+ namespace: "production", // Switch to production namespace
290
+ },
291
+ });
292
+
293
+ const response = await agent.generate("Find information about deployment", {
294
+ requestContext,
295
+ });
296
+ ```
297
+
298
+ This approach allows you to:
299
+
300
+ - Switch between environments (dev/staging/prod)
301
+ - Adjust performance parameters based on load
302
+ - Apply different filtering strategies per request
303
+
304
+ ## Example: Using Request Context
305
+
306
+ ```typescript
307
+ const queryTool = createVectorQueryTool({
308
+ vectorStoreName: "pinecone",
309
+ indexName: "docs",
310
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
311
+ });
312
+ ```
313
+
314
+ When using request context, provide required parameters at execution time via the request context:
315
+
316
+ ```typescript
317
+ const requestContext = new RequestContext<{
318
+ vectorStoreName: string;
319
+ indexName: string;
320
+ topK: number;
321
+ filter: VectorFilter;
322
+ databaseConfig: DatabaseConfig;
323
+ }>();
324
+ requestContext.set("vectorStoreName", "my-store");
325
+ requestContext.set("indexName", "my-index");
326
+ requestContext.set("topK", 5);
327
+ requestContext.set("filter", { category: "docs" });
328
+ requestContext.set("databaseConfig", {
329
+ pinecone: { namespace: "runtime-namespace" },
330
+ });
331
+ requestContext.set("model", "openai/text-embedding-3-small");
332
+
333
+ const response = await agent.generate(
334
+ "Find documentation from the knowledge base.",
335
+ {
336
+ requestContext,
337
+ },
338
+ );
339
+ ```
340
+
341
+ For more information on request context, please see:
342
+
343
+ - [Agent Request Context](https://mastra.ai/docs/v1/server/request-context)
344
+ - [Request Context](https://mastra.ai/docs/v1/server/request-context#accessing-values-with-tools)
345
+
346
+ ## Usage Without a Mastra Server
347
+
348
+ The tool can be used by itself to retrieve documents matching a query:
349
+
350
+ ```typescript title="src/index.ts"
351
+ import { RequestContext } from "@mastra/core/request-context";
352
+ import { createVectorQueryTool } from "@mastra/rag";
353
+ import { PgVector } from "@mastra/pg";
354
+
355
+ const pgVector = new PgVector({
356
+ id: 'pg-vector',
357
+ connectionString: process.env.POSTGRES_CONNECTION_STRING!,
358
+ });
359
+
360
+ const vectorQueryTool = createVectorQueryTool({
361
+ vectorStoreName: "pgVector", // optional since we're passing in a store
362
+ vectorStore: pgVector,
363
+ indexName: "embeddings",
364
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
365
+ });
366
+
367
+ const requestContext = new RequestContext();
368
+ const queryResult = await vectorQueryTool.execute(
369
+ { queryText: "foo", topK: 1 },
370
+ { requestContext }
371
+ );
372
+
373
+ console.log(queryResult.sources);
374
+ ```
375
+
376
+ ## Dynamic Vector Store for Multi-Tenant Applications
377
+
378
+ 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:
379
+
380
+ ```typescript title="src/index.ts"
381
+ import { createVectorQueryTool, VectorStoreResolver } from "@mastra/rag";
382
+ import { PgVector } from "@mastra/pg";
383
+
384
+ // Cache for tenant-specific vector stores
385
+ const vectorStoreCache = new Map<string, PgVector>();
386
+
387
+ // Resolver function that returns the correct vector store based on tenant
388
+ const vectorStoreResolver: VectorStoreResolver = async ({ requestContext }) => {
389
+ const tenantId = requestContext?.get("tenantId");
390
+
391
+ if (!tenantId) {
392
+ throw new Error("tenantId is required in request context");
393
+ }
394
+
395
+ // Return cached instance or create new one
396
+ if (!vectorStoreCache.has(tenantId)) {
397
+ vectorStoreCache.set(tenantId, new PgVector({
398
+ id: `pg-vector-${tenantId}`,
399
+ connectionString: process.env.POSTGRES_CONNECTION_STRING!,
400
+ schemaName: `tenant_${tenantId}`, // Each tenant has their own schema
401
+ }));
402
+ }
403
+
404
+ return vectorStoreCache.get(tenantId)!;
405
+ };
406
+
407
+ const vectorQueryTool = createVectorQueryTool({
408
+ indexName: "embeddings",
409
+ model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
410
+ vectorStore: vectorStoreResolver, // Dynamic resolution!
411
+ });
412
+
413
+ // Usage with tenant context
414
+ const requestContext = new RequestContext();
415
+ requestContext.set("tenantId", "acme-corp");
416
+
417
+ const result = await vectorQueryTool.execute(
418
+ { queryText: "company policies", topK: 5 },
419
+ { requestContext }
420
+ );
421
+ ```
422
+
423
+ This pattern is similar to how `Agent.memory` supports dynamic configuration and enables:
424
+
425
+ - **Schema isolation**: Each tenant's data in separate PostgreSQL schemas
426
+ - **Database isolation**: Route to different database instances per tenant
427
+ - **Dynamic configuration**: Adjust vector store settings based on request context
428
+
429
+ ## Tool Details
430
+
431
+ The tool is created with:
432
+
433
+ - **ID**: `VectorQuery {vectorStoreName} {indexName} Tool`
434
+ - **Input Schema**: Requires queryText and filter objects
435
+ - **Output Schema**: Returns relevantContext string
436
+
437
+ ## Related
438
+
439
+ - [rerank()](../rag/rerank)
440
+ - [createGraphRAGTool](./graph-rag-tool)