@mastra/libsql 0.0.0-workflow-timeout-issue-20260220185702 → 0.0.0-workflow-graph-crash-20260527191009
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.
- package/CHANGELOG.md +1085 -3
- package/LICENSE.md +15 -0
- package/dist/docs/SKILL.md +18 -19
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-agent-builder-deploying.md +121 -0
- package/dist/docs/references/docs-agent-builder-overview.md +97 -0
- package/dist/docs/references/docs-agents-agent-approval.md +336 -202
- package/dist/docs/references/docs-agents-networks.md +107 -215
- package/dist/docs/references/docs-memory-memory-processors.md +86 -88
- package/dist/docs/references/docs-memory-message-history.md +71 -63
- package/dist/docs/references/docs-memory-multi-user-threads.md +206 -0
- package/dist/docs/references/docs-memory-overview.md +221 -24
- package/dist/docs/references/docs-memory-semantic-recall.md +162 -75
- package/dist/docs/references/docs-memory-storage.md +72 -73
- package/dist/docs/references/docs-memory-working-memory.md +62 -62
- package/dist/docs/references/docs-rag-retrieval.md +132 -135
- package/dist/docs/references/docs-workflows-snapshots.md +31 -31
- package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +36 -36
- package/dist/docs/references/reference-core-getMemory.md +15 -16
- package/dist/docs/references/reference-core-listMemory.md +17 -18
- package/dist/docs/references/reference-core-mastra-class.md +37 -27
- package/dist/docs/references/reference-memory-memory-class.md +41 -43
- package/dist/docs/references/reference-storage-composite.md +65 -55
- package/dist/docs/references/reference-storage-dynamodb.md +36 -36
- package/dist/docs/references/reference-storage-libsql.md +27 -27
- package/dist/docs/references/reference-vectors-libsql.md +72 -72
- package/dist/index.cjs +7896 -3257
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +7888 -3259
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +14 -1
- package/dist/storage/db/index.d.ts.map +1 -1
- package/dist/storage/db/utils.d.ts +16 -0
- package/dist/storage/db/utils.d.ts.map +1 -1
- package/dist/storage/domains/agents/index.d.ts.map +1 -1
- package/dist/storage/domains/background-tasks/index.d.ts +18 -0
- package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
- package/dist/storage/domains/blobs/index.d.ts +17 -0
- package/dist/storage/domains/blobs/index.d.ts.map +1 -0
- package/dist/storage/domains/channels/index.d.ts +20 -0
- package/dist/storage/domains/channels/index.d.ts.map +1 -0
- package/dist/storage/domains/datasets/index.d.ts +43 -0
- package/dist/storage/domains/datasets/index.d.ts.map +1 -0
- package/dist/storage/domains/experiments/index.d.ts +31 -0
- package/dist/storage/domains/experiments/index.d.ts.map +1 -0
- package/dist/storage/domains/favorites/index.d.ts +17 -0
- package/dist/storage/domains/favorites/index.d.ts.map +1 -0
- package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -1
- package/dist/storage/domains/mcp-servers/index.d.ts +26 -0
- package/dist/storage/domains/mcp-servers/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +7 -3
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +2 -1
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -1
- package/dist/storage/domains/schedules/index.d.ts +19 -0
- package/dist/storage/domains/schedules/index.d.ts.map +1 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -1
- package/dist/storage/domains/skills/index.d.ts +26 -0
- package/dist/storage/domains/skills/index.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +1 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/domains/workspaces/index.d.ts +26 -0
- package/dist/storage/domains/workspaces/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +38 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +14 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +12 -12
- package/dist/docs/references/docs-agents-agent-memory.md +0 -212
- package/dist/docs/references/docs-agents-network-approval.md +0 -275
- package/dist/docs/references/docs-observability-overview.md +0 -70
- package/dist/docs/references/docs-observability-tracing-exporters-default.md +0 -211
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# Retrieval in RAG
|
|
1
|
+
# Retrieval in RAG systems
|
|
2
2
|
|
|
3
3
|
After storing embeddings, you need to retrieve relevant chunks to answer user queries.
|
|
4
4
|
|
|
5
5
|
Mastra provides flexible retrieval options with support for semantic search, filtering, and re-ranking.
|
|
6
6
|
|
|
7
|
-
## How
|
|
7
|
+
## How retrieval works
|
|
8
8
|
|
|
9
9
|
1. The user's query is converted to an embedding using the same model used for document embeddings
|
|
10
10
|
2. This embedding is compared to stored embeddings using vector similarity
|
|
@@ -14,34 +14,34 @@ Mastra provides flexible retrieval options with support for semantic search, fil
|
|
|
14
14
|
- Re-ranked for better relevance
|
|
15
15
|
- Processed through a knowledge graph
|
|
16
16
|
|
|
17
|
-
## Basic
|
|
17
|
+
## Basic retrieval
|
|
18
18
|
|
|
19
19
|
The simplest approach is direct semantic search. This method uses vector similarity to find chunks that are semantically similar to the query:
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
|
-
import { embed } from
|
|
23
|
-
import { PgVector } from
|
|
24
|
-
import { ModelRouterEmbeddingModel } from
|
|
22
|
+
import { embed } from 'ai'
|
|
23
|
+
import { PgVector } from '@mastra/pg'
|
|
24
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
25
25
|
|
|
26
26
|
// Convert query to embedding
|
|
27
27
|
const { embedding } = await embed({
|
|
28
|
-
value:
|
|
29
|
-
model: new ModelRouterEmbeddingModel(
|
|
30
|
-
})
|
|
28
|
+
value: 'What are the main points in the article?',
|
|
29
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
30
|
+
})
|
|
31
31
|
|
|
32
32
|
// Query vector store
|
|
33
33
|
const pgVector = new PgVector({
|
|
34
34
|
id: 'pg-vector',
|
|
35
35
|
connectionString: process.env.POSTGRES_CONNECTION_STRING,
|
|
36
|
-
})
|
|
36
|
+
})
|
|
37
37
|
const results = await pgVector.query({
|
|
38
|
-
indexName:
|
|
38
|
+
indexName: 'embeddings',
|
|
39
39
|
queryVector: embedding,
|
|
40
40
|
topK: 10,
|
|
41
|
-
})
|
|
41
|
+
})
|
|
42
42
|
|
|
43
43
|
// Display results
|
|
44
|
-
console.log(results)
|
|
44
|
+
console.log(results)
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
The `topK` parameter specifies the maximum number of most similar results to return from the vector search.
|
|
@@ -51,19 +51,19 @@ Results include both the text content and a similarity score:
|
|
|
51
51
|
```ts
|
|
52
52
|
[
|
|
53
53
|
{
|
|
54
|
-
text:
|
|
54
|
+
text: 'Climate change poses significant challenges...',
|
|
55
55
|
score: 0.89,
|
|
56
|
-
metadata: { source:
|
|
56
|
+
metadata: { source: 'article1.txt' },
|
|
57
57
|
},
|
|
58
58
|
{
|
|
59
|
-
text:
|
|
59
|
+
text: 'Rising temperatures affect crop yields...',
|
|
60
60
|
score: 0.82,
|
|
61
|
-
metadata: { source:
|
|
61
|
+
metadata: { source: 'article1.txt' },
|
|
62
62
|
},
|
|
63
|
-
]
|
|
63
|
+
]
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
-
## Advanced
|
|
66
|
+
## Advanced retrieval options
|
|
67
67
|
|
|
68
68
|
### Metadata Filtering
|
|
69
69
|
|
|
@@ -78,56 +78,56 @@ Basic filtering examples:
|
|
|
78
78
|
```ts
|
|
79
79
|
// Simple equality filter
|
|
80
80
|
const results = await pgVector.query({
|
|
81
|
-
indexName:
|
|
81
|
+
indexName: 'embeddings',
|
|
82
82
|
queryVector: embedding,
|
|
83
83
|
topK: 10,
|
|
84
84
|
filter: {
|
|
85
|
-
source:
|
|
85
|
+
source: 'article1.txt',
|
|
86
86
|
},
|
|
87
|
-
})
|
|
87
|
+
})
|
|
88
88
|
|
|
89
89
|
// Numeric comparison
|
|
90
90
|
const results = await pgVector.query({
|
|
91
|
-
indexName:
|
|
91
|
+
indexName: 'embeddings',
|
|
92
92
|
queryVector: embedding,
|
|
93
93
|
topK: 10,
|
|
94
94
|
filter: {
|
|
95
95
|
price: { $gt: 100 },
|
|
96
96
|
},
|
|
97
|
-
})
|
|
97
|
+
})
|
|
98
98
|
|
|
99
99
|
// Multiple conditions
|
|
100
100
|
const results = await pgVector.query({
|
|
101
|
-
indexName:
|
|
101
|
+
indexName: 'embeddings',
|
|
102
102
|
queryVector: embedding,
|
|
103
103
|
topK: 10,
|
|
104
104
|
filter: {
|
|
105
|
-
category:
|
|
105
|
+
category: 'electronics',
|
|
106
106
|
price: { $lt: 1000 },
|
|
107
107
|
inStock: true,
|
|
108
108
|
},
|
|
109
|
-
})
|
|
109
|
+
})
|
|
110
110
|
|
|
111
111
|
// Array operations
|
|
112
112
|
const results = await pgVector.query({
|
|
113
|
-
indexName:
|
|
113
|
+
indexName: 'embeddings',
|
|
114
114
|
queryVector: embedding,
|
|
115
115
|
topK: 10,
|
|
116
116
|
filter: {
|
|
117
|
-
tags: { $in: [
|
|
117
|
+
tags: { $in: ['sale', 'new'] },
|
|
118
118
|
},
|
|
119
|
-
})
|
|
119
|
+
})
|
|
120
120
|
|
|
121
121
|
// Logical operators
|
|
122
122
|
const results = await pgVector.query({
|
|
123
|
-
indexName:
|
|
123
|
+
indexName: 'embeddings',
|
|
124
124
|
queryVector: embedding,
|
|
125
125
|
topK: 10,
|
|
126
126
|
filter: {
|
|
127
|
-
$or: [{ category:
|
|
127
|
+
$or: [{ category: 'electronics' }, { category: 'accessories' }],
|
|
128
128
|
$and: [{ price: { $gt: 50 } }, { price: { $lt: 200 } }],
|
|
129
129
|
},
|
|
130
|
-
})
|
|
130
|
+
})
|
|
131
131
|
```
|
|
132
132
|
|
|
133
133
|
Common use cases for metadata filtering:
|
|
@@ -144,14 +144,14 @@ Common use cases for metadata filtering:
|
|
|
144
144
|
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.
|
|
145
145
|
|
|
146
146
|
```ts
|
|
147
|
-
import { createVectorQueryTool } from
|
|
148
|
-
import { ModelRouterEmbeddingModel } from
|
|
147
|
+
import { createVectorQueryTool } from '@mastra/rag'
|
|
148
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
149
149
|
|
|
150
150
|
const vectorQueryTool = createVectorQueryTool({
|
|
151
|
-
vectorStoreName:
|
|
152
|
-
indexName:
|
|
153
|
-
model: new ModelRouterEmbeddingModel(
|
|
154
|
-
})
|
|
151
|
+
vectorStoreName: 'pgVector',
|
|
152
|
+
indexName: 'embeddings',
|
|
153
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
154
|
+
})
|
|
155
155
|
```
|
|
156
156
|
|
|
157
157
|
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."
|
|
@@ -171,26 +171,26 @@ The Vector Query Tool supports database-specific configurations that enable you
|
|
|
171
171
|
> Connection credentials (URLs, auth tokens) are configured when you instantiate the vector store class (e.g., `new LibSQLVector({ url: '...' })`).
|
|
172
172
|
|
|
173
173
|
```ts
|
|
174
|
-
import { createVectorQueryTool } from
|
|
175
|
-
import { ModelRouterEmbeddingModel } from
|
|
174
|
+
import { createVectorQueryTool } from '@mastra/rag'
|
|
175
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
176
176
|
|
|
177
177
|
// Pinecone with namespace
|
|
178
178
|
const pineconeQueryTool = createVectorQueryTool({
|
|
179
|
-
vectorStoreName:
|
|
180
|
-
indexName:
|
|
181
|
-
model: new ModelRouterEmbeddingModel(
|
|
179
|
+
vectorStoreName: 'pinecone',
|
|
180
|
+
indexName: 'docs',
|
|
181
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
182
182
|
databaseConfig: {
|
|
183
183
|
pinecone: {
|
|
184
|
-
namespace:
|
|
184
|
+
namespace: 'production', // Isolate data by environment
|
|
185
185
|
},
|
|
186
186
|
},
|
|
187
|
-
})
|
|
187
|
+
})
|
|
188
188
|
|
|
189
189
|
// pgVector with performance tuning
|
|
190
190
|
const pgVectorQueryTool = createVectorQueryTool({
|
|
191
|
-
vectorStoreName:
|
|
192
|
-
indexName:
|
|
193
|
-
model: new ModelRouterEmbeddingModel(
|
|
191
|
+
vectorStoreName: 'postgres',
|
|
192
|
+
indexName: 'embeddings',
|
|
193
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
194
194
|
databaseConfig: {
|
|
195
195
|
pgvector: {
|
|
196
196
|
minScore: 0.7, // Filter low-quality results
|
|
@@ -198,33 +198,33 @@ const pgVectorQueryTool = createVectorQueryTool({
|
|
|
198
198
|
probes: 10, // IVFFlat probe parameter
|
|
199
199
|
},
|
|
200
200
|
},
|
|
201
|
-
})
|
|
201
|
+
})
|
|
202
202
|
|
|
203
203
|
// Chroma with advanced filtering
|
|
204
204
|
const chromaQueryTool = createVectorQueryTool({
|
|
205
|
-
vectorStoreName:
|
|
206
|
-
indexName:
|
|
207
|
-
model: new ModelRouterEmbeddingModel(
|
|
205
|
+
vectorStoreName: 'chroma',
|
|
206
|
+
indexName: 'documents',
|
|
207
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
208
208
|
databaseConfig: {
|
|
209
209
|
chroma: {
|
|
210
|
-
where: { category:
|
|
211
|
-
whereDocument: { $contains:
|
|
210
|
+
where: { category: 'technical' },
|
|
211
|
+
whereDocument: { $contains: 'API' },
|
|
212
212
|
},
|
|
213
213
|
},
|
|
214
|
-
})
|
|
214
|
+
})
|
|
215
215
|
|
|
216
216
|
// LanceDB with table specificity
|
|
217
217
|
const lanceQueryTool = createVectorQueryTool({
|
|
218
|
-
vectorStoreName:
|
|
219
|
-
indexName:
|
|
220
|
-
model: new ModelRouterEmbeddingModel(
|
|
218
|
+
vectorStoreName: 'lance',
|
|
219
|
+
indexName: 'documents',
|
|
220
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
221
221
|
databaseConfig: {
|
|
222
222
|
lance: {
|
|
223
|
-
tableName:
|
|
223
|
+
tableName: 'myVectors', // Specify which table to query
|
|
224
224
|
includeAllColumns: true, // Include all metadata columns in results
|
|
225
225
|
},
|
|
226
226
|
},
|
|
227
|
-
})
|
|
227
|
+
})
|
|
228
228
|
```
|
|
229
229
|
|
|
230
230
|
**Key Benefits:**
|
|
@@ -246,19 +246,16 @@ const lanceQueryTool = createVectorQueryTool({
|
|
|
246
246
|
You can also override these configurations at runtime using the request context:
|
|
247
247
|
|
|
248
248
|
```ts
|
|
249
|
-
import { RequestContext } from
|
|
249
|
+
import { RequestContext } from '@mastra/core/request-context'
|
|
250
250
|
|
|
251
|
-
const requestContext = new RequestContext()
|
|
252
|
-
requestContext.set(
|
|
251
|
+
const requestContext = new RequestContext()
|
|
252
|
+
requestContext.set('databaseConfig', {
|
|
253
253
|
pinecone: {
|
|
254
|
-
namespace:
|
|
254
|
+
namespace: 'runtime-namespace',
|
|
255
255
|
},
|
|
256
|
-
})
|
|
256
|
+
})
|
|
257
257
|
|
|
258
|
-
await pineconeQueryTool.execute(
|
|
259
|
-
{ queryText: "search query" },
|
|
260
|
-
{ mastra, requestContext }
|
|
261
|
-
);
|
|
258
|
+
await pineconeQueryTool.execute({ queryText: 'search query' }, { mastra, requestContext })
|
|
262
259
|
```
|
|
263
260
|
|
|
264
261
|
For detailed configuration options and advanced usage, see the [Vector Query Tool Reference](https://mastra.ai/reference/tools/vector-query-tool).
|
|
@@ -270,188 +267,188 @@ Vector store prompts define query patterns and filtering capabilities for each v
|
|
|
270
267
|
**pgVector**:
|
|
271
268
|
|
|
272
269
|
```ts
|
|
273
|
-
import { PGVECTOR_PROMPT } from
|
|
270
|
+
import { PGVECTOR_PROMPT } from '@mastra/pg'
|
|
274
271
|
|
|
275
272
|
export const ragAgent = new Agent({
|
|
276
|
-
id:
|
|
277
|
-
name:
|
|
278
|
-
model:
|
|
273
|
+
id: 'rag-agent',
|
|
274
|
+
name: 'RAG Agent',
|
|
275
|
+
model: 'openai/gpt-5.4',
|
|
279
276
|
instructions: `
|
|
280
277
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
281
278
|
${PGVECTOR_PROMPT}
|
|
282
279
|
`,
|
|
283
280
|
tools: { vectorQueryTool },
|
|
284
|
-
})
|
|
281
|
+
})
|
|
285
282
|
```
|
|
286
283
|
|
|
287
284
|
**Pinecone**:
|
|
288
285
|
|
|
289
286
|
```ts
|
|
290
|
-
import { PINECONE_PROMPT } from
|
|
287
|
+
import { PINECONE_PROMPT } from '@mastra/pinecone'
|
|
291
288
|
|
|
292
289
|
export const ragAgent = new Agent({
|
|
293
|
-
id:
|
|
294
|
-
name:
|
|
295
|
-
model:
|
|
290
|
+
id: 'rag-agent',
|
|
291
|
+
name: 'RAG Agent',
|
|
292
|
+
model: 'openai/gpt-5.4',
|
|
296
293
|
instructions: `
|
|
297
294
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
298
295
|
${PINECONE_PROMPT}
|
|
299
296
|
`,
|
|
300
297
|
tools: { vectorQueryTool },
|
|
301
|
-
})
|
|
298
|
+
})
|
|
302
299
|
```
|
|
303
300
|
|
|
304
301
|
**Qdrant**:
|
|
305
302
|
|
|
306
303
|
```ts
|
|
307
|
-
import { QDRANT_PROMPT } from
|
|
304
|
+
import { QDRANT_PROMPT } from '@mastra/qdrant'
|
|
308
305
|
|
|
309
306
|
export const ragAgent = new Agent({
|
|
310
|
-
id:
|
|
311
|
-
name:
|
|
312
|
-
model:
|
|
307
|
+
id: 'rag-agent',
|
|
308
|
+
name: 'RAG Agent',
|
|
309
|
+
model: 'openai/gpt-5.4',
|
|
313
310
|
instructions: `
|
|
314
311
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
315
312
|
${QDRANT_PROMPT}
|
|
316
313
|
`,
|
|
317
314
|
tools: { vectorQueryTool },
|
|
318
|
-
})
|
|
315
|
+
})
|
|
319
316
|
```
|
|
320
317
|
|
|
321
318
|
**Chroma**:
|
|
322
319
|
|
|
323
320
|
```ts
|
|
324
|
-
import { CHROMA_PROMPT } from
|
|
321
|
+
import { CHROMA_PROMPT } from '@mastra/chroma'
|
|
325
322
|
|
|
326
323
|
export const ragAgent = new Agent({
|
|
327
|
-
id:
|
|
328
|
-
name:
|
|
329
|
-
model:
|
|
324
|
+
id: 'rag-agent',
|
|
325
|
+
name: 'RAG Agent',
|
|
326
|
+
model: 'openai/gpt-5.4',
|
|
330
327
|
instructions: `
|
|
331
328
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
332
329
|
${CHROMA_PROMPT}
|
|
333
330
|
`,
|
|
334
331
|
tools: { vectorQueryTool },
|
|
335
|
-
})
|
|
332
|
+
})
|
|
336
333
|
```
|
|
337
334
|
|
|
338
335
|
**Astra**:
|
|
339
336
|
|
|
340
337
|
```ts
|
|
341
|
-
import { ASTRA_PROMPT } from
|
|
338
|
+
import { ASTRA_PROMPT } from '@mastra/astra'
|
|
342
339
|
|
|
343
340
|
export const ragAgent = new Agent({
|
|
344
|
-
id:
|
|
345
|
-
name:
|
|
346
|
-
model:
|
|
341
|
+
id: 'rag-agent',
|
|
342
|
+
name: 'RAG Agent',
|
|
343
|
+
model: 'openai/gpt-5.4',
|
|
347
344
|
instructions: `
|
|
348
345
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
349
346
|
${ASTRA_PROMPT}
|
|
350
347
|
`,
|
|
351
348
|
tools: { vectorQueryTool },
|
|
352
|
-
})
|
|
349
|
+
})
|
|
353
350
|
```
|
|
354
351
|
|
|
355
352
|
**libSQL**:
|
|
356
353
|
|
|
357
354
|
```ts
|
|
358
|
-
import { LIBSQL_PROMPT } from
|
|
355
|
+
import { LIBSQL_PROMPT } from '@mastra/libsql'
|
|
359
356
|
|
|
360
357
|
export const ragAgent = new Agent({
|
|
361
|
-
id:
|
|
362
|
-
name:
|
|
363
|
-
model:
|
|
358
|
+
id: 'rag-agent',
|
|
359
|
+
name: 'RAG Agent',
|
|
360
|
+
model: 'openai/gpt-5.4',
|
|
364
361
|
instructions: `
|
|
365
362
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
366
363
|
${LIBSQL_PROMPT}
|
|
367
364
|
`,
|
|
368
365
|
tools: { vectorQueryTool },
|
|
369
|
-
})
|
|
366
|
+
})
|
|
370
367
|
```
|
|
371
368
|
|
|
372
369
|
**Upstash**:
|
|
373
370
|
|
|
374
371
|
```ts
|
|
375
|
-
import { UPSTASH_PROMPT } from
|
|
372
|
+
import { UPSTASH_PROMPT } from '@mastra/upstash'
|
|
376
373
|
|
|
377
374
|
export const ragAgent = new Agent({
|
|
378
|
-
id:
|
|
379
|
-
name:
|
|
380
|
-
model:
|
|
375
|
+
id: 'rag-agent',
|
|
376
|
+
name: 'RAG Agent',
|
|
377
|
+
model: 'openai/gpt-5.4',
|
|
381
378
|
instructions: `
|
|
382
379
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
383
380
|
${UPSTASH_PROMPT}
|
|
384
381
|
`,
|
|
385
382
|
tools: { vectorQueryTool },
|
|
386
|
-
})
|
|
383
|
+
})
|
|
387
384
|
```
|
|
388
385
|
|
|
389
386
|
**Vectorize**:
|
|
390
387
|
|
|
391
388
|
```ts
|
|
392
|
-
import { VECTORIZE_PROMPT } from
|
|
389
|
+
import { VECTORIZE_PROMPT } from '@mastra/vectorize'
|
|
393
390
|
|
|
394
391
|
export const ragAgent = new Agent({
|
|
395
|
-
id:
|
|
396
|
-
name:
|
|
397
|
-
model:
|
|
392
|
+
id: 'rag-agent',
|
|
393
|
+
name: 'RAG Agent',
|
|
394
|
+
model: 'openai/gpt-5.4',
|
|
398
395
|
instructions: `
|
|
399
396
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
400
397
|
${VECTORIZE_PROMPT}
|
|
401
398
|
`,
|
|
402
399
|
tools: { vectorQueryTool },
|
|
403
|
-
})
|
|
400
|
+
})
|
|
404
401
|
```
|
|
405
402
|
|
|
406
403
|
**MongoDB**:
|
|
407
404
|
|
|
408
405
|
```ts
|
|
409
|
-
import { MONGODB_PROMPT } from
|
|
406
|
+
import { MONGODB_PROMPT } from '@mastra/mongodb'
|
|
410
407
|
|
|
411
408
|
export const ragAgent = new Agent({
|
|
412
|
-
id:
|
|
413
|
-
name:
|
|
414
|
-
model:
|
|
409
|
+
id: 'rag-agent',
|
|
410
|
+
name: 'RAG Agent',
|
|
411
|
+
model: 'openai/gpt-5.4',
|
|
415
412
|
instructions: `
|
|
416
413
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
417
414
|
${MONGODB_PROMPT}
|
|
418
415
|
`,
|
|
419
416
|
tools: { vectorQueryTool },
|
|
420
|
-
})
|
|
417
|
+
})
|
|
421
418
|
```
|
|
422
419
|
|
|
423
420
|
**OpenSearch**:
|
|
424
421
|
|
|
425
422
|
```ts
|
|
426
|
-
import { OPENSEARCH_PROMPT } from
|
|
423
|
+
import { OPENSEARCH_PROMPT } from '@mastra/opensearch'
|
|
427
424
|
|
|
428
425
|
export const ragAgent = new Agent({
|
|
429
|
-
id:
|
|
430
|
-
name:
|
|
431
|
-
model:
|
|
426
|
+
id: 'rag-agent',
|
|
427
|
+
name: 'RAG Agent',
|
|
428
|
+
model: 'openai/gpt-5.4',
|
|
432
429
|
instructions: `
|
|
433
430
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
434
431
|
${OPENSEARCH_PROMPT}
|
|
435
432
|
`,
|
|
436
433
|
tools: { vectorQueryTool },
|
|
437
|
-
})
|
|
434
|
+
})
|
|
438
435
|
```
|
|
439
436
|
|
|
440
437
|
**S3Vectors**:
|
|
441
438
|
|
|
442
439
|
```ts
|
|
443
|
-
import { S3VECTORS_PROMPT } from
|
|
440
|
+
import { S3VECTORS_PROMPT } from '@mastra/s3vectors'
|
|
444
441
|
|
|
445
442
|
export const ragAgent = new Agent({
|
|
446
|
-
id:
|
|
447
|
-
name:
|
|
448
|
-
model:
|
|
443
|
+
id: 'rag-agent',
|
|
444
|
+
name: 'RAG Agent',
|
|
445
|
+
model: 'openai/gpt-5.4',
|
|
449
446
|
instructions: `
|
|
450
447
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
451
448
|
${S3VECTORS_PROMPT}
|
|
452
449
|
`,
|
|
453
450
|
tools: { vectorQueryTool },
|
|
454
|
-
})
|
|
451
|
+
})
|
|
455
452
|
```
|
|
456
453
|
|
|
457
454
|
### Re-ranking
|
|
@@ -465,20 +462,20 @@ Initial vector similarity search can sometimes miss nuanced relevance. Re-rankin
|
|
|
465
462
|
Here's how to use re-ranking:
|
|
466
463
|
|
|
467
464
|
```ts
|
|
468
|
-
import {
|
|
469
|
-
rerankWithScorer as rerank,
|
|
470
|
-
MastraAgentRelevanceScorer
|
|
471
|
-
} from "@mastra/rag";
|
|
465
|
+
import { rerankWithScorer as rerank, MastraAgentRelevanceScorer } from '@mastra/rag'
|
|
472
466
|
|
|
473
467
|
// Get initial results from vector search
|
|
474
468
|
const initialResults = await pgVector.query({
|
|
475
|
-
indexName:
|
|
469
|
+
indexName: 'embeddings',
|
|
476
470
|
queryVector: queryEmbedding,
|
|
477
471
|
topK: 10,
|
|
478
|
-
})
|
|
472
|
+
})
|
|
479
473
|
|
|
480
474
|
// Create a relevance scorer
|
|
481
|
-
const relevanceProvider = new MastraAgentRelevanceScorer(
|
|
475
|
+
const relevanceProvider = new MastraAgentRelevanceScorer(
|
|
476
|
+
'relevance-scorer',
|
|
477
|
+
'openai/gpt-5.4',
|
|
478
|
+
)
|
|
482
479
|
|
|
483
480
|
// Re-rank the results
|
|
484
481
|
const rerankedResults = await rerank({
|
|
@@ -493,7 +490,7 @@ const rerankedResults = await rerank({
|
|
|
493
490
|
},
|
|
494
491
|
topK: 10,
|
|
495
492
|
},
|
|
496
|
-
)
|
|
493
|
+
})
|
|
497
494
|
```
|
|
498
495
|
|
|
499
496
|
The weights control how different factors influence the final ranking:
|
|
@@ -507,11 +504,11 @@ The weights control how different factors influence the final ranking:
|
|
|
507
504
|
You can also use other relevance score providers like Cohere or ZeroEntropy:
|
|
508
505
|
|
|
509
506
|
```ts
|
|
510
|
-
const relevanceProvider = new CohereRelevanceScorer(
|
|
507
|
+
const relevanceProvider = new CohereRelevanceScorer('rerank-v3.5')
|
|
511
508
|
```
|
|
512
509
|
|
|
513
510
|
```ts
|
|
514
|
-
const relevanceProvider = new ZeroEntropyRelevanceScorer(
|
|
511
|
+
const relevanceProvider = new ZeroEntropyRelevanceScorer('zerank-1')
|
|
515
512
|
```
|
|
516
513
|
|
|
517
514
|
The re-ranked results combine vector similarity with semantic understanding to improve retrieval quality.
|