@mastra/mongodb 1.12.1 → 1.13.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.
- package/CHANGELOG.md +26 -0
- package/dist/docs/SKILL.md +4 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
- package/dist/docs/references/docs-memory-working-memory.md +3 -3
- package/dist/docs/references/docs-rag-vector-databases.md +7 -2
- package/dist/docs/references/docs-storage-overview.md +214 -0
- package/dist/docs/references/reference-storage-composite.md +337 -0
- package/dist/docs/references/reference-storage-mongodb.md +46 -46
- package/dist/docs/references/reference-vectors-mongodb.md +93 -4
- package/dist/index.cjs +68 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +69 -67
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/datasets/index.d.ts.map +1 -1
- package/package.json +10 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @mastra/mongodb
|
|
2
2
|
|
|
3
|
+
## 1.13.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Added atomic caller-defined dataset IDs for idempotent dataset creation across built-in storage adapters. Supplying `id` to `mastra.datasets.create()` now creates the dataset once and resolves compatible retries to the persisted record; incompatible immutable identity fields throw `DATASET_ID_CONFLICT`. ([#19370](https://github.com/mastra-ai/mastra/pull/19370))
|
|
8
|
+
|
|
9
|
+
- Added caller-defined dataset item identities for safe retries across all dataset storage adapters. ([#19384](https://github.com/mastra-ai/mastra/pull/19384))
|
|
10
|
+
|
|
11
|
+
Dataset items can now include an `externalId` when calling `addItem` or `addItems`:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
await dataset.addItem({
|
|
15
|
+
externalId: 'source-item-123',
|
|
16
|
+
input: { prompt: 'Hello' },
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Retrying with the same identity and payload returns the existing item. Reusing an identity with different content returns a typed conflict, including during concurrent writes. Updates and deletes preserve the identity, Spanner retries transactions without changing the outcome, and MySQL batch writes now preserve every supported dataset item field.
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- Raised the `@mastra/core` peer dependency floor to `>=1.51.0-0` so the dataset item identity helpers used by the storage adapters are available at runtime. ([#19384](https://github.com/mastra-ai/mastra/pull/19384))
|
|
25
|
+
|
|
26
|
+
- Updated dependencies [[`a99eae8`](https://github.com/mastra-ai/mastra/commit/a99eae8908e500c1b2d12f9d277be616b98617a5), [`fd13f8e`](https://github.com/mastra-ai/mastra/commit/fd13f8e21990f9904c3eedba3a626bb4a929cdb8), [`f703f87`](https://github.com/mastra-ai/mastra/commit/f703f878de072d51fda557f9c50867d8252bef05), [`0ad646f`](https://github.com/mastra-ai/mastra/commit/0ad646f71a530f2454664299e5e01bfd13fa12e5)]:
|
|
27
|
+
- @mastra/core@1.51.0-alpha.13
|
|
28
|
+
|
|
3
29
|
## 1.12.1
|
|
4
30
|
|
|
5
31
|
### Patch Changes
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -3,7 +3,7 @@ name: mastra-mongodb
|
|
|
3
3
|
description: Documentation for @mastra/mongodb. Use when working with @mastra/mongodb APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/mongodb"
|
|
6
|
-
version: "1.
|
|
6
|
+
version: "1.13.0-alpha.0"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -16,12 +16,15 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
16
16
|
|
|
17
17
|
### Docs
|
|
18
18
|
|
|
19
|
+
- [Semantic recall](references/docs-memory-semantic-recall.md) - Learn how to use semantic recall in Mastra to retrieve relevant messages from past conversations using vector search and embeddings.
|
|
19
20
|
- [Working memory](references/docs-memory-working-memory.md) - Learn how to configure working memory in Mastra to store persistent user data, preferences.
|
|
20
21
|
- [Retrieval, semantic search, reranking](references/docs-rag-retrieval.md) - Guide on retrieval processes in Mastra's RAG systems, including semantic search, filtering, and re-ranking.
|
|
21
22
|
- [Storing embeddings in a vector database](references/docs-rag-vector-databases.md) - Guide on vector storage options in Mastra, including embedded and dedicated vector databases for similarity search.
|
|
23
|
+
- [Storage overview](references/docs-storage-overview.md) - Configure storage for Mastra to persist runtime state across agents, workflows, observability, evals, schedules, and memory.
|
|
22
24
|
|
|
23
25
|
### Reference
|
|
24
26
|
|
|
27
|
+
- [Reference: Composite storage](references/reference-storage-composite.md) - Documentation for combining multiple storage backends in Mastra.
|
|
25
28
|
- [Reference: MongoDB storage](references/reference-storage-mongodb.md) - Documentation for the MongoDB storage implementation in Mastra.
|
|
26
29
|
- [Reference: MongoDB vector store](references/reference-vectors-mongodb.md) - Documentation for the MongoDBVector class in Mastra, which provides vector search using MongoDB Atlas and Atlas Vector Search.
|
|
27
30
|
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Semantic recall
|
|
4
|
+
|
|
5
|
+
If you ask your friend what they did last weekend, they will search in their memory for events associated with "last weekend" and then tell you what they did. That's sort of like how semantic recall works in Mastra.
|
|
6
|
+
|
|
7
|
+
> **📹 Watch:** Watch [Mastra semantic recall](https://www.youtube.com/watch?v=UVZtK8cK8xQ\&pp=ygUVbWFzdHJhIHdvcmtpbmcgbWVtb3J5) to see how agents retrieve relevant messages from past conversations.
|
|
8
|
+
|
|
9
|
+
## How semantic recall works
|
|
10
|
+
|
|
11
|
+
Semantic recall is RAG-based search that helps agents maintain context across longer interactions when messages are no longer within [recent message history](https://mastra.ai/docs/memory/message-history).
|
|
12
|
+
|
|
13
|
+
It uses vector embeddings of messages for similarity search, integrates with various vector stores, and has configurable context windows around retrieved messages.
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
When it's enabled, new messages are used to query a vector DB for semantically similar messages.
|
|
18
|
+
|
|
19
|
+
After getting a response from the LLM, all new messages (user, assistant, and tool calls/results) are inserted into the vector DB to be recalled in later interactions.
|
|
20
|
+
|
|
21
|
+
## Quickstart
|
|
22
|
+
|
|
23
|
+
Semantic recall is disabled by default. To enable it, set `semanticRecall: true` in `options` and provide a `vector` store and `embedder`:
|
|
24
|
+
|
|
25
|
+
**LibSQL**:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Agent } from '@mastra/core/agent'
|
|
29
|
+
import { Memory } from '@mastra/memory'
|
|
30
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
|
|
31
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
32
|
+
|
|
33
|
+
const agent = new Agent({
|
|
34
|
+
id: 'support-agent',
|
|
35
|
+
name: 'SupportAgent',
|
|
36
|
+
instructions: 'You are a helpful support agent.',
|
|
37
|
+
model: 'openai/gpt-5.5',
|
|
38
|
+
memory: new Memory({
|
|
39
|
+
storage: new LibSQLStore({
|
|
40
|
+
id: 'agent-storage',
|
|
41
|
+
url: 'file:./local.db',
|
|
42
|
+
}),
|
|
43
|
+
vector: new LibSQLVector({
|
|
44
|
+
id: 'agent-vector',
|
|
45
|
+
url: 'file:./local.db',
|
|
46
|
+
}),
|
|
47
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
48
|
+
options: {
|
|
49
|
+
semanticRecall: true,
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
52
|
+
})
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**MongoDB**:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { Agent } from '@mastra/core/agent'
|
|
59
|
+
import { Memory } from '@mastra/memory'
|
|
60
|
+
import { MongoDBStore, MongoDBVector } from '@mastra/mongodb'
|
|
61
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
62
|
+
|
|
63
|
+
const agent = new Agent({
|
|
64
|
+
id: 'support-agent',
|
|
65
|
+
name: 'SupportAgent',
|
|
66
|
+
instructions: 'You are a helpful support agent.',
|
|
67
|
+
model: 'openai/gpt-5.5',
|
|
68
|
+
memory: new Memory({
|
|
69
|
+
storage: new MongoDBStore({
|
|
70
|
+
id: 'agent-storage',
|
|
71
|
+
uri: process.env.MONGODB_URI,
|
|
72
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
73
|
+
}),
|
|
74
|
+
vector: new MongoDBVector({
|
|
75
|
+
id: 'agent-vector',
|
|
76
|
+
uri: process.env.MONGODB_URI,
|
|
77
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
78
|
+
}),
|
|
79
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
80
|
+
options: {
|
|
81
|
+
semanticRecall: true,
|
|
82
|
+
},
|
|
83
|
+
}),
|
|
84
|
+
})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Using the `recall()` method
|
|
88
|
+
|
|
89
|
+
While `listMessages` retrieves messages by thread ID with basic pagination, [`recall()`](https://mastra.ai/reference/memory/recall) adds support for **semantic search**. When you need to find messages by meaning rather than recency, use `recall()` with a `vectorSearchString`:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
const memory = await agent.getMemory()
|
|
93
|
+
|
|
94
|
+
// Basic recall - similar to listMessages
|
|
95
|
+
const { messages } = await memory!.recall({
|
|
96
|
+
threadId: 'thread-123',
|
|
97
|
+
perPage: 50,
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
// Semantic recall - find messages by meaning
|
|
101
|
+
const { messages: relevantMessages } = await memory!.recall({
|
|
102
|
+
threadId: 'thread-123',
|
|
103
|
+
vectorSearchString: 'What did we discuss about the project deadline?',
|
|
104
|
+
threadConfig: {
|
|
105
|
+
semanticRecall: true,
|
|
106
|
+
},
|
|
107
|
+
})
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Storage configuration
|
|
111
|
+
|
|
112
|
+
Semantic recall relies on a [storage and vector db](https://mastra.ai/reference/memory/memory-class) to store messages and their embeddings.
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { Memory } from '@mastra/memory'
|
|
116
|
+
import { Agent } from '@mastra/core/agent'
|
|
117
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
|
|
118
|
+
|
|
119
|
+
const agent = new Agent({
|
|
120
|
+
memory: new Memory({
|
|
121
|
+
// this is the default storage db if omitted
|
|
122
|
+
storage: new LibSQLStore({
|
|
123
|
+
id: 'agent-storage',
|
|
124
|
+
url: 'file:./local.db',
|
|
125
|
+
}),
|
|
126
|
+
// this is the default vector db if omitted
|
|
127
|
+
vector: new LibSQLVector({
|
|
128
|
+
id: 'agent-vector',
|
|
129
|
+
url: 'file:./local.db',
|
|
130
|
+
}),
|
|
131
|
+
options: {
|
|
132
|
+
semanticRecall: true,
|
|
133
|
+
},
|
|
134
|
+
}),
|
|
135
|
+
})
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Each vector store page below includes installation instructions, configuration parameters, and usage examples:
|
|
139
|
+
|
|
140
|
+
- [Astra](https://mastra.ai/reference/vectors/astra)
|
|
141
|
+
- [Chroma](https://mastra.ai/reference/vectors/chroma)
|
|
142
|
+
- [Cloudflare Vectorize](https://mastra.ai/reference/vectors/vectorize)
|
|
143
|
+
- [Convex](https://mastra.ai/reference/vectors/convex)
|
|
144
|
+
- [Couchbase](https://mastra.ai/reference/vectors/couchbase)
|
|
145
|
+
- [DuckDB](https://mastra.ai/reference/vectors/duckdb)
|
|
146
|
+
- [Elasticsearch](https://mastra.ai/reference/vectors/elasticsearch)
|
|
147
|
+
- [LanceDB](https://mastra.ai/reference/vectors/lance)
|
|
148
|
+
- [libSQL](https://mastra.ai/reference/vectors/libsql)
|
|
149
|
+
- [MongoDB](https://mastra.ai/reference/vectors/mongodb)
|
|
150
|
+
- [OpenSearch](https://mastra.ai/reference/vectors/opensearch)
|
|
151
|
+
- [Pinecone](https://mastra.ai/reference/vectors/pinecone)
|
|
152
|
+
- [PostgreSQL](https://mastra.ai/reference/vectors/pg)
|
|
153
|
+
- [Qdrant](https://mastra.ai/reference/vectors/qdrant)
|
|
154
|
+
- [S3 Vectors](https://mastra.ai/reference/vectors/s3vectors)
|
|
155
|
+
- [Turbopuffer](https://mastra.ai/reference/vectors/turbopuffer)
|
|
156
|
+
- [Upstash](https://mastra.ai/reference/vectors/upstash)
|
|
157
|
+
|
|
158
|
+
## Recall configuration
|
|
159
|
+
|
|
160
|
+
The following options control semantic recall behavior:
|
|
161
|
+
|
|
162
|
+
1. **topK**: The number of similar messages to retrieve
|
|
163
|
+
2. **messageRange**: The surrounding messages to include with each match
|
|
164
|
+
3. **scope**: Whether to search the current thread or all threads for a resource
|
|
165
|
+
4. **filter**: Metadata criteria that restrict search results
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
const agent = new Agent({
|
|
169
|
+
id: 'agent',
|
|
170
|
+
memory: new Memory({
|
|
171
|
+
options: {
|
|
172
|
+
semanticRecall: {
|
|
173
|
+
topK: 3, // Retrieve 3 similar messages
|
|
174
|
+
messageRange: 2, // Include 2 messages before and after each match
|
|
175
|
+
scope: 'resource', // Search all threads for this resource
|
|
176
|
+
filter: { projectId: { $eq: 'project-a' } },
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
}),
|
|
180
|
+
})
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
> **Note:** `scope: 'resource'` is supported by the LibSQL, PostgreSQL, MongoDB, and Upstash storage adapters.
|
|
184
|
+
|
|
185
|
+
### Metadata filtering
|
|
186
|
+
|
|
187
|
+
The `filter` option restricts semantic recall results to messages with matching thread metadata.
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
const agent = new Agent({
|
|
191
|
+
id: 'agent',
|
|
192
|
+
memory: new Memory({
|
|
193
|
+
options: {
|
|
194
|
+
semanticRecall: {
|
|
195
|
+
scope: 'resource',
|
|
196
|
+
filter: {
|
|
197
|
+
projectId: { $eq: 'project-a' },
|
|
198
|
+
category: { $in: ['work', 'personal'] },
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
}),
|
|
203
|
+
})
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Filters match metadata stored on message embeddings when messages are saved. If thread metadata changes later, existing embeddings keep their previous metadata until those messages are saved or indexed again.
|
|
207
|
+
|
|
208
|
+
Supported filter operators:
|
|
209
|
+
|
|
210
|
+
- `$and`: Logical AND
|
|
211
|
+
- `$eq`: Equal to
|
|
212
|
+
- `$gt`: Greater than
|
|
213
|
+
- `$gte`: Greater than or equal
|
|
214
|
+
- `$in`: In array
|
|
215
|
+
- `$lt`: Less than
|
|
216
|
+
- `$lte`: Less than or equal
|
|
217
|
+
- `$ne`: Not equal to
|
|
218
|
+
- `$nin`: Not in array
|
|
219
|
+
- `$or`: Logical OR
|
|
220
|
+
|
|
221
|
+
The following example demonstrates metadata filters for common use cases:
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
// Filter by project
|
|
225
|
+
const options = {
|
|
226
|
+
semanticRecall: { filter: { projectId: { $eq: 'my-project' } } },
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Filter by multiple categories
|
|
230
|
+
const options = {
|
|
231
|
+
semanticRecall: { filter: { category: { $in: ['work', 'research'] } } },
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Filter by project and priority
|
|
235
|
+
const options = {
|
|
236
|
+
semanticRecall: {
|
|
237
|
+
filter: {
|
|
238
|
+
$and: [{ projectId: { $eq: 'project-a' } }, { priority: { $gte: 3 } }],
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Embedder configuration
|
|
245
|
+
|
|
246
|
+
Semantic recall relies on an [embedding model](https://mastra.ai/reference/memory/memory-class) to convert messages into embeddings. Mastra supports embedding models through the model router using `provider/model` strings, or you can use any [embedding model](https://sdk.vercel.ai/docs/ai-sdk-core/embeddings) compatible with the AI SDK.
|
|
247
|
+
|
|
248
|
+
### Using the Model Router (Recommended)
|
|
249
|
+
|
|
250
|
+
The simplest way is to use a `provider/model` string with autocomplete support:
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
import { Memory } from '@mastra/memory'
|
|
254
|
+
import { Agent } from '@mastra/core/agent'
|
|
255
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
256
|
+
|
|
257
|
+
const agent = new Agent({
|
|
258
|
+
id: 'agent',
|
|
259
|
+
memory: new Memory({
|
|
260
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
261
|
+
options: {
|
|
262
|
+
semanticRecall: true,
|
|
263
|
+
},
|
|
264
|
+
}),
|
|
265
|
+
})
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Supported embedding models:
|
|
269
|
+
|
|
270
|
+
- **OpenAI**: `text-embedding-3-small`, `text-embedding-3-large`, `text-embedding-ada-002`
|
|
271
|
+
- **Google**: `gemini-embedding-001`
|
|
272
|
+
- **OpenRouter**: Access embedding models from various providers
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
import { Agent } from '@mastra/core/agent'
|
|
276
|
+
import { Memory } from '@mastra/memory'
|
|
277
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
278
|
+
|
|
279
|
+
const agent = new Agent({
|
|
280
|
+
id: 'agent',
|
|
281
|
+
memory: new Memory({
|
|
282
|
+
embedder: new ModelRouterEmbeddingModel({
|
|
283
|
+
providerId: 'openrouter',
|
|
284
|
+
modelId: 'openai/text-embedding-3-small',
|
|
285
|
+
}),
|
|
286
|
+
}),
|
|
287
|
+
})
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
The model router automatically handles API key detection from environment variables (`OPENAI_API_KEY`, `GOOGLE_API_KEY`, `OPENROUTER_API_KEY`). Google models also fall back to `GOOGLE_GENERATIVE_AI_API_KEY`.
|
|
291
|
+
|
|
292
|
+
### Using AI SDK Packages
|
|
293
|
+
|
|
294
|
+
You can also use AI SDK embedding models directly:
|
|
295
|
+
|
|
296
|
+
```ts
|
|
297
|
+
import { Memory } from '@mastra/memory'
|
|
298
|
+
import { Agent } from '@mastra/core/agent'
|
|
299
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
300
|
+
|
|
301
|
+
const agent = new Agent({
|
|
302
|
+
id: 'agent',
|
|
303
|
+
memory: new Memory({
|
|
304
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
305
|
+
}),
|
|
306
|
+
})
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Using FastEmbed (local)
|
|
310
|
+
|
|
311
|
+
To use FastEmbed (a local embedding model), install `@mastra/fastembed`:
|
|
312
|
+
|
|
313
|
+
**npm**:
|
|
314
|
+
|
|
315
|
+
```bash
|
|
316
|
+
npm install @mastra/fastembed@latest
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
**pnpm**:
|
|
320
|
+
|
|
321
|
+
```bash
|
|
322
|
+
pnpm add @mastra/fastembed@latest
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
**Yarn**:
|
|
326
|
+
|
|
327
|
+
```bash
|
|
328
|
+
yarn add @mastra/fastembed@latest
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
**Bun**:
|
|
332
|
+
|
|
333
|
+
```bash
|
|
334
|
+
bun add @mastra/fastembed@latest
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
Then configure it in your memory:
|
|
338
|
+
|
|
339
|
+
```ts
|
|
340
|
+
import { Memory } from '@mastra/memory'
|
|
341
|
+
import { Agent } from '@mastra/core/agent'
|
|
342
|
+
import { fastembed } from '@mastra/fastembed'
|
|
343
|
+
|
|
344
|
+
const agent = new Agent({
|
|
345
|
+
id: 'agent',
|
|
346
|
+
memory: new Memory({
|
|
347
|
+
embedder: fastembed,
|
|
348
|
+
}),
|
|
349
|
+
})
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## PostgreSQL index optimization
|
|
353
|
+
|
|
354
|
+
When using PostgreSQL as your vector store, you can optimize semantic recall performance by configuring the vector index. This is particularly important for large-scale deployments with thousands of messages.
|
|
355
|
+
|
|
356
|
+
PostgreSQL supports both IVFFlat and HNSW indexes. By default, Mastra creates an IVFFlat index, but HNSW indexes typically provide better performance, especially with OpenAI embeddings which use inner product distance.
|
|
357
|
+
|
|
358
|
+
```typescript
|
|
359
|
+
import { Memory } from '@mastra/memory'
|
|
360
|
+
import { PgStore, PgVector } from '@mastra/pg'
|
|
361
|
+
|
|
362
|
+
const agent = new Agent({
|
|
363
|
+
memory: new Memory({
|
|
364
|
+
storage: new PgStore({
|
|
365
|
+
id: 'agent-storage',
|
|
366
|
+
connectionString: process.env.DATABASE_URL,
|
|
367
|
+
}),
|
|
368
|
+
vector: new PgVector({
|
|
369
|
+
id: 'agent-vector',
|
|
370
|
+
connectionString: process.env.DATABASE_URL,
|
|
371
|
+
}),
|
|
372
|
+
options: {
|
|
373
|
+
semanticRecall: {
|
|
374
|
+
topK: 5,
|
|
375
|
+
messageRange: 2,
|
|
376
|
+
indexConfig: {
|
|
377
|
+
type: 'hnsw', // Use HNSW for better performance
|
|
378
|
+
metric: 'dotproduct', // Best for OpenAI embeddings
|
|
379
|
+
m: 16, // Number of bi-directional links (default: 16)
|
|
380
|
+
efConstruction: 64, // Size of candidate list during construction (default: 64)
|
|
381
|
+
},
|
|
382
|
+
},
|
|
383
|
+
},
|
|
384
|
+
}),
|
|
385
|
+
})
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
For detailed information about index configuration options and performance tuning, see the [PgVector configuration guide](https://mastra.ai/reference/vectors/pg).
|
|
389
|
+
|
|
390
|
+
## Disable semantic recall
|
|
391
|
+
|
|
392
|
+
Semantic recall is disabled by default (`semanticRecall: false`). Each call adds latency because new messages are converted into embeddings and used to query a vector database before the LLM receives them.
|
|
393
|
+
|
|
394
|
+
Keep semantic recall disabled when:
|
|
395
|
+
|
|
396
|
+
- Message history provides sufficient context for the current conversation.
|
|
397
|
+
- You're building performance-sensitive applications, like realtime two-way audio, where embedding and vector query latency is noticeable.
|
|
398
|
+
|
|
399
|
+
## Viewing recalled messages
|
|
400
|
+
|
|
401
|
+
When tracing is enabled, any messages retrieved via semantic recall will appear in the agent's trace output, alongside recent message history (if configured).
|
|
@@ -10,6 +10,8 @@ This is useful for maintaining ongoing state that's always relevant and should a
|
|
|
10
10
|
|
|
11
11
|
If you use [Observational Memory](https://mastra.ai/docs/memory/observational-memory), `observationalMemory.observation.manageWorkingMemory` lets OM update working memory for the agent.
|
|
12
12
|
|
|
13
|
+
> **📹 Watch:** Watch [Mastra working memory](https://www.youtube.com/watch?v=UMy_JHLf1n8\&pp=ygUVbWFzdHJhIHdvcmtpbmcgbWVtb3J5) to see how agents keep persistent user context available across interactions.
|
|
14
|
+
|
|
13
15
|
Working memory can persist at two different scopes:
|
|
14
16
|
|
|
15
17
|
- **Resource-scoped** (default): Memory persists across all conversation threads for the same user
|
|
@@ -43,9 +45,7 @@ const agent = new Agent({
|
|
|
43
45
|
|
|
44
46
|
## How it works
|
|
45
47
|
|
|
46
|
-
Working memory is a block of Markdown text that the agent is able to update over time to store continuously relevant information
|
|
47
|
-
|
|
48
|
-
[YouTube video player](https://www.youtube-nocookie.com/embed/UMy_JHLf1n8)
|
|
48
|
+
Working memory is a block of Markdown text that the agent is able to update over time to store continuously relevant information.
|
|
49
49
|
|
|
50
50
|
## Memory persistence scopes
|
|
51
51
|
|
|
@@ -14,7 +14,7 @@ import { MongoDBVector } from '@mastra/mongodb'
|
|
|
14
14
|
const store = new MongoDBVector({
|
|
15
15
|
id: 'mongodb-vector',
|
|
16
16
|
uri: process.env.MONGODB_URI,
|
|
17
|
-
dbName: process.env.
|
|
17
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
18
18
|
})
|
|
19
19
|
await store.createIndex({
|
|
20
20
|
indexName: 'myCollection',
|
|
@@ -27,10 +27,14 @@ await store.upsert({
|
|
|
27
27
|
})
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
### Using MongoDB Atlas Vector
|
|
30
|
+
### Using MongoDB Atlas Vector Search
|
|
31
31
|
|
|
32
32
|
For detailed setup instructions and best practices, see the [official MongoDB Atlas Vector Search documentation](https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-overview/?utm_campaign=devrel\&utm_source=third-party-content\&utm_medium=cta\&utm_content=mastra-docs).
|
|
33
33
|
|
|
34
|
+
### Using VoyageAI with MongoDB
|
|
35
|
+
|
|
36
|
+
MongoDB works seamlessly with VoyageAI's embedding models, which are optimized for retrieval tasks. For complete examples and specialized models, see the [VoyageAI embeddings documentation](https://mastra.ai/models/embeddings) and [MongoDB vector reference](https://mastra.ai/reference/vectors/mongodb).
|
|
37
|
+
|
|
34
38
|
**PgVector**:
|
|
35
39
|
|
|
36
40
|
```ts
|
|
@@ -359,6 +363,7 @@ The dimension size must match the output dimension of your chosen embedding mode
|
|
|
359
363
|
|
|
360
364
|
- `OpenAI text-embedding-3-small`: 1536 dimensions (or custom, e.g., 256)
|
|
361
365
|
- `Cohere embed-multilingual-v3`: 1024 dimensions
|
|
366
|
+
- `VoyageAI voyage-3.5`: 1024 dimensions (or custom: 256, 512, 1024, 2048)
|
|
362
367
|
- `Google gemini-embedding-001`: 768 dimensions (or custom)
|
|
363
368
|
|
|
364
369
|
> **Warning:** Index dimensions can't be changed after creation. To use a different model, delete and recreate the index with the new dimension size.
|