@mastra/pg 1.8.0-alpha.0 → 1.8.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 +62 -0
- package/dist/docs/SKILL.md +14 -14
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-memory-semantic-recall.md +7 -7
- package/dist/docs/references/docs-memory-working-memory.md +14 -14
- package/dist/docs/references/docs-rag-overview.md +2 -2
- package/dist/docs/references/docs-rag-retrieval.md +4 -4
- package/dist/docs/references/docs-rag-vector-databases.md +11 -11
- package/dist/docs/references/reference-memory-memory-class.md +1 -1
- package/dist/docs/references/reference-rag-metadata-filters.md +5 -5
- package/dist/docs/references/reference-storage-composite.md +1 -1
- package/dist/docs/references/reference-storage-dynamodb.md +5 -5
- package/dist/docs/references/reference-storage-postgresql.md +5 -5
- package/dist/docs/references/reference-tools-vector-query-tool.md +12 -12
- package/dist/docs/references/reference-vectors-pg.md +21 -21
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,67 @@
|
|
|
1
1
|
# @mastra/pg
|
|
2
2
|
|
|
3
|
+
## 1.8.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Added `metadataIndexes` option to `createIndex()` for PgVector. This allows creating btree indexes on specific metadata fields in vector tables, significantly improving query performance when filtering by those fields. This is especially impactful for Memory's `memory_messages` table, which filters by `thread_id` and `resource_id` — previously causing sequential scans under load. ([#14034](https://github.com/mastra-ai/mastra/pull/14034))
|
|
8
|
+
|
|
9
|
+
**Usage example:**
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
await pgVector.createIndex({
|
|
13
|
+
indexName: 'my_vectors',
|
|
14
|
+
dimension: 1536,
|
|
15
|
+
metadataIndexes: ['thread_id', 'resource_id'],
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Fixes #12109
|
|
20
|
+
|
|
21
|
+
- Add support for pgvector's `bit` and `sparsevec` vector storage types ([#12815](https://github.com/mastra-ai/mastra/pull/12815))
|
|
22
|
+
|
|
23
|
+
You can now store binary and sparse vectors in `@mastra/pg`:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
// Binary vectors for fast similarity search
|
|
27
|
+
await db.createIndex({
|
|
28
|
+
indexName: 'my_binary_index',
|
|
29
|
+
dimension: 128,
|
|
30
|
+
metric: 'hamming', // or 'jaccard'
|
|
31
|
+
vectorType: 'bit',
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Sparse vectors for BM25/TF-IDF representations
|
|
35
|
+
await db.createIndex({
|
|
36
|
+
indexName: 'my_sparse_index',
|
|
37
|
+
dimension: 500,
|
|
38
|
+
metric: 'cosine',
|
|
39
|
+
vectorType: 'sparsevec',
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
What's new:
|
|
44
|
+
- `vectorType: 'bit'` for binary vectors with `'hamming'` and `'jaccard'` distance metrics
|
|
45
|
+
- `vectorType: 'sparsevec'` for sparse vectors (cosine, euclidean, dotproduct)
|
|
46
|
+
- Automatic metric normalization: `bit` defaults to `'hamming'` when no metric is specified
|
|
47
|
+
- `includeVector` round-trips work correctly for all vector types
|
|
48
|
+
- Requires pgvector >= 0.7.0
|
|
49
|
+
|
|
50
|
+
- Added `requestContext` column to the spans table. Request context data from tracing is now persisted alongside other span data. ([#14020](https://github.com/mastra-ai/mastra/pull/14020))
|
|
51
|
+
|
|
52
|
+
- Added `requestContext` and `requestContextSchema` column support to dataset storage. Dataset items now persist request context alongside input and ground truth data. ([#13938](https://github.com/mastra-ai/mastra/pull/13938))
|
|
53
|
+
|
|
54
|
+
### Patch Changes
|
|
55
|
+
|
|
56
|
+
- Added resilient column handling to insert and update operations. Unknown columns in records are now silently dropped instead of causing SQL errors, ensuring forward compatibility when newer domain packages add fields that haven't been migrated yet. ([#14021](https://github.com/mastra-ai/mastra/pull/14021))
|
|
57
|
+
|
|
58
|
+
For example, calling `db.insert({ tableName, record: { id: '1', title: 'Hello', futureField: 'value' } })` will silently ignore `futureField` if it doesn't exist in the database table, rather than throwing. The same applies to `update` — unknown fields in the data payload are dropped before building the SQL statement.
|
|
59
|
+
|
|
60
|
+
- Fixed slow semantic recall in the Postgres storage adapter for large threads. Recall now completes in under 500ms even for threads with 7,000+ messages, down from ~30 seconds. (Fixes #11702) ([#14022](https://github.com/mastra-ai/mastra/pull/14022))
|
|
61
|
+
|
|
62
|
+
- Updated dependencies [[`4f71b43`](https://github.com/mastra-ai/mastra/commit/4f71b436a4a6b8839842d8da47b57b84509af56c), [`a070277`](https://github.com/mastra-ai/mastra/commit/a07027766ce195ba74d0783116d894cbab25d44c), [`b628b91`](https://github.com/mastra-ai/mastra/commit/b628b9128b372c0f54214d902b07279f03443900), [`332c014`](https://github.com/mastra-ai/mastra/commit/332c014e076b81edf7fe45b58205882726415e90), [`6b63153`](https://github.com/mastra-ai/mastra/commit/6b63153878ea841c0f4ce632ba66bb33e57e9c1b), [`4246e34`](https://github.com/mastra-ai/mastra/commit/4246e34cec9c26636d0965942268e6d07c346671), [`b8837ee`](https://github.com/mastra-ai/mastra/commit/b8837ee77e2e84197609762bfabd8b3da326d30c), [`866cc2c`](https://github.com/mastra-ai/mastra/commit/866cc2cb1f0e3b314afab5194f69477fada745d1), [`5d950f7`](https://github.com/mastra-ai/mastra/commit/5d950f7bf426a215a1808f0abef7de5c8336ba1c), [`28c85b1`](https://github.com/mastra-ai/mastra/commit/28c85b184fc32b40f7f160483c982da6d388ecbd), [`e9a08fb`](https://github.com/mastra-ai/mastra/commit/e9a08fbef1ada7e50e961e2f54f55e8c10b4a45c), [`1d0a8a8`](https://github.com/mastra-ai/mastra/commit/1d0a8a8acf33203d5744fc429b090ad8598aa8ed), [`631ffd8`](https://github.com/mastra-ai/mastra/commit/631ffd82fed108648b448b28e6a90e38c5f53bf5), [`6bcbf8a`](https://github.com/mastra-ai/mastra/commit/6bcbf8a6774d5a53b21d61db8a45ce2593ca1616), [`aae2295`](https://github.com/mastra-ai/mastra/commit/aae2295838a2d329ad6640829e87934790ffe5b8), [`aa61f29`](https://github.com/mastra-ai/mastra/commit/aa61f29ff8095ce46a4ae16e46c4d8c79b2b685b), [`7ff3714`](https://github.com/mastra-ai/mastra/commit/7ff37148515439bb3be009a60e02c3e363299760), [`18c3a90`](https://github.com/mastra-ai/mastra/commit/18c3a90c9e48cf69500e308affeb8eba5860b2af), [`41d79a1`](https://github.com/mastra-ai/mastra/commit/41d79a14bd8cb6de1e2565fd0a04786bae2f211b), [`f35487b`](https://github.com/mastra-ai/mastra/commit/f35487bb2d46c636e22aa71d90025613ae38235a), [`6dc2192`](https://github.com/mastra-ai/mastra/commit/6dc21921aef0f0efab15cd0805fa3d18f277a76f), [`eeb3a3f`](https://github.com/mastra-ai/mastra/commit/eeb3a3f43aca10cf49479eed2a84b7d9ecea02ba), [`e673376`](https://github.com/mastra-ai/mastra/commit/e6733763ad1321aa7e5ae15096b9c2104f93b1f3), [`05f8d90`](https://github.com/mastra-ai/mastra/commit/05f8d9009290ce6aa03428b3add635268615db85), [`b2204c9`](https://github.com/mastra-ai/mastra/commit/b2204c98a42848bbfb6f0440f005dc2b6354f1cd), [`a1bf1e3`](https://github.com/mastra-ai/mastra/commit/a1bf1e385ed4c0ef6f11b56c5887442970d127f2), [`b6f647a`](https://github.com/mastra-ai/mastra/commit/b6f647ae2388e091f366581595feb957e37d5b40), [`0c57b8b`](https://github.com/mastra-ai/mastra/commit/0c57b8b0a69a97b5a4ae3f79be6c610f29f3cf7b), [`b081f27`](https://github.com/mastra-ai/mastra/commit/b081f272cf411716e1d6bd72ceac4bcee2657b19), [`4b8da97`](https://github.com/mastra-ai/mastra/commit/4b8da97a5ce306e97869df6c39535d9069e563db), [`0c09eac`](https://github.com/mastra-ai/mastra/commit/0c09eacb1926f64cfdc9ae5c6d63385cf8c9f72c), [`6b9b93d`](https://github.com/mastra-ai/mastra/commit/6b9b93d6f459d1ba6e36f163abf62a085ddb3d64), [`31b6067`](https://github.com/mastra-ai/mastra/commit/31b6067d0cc3ab10e1b29c36147f3b5266bc714a), [`797ac42`](https://github.com/mastra-ai/mastra/commit/797ac4276de231ad2d694d9aeca75980f6cd0419), [`0bc289e`](https://github.com/mastra-ai/mastra/commit/0bc289e2d476bf46c5b91c21969e8d0c6864691c), [`9b75a06`](https://github.com/mastra-ai/mastra/commit/9b75a06e53ebb0b950ba7c1e83a0142047185f46), [`4c3a1b1`](https://github.com/mastra-ai/mastra/commit/4c3a1b122ea083e003d71092f30f3b31680b01c0), [`256df35`](https://github.com/mastra-ai/mastra/commit/256df3571d62beb3ad4971faa432927cc140e603), [`85cc3b3`](https://github.com/mastra-ai/mastra/commit/85cc3b3b6f32ae4b083c26498f50d5b250ba944b), [`97ea28c`](https://github.com/mastra-ai/mastra/commit/97ea28c746e9e4147d56047bbb1c4a92417a3fec), [`d567299`](https://github.com/mastra-ai/mastra/commit/d567299cf81e02bd9d5221d4bc05967d6c224161), [`716ffe6`](https://github.com/mastra-ai/mastra/commit/716ffe68bed81f7c2690bc8581b9e140f7bf1c3d), [`8296332`](https://github.com/mastra-ai/mastra/commit/8296332de21c16e3dfc3d0b2d615720a6dc88f2f), [`4df2116`](https://github.com/mastra-ai/mastra/commit/4df211619dd922c047d396ca41cd7027c8c4c8e7), [`2219c1a`](https://github.com/mastra-ai/mastra/commit/2219c1acbd21da116da877f0036ffb985a9dd5a3), [`17c4145`](https://github.com/mastra-ai/mastra/commit/17c4145166099354545582335b5252bdfdfd908b)]:
|
|
63
|
+
- @mastra/core@1.11.0
|
|
64
|
+
|
|
3
65
|
## 1.8.0-alpha.0
|
|
4
66
|
|
|
5
67
|
### Minor Changes
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -3,7 +3,7 @@ name: mastra-pg
|
|
|
3
3
|
description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/pg"
|
|
6
|
-
version: "1.8.0
|
|
6
|
+
version: "1.8.0"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -16,25 +16,25 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
16
16
|
|
|
17
17
|
### Docs
|
|
18
18
|
|
|
19
|
-
- [Semantic
|
|
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.
|
|
20
20
|
- [Storage](references/docs-memory-storage.md) - Configure storage for Mastra's memory system to persist conversations, workflows, and traces.
|
|
21
|
-
- [Working
|
|
21
|
+
- [Working memory](references/docs-memory-working-memory.md) - Learn how to configure working memory in Mastra to store persistent user data, preferences.
|
|
22
22
|
- [RAG (Retrieval-Augmented Generation) in Mastra](references/docs-rag-overview.md) - Overview of Retrieval-Augmented Generation (RAG) in Mastra, detailing its capabilities for enhancing LLM outputs with relevant context.
|
|
23
|
-
- [Retrieval,
|
|
24
|
-
- [Storing
|
|
23
|
+
- [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.
|
|
24
|
+
- [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.
|
|
25
25
|
|
|
26
26
|
### Reference
|
|
27
27
|
|
|
28
|
-
- [Reference: Memory
|
|
29
|
-
- [Reference:
|
|
30
|
-
- [Reference:
|
|
31
|
-
- [Reference:
|
|
32
|
-
- [Reference: Metadata
|
|
33
|
-
- [Reference: Composite
|
|
34
|
-
- [Reference: DynamoDB
|
|
35
|
-
- [Reference: PostgreSQL
|
|
28
|
+
- [Reference: Memory class](references/reference-memory-memory-class.md) - Documentation for the `Memory` class in Mastra, which provides a robust system for managing conversation history and thread-based message storage.
|
|
29
|
+
- [Reference: MessageHistory](references/reference-processors-message-history-processor.md) - Documentation for the MessageHistory processor in Mastra, which handles retrieval and persistence of conversation history.
|
|
30
|
+
- [Reference: SemanticRecall](references/reference-processors-semantic-recall-processor.md) - Documentation for the SemanticRecall processor in Mastra, which enables semantic search over conversation history using vector embeddings.
|
|
31
|
+
- [Reference: WorkingMemory](references/reference-processors-working-memory-processor.md) - Documentation for the WorkingMemory processor in Mastra, which injects persistent user/context data as system instructions.
|
|
32
|
+
- [Reference: Metadata filters](references/reference-rag-metadata-filters.md) - Documentation for metadata filtering capabilities in Mastra, which allow for precise querying of vector search results across different vector stores.
|
|
33
|
+
- [Reference: Composite storage](references/reference-storage-composite.md) - Documentation for combining multiple storage backends in Mastra.
|
|
34
|
+
- [Reference: DynamoDB storage](references/reference-storage-dynamodb.md) - Documentation for the DynamoDB storage implementation in Mastra, using a single-table design with ElectroDB.
|
|
35
|
+
- [Reference: PostgreSQL storage](references/reference-storage-postgresql.md) - Documentation for the PostgreSQL storage implementation in Mastra.
|
|
36
36
|
- [Reference: createVectorQueryTool()](references/reference-tools-vector-query-tool.md) - Documentation for the Vector Query Tool in Mastra, which facilitates semantic search over vector stores with filtering and reranking capabilities.
|
|
37
|
-
- [Reference: PG
|
|
37
|
+
- [Reference: PG vector store](references/reference-vectors-pg.md) - Documentation for the PgVector class in Mastra, which provides vector search using PostgreSQL with pgvector extension.
|
|
38
38
|
|
|
39
39
|
|
|
40
40
|
Read [assets/SOURCE_MAP.json](assets/SOURCE_MAP.json) for source code references.
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# Semantic
|
|
1
|
+
# Semantic recall
|
|
2
2
|
|
|
3
3
|
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.
|
|
4
4
|
|
|
5
5
|
> **Watch 📹:** What semantic recall is, how it works, and how to configure it in Mastra → [YouTube (5 minutes)](https://youtu.be/UVZtK8cK8xQ)
|
|
6
6
|
|
|
7
|
-
## How
|
|
7
|
+
## How semantic recall works
|
|
8
8
|
|
|
9
9
|
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).
|
|
10
10
|
|
|
@@ -16,7 +16,7 @@ When it's enabled, new messages are used to query a vector DB for semantically s
|
|
|
16
16
|
|
|
17
17
|
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.
|
|
18
18
|
|
|
19
|
-
## Quick
|
|
19
|
+
## Quick start
|
|
20
20
|
|
|
21
21
|
Semantic recall is enabled by default, so if you give your agent memory it will be included:
|
|
22
22
|
|
|
@@ -33,7 +33,7 @@ const agent = new Agent({
|
|
|
33
33
|
})
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
## Using the recall()
|
|
36
|
+
## Using the `recall()` method
|
|
37
37
|
|
|
38
38
|
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`:
|
|
39
39
|
|
|
@@ -182,7 +182,7 @@ const agent = new Agent({
|
|
|
182
182
|
})
|
|
183
183
|
```
|
|
184
184
|
|
|
185
|
-
### Using FastEmbed (
|
|
185
|
+
### Using FastEmbed (local)
|
|
186
186
|
|
|
187
187
|
To use FastEmbed (a local embedding model), install `@mastra/fastembed`:
|
|
188
188
|
|
|
@@ -224,7 +224,7 @@ const agent = new Agent({
|
|
|
224
224
|
})
|
|
225
225
|
```
|
|
226
226
|
|
|
227
|
-
## PostgreSQL
|
|
227
|
+
## PostgreSQL index optimization
|
|
228
228
|
|
|
229
229
|
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.
|
|
230
230
|
|
|
@@ -283,6 +283,6 @@ You might want to disable semantic recall in scenarios like:
|
|
|
283
283
|
- When message history provides sufficient context for the current conversation.
|
|
284
284
|
- In performance-sensitive applications, like realtime two-way audio, where the added latency of creating embeddings and running vector queries is noticeable.
|
|
285
285
|
|
|
286
|
-
## Viewing
|
|
286
|
+
## Viewing recalled messages
|
|
287
287
|
|
|
288
288
|
When tracing is enabled, any messages retrieved via semantic recall will appear in the agent's trace output, alongside recent message history (if configured).
|
|
@@ -13,7 +13,7 @@ Working memory can persist at two different scopes:
|
|
|
13
13
|
|
|
14
14
|
**Important:** Switching between scopes means the agent won't see memory from the other scope - thread-scoped memory is completely separate from resource-scoped memory.
|
|
15
15
|
|
|
16
|
-
## Quick
|
|
16
|
+
## Quick start
|
|
17
17
|
|
|
18
18
|
Here's a minimal example of setting up an agent with working memory:
|
|
19
19
|
|
|
@@ -37,13 +37,13 @@ const agent = new Agent({
|
|
|
37
37
|
})
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
## How it
|
|
40
|
+
## How it works
|
|
41
41
|
|
|
42
42
|
Working memory is a block of Markdown text that the agent is able to update over time to store continuously relevant information:
|
|
43
43
|
|
|
44
44
|
[YouTube video player](https://www.youtube-nocookie.com/embed/UMy_JHLf1n8)
|
|
45
45
|
|
|
46
|
-
## Memory
|
|
46
|
+
## Memory persistence scopes
|
|
47
47
|
|
|
48
48
|
Working memory can operate in two different scopes, allowing you to choose how memory persists across conversations:
|
|
49
49
|
|
|
@@ -117,7 +117,7 @@ const memory = new Memory({
|
|
|
117
117
|
- Temporary or session-specific information
|
|
118
118
|
- Workflows where each thread needs working memory but threads are ephemeral and not related to each other
|
|
119
119
|
|
|
120
|
-
## Storage
|
|
120
|
+
## Storage adapter support
|
|
121
121
|
|
|
122
122
|
Resource-scoped working memory requires specific storage adapters that support the `mastra_resources` table:
|
|
123
123
|
|
|
@@ -128,7 +128,7 @@ Resource-scoped working memory requires specific storage adapters that support t
|
|
|
128
128
|
- **Upstash** (`@mastra/upstash`)
|
|
129
129
|
- **MongoDB** (`@mastra/mongodb`)
|
|
130
130
|
|
|
131
|
-
## Custom
|
|
131
|
+
## Custom templates
|
|
132
132
|
|
|
133
133
|
Templates guide the agent on what information to track and update in working memory. While a default template is used if none is provided, you'll typically want to define a custom template tailored to your agent's specific use case to ensure it remembers the most relevant information.
|
|
134
134
|
|
|
@@ -142,7 +142,7 @@ const memory = new Memory({
|
|
|
142
142
|
template: `
|
|
143
143
|
# User Profile
|
|
144
144
|
|
|
145
|
-
## Personal
|
|
145
|
+
## Personal info
|
|
146
146
|
|
|
147
147
|
- Name:
|
|
148
148
|
- Location:
|
|
@@ -156,7 +156,7 @@ const memory = new Memory({
|
|
|
156
156
|
- [Deadline 1]: [Date]
|
|
157
157
|
- [Deadline 2]: [Date]
|
|
158
158
|
|
|
159
|
-
## Session
|
|
159
|
+
## Session state
|
|
160
160
|
|
|
161
161
|
- Last Task Discussed:
|
|
162
162
|
- Open Questions:
|
|
@@ -168,7 +168,7 @@ const memory = new Memory({
|
|
|
168
168
|
})
|
|
169
169
|
```
|
|
170
170
|
|
|
171
|
-
## Designing
|
|
171
|
+
## Designing effective templates
|
|
172
172
|
|
|
173
173
|
A well-structured template keeps the information straightforward for the agent to parse and update. Treat the template as a short form that you want the assistant to keep up to date.
|
|
174
174
|
|
|
@@ -206,7 +206,7 @@ const paragraphMemory = new Memory({
|
|
|
206
206
|
})
|
|
207
207
|
```
|
|
208
208
|
|
|
209
|
-
## Structured
|
|
209
|
+
## Structured working memory
|
|
210
210
|
|
|
211
211
|
Working memory can also be defined using a structured schema instead of a Markdown template. This allows you to specify the exact fields and types that should be tracked, using a [Zod](https://zod.dev/) schema. When using a schema, the agent will see and update working memory as a JSON object matching your schema.
|
|
212
212
|
|
|
@@ -265,20 +265,20 @@ Schema-based working memory uses **merge semantics**, meaning the agent only nee
|
|
|
265
265
|
- **Set a field to `null` to delete it:** This explicitly removes the field from memory
|
|
266
266
|
- **Arrays are replaced entirely:** When an array field is provided, it replaces the existing array (arrays aren't merged element-by-element)
|
|
267
267
|
|
|
268
|
-
## Choosing
|
|
268
|
+
## Choosing between template and schema
|
|
269
269
|
|
|
270
270
|
- Use a **template** (Markdown) if you want the agent to maintain memory as a free-form text block, such as a user profile or scratchpad. Templates use **replace semantics** — the agent must provide the complete memory content on each update.
|
|
271
271
|
- Use a **schema** if you need structured, type-safe data that can be validated and programmatically accessed as JSON. Schemas use **merge semantics** — the agent only provides fields to update, and existing fields are preserved.
|
|
272
272
|
- Only one mode can be active at a time: setting both `template` and `schema` isn't supported.
|
|
273
273
|
|
|
274
|
-
## Example: Multi-step
|
|
274
|
+
## Example: Multi-step retention
|
|
275
275
|
|
|
276
276
|
Below is a simplified view of how the `User Profile` template updates across a short user conversation:
|
|
277
277
|
|
|
278
278
|
```nohighlight
|
|
279
279
|
# User Profile
|
|
280
280
|
|
|
281
|
-
## Personal
|
|
281
|
+
## Personal info
|
|
282
282
|
|
|
283
283
|
- Name:
|
|
284
284
|
- Location:
|
|
@@ -303,7 +303,7 @@ The agent can now refer to `Sam` or `Berlin` in later responses without requesti
|
|
|
303
303
|
|
|
304
304
|
If your agent isn't properly updating working memory when you expect it to, you can add system instructions on _how_ and _when_ to use this template in your agent's `instructions` setting.
|
|
305
305
|
|
|
306
|
-
## Setting
|
|
306
|
+
## Setting initial working memory
|
|
307
307
|
|
|
308
308
|
While agents typically update working memory through the `updateWorkingMemory` tool, you can also set initial working memory programmatically when creating or updating threads. This is useful for injecting user data (like their name, preferences, or other info) that you want available to the agent without passing it in every request.
|
|
309
309
|
|
|
@@ -372,7 +372,7 @@ await memory.updateWorkingMemory({
|
|
|
372
372
|
})
|
|
373
373
|
```
|
|
374
374
|
|
|
375
|
-
## Read-
|
|
375
|
+
## Read-only working memory
|
|
376
376
|
|
|
377
377
|
In some scenarios, you may want an agent to have access to working memory data without the ability to modify it. This is useful for:
|
|
378
378
|
|
|
@@ -59,11 +59,11 @@ console.log('Similar chunks:', results)
|
|
|
59
59
|
|
|
60
60
|
This example shows the essentials: initialize a document, create chunks, generate embeddings, store them, and query for similar content.
|
|
61
61
|
|
|
62
|
-
## Document
|
|
62
|
+
## Document processing
|
|
63
63
|
|
|
64
64
|
The basic building block of RAG is document processing. Documents can be chunked using various strategies (recursive, sliding window, etc.) and enriched with metadata. See the [chunking and embedding doc](https://mastra.ai/docs/rag/chunking-and-embedding).
|
|
65
65
|
|
|
66
|
-
## Vector
|
|
66
|
+
## Vector storage
|
|
67
67
|
|
|
68
68
|
Mastra supports multiple vector stores for embedding persistence and similarity search, including pgvector, Pinecone, Qdrant, and MongoDB. See the [vector database doc](https://mastra.ai/docs/rag/vector-databases).
|
|
69
69
|
|
|
@@ -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,7 +14,7 @@ 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
|
|
|
@@ -63,7 +63,7 @@ Results include both the text content and a similarity score:
|
|
|
63
63
|
]
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
-
## Advanced
|
|
66
|
+
## Advanced retrieval options
|
|
67
67
|
|
|
68
68
|
### Metadata Filtering
|
|
69
69
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
# Storing
|
|
1
|
+
# Storing embeddings in a vector database
|
|
2
2
|
|
|
3
3
|
After generating embeddings, you need to store them in a database that supports vector similarity search. Mastra provides a consistent interface for storing and querying embeddings across various vector databases.
|
|
4
4
|
|
|
5
|
-
## Supported
|
|
5
|
+
## Supported databases
|
|
6
6
|
|
|
7
7
|
**MongoDB**:
|
|
8
8
|
|
|
@@ -234,7 +234,7 @@ await store.upsert({
|
|
|
234
234
|
})
|
|
235
235
|
```
|
|
236
236
|
|
|
237
|
-
**
|
|
237
|
+
**Elasticsearch**:
|
|
238
238
|
|
|
239
239
|
```ts
|
|
240
240
|
import { ElasticSearchVector } from '@mastra/elasticsearch'
|
|
@@ -337,7 +337,7 @@ await store.upsert({
|
|
|
337
337
|
})
|
|
338
338
|
```
|
|
339
339
|
|
|
340
|
-
## Using
|
|
340
|
+
## Using vector storage
|
|
341
341
|
|
|
342
342
|
Once initialized, all vector stores share the same interface for creating indexes, upserting embeddings, and querying.
|
|
343
343
|
|
|
@@ -355,9 +355,9 @@ await store.createIndex({
|
|
|
355
355
|
|
|
356
356
|
The dimension size must match the output dimension of your chosen embedding model. Common dimension sizes are:
|
|
357
357
|
|
|
358
|
-
- OpenAI text-embedding-3-small
|
|
359
|
-
- Cohere embed-multilingual-v3
|
|
360
|
-
- Google gemini-embedding-001
|
|
358
|
+
- `OpenAI text-embedding-3-small`: 1536 dimensions (or custom, e.g., 256)
|
|
359
|
+
- `Cohere embed-multilingual-v3`: 1024 dimensions
|
|
360
|
+
- `Google gemini-embedding-001`: 768 dimensions (or custom)
|
|
361
361
|
|
|
362
362
|
> **Warning:** Index dimensions can't be changed after creation. To use a different model, delete and recreate the index with the new dimension size.
|
|
363
363
|
|
|
@@ -490,7 +490,7 @@ Index names must:
|
|
|
490
490
|
- Example: `My_Index` is not valid (contains uppercase letters)
|
|
491
491
|
- Example: `_myindex` is not valid (begins with underscore)
|
|
492
492
|
|
|
493
|
-
**
|
|
493
|
+
**Elasticsearch**:
|
|
494
494
|
|
|
495
495
|
Index names must:
|
|
496
496
|
|
|
@@ -543,7 +543,7 @@ The upsert operation:
|
|
|
543
543
|
- Creates new vectors if they don't exist
|
|
544
544
|
- Automatically handles batching for large datasets
|
|
545
545
|
|
|
546
|
-
## Adding
|
|
546
|
+
## Adding metadata
|
|
547
547
|
|
|
548
548
|
Vector stores support rich metadata (any JSON-serializable fields) for filtering and organization. Since metadata is stored with no fixed schema, use consistent field naming to avoid unexpected query results.
|
|
549
549
|
|
|
@@ -581,7 +581,7 @@ Key metadata considerations:
|
|
|
581
581
|
- Only include fields you plan to filter or sort by - extra fields add overhead
|
|
582
582
|
- Add timestamps (e.g., 'createdAt', 'lastUpdated') to track content freshness
|
|
583
583
|
|
|
584
|
-
## Deleting
|
|
584
|
+
## Deleting vectors
|
|
585
585
|
|
|
586
586
|
When building RAG applications, you often need to clean up stale vectors when documents are deleted or updated. Mastra provides the `deleteVectors` method that supports deleting vectors by metadata filters, making it straightforward to remove all embeddings associated with a specific document.
|
|
587
587
|
|
|
@@ -637,7 +637,7 @@ await store.deleteVectors({
|
|
|
637
637
|
})
|
|
638
638
|
```
|
|
639
639
|
|
|
640
|
-
## Best
|
|
640
|
+
## Best practices
|
|
641
641
|
|
|
642
642
|
- Create indexes before bulk insertions
|
|
643
643
|
- Use batch operations for large insertions (the upsert method handles batching automatically)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Memory
|
|
1
|
+
# Memory class
|
|
2
2
|
|
|
3
3
|
The `Memory` class provides a robust system for managing conversation history and thread-based message storage in Mastra. It enables persistent storage of conversations, semantic search capabilities, and efficient message retrieval. You must configure a storage provider for conversation history, and if you enable semantic recall you will also need to provide a vector store and embedder.
|
|
4
4
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
# Metadata
|
|
1
|
+
# Metadata filters
|
|
2
2
|
|
|
3
3
|
Mastra provides a unified metadata filtering syntax across all vector stores, based on MongoDB/Sift query syntax. Each vector store translates these filters into their native format.
|
|
4
4
|
|
|
5
|
-
## Basic
|
|
5
|
+
## Basic example
|
|
6
6
|
|
|
7
7
|
```typescript
|
|
8
8
|
import { PgVector } from '@mastra/pg'
|
|
@@ -24,7 +24,7 @@ const results = await store.query({
|
|
|
24
24
|
})
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
## Supported
|
|
27
|
+
## Supported operators
|
|
28
28
|
|
|
29
29
|
### Basic Comparison
|
|
30
30
|
|
|
@@ -46,7 +46,7 @@ const results = await store.query({
|
|
|
46
46
|
|
|
47
47
|
`$contains`Text contains substring{ description: { $contains: "sale" } }Supported by: Upstash, libSQL, PgVector`$regex`Regular expression match{ name: { $regex: "^test" } }Supported by: Qdrant, PgVector, Upstash, MongoDB`$size`Array length check{ tags: { $size: { $gt: 2 } } }Supported by: Astra, libSQL, PgVector, MongoDB`$geo`Geospatial query{ location: { $geo: { type: "radius", ... } } }Supported by: Qdrant`$datetime`Datetime range query{ created: { $datetime: { range: { gt: "2024-01-01" } } } }Supported by: Qdrant`$hasId`Vector ID existence check{ $hasId: \["id1", "id2"] }Supported by: Qdrant`$hasVector`Vector existence check{ $hasVector: true }Supported by: Qdrant
|
|
48
48
|
|
|
49
|
-
## Common
|
|
49
|
+
## Common rules and restrictions
|
|
50
50
|
|
|
51
51
|
1. Field names can't:
|
|
52
52
|
|
|
@@ -87,7 +87,7 @@ const results = await store.query({
|
|
|
87
87
|
- Valid: `{ "$and": [{ "field": { "$gt": 100 } }] }`
|
|
88
88
|
- Invalid: `{ "$and": [{ "$gt": 100 }] }`
|
|
89
89
|
|
|
90
|
-
## Store-
|
|
90
|
+
## Store-specific notes
|
|
91
91
|
|
|
92
92
|
### Astra
|
|
93
93
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# DynamoDB
|
|
1
|
+
# DynamoDB storage
|
|
2
2
|
|
|
3
3
|
The DynamoDB storage implementation provides a scalable and performant NoSQL database solution for Mastra, leveraging a single-table design pattern with [ElectroDB](https://electrodb.dev/).
|
|
4
4
|
|
|
@@ -120,7 +120,7 @@ For local development, you can use [DynamoDB Local](https://docs.aws.amazon.com/
|
|
|
120
120
|
|
|
121
121
|
**config.ttl** (`object`): TTL (Time To Live) configuration for automatic data expiration. Configure per entity type: thread, message, trace, eval, workflow\_snapshot, resource, score. Each entity config includes: enabled (boolean), attributeName (string, default: 'ttl'), defaultTtlSeconds (number).
|
|
122
122
|
|
|
123
|
-
## TTL (
|
|
123
|
+
## TTL (time to live) configuration
|
|
124
124
|
|
|
125
125
|
DynamoDB TTL allows you to automatically delete items after a specified time period. This is useful for:
|
|
126
126
|
|
|
@@ -216,7 +216,7 @@ aws dynamodb update-time-to-live \
|
|
|
216
216
|
|
|
217
217
|
> **Note:** DynamoDB deletes expired items within 48 hours after expiration. Items remain queryable until actually deleted.
|
|
218
218
|
|
|
219
|
-
## AWS IAM
|
|
219
|
+
## AWS IAM permissions
|
|
220
220
|
|
|
221
221
|
The IAM role or user executing the code needs appropriate permissions to interact with the specified DynamoDB table and its indexes. Below is a sample policy. Replace `${YOUR_TABLE_NAME}` with your actual table name and `${YOUR_AWS_REGION}` and `${YOUR_AWS_ACCOUNT_ID}` with appropriate values.
|
|
222
222
|
|
|
@@ -246,7 +246,7 @@ The IAM role or user executing the code needs appropriate permissions to interac
|
|
|
246
246
|
}
|
|
247
247
|
```
|
|
248
248
|
|
|
249
|
-
## Key
|
|
249
|
+
## Key considerations
|
|
250
250
|
|
|
251
251
|
Before diving into the architectural details, keep these key points in mind when working with the DynamoDB storage adapter:
|
|
252
252
|
|
|
@@ -255,7 +255,7 @@ Before diving into the architectural details, keep these key points in mind when
|
|
|
255
255
|
- **Understanding GSIs:** Familiarity with how the GSIs are structured (as per `TABLE_SETUP.md`) is important for understanding data retrieval and potential query patterns.
|
|
256
256
|
- **ElectroDB:** The adapter uses ElectroDB to manage interactions with DynamoDB, providing a layer of abstraction and type safety over raw DynamoDB operations.
|
|
257
257
|
|
|
258
|
-
## Architectural
|
|
258
|
+
## Architectural approach
|
|
259
259
|
|
|
260
260
|
This storage adapter utilizes a **single-table design pattern** leveraging [ElectroDB](https://electrodb.dev/), a common and recommended approach for DynamoDB. This differs architecturally from relational database adapters (like `@mastra/pg` or `@mastra/libsql`) that typically use multiple tables, each dedicated to a specific entity (threads, messages, etc.).
|
|
261
261
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# PostgreSQL
|
|
1
|
+
# PostgreSQL storage
|
|
2
2
|
|
|
3
3
|
The PostgreSQL storage implementation provides a production-ready storage solution using PostgreSQL databases.
|
|
4
4
|
|
|
@@ -71,7 +71,7 @@ const storage = new PostgresStore({
|
|
|
71
71
|
|
|
72
72
|
**indexes** (`CreateIndexOptions[]`): Custom indexes to create during initialization.
|
|
73
73
|
|
|
74
|
-
## Constructor
|
|
74
|
+
## Constructor examples
|
|
75
75
|
|
|
76
76
|
You can instantiate `PostgresStore` in the following ways:
|
|
77
77
|
|
|
@@ -119,7 +119,7 @@ const store4 = new PostgresStore({
|
|
|
119
119
|
})
|
|
120
120
|
```
|
|
121
121
|
|
|
122
|
-
## Additional
|
|
122
|
+
## Additional notes
|
|
123
123
|
|
|
124
124
|
### Schema Management
|
|
125
125
|
|
|
@@ -316,7 +316,7 @@ This pattern ensures only one `PostgresStore` instance is created regardless of
|
|
|
316
316
|
|
|
317
317
|
> **Tip:** This singleton pattern is only necessary during local development with HMR. In production builds, modules are only loaded once.
|
|
318
318
|
|
|
319
|
-
## Usage
|
|
319
|
+
## Usage example
|
|
320
320
|
|
|
321
321
|
### Adding memory to an agent
|
|
322
322
|
|
|
@@ -387,7 +387,7 @@ for await (const chunk of stream.textStream) {
|
|
|
387
387
|
}
|
|
388
388
|
```
|
|
389
389
|
|
|
390
|
-
## Index
|
|
390
|
+
## Index management
|
|
391
391
|
|
|
392
392
|
PostgreSQL storage provides index management to optimize query performance.
|
|
393
393
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
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
4
|
|
|
5
|
-
## Basic
|
|
5
|
+
## Basic usage
|
|
6
6
|
|
|
7
7
|
```typescript
|
|
8
8
|
import { createVectorQueryTool } from '@mastra/rag'
|
|
@@ -79,7 +79,7 @@ The tool returns an object with:
|
|
|
79
79
|
|
|
80
80
|
**sources** (`QueryResult[]`): Array of full retrieval result objects. Each object contains all information needed to reference the original document, chunk, and similarity score.
|
|
81
81
|
|
|
82
|
-
### QueryResult object structure
|
|
82
|
+
### `QueryResult` object structure
|
|
83
83
|
|
|
84
84
|
```typescript
|
|
85
85
|
{
|
|
@@ -91,7 +91,7 @@ The tool returns an object with:
|
|
|
91
91
|
}
|
|
92
92
|
```
|
|
93
93
|
|
|
94
|
-
## Default
|
|
94
|
+
## Default tool description
|
|
95
95
|
|
|
96
96
|
The default description focuses on:
|
|
97
97
|
|
|
@@ -99,11 +99,11 @@ The default description focuses on:
|
|
|
99
99
|
- Answering user questions
|
|
100
100
|
- Retrieving factual content
|
|
101
101
|
|
|
102
|
-
## Result
|
|
102
|
+
## Result handling
|
|
103
103
|
|
|
104
104
|
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.
|
|
105
105
|
|
|
106
|
-
## Example with
|
|
106
|
+
## Example with filters
|
|
107
107
|
|
|
108
108
|
```typescript
|
|
109
109
|
const queryTool = createVectorQueryTool({
|
|
@@ -134,7 +134,7 @@ For detailed filter syntax and store-specific capabilities, see the [Metadata Fi
|
|
|
134
134
|
|
|
135
135
|
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.
|
|
136
136
|
|
|
137
|
-
## Example with
|
|
137
|
+
## Example with reranking
|
|
138
138
|
|
|
139
139
|
```typescript
|
|
140
140
|
const queryTool = createVectorQueryTool({
|
|
@@ -164,7 +164,7 @@ Reranking improves result quality by combining:
|
|
|
164
164
|
|
|
165
165
|
The reranker processes the initial vector search results and returns a reordered list optimized for relevance.
|
|
166
166
|
|
|
167
|
-
## Example with
|
|
167
|
+
## Example with custom description
|
|
168
168
|
|
|
169
169
|
```typescript
|
|
170
170
|
const queryTool = createVectorQueryTool({
|
|
@@ -178,7 +178,7 @@ const queryTool = createVectorQueryTool({
|
|
|
178
178
|
|
|
179
179
|
This example shows how to customize the tool description for a specific use case while maintaining its core purpose of information retrieval.
|
|
180
180
|
|
|
181
|
-
## Database-
|
|
181
|
+
## Database-specific configuration examples
|
|
182
182
|
|
|
183
183
|
The `databaseConfig` parameter allows you to leverage unique features and optimizations specific to each vector database. These configurations are automatically applied during query execution.
|
|
184
184
|
|
|
@@ -335,7 +335,7 @@ This approach allows you to:
|
|
|
335
335
|
- Adjust performance parameters based on load
|
|
336
336
|
- Apply different filtering strategies per request
|
|
337
337
|
|
|
338
|
-
## Example: Using
|
|
338
|
+
## Example: Using request context
|
|
339
339
|
|
|
340
340
|
```typescript
|
|
341
341
|
const queryTool = createVectorQueryTool({
|
|
@@ -374,7 +374,7 @@ For more information on request context, please see:
|
|
|
374
374
|
- [Agent Request Context](https://mastra.ai/docs/server/request-context)
|
|
375
375
|
- [Request Context](https://mastra.ai/docs/server/request-context)
|
|
376
376
|
|
|
377
|
-
## Usage
|
|
377
|
+
## Usage without a Mastra server
|
|
378
378
|
|
|
379
379
|
The tool can be used by itself to retrieve documents matching a query:
|
|
380
380
|
|
|
@@ -401,7 +401,7 @@ const queryResult = await vectorQueryTool.execute({ queryText: 'foo', topK: 1 },
|
|
|
401
401
|
console.log(queryResult.sources)
|
|
402
402
|
```
|
|
403
403
|
|
|
404
|
-
## Dynamic
|
|
404
|
+
## Dynamic vector store for multi-tenant applications
|
|
405
405
|
|
|
406
406
|
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:
|
|
407
407
|
|
|
@@ -457,7 +457,7 @@ This pattern is similar to how `Agent.memory` supports dynamic configuration and
|
|
|
457
457
|
- **Database isolation**: Route to different database instances per tenant
|
|
458
458
|
- **Dynamic configuration**: Adjust vector store settings based on request context
|
|
459
459
|
|
|
460
|
-
## Tool
|
|
460
|
+
## Tool details
|
|
461
461
|
|
|
462
462
|
The tool is created with:
|
|
463
463
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
# PG
|
|
1
|
+
# PG vector store
|
|
2
2
|
|
|
3
3
|
The PgVector class provides vector search using [PostgreSQL](https://www.postgresql.org/) with [pgvector](https://github.com/pgvector/pgvector) extension. It provides robust vector similarity search capabilities within your existing PostgreSQL database.
|
|
4
4
|
|
|
5
|
-
## Constructor
|
|
5
|
+
## Constructor options
|
|
6
6
|
|
|
7
7
|
**connectionString** (`string`): PostgreSQL connection URL
|
|
8
8
|
|
|
@@ -26,7 +26,7 @@ The PgVector class provides vector search using [PostgreSQL](https://www.postgre
|
|
|
26
26
|
|
|
27
27
|
**pgPoolOptions** (`PoolConfig`): Additional pg pool configuration options
|
|
28
28
|
|
|
29
|
-
## Constructor
|
|
29
|
+
## Constructor examples
|
|
30
30
|
|
|
31
31
|
### Connection String
|
|
32
32
|
|
|
@@ -70,7 +70,7 @@ const vectorStore = new PgVector({
|
|
|
70
70
|
|
|
71
71
|
## Methods
|
|
72
72
|
|
|
73
|
-
### createIndex()
|
|
73
|
+
### `createIndex()`
|
|
74
74
|
|
|
75
75
|
**indexName** (`string`): Name of the index to create
|
|
76
76
|
|
|
@@ -84,7 +84,7 @@ const vectorStore = new PgVector({
|
|
|
84
84
|
|
|
85
85
|
**metadataIndexes** (`string[]`): Array of metadata field names to create btree indexes on. Improves query performance when filtering by these metadata fields.
|
|
86
86
|
|
|
87
|
-
#### IndexConfig
|
|
87
|
+
#### `IndexConfig`
|
|
88
88
|
|
|
89
89
|
**type** (`'flat' | 'hnsw' | 'ivfflat'`): Index type (Default: `ivfflat`)
|
|
90
90
|
|
|
@@ -114,7 +114,7 @@ HNSW indexes require significant shared memory during construction. For 100K vec
|
|
|
114
114
|
|
|
115
115
|
Higher M values or efConstruction values will increase memory requirements significantly. Adjust your system's shared memory limits if needed.
|
|
116
116
|
|
|
117
|
-
### upsert()
|
|
117
|
+
### `upsert()`
|
|
118
118
|
|
|
119
119
|
**indexName** (`string`): Name of the index to upsert vectors into
|
|
120
120
|
|
|
@@ -124,7 +124,7 @@ Higher M values or efConstruction values will increase memory requirements signi
|
|
|
124
124
|
|
|
125
125
|
**ids** (`string[]`): Optional vector IDs (auto-generated if not provided)
|
|
126
126
|
|
|
127
|
-
### query()
|
|
127
|
+
### `query()`
|
|
128
128
|
|
|
129
129
|
**indexName** (`string`): Name of the index to query
|
|
130
130
|
|
|
@@ -144,11 +144,11 @@ Higher M values or efConstruction values will increase memory requirements signi
|
|
|
144
144
|
|
|
145
145
|
**options.probes** (`number`): IVF search parameter
|
|
146
146
|
|
|
147
|
-
### listIndexes()
|
|
147
|
+
### `listIndexes()`
|
|
148
148
|
|
|
149
149
|
Returns an array of index names as strings.
|
|
150
150
|
|
|
151
|
-
### describeIndex()
|
|
151
|
+
### `describeIndex()`
|
|
152
152
|
|
|
153
153
|
**indexName** (`string`): Name of the index to describe
|
|
154
154
|
|
|
@@ -169,11 +169,11 @@ interface PGIndexStats {
|
|
|
169
169
|
}
|
|
170
170
|
```
|
|
171
171
|
|
|
172
|
-
### deleteIndex()
|
|
172
|
+
### `deleteIndex()`
|
|
173
173
|
|
|
174
174
|
**indexName** (`string`): Name of the index to delete
|
|
175
175
|
|
|
176
|
-
### updateVector()
|
|
176
|
+
### `updateVector()`
|
|
177
177
|
|
|
178
178
|
Update a single vector by ID or by metadata filter. Either `id` or `filter` must be provided, but not both.
|
|
179
179
|
|
|
@@ -208,7 +208,7 @@ await pgVector.updateVector({
|
|
|
208
208
|
})
|
|
209
209
|
```
|
|
210
210
|
|
|
211
|
-
### deleteVector()
|
|
211
|
+
### `deleteVector()`
|
|
212
212
|
|
|
213
213
|
**indexName** (`string`): Name of the index containing the vector
|
|
214
214
|
|
|
@@ -220,7 +220,7 @@ Deletes a single vector by ID from the specified index.
|
|
|
220
220
|
await pgVector.deleteVector({ indexName: 'my_vectors', id: 'vector123' })
|
|
221
221
|
```
|
|
222
222
|
|
|
223
|
-
### deleteVectors()
|
|
223
|
+
### `deleteVectors()`
|
|
224
224
|
|
|
225
225
|
Delete multiple vectors by IDs or by metadata filter. Either `ids` or `filter` must be provided, but not both.
|
|
226
226
|
|
|
@@ -230,11 +230,11 @@ Delete multiple vectors by IDs or by metadata filter. Either `ids` or `filter` m
|
|
|
230
230
|
|
|
231
231
|
**filter** (`Record<string, any>`): Metadata filter to identify vectors to delete (mutually exclusive with ids)
|
|
232
232
|
|
|
233
|
-
### disconnect()
|
|
233
|
+
### `disconnect()`
|
|
234
234
|
|
|
235
235
|
Closes the database connection pool. Should be called when done using the store.
|
|
236
236
|
|
|
237
|
-
### buildIndex()
|
|
237
|
+
### `buildIndex()`
|
|
238
238
|
|
|
239
239
|
**indexName** (`string`): Name of the index to define
|
|
240
240
|
|
|
@@ -268,7 +268,7 @@ await pgVector.buildIndex('my_vectors', 'cosine', {
|
|
|
268
268
|
})
|
|
269
269
|
```
|
|
270
270
|
|
|
271
|
-
## Response
|
|
271
|
+
## Response types
|
|
272
272
|
|
|
273
273
|
Query results are returned in this format:
|
|
274
274
|
|
|
@@ -281,7 +281,7 @@ interface QueryResult {
|
|
|
281
281
|
}
|
|
282
282
|
```
|
|
283
283
|
|
|
284
|
-
## Error
|
|
284
|
+
## Error handling
|
|
285
285
|
|
|
286
286
|
The store throws typed errors that can be caught:
|
|
287
287
|
|
|
@@ -299,7 +299,7 @@ try {
|
|
|
299
299
|
}
|
|
300
300
|
```
|
|
301
301
|
|
|
302
|
-
## Index
|
|
302
|
+
## Index configuration guide
|
|
303
303
|
|
|
304
304
|
### Performance Optimization
|
|
305
305
|
|
|
@@ -331,14 +331,14 @@ The system automatically detects configuration changes and only rebuilds indexes
|
|
|
331
331
|
- Changed configuration: Index is dropped and rebuilt
|
|
332
332
|
- This prevents the performance issues from unnecessary index recreations
|
|
333
333
|
|
|
334
|
-
## Best
|
|
334
|
+
## Best practices
|
|
335
335
|
|
|
336
336
|
- Regularly evaluate your index configuration to ensure optimal performance.
|
|
337
337
|
- Adjust parameters like `lists` and `m` based on dataset size and query requirements.
|
|
338
338
|
- **Monitor index performance** using `describeIndex()` to track usage
|
|
339
339
|
- Rebuild indexes periodically to maintain efficiency, especially after significant data changes
|
|
340
340
|
|
|
341
|
-
## Direct
|
|
341
|
+
## Direct pool access
|
|
342
342
|
|
|
343
343
|
The `PgVector` class exposes its underlying PostgreSQL connection pool as a public field:
|
|
344
344
|
|
|
@@ -354,7 +354,7 @@ This enables advanced usage such as running direct SQL queries, managing transac
|
|
|
354
354
|
|
|
355
355
|
This design supports advanced use cases but requires careful resource management by the user.
|
|
356
356
|
|
|
357
|
-
## Usage
|
|
357
|
+
## Usage example
|
|
358
358
|
|
|
359
359
|
### Local embeddings with fastembed
|
|
360
360
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/pg",
|
|
3
|
-
"version": "1.8.0
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Postgres provider for Mastra - includes both vector and db storage capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
"tsup": "^8.5.1",
|
|
34
34
|
"typescript": "^5.9.3",
|
|
35
35
|
"vitest": "4.0.18",
|
|
36
|
-
"@internal/lint": "0.0.
|
|
37
|
-
"@internal/types-builder": "0.0.
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
36
|
+
"@internal/lint": "0.0.67",
|
|
37
|
+
"@internal/types-builder": "0.0.42",
|
|
38
|
+
"@internal/storage-test-utils": "0.0.63",
|
|
39
|
+
"@mastra/core": "1.11.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@mastra/core": ">=1.4.0-0 <2.0.0-0"
|