@mastra/oracledb 0.0.0 → 0.2.0-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.
Files changed (92) hide show
  1. package/LICENSE.md +30 -0
  2. package/dist/docs/SKILL.md +37 -0
  3. package/dist/docs/assets/SOURCE_MAP.json +6 -0
  4. package/dist/docs/references/docs-memory-observational-memory.md +835 -0
  5. package/dist/docs/references/docs-memory-semantic-recall.md +402 -0
  6. package/dist/docs/references/docs-memory-working-memory.md +432 -0
  7. package/dist/docs/references/docs-storage-overview.md +215 -0
  8. package/dist/docs/references/guides-rag-overview.md +74 -0
  9. package/dist/docs/references/guides-rag-retrieval.md +537 -0
  10. package/dist/docs/references/guides-rag-vector-databases.md +710 -0
  11. package/dist/docs/references/reference-rag-metadata-filters.md +227 -0
  12. package/dist/docs/references/reference-storage-oracledb.md +239 -0
  13. package/dist/docs/references/reference-vectors-oracledb.md +347 -0
  14. package/dist/index.cjs +10217 -0
  15. package/dist/index.cjs.map +1 -0
  16. package/dist/index.d.ts +7 -0
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +10196 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/schema.d.ts +27 -0
  21. package/dist/schema.d.ts.map +1 -0
  22. package/dist/shared/connection.d.ts +46 -0
  23. package/dist/shared/connection.d.ts.map +1 -0
  24. package/dist/storage/db/index.d.ts +128 -0
  25. package/dist/storage/db/index.d.ts.map +1 -0
  26. package/dist/storage/domain-utils.d.ts +18 -0
  27. package/dist/storage/domain-utils.d.ts.map +1 -0
  28. package/dist/storage/domains/agents/index.d.ts +54 -0
  29. package/dist/storage/domains/agents/index.d.ts.map +1 -0
  30. package/dist/storage/domains/mcp-clients/index.d.ts +46 -0
  31. package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -0
  32. package/dist/storage/domains/memory/index.d.ts +88 -0
  33. package/dist/storage/domains/memory/index.d.ts.map +1 -0
  34. package/dist/storage/domains/memory/messages.d.ts +37 -0
  35. package/dist/storage/domains/memory/messages.d.ts.map +1 -0
  36. package/dist/storage/domains/memory/observational-buffering.d.ts +7 -0
  37. package/dist/storage/domains/memory/observational-buffering.d.ts.map +1 -0
  38. package/dist/storage/domains/memory/observational.d.ts +56 -0
  39. package/dist/storage/domains/memory/observational.d.ts.map +1 -0
  40. package/dist/storage/domains/memory/resources.d.ts +14 -0
  41. package/dist/storage/domains/memory/resources.d.ts.map +1 -0
  42. package/dist/storage/domains/memory/schema.d.ts +43 -0
  43. package/dist/storage/domains/memory/schema.d.ts.map +1 -0
  44. package/dist/storage/domains/memory/threads.d.ts +37 -0
  45. package/dist/storage/domains/memory/threads.d.ts.map +1 -0
  46. package/dist/storage/domains/memory/utils.d.ts +68 -0
  47. package/dist/storage/domains/memory/utils.d.ts.map +1 -0
  48. package/dist/storage/domains/observability/binds.d.ts +21 -0
  49. package/dist/storage/domains/observability/binds.d.ts.map +1 -0
  50. package/dist/storage/domains/observability/index.d.ts +49 -0
  51. package/dist/storage/domains/observability/index.d.ts.map +1 -0
  52. package/dist/storage/domains/observability/logs.d.ts +5 -0
  53. package/dist/storage/domains/observability/logs.d.ts.map +1 -0
  54. package/dist/storage/domains/observability/schema.d.ts +38 -0
  55. package/dist/storage/domains/observability/schema.d.ts.map +1 -0
  56. package/dist/storage/domains/observability/scores-bridge.d.ts +7 -0
  57. package/dist/storage/domains/observability/scores-bridge.d.ts.map +1 -0
  58. package/dist/storage/domains/observability/spans.d.ts +18 -0
  59. package/dist/storage/domains/observability/spans.d.ts.map +1 -0
  60. package/dist/storage/domains/scorer-definitions/index.d.ts +46 -0
  61. package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
  62. package/dist/storage/domains/scores/index.d.ts +63 -0
  63. package/dist/storage/domains/scores/index.d.ts.map +1 -0
  64. package/dist/storage/domains/workflows/index.d.ts +61 -0
  65. package/dist/storage/domains/workflows/index.d.ts.map +1 -0
  66. package/dist/storage/index.d.ts +45 -0
  67. package/dist/storage/index.d.ts.map +1 -0
  68. package/dist/storage/migrations.d.ts +55 -0
  69. package/dist/storage/migrations.d.ts.map +1 -0
  70. package/dist/storage/types.d.ts +44 -0
  71. package/dist/storage/types.d.ts.map +1 -0
  72. package/dist/vector/ddl.d.ts +58 -0
  73. package/dist/vector/ddl.d.ts.map +1 -0
  74. package/dist/vector/filter.d.ts +7 -0
  75. package/dist/vector/filter.d.ts.map +1 -0
  76. package/dist/vector/identifiers.d.ts +11 -0
  77. package/dist/vector/identifiers.d.ts.map +1 -0
  78. package/dist/vector/index.d.ts +32 -0
  79. package/dist/vector/index.d.ts.map +1 -0
  80. package/dist/vector/prompt.d.ts +6 -0
  81. package/dist/vector/prompt.d.ts.map +1 -0
  82. package/dist/vector/query.d.ts +5 -0
  83. package/dist/vector/query.d.ts.map +1 -0
  84. package/dist/vector/sql.d.ts +12 -0
  85. package/dist/vector/sql.d.ts.map +1 -0
  86. package/dist/vector/stats.d.ts +9 -0
  87. package/dist/vector/stats.d.ts.map +1 -0
  88. package/dist/vector/types.d.ts +80 -0
  89. package/dist/vector/types.d.ts.map +1 -0
  90. package/dist/vector/upsert.d.ts +20 -0
  91. package/dist/vector/upsert.d.ts.map +1 -0
  92. package/package.json +23 -24
@@ -0,0 +1,74 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # RAG (Retrieval-Augmented Generation) in Mastra
4
+
5
+ RAG in Mastra helps you enhance LLM outputs by incorporating relevant context from your own data sources, improving accuracy and grounding responses in real information.
6
+
7
+ Mastra's RAG system provides:
8
+
9
+ - Standardized APIs to process and embed documents
10
+ - Support for multiple vector stores
11
+ - Chunking and embedding strategies for optimal retrieval
12
+ - Observability for tracking embedding and retrieval performance
13
+
14
+ ## Example
15
+
16
+ To implement RAG, you process your documents into chunks, create embeddings, store them in a vector database, and then retrieve relevant context at query time.
17
+
18
+ ```ts
19
+ import { embedMany } from 'ai'
20
+ import { PgVector } from '@mastra/pg'
21
+ import { MDocument } from '@mastra/rag'
22
+ import { z } from 'zod'
23
+
24
+ // 1. Initialize document
25
+ const doc = MDocument.fromText(`Your document text here...`)
26
+
27
+ // 2. Create chunks
28
+ const chunks = await doc.chunk({
29
+ strategy: 'recursive',
30
+ size: 512,
31
+ overlap: 50,
32
+ })
33
+
34
+ // 3. Generate embeddings; we need to pass the text of each chunk
35
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
36
+
37
+ const { embeddings } = await embedMany({
38
+ values: chunks.map(chunk => chunk.text),
39
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
40
+ })
41
+
42
+ // 4. Store in vector database
43
+ const pgVector = new PgVector({
44
+ id: 'pg-vector',
45
+ connectionString: process.env.POSTGRES_CONNECTION_STRING,
46
+ })
47
+ await pgVector.upsert({
48
+ indexName: 'embeddings',
49
+ vectors: embeddings,
50
+ }) // using an index name of 'embeddings'
51
+
52
+ // 5. Query similar chunks
53
+ const results = await pgVector.query({
54
+ indexName: 'embeddings',
55
+ queryVector: queryVector,
56
+ topK: 3,
57
+ }) // queryVector is the embedding of the query
58
+
59
+ console.log('Similar chunks:', results)
60
+ ```
61
+
62
+ This example shows the essentials. Initialize a document and create chunks, then generate and store embeddings before querying for similar content.
63
+
64
+ ## Document processing
65
+
66
+ The basic building block of RAG is document processing. Documents can be chunked using strategies (recursive, sliding window, etc.) and enriched with metadata. See the [chunking and embedding doc](https://mastra.ai/guides/rag/chunking-and-embedding).
67
+
68
+ ## Vector storage
69
+
70
+ Mastra supports multiple vector stores for embedding persistence and similarity search, including pgvector, OracleDB, Pinecone, Qdrant, and MongoDB. See the [vector database doc](https://mastra.ai/guides/rag/vector-databases).
71
+
72
+ ## More resources
73
+
74
+ - [Chain of Thought RAG Example](https://github.com/mastra-ai/mastra/tree/main/examples/basics/rag/cot-rag)
@@ -0,0 +1,537 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # Retrieval in RAG systems
4
+
5
+ After storing embeddings, you need to retrieve relevant chunks to answer user queries.
6
+
7
+ Mastra provides flexible retrieval options with support for semantic search, filtering, and re-ranking.
8
+
9
+ ## How retrieval works
10
+
11
+ 1. The user's query is converted to an embedding using the same model used for document embeddings
12
+ 2. This embedding is compared to stored embeddings using vector similarity
13
+ 3. The most similar chunks are retrieved and can be optionally:
14
+
15
+ - Filtered by metadata
16
+ - Re-ranked for better relevance
17
+ - Processed through a knowledge graph
18
+
19
+ ## Basic retrieval
20
+
21
+ The simplest approach is direct semantic search. This method uses vector similarity to find chunks that are semantically similar to the query:
22
+
23
+ ```ts
24
+ import { embed } from 'ai'
25
+ import { PgVector } from '@mastra/pg'
26
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
27
+
28
+ // Convert query to embedding
29
+ const { embedding } = await embed({
30
+ value: 'What are the main points in the article?',
31
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
32
+ })
33
+
34
+ // Query vector store
35
+ const pgVector = new PgVector({
36
+ id: 'pg-vector',
37
+ connectionString: process.env.POSTGRES_CONNECTION_STRING,
38
+ })
39
+ const results = await pgVector.query({
40
+ indexName: 'embeddings',
41
+ queryVector: embedding,
42
+ topK: 10,
43
+ })
44
+
45
+ // Display results
46
+ console.log(results)
47
+ ```
48
+
49
+ The `topK` parameter specifies the maximum number of most similar results to return from the vector search.
50
+
51
+ Results include both the text content and a similarity score:
52
+
53
+ ```ts
54
+ [
55
+ {
56
+ text: 'Climate change poses significant challenges...',
57
+ score: 0.89,
58
+ metadata: { source: 'article1.txt' },
59
+ },
60
+ {
61
+ text: 'Rising temperatures affect crop yields...',
62
+ score: 0.82,
63
+ metadata: { source: 'article1.txt' },
64
+ },
65
+ ]
66
+ ```
67
+
68
+ ## Advanced retrieval options
69
+
70
+ ### Metadata Filtering
71
+
72
+ Filter results based on metadata fields to narrow down the search space. This approach - combining vector similarity search with metadata filters - is sometimes called hybrid vector search, as it merges semantic search with structured filtering criteria.
73
+
74
+ This is useful when you have documents from different sources, time periods, or with specific attributes. Mastra provides a unified MongoDB-style query syntax that works across all supported vector stores.
75
+
76
+ For detailed information about available operators and syntax, see the [Metadata Filters Reference](https://mastra.ai/reference/rag/metadata-filters).
77
+
78
+ Basic filtering examples:
79
+
80
+ ```ts
81
+ // Simple equality filter
82
+ const results = await pgVector.query({
83
+ indexName: 'embeddings',
84
+ queryVector: embedding,
85
+ topK: 10,
86
+ filter: {
87
+ source: 'article1.txt',
88
+ },
89
+ })
90
+
91
+ // Numeric comparison
92
+ const results = await pgVector.query({
93
+ indexName: 'embeddings',
94
+ queryVector: embedding,
95
+ topK: 10,
96
+ filter: {
97
+ price: { $gt: 100 },
98
+ },
99
+ })
100
+
101
+ // Multiple conditions
102
+ const results = await pgVector.query({
103
+ indexName: 'embeddings',
104
+ queryVector: embedding,
105
+ topK: 10,
106
+ filter: {
107
+ category: 'electronics',
108
+ price: { $lt: 1000 },
109
+ inStock: true,
110
+ },
111
+ })
112
+
113
+ // Array operations
114
+ const results = await pgVector.query({
115
+ indexName: 'embeddings',
116
+ queryVector: embedding,
117
+ topK: 10,
118
+ filter: {
119
+ tags: { $in: ['sale', 'new'] },
120
+ },
121
+ })
122
+
123
+ // Logical operators
124
+ const results = await pgVector.query({
125
+ indexName: 'embeddings',
126
+ queryVector: embedding,
127
+ topK: 10,
128
+ filter: {
129
+ $or: [{ category: 'electronics' }, { category: 'accessories' }],
130
+ $and: [{ price: { $gt: 50 } }, { price: { $lt: 200 } }],
131
+ },
132
+ })
133
+ ```
134
+
135
+ Common use cases for metadata filtering:
136
+
137
+ - Filter by document source or type
138
+ - Filter by date ranges
139
+ - Filter by specific categories or tags
140
+ - Filter by numerical ranges (e.g., price, rating)
141
+ - Combine multiple conditions for precise querying
142
+ - Filter by document attributes (e.g., language, author)
143
+
144
+ ### Vector Query Tool
145
+
146
+ Sometimes you want to give your agent the ability to query a vector database directly. The Vector Query Tool allows your agent to be in charge of retrieval decisions, combining semantic search with optional filtering and reranking based on the agent's understanding of the user's needs.
147
+
148
+ ```ts
149
+ import { createVectorQueryTool } from '@mastra/rag'
150
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
151
+
152
+ const vectorQueryTool = createVectorQueryTool({
153
+ vectorStoreName: 'pgVector',
154
+ indexName: 'embeddings',
155
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
156
+ })
157
+ ```
158
+
159
+ When creating the tool, pay special attention to the tool's name and description - these help the agent understand when and how to use the retrieval capabilities. For example, you might name it "SearchKnowledgeBase" and describe it as "Search through our documentation to find relevant information about X topic."
160
+
161
+ This is particularly useful when:
162
+
163
+ - Your agent needs to decide at runtime what information to retrieve
164
+ - The retrieval process requires complex decision-making
165
+ - You want the agent to combine multiple retrieval strategies based on context
166
+
167
+ #### Database-Specific Configurations
168
+
169
+ The Vector Query Tool supports database-specific configurations that enable you to use unique features and optimizations of different vector stores.
170
+
171
+ > **Note:** These configurations are for **query-time options** like namespaces, performance tuning, and filtering, not for database connection setup.
172
+ >
173
+ > Connection credentials (URLs, auth tokens) are configured when you instantiate the vector store class (e.g., `new LibSQLVector({ url: '...' })`).
174
+
175
+ ```ts
176
+ import { createVectorQueryTool } from '@mastra/rag'
177
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
178
+
179
+ // Pinecone with namespace
180
+ const pineconeQueryTool = createVectorQueryTool({
181
+ vectorStoreName: 'pinecone',
182
+ indexName: 'docs',
183
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
184
+ databaseConfig: {
185
+ pinecone: {
186
+ namespace: 'production', // Isolate data by environment
187
+ },
188
+ },
189
+ })
190
+
191
+ // pgVector with performance tuning
192
+ const pgVectorQueryTool = createVectorQueryTool({
193
+ vectorStoreName: 'postgres',
194
+ indexName: 'embeddings',
195
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
196
+ databaseConfig: {
197
+ pgvector: {
198
+ minScore: 0.7, // Filter low-quality results
199
+ ef: 200, // HNSW search parameter
200
+ probes: 10, // IVFFlat probe parameter
201
+ },
202
+ },
203
+ })
204
+
205
+ // Chroma with advanced filtering
206
+ const chromaQueryTool = createVectorQueryTool({
207
+ vectorStoreName: 'chroma',
208
+ indexName: 'documents',
209
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
210
+ databaseConfig: {
211
+ chroma: {
212
+ where: { category: 'technical' },
213
+ whereDocument: { $contains: 'API' },
214
+ },
215
+ },
216
+ })
217
+
218
+ // LanceDB with table specificity
219
+ const lanceQueryTool = createVectorQueryTool({
220
+ vectorStoreName: 'lance',
221
+ indexName: 'documents',
222
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
223
+ databaseConfig: {
224
+ lance: {
225
+ tableName: 'myVectors', // Specify which table to query
226
+ includeAllColumns: true, // Include all metadata columns in results
227
+ },
228
+ },
229
+ })
230
+ ```
231
+
232
+ **Key Benefits:**
233
+
234
+ - **Pinecone namespaces**: Organize vectors by tenant, environment, or data type
235
+ - **pgVector optimization**: Control search accuracy and speed with ef/probes parameters
236
+ - **Quality filtering**: Set minimum similarity thresholds to improve result relevance
237
+ - **LanceDB tables**: Separate data into tables for better organization and performance
238
+ - **Runtime flexibility**: Override configurations at runtime based on context
239
+
240
+ **Common Use Cases:**
241
+
242
+ - Multi-tenant applications using Pinecone namespaces
243
+ - Performance optimization in high-load scenarios
244
+ - Environment-specific configurations (dev/staging/prod)
245
+ - Quality-gated search results
246
+ - Embedded, file-based vector storage with LanceDB for edge deployment scenarios
247
+
248
+ You can also override these configurations at runtime using the request context:
249
+
250
+ ```ts
251
+ import { RequestContext } from '@mastra/core/request-context'
252
+
253
+ const requestContext = new RequestContext()
254
+ requestContext.set('databaseConfig', {
255
+ pinecone: {
256
+ namespace: 'runtime-namespace',
257
+ },
258
+ })
259
+
260
+ await pineconeQueryTool.execute({ queryText: 'search query' }, { mastra, requestContext })
261
+ ```
262
+
263
+ For detailed configuration options and advanced usage, see the [Vector Query Tool Reference](https://mastra.ai/reference/tools/vector-query-tool).
264
+
265
+ ### Vector Store Prompts
266
+
267
+ Vector store prompts define query patterns and filtering capabilities for each vector database implementation. When implementing filtering, these prompts are required in the agent's instructions to specify valid operators and syntax for each vector store implementation.
268
+
269
+ **pgVector**:
270
+
271
+ ```ts
272
+ import { PGVECTOR_PROMPT } from '@mastra/pg'
273
+
274
+ export const ragAgent = new Agent({
275
+ id: 'rag-agent',
276
+ name: 'RAG Agent',
277
+ model: 'openai/gpt-5.6-sol',
278
+ instructions: `
279
+ Process queries using the provided context. Structure responses to be concise and relevant.
280
+ ${PGVECTOR_PROMPT}
281
+ `,
282
+ tools: { vectorQueryTool },
283
+ })
284
+ ```
285
+
286
+ **Pinecone**:
287
+
288
+ ```ts
289
+ import { PINECONE_PROMPT } from '@mastra/pinecone'
290
+
291
+ export const ragAgent = new Agent({
292
+ id: 'rag-agent',
293
+ name: 'RAG Agent',
294
+ model: 'openai/gpt-5.6-sol',
295
+ instructions: `
296
+ Process queries using the provided context. Structure responses to be concise and relevant.
297
+ ${PINECONE_PROMPT}
298
+ `,
299
+ tools: { vectorQueryTool },
300
+ })
301
+ ```
302
+
303
+ **Qdrant**:
304
+
305
+ ```ts
306
+ import { QDRANT_PROMPT } from '@mastra/qdrant'
307
+
308
+ export const ragAgent = new Agent({
309
+ id: 'rag-agent',
310
+ name: 'RAG Agent',
311
+ model: 'openai/gpt-5.6-sol',
312
+ instructions: `
313
+ Process queries using the provided context. Structure responses to be concise and relevant.
314
+ ${QDRANT_PROMPT}
315
+ `,
316
+ tools: { vectorQueryTool },
317
+ })
318
+ ```
319
+
320
+ **Chroma**:
321
+
322
+ ```ts
323
+ import { CHROMA_PROMPT } from '@mastra/chroma'
324
+
325
+ export const ragAgent = new Agent({
326
+ id: 'rag-agent',
327
+ name: 'RAG Agent',
328
+ model: 'openai/gpt-5.6-sol',
329
+ instructions: `
330
+ Process queries using the provided context. Structure responses to be concise and relevant.
331
+ ${CHROMA_PROMPT}
332
+ `,
333
+ tools: { vectorQueryTool },
334
+ })
335
+ ```
336
+
337
+ **Astra**:
338
+
339
+ ```ts
340
+ import { ASTRA_PROMPT } from '@mastra/astra'
341
+
342
+ export const ragAgent = new Agent({
343
+ id: 'rag-agent',
344
+ name: 'RAG Agent',
345
+ model: 'openai/gpt-5.6-sol',
346
+ instructions: `
347
+ Process queries using the provided context. Structure responses to be concise and relevant.
348
+ ${ASTRA_PROMPT}
349
+ `,
350
+ tools: { vectorQueryTool },
351
+ })
352
+ ```
353
+
354
+ **libSQL**:
355
+
356
+ ```ts
357
+ import { LIBSQL_PROMPT } from '@mastra/libsql'
358
+
359
+ export const ragAgent = new Agent({
360
+ id: 'rag-agent',
361
+ name: 'RAG Agent',
362
+ model: 'openai/gpt-5.6-sol',
363
+ instructions: `
364
+ Process queries using the provided context. Structure responses to be concise and relevant.
365
+ ${LIBSQL_PROMPT}
366
+ `,
367
+ tools: { vectorQueryTool },
368
+ })
369
+ ```
370
+
371
+ **Upstash**:
372
+
373
+ ```ts
374
+ import { UPSTASH_PROMPT } from '@mastra/upstash'
375
+
376
+ export const ragAgent = new Agent({
377
+ id: 'rag-agent',
378
+ name: 'RAG Agent',
379
+ model: 'openai/gpt-5.6-sol',
380
+ instructions: `
381
+ Process queries using the provided context. Structure responses to be concise and relevant.
382
+ ${UPSTASH_PROMPT}
383
+ `,
384
+ tools: { vectorQueryTool },
385
+ })
386
+ ```
387
+
388
+ **Vectorize**:
389
+
390
+ ```ts
391
+ import { VECTORIZE_PROMPT } from '@mastra/vectorize'
392
+
393
+ export const ragAgent = new Agent({
394
+ id: 'rag-agent',
395
+ name: 'RAG Agent',
396
+ model: 'openai/gpt-5.6-sol',
397
+ instructions: `
398
+ Process queries using the provided context. Structure responses to be concise and relevant.
399
+ ${VECTORIZE_PROMPT}
400
+ `,
401
+ tools: { vectorQueryTool },
402
+ })
403
+ ```
404
+
405
+ **MongoDB**:
406
+
407
+ ```ts
408
+ import { MONGODB_PROMPT } from '@mastra/mongodb'
409
+
410
+ export const ragAgent = new Agent({
411
+ id: 'rag-agent',
412
+ name: 'RAG Agent',
413
+ model: 'openai/gpt-5.6-sol',
414
+ instructions: `
415
+ Process queries using the provided context. Structure responses to be concise and relevant.
416
+ ${MONGODB_PROMPT}
417
+ `,
418
+ tools: { vectorQueryTool },
419
+ })
420
+ ```
421
+
422
+ **OpenSearch**:
423
+
424
+ ```ts
425
+ import { OPENSEARCH_PROMPT } from '@mastra/opensearch'
426
+
427
+ export const ragAgent = new Agent({
428
+ id: 'rag-agent',
429
+ name: 'RAG Agent',
430
+ model: 'openai/gpt-5.6-sol',
431
+ instructions: `
432
+ Process queries using the provided context. Structure responses to be concise and relevant.
433
+ ${OPENSEARCH_PROMPT}
434
+ `,
435
+ tools: { vectorQueryTool },
436
+ })
437
+ ```
438
+
439
+ **OracleDB**:
440
+
441
+ ```ts
442
+ import { ORACLEDB_PROMPT } from '@mastra/oracledb'
443
+
444
+ export const ragAgent = new Agent({
445
+ id: 'rag-agent',
446
+ name: 'RAG Agent',
447
+ model: 'openai/gpt-5.6-sol',
448
+ instructions: `
449
+ Process queries using the provided context. Structure responses to be concise and relevant.
450
+ ${ORACLEDB_PROMPT}
451
+ `,
452
+ tools: { vectorQueryTool },
453
+ })
454
+ ```
455
+
456
+ **S3Vectors**:
457
+
458
+ ```ts
459
+ import { S3VECTORS_PROMPT } from '@mastra/s3vectors'
460
+
461
+ export const ragAgent = new Agent({
462
+ id: 'rag-agent',
463
+ name: 'RAG Agent',
464
+ model: 'openai/gpt-5.6-sol',
465
+ instructions: `
466
+ Process queries using the provided context. Structure responses to be concise and relevant.
467
+ ${S3VECTORS_PROMPT}
468
+ `,
469
+ tools: { vectorQueryTool },
470
+ })
471
+ ```
472
+
473
+ ### Re-ranking
474
+
475
+ Initial vector similarity search can sometimes miss detailed relevance. Re-ranking is a more computationally expensive process, but more accurate algorithm that improves results by:
476
+
477
+ - Considering word order and exact matches
478
+ - Applying more advanced relevance scoring
479
+ - Using a method called cross-attention between query and documents
480
+
481
+ Here's how to use re-ranking:
482
+
483
+ ```ts
484
+ import { rerankWithScorer as rerank, MastraAgentRelevanceScorer } from '@mastra/rag'
485
+
486
+ // Get initial results from vector search
487
+ const initialResults = await pgVector.query({
488
+ indexName: 'embeddings',
489
+ queryVector: queryEmbedding,
490
+ topK: 10,
491
+ })
492
+
493
+ // Create a relevance scorer
494
+ const relevanceProvider = new MastraAgentRelevanceScorer(
495
+ 'relevance-scorer',
496
+ 'openai/gpt-5.6-sol',
497
+ )
498
+
499
+ // Re-rank the results
500
+ const rerankedResults = await rerank({
501
+ results: initialResults,
502
+ query,
503
+ scorer: relevanceProvider,
504
+ options: {
505
+ weights: {
506
+ semantic: 0.5, // How well the content matches the query semantically
507
+ vector: 0.3, // Original vector similarity score
508
+ position: 0.2, // Preserves original result ordering
509
+ },
510
+ topK: 10,
511
+ },
512
+ })
513
+ ```
514
+
515
+ The weights control how different factors influence the final ranking:
516
+
517
+ - `semantic`: Higher values prioritize semantic understanding and relevance to the query
518
+ - `vector`: Higher values favor the original vector similarity scores
519
+ - `position`: Higher values help maintain the original ordering of results
520
+
521
+ > **Note:** For semantic scoring to work properly during re-ranking, each result must include the text content in its `metadata.text` field.
522
+
523
+ You can also use other relevance score providers like Cohere or ZeroEntropy:
524
+
525
+ ```ts
526
+ const relevanceProvider = new CohereRelevanceScorer('rerank-v3.5')
527
+ ```
528
+
529
+ ```ts
530
+ const relevanceProvider = new ZeroEntropyRelevanceScorer('zerank-1')
531
+ ```
532
+
533
+ The re-ranked results combine vector similarity with semantic understanding to improve retrieval quality.
534
+
535
+ For more details about re-ranking, see the [rerank()](https://mastra.ai/reference/rag/rerankWithScorer) method.
536
+
537
+ For graph-based retrieval that follows connections between chunks, see the [GraphRAG](https://mastra.ai/guides/rag/graph-rag) documentation.