@mastra/pg 1.0.0-beta.10 → 1.0.0-beta.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +143 -0
- package/dist/docs/README.md +36 -0
- package/dist/docs/SKILL.md +37 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/memory/01-storage.md +181 -0
- package/dist/docs/memory/02-working-memory.md +386 -0
- package/dist/docs/memory/03-semantic-recall.md +235 -0
- package/dist/docs/memory/04-reference.md +135 -0
- package/dist/docs/processors/01-reference.md +295 -0
- package/dist/docs/rag/01-overview.md +74 -0
- package/dist/docs/rag/02-vector-databases.md +638 -0
- package/dist/docs/rag/03-retrieval.md +549 -0
- package/dist/docs/rag/04-reference.md +351 -0
- package/dist/docs/storage/01-reference.md +667 -0
- package/dist/docs/tools/01-reference.md +440 -0
- package/dist/docs/vectors/01-reference.md +307 -0
- package/dist/index.cjs +499 -156
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +500 -157
- package/dist/index.js.map +1 -1
- package/dist/shared/config.d.ts +61 -66
- package/dist/shared/config.d.ts.map +1 -1
- package/dist/storage/client.d.ts +91 -0
- package/dist/storage/client.d.ts.map +1 -0
- package/dist/storage/db/index.d.ts +36 -17
- package/dist/storage/db/index.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +2 -1
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +35 -14
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/test-utils.d.ts.map +1 -1
- package/package.json +8 -8
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# Memory API Reference
|
|
2
|
+
|
|
3
|
+
> API reference for memory - 1 entries
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Reference: Memory Class
|
|
9
|
+
|
|
10
|
+
> Documentation for the `Memory` class in Mastra, which provides a robust system for managing conversation history and thread-based message storage.
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
## Usage example
|
|
15
|
+
|
|
16
|
+
```typescript title="src/mastra/agents/test-agent.ts"
|
|
17
|
+
import { Memory } from "@mastra/memory";
|
|
18
|
+
import { Agent } from "@mastra/core/agent";
|
|
19
|
+
|
|
20
|
+
export const agent = new Agent({
|
|
21
|
+
name: "test-agent",
|
|
22
|
+
instructions: "You are an agent with memory.",
|
|
23
|
+
model: "openai/gpt-5.1",
|
|
24
|
+
memory: new Memory({
|
|
25
|
+
options: {
|
|
26
|
+
workingMemory: {
|
|
27
|
+
enabled: true,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
}),
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
> To enable `workingMemory` on an agent, you’ll need a storage provider configured on your main Mastra instance. See [Mastra class](../core/mastra-class) for more information.
|
|
35
|
+
|
|
36
|
+
## Constructor parameters
|
|
37
|
+
|
|
38
|
+
### Options parameters
|
|
39
|
+
|
|
40
|
+
## Returns
|
|
41
|
+
|
|
42
|
+
## Extended usage example
|
|
43
|
+
|
|
44
|
+
```typescript title="src/mastra/agents/test-agent.ts"
|
|
45
|
+
import { Memory } from "@mastra/memory";
|
|
46
|
+
import { Agent } from "@mastra/core/agent";
|
|
47
|
+
import { LibSQLStore, LibSQLVector } from "@mastra/libsql";
|
|
48
|
+
|
|
49
|
+
export const agent = new Agent({
|
|
50
|
+
name: "test-agent",
|
|
51
|
+
instructions: "You are an agent with memory.",
|
|
52
|
+
model: "openai/gpt-5.1",
|
|
53
|
+
memory: new Memory({
|
|
54
|
+
storage: new LibSQLStore({
|
|
55
|
+
id: 'test-agent-storage',
|
|
56
|
+
url: "file:./working-memory.db",
|
|
57
|
+
}),
|
|
58
|
+
vector: new LibSQLVector({
|
|
59
|
+
id: 'test-agent-vector',
|
|
60
|
+
connectionUrl: "file:./vector-memory.db",
|
|
61
|
+
}),
|
|
62
|
+
options: {
|
|
63
|
+
lastMessages: 10,
|
|
64
|
+
semanticRecall: {
|
|
65
|
+
topK: 3,
|
|
66
|
+
messageRange: 2,
|
|
67
|
+
scope: "resource",
|
|
68
|
+
},
|
|
69
|
+
workingMemory: {
|
|
70
|
+
enabled: true,
|
|
71
|
+
},
|
|
72
|
+
threads: {
|
|
73
|
+
generateTitle: true,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## PostgreSQL with index configuration
|
|
81
|
+
|
|
82
|
+
```typescript title="src/mastra/agents/pg-agent.ts"
|
|
83
|
+
import { Memory } from "@mastra/memory";
|
|
84
|
+
import { Agent } from "@mastra/core/agent";
|
|
85
|
+
import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
|
|
86
|
+
import { PgStore, PgVector } from "@mastra/pg";
|
|
87
|
+
|
|
88
|
+
export const agent = new Agent({
|
|
89
|
+
name: "pg-agent",
|
|
90
|
+
instructions: "You are an agent with optimized PostgreSQL memory.",
|
|
91
|
+
model: "openai/gpt-5.1",
|
|
92
|
+
memory: new Memory({
|
|
93
|
+
storage: new PgStore({
|
|
94
|
+
id: 'pg-agent-storage',
|
|
95
|
+
connectionString: process.env.DATABASE_URL,
|
|
96
|
+
}),
|
|
97
|
+
vector: new PgVector({
|
|
98
|
+
id: 'pg-agent-vector',
|
|
99
|
+
connectionString: process.env.DATABASE_URL,
|
|
100
|
+
}),
|
|
101
|
+
embedder: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
|
|
102
|
+
options: {
|
|
103
|
+
lastMessages: 20,
|
|
104
|
+
semanticRecall: {
|
|
105
|
+
topK: 5,
|
|
106
|
+
messageRange: 3,
|
|
107
|
+
scope: "resource",
|
|
108
|
+
indexConfig: {
|
|
109
|
+
type: "hnsw", // Use HNSW for better performance
|
|
110
|
+
metric: "dotproduct", // Optimal for OpenAI embeddings
|
|
111
|
+
m: 16, // Number of bi-directional links
|
|
112
|
+
efConstruction: 64, // Construction-time candidate list size
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
workingMemory: {
|
|
116
|
+
enabled: true,
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
}),
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Related
|
|
124
|
+
|
|
125
|
+
- [Getting Started with Memory](https://mastra.ai/docs/v1/memory/overview)
|
|
126
|
+
- [Semantic Recall](https://mastra.ai/docs/v1/memory/semantic-recall)
|
|
127
|
+
- [Working Memory](https://mastra.ai/docs/v1/memory/working-memory)
|
|
128
|
+
- [Memory Processors](https://mastra.ai/docs/v1/memory/memory-processors)
|
|
129
|
+
- [createThread](https://mastra.ai/reference/v1/memory/createThread)
|
|
130
|
+
- [recall](https://mastra.ai/reference/v1/memory/recall)
|
|
131
|
+
- [getThreadById](https://mastra.ai/reference/v1/memory/getThreadById)
|
|
132
|
+
- [listThreadsByResourceId](https://mastra.ai/reference/v1/memory/listThreadsByResourceId)
|
|
133
|
+
- [deleteMessages](https://mastra.ai/reference/v1/memory/deleteMessages)
|
|
134
|
+
- [cloneThread](https://mastra.ai/reference/v1/memory/cloneThread)
|
|
135
|
+
- [Clone Utility Methods](https://mastra.ai/reference/v1/memory/clone-utilities)
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
# Processors API Reference
|
|
2
|
+
|
|
3
|
+
> API reference for processors - 3 entries
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Reference: Message History Processor
|
|
9
|
+
|
|
10
|
+
> Documentation for the MessageHistory processor in Mastra, which handles retrieval and persistence of conversation history.
|
|
11
|
+
|
|
12
|
+
The `MessageHistory` is a **hybrid processor** that handles both retrieval and persistence of message history. On input, it fetches historical messages from storage and prepends them to the conversation. On output, it persists new messages to storage.
|
|
13
|
+
|
|
14
|
+
## Usage example
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { MessageHistory } from "@mastra/core/processors";
|
|
18
|
+
|
|
19
|
+
const processor = new MessageHistory({
|
|
20
|
+
storage: memoryStorage,
|
|
21
|
+
lastMessages: 50,
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Constructor parameters
|
|
26
|
+
|
|
27
|
+
### Options
|
|
28
|
+
|
|
29
|
+
## Returns
|
|
30
|
+
|
|
31
|
+
## Extended usage example
|
|
32
|
+
|
|
33
|
+
```typescript title="src/mastra/agents/memory-agent.ts"
|
|
34
|
+
import { Agent } from "@mastra/core/agent";
|
|
35
|
+
import { MessageHistory } from "@mastra/core/processors";
|
|
36
|
+
import { PostgresStorage } from "@mastra/pg";
|
|
37
|
+
|
|
38
|
+
const storage = new PostgresStorage({
|
|
39
|
+
connectionString: process.env.DATABASE_URL,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
export const agent = new Agent({
|
|
43
|
+
name: "memory-agent",
|
|
44
|
+
instructions: "You are a helpful assistant with conversation memory",
|
|
45
|
+
model: "openai:gpt-4o",
|
|
46
|
+
inputProcessors: [
|
|
47
|
+
new MessageHistory({
|
|
48
|
+
storage,
|
|
49
|
+
lastMessages: 100,
|
|
50
|
+
}),
|
|
51
|
+
],
|
|
52
|
+
outputProcessors: [
|
|
53
|
+
new MessageHistory({
|
|
54
|
+
storage,
|
|
55
|
+
}),
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Behavior
|
|
61
|
+
|
|
62
|
+
### Input processing
|
|
63
|
+
1. Retrieves `threadId` from the request context
|
|
64
|
+
2. Fetches historical messages from storage (ordered by creation date, descending)
|
|
65
|
+
3. Filters out system messages (they should not be stored in the database)
|
|
66
|
+
4. Merges historical messages with incoming messages, avoiding duplicates by ID
|
|
67
|
+
5. Adds historical messages with `source: 'memory'` tag
|
|
68
|
+
|
|
69
|
+
### Output processing
|
|
70
|
+
1. Retrieves `threadId` from the request context
|
|
71
|
+
2. Skips persistence if `readOnly` is set in memory config
|
|
72
|
+
3. Filters out incomplete tool calls from messages
|
|
73
|
+
4. Persists new user input and assistant response messages to storage
|
|
74
|
+
5. Updates the thread's `updatedAt` timestamp
|
|
75
|
+
|
|
76
|
+
## Related
|
|
77
|
+
|
|
78
|
+
- [Guardrails](https://mastra.ai/docs/v1/agents/guardrails)
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Reference: Semantic Recall Processor
|
|
83
|
+
|
|
84
|
+
> Documentation for the SemanticRecall processor in Mastra, which enables semantic search over conversation history using vector embeddings.
|
|
85
|
+
|
|
86
|
+
The `SemanticRecall` is a **hybrid processor** that enables semantic search over conversation history using vector embeddings. On input, it performs semantic search to find relevant historical messages. On output, it creates embeddings for new messages to enable future semantic retrieval.
|
|
87
|
+
|
|
88
|
+
## Usage example
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { SemanticRecall } from "@mastra/core/processors";
|
|
92
|
+
import { openai } from "@ai-sdk/openai";
|
|
93
|
+
|
|
94
|
+
const processor = new SemanticRecall({
|
|
95
|
+
storage: memoryStorage,
|
|
96
|
+
vector: vectorStore,
|
|
97
|
+
embedder: openai.embedding("text-embedding-3-small"),
|
|
98
|
+
topK: 5,
|
|
99
|
+
messageRange: 2,
|
|
100
|
+
scope: "resource",
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Constructor parameters
|
|
105
|
+
|
|
106
|
+
### Options
|
|
107
|
+
|
|
108
|
+
## Returns
|
|
109
|
+
|
|
110
|
+
## Extended usage example
|
|
111
|
+
|
|
112
|
+
```typescript title="src/mastra/agents/semantic-memory-agent.ts"
|
|
113
|
+
import { Agent } from "@mastra/core/agent";
|
|
114
|
+
import { SemanticRecall, MessageHistory } from "@mastra/core/processors";
|
|
115
|
+
import { PostgresStorage } from "@mastra/pg";
|
|
116
|
+
import { PgVector } from "@mastra/pg";
|
|
117
|
+
import { openai } from "@ai-sdk/openai";
|
|
118
|
+
|
|
119
|
+
const storage = new PostgresStorage({
|
|
120
|
+
connectionString: process.env.DATABASE_URL,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const vector = new PgVector({
|
|
124
|
+
connectionString: process.env.DATABASE_URL,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const semanticRecall = new SemanticRecall({
|
|
128
|
+
storage,
|
|
129
|
+
vector,
|
|
130
|
+
embedder: openai.embedding("text-embedding-3-small"),
|
|
131
|
+
topK: 5,
|
|
132
|
+
messageRange: { before: 2, after: 1 },
|
|
133
|
+
scope: "resource",
|
|
134
|
+
threshold: 0.7,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
export const agent = new Agent({
|
|
138
|
+
name: "semantic-memory-agent",
|
|
139
|
+
instructions: "You are a helpful assistant with semantic memory recall",
|
|
140
|
+
model: "openai:gpt-4o",
|
|
141
|
+
inputProcessors: [
|
|
142
|
+
semanticRecall,
|
|
143
|
+
new MessageHistory({ storage, lastMessages: 50 }),
|
|
144
|
+
],
|
|
145
|
+
outputProcessors: [
|
|
146
|
+
semanticRecall,
|
|
147
|
+
new MessageHistory({ storage }),
|
|
148
|
+
],
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Behavior
|
|
153
|
+
|
|
154
|
+
### Input processing
|
|
155
|
+
1. Extracts the user query from the last user message
|
|
156
|
+
2. Generates embeddings for the query
|
|
157
|
+
3. Performs vector search to find semantically similar messages
|
|
158
|
+
4. Retrieves matched messages along with surrounding context (based on `messageRange`)
|
|
159
|
+
5. For `scope: 'resource'`, formats cross-thread messages as a system message with timestamps
|
|
160
|
+
6. Adds recalled messages with `source: 'memory'` tag
|
|
161
|
+
|
|
162
|
+
### Output processing
|
|
163
|
+
1. Extracts text content from new user and assistant messages
|
|
164
|
+
2. Generates embeddings for each message
|
|
165
|
+
3. Stores embeddings in the vector store with metadata (message ID, thread ID, resource ID, role, content, timestamp)
|
|
166
|
+
4. Uses LRU caching for embeddings to avoid redundant API calls
|
|
167
|
+
|
|
168
|
+
### Cross-thread recall
|
|
169
|
+
When `scope` is set to `'resource'`, the processor can recall messages from other threads. These cross-thread messages are formatted as a system message with timestamps and conversation labels to provide context about when and where the conversation occurred.
|
|
170
|
+
|
|
171
|
+
## Related
|
|
172
|
+
|
|
173
|
+
- [Guardrails](https://mastra.ai/docs/v1/agents/guardrails)
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## Reference: Working Memory Processor
|
|
178
|
+
|
|
179
|
+
> Documentation for the WorkingMemory processor in Mastra, which injects persistent user/context data as system instructions.
|
|
180
|
+
|
|
181
|
+
The `WorkingMemory` is an **input processor** that injects working memory data as a system message. It retrieves persistent information from storage and formats it as instructions for the LLM, enabling the agent to maintain context about users across conversations.
|
|
182
|
+
|
|
183
|
+
## Usage example
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import { WorkingMemory } from "@mastra/core/processors";
|
|
187
|
+
|
|
188
|
+
const processor = new WorkingMemory({
|
|
189
|
+
storage: memoryStorage,
|
|
190
|
+
scope: "resource",
|
|
191
|
+
template: {
|
|
192
|
+
format: "markdown",
|
|
193
|
+
content: `# User Profile
|
|
194
|
+
- **Name**:
|
|
195
|
+
- **Preferences**:
|
|
196
|
+
- **Goals**:
|
|
197
|
+
`,
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Constructor parameters
|
|
203
|
+
|
|
204
|
+
### Options
|
|
205
|
+
|
|
206
|
+
### WorkingMemoryTemplate
|
|
207
|
+
|
|
208
|
+
## Returns
|
|
209
|
+
|
|
210
|
+
## Extended usage example
|
|
211
|
+
|
|
212
|
+
```typescript title="src/mastra/agents/personalized-agent.ts"
|
|
213
|
+
import { Agent } from "@mastra/core/agent";
|
|
214
|
+
import { WorkingMemory, MessageHistory } from "@mastra/core/processors";
|
|
215
|
+
import { PostgresStorage } from "@mastra/pg";
|
|
216
|
+
|
|
217
|
+
const storage = new PostgresStorage({
|
|
218
|
+
connectionString: process.env.DATABASE_URL,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
export const agent = new Agent({
|
|
222
|
+
name: "personalized-agent",
|
|
223
|
+
instructions: "You are a helpful assistant that remembers user preferences",
|
|
224
|
+
model: "openai:gpt-4o",
|
|
225
|
+
inputProcessors: [
|
|
226
|
+
new WorkingMemory({
|
|
227
|
+
storage,
|
|
228
|
+
scope: "resource",
|
|
229
|
+
template: {
|
|
230
|
+
format: "markdown",
|
|
231
|
+
content: `# User Information
|
|
232
|
+
- **Name**:
|
|
233
|
+
- **Location**:
|
|
234
|
+
- **Preferences**:
|
|
235
|
+
- **Communication Style**:
|
|
236
|
+
- **Current Projects**:
|
|
237
|
+
`,
|
|
238
|
+
},
|
|
239
|
+
}),
|
|
240
|
+
new MessageHistory({ storage, lastMessages: 50 }),
|
|
241
|
+
],
|
|
242
|
+
outputProcessors: [
|
|
243
|
+
new MessageHistory({ storage }),
|
|
244
|
+
],
|
|
245
|
+
});
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## JSON format example
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
import { WorkingMemory } from "@mastra/core/processors";
|
|
252
|
+
|
|
253
|
+
const processor = new WorkingMemory({
|
|
254
|
+
storage: memoryStorage,
|
|
255
|
+
scope: "resource",
|
|
256
|
+
template: {
|
|
257
|
+
format: "json",
|
|
258
|
+
content: JSON.stringify({
|
|
259
|
+
user: {
|
|
260
|
+
name: { type: "string" },
|
|
261
|
+
preferences: { type: "object" },
|
|
262
|
+
goals: { type: "array" },
|
|
263
|
+
},
|
|
264
|
+
}),
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Behavior
|
|
270
|
+
|
|
271
|
+
### Input processing
|
|
272
|
+
1. Retrieves `threadId` and `resourceId` from the request context
|
|
273
|
+
2. Based on scope, fetches working memory from either:
|
|
274
|
+
- Thread metadata (`scope: 'thread'`)
|
|
275
|
+
- Resource record (`scope: 'resource'`)
|
|
276
|
+
3. Resolves the template (from provider, options, or default)
|
|
277
|
+
4. Generates system instructions that include:
|
|
278
|
+
- Guidelines for the LLM on storing and updating information
|
|
279
|
+
- The template structure
|
|
280
|
+
- Current working memory data
|
|
281
|
+
5. Adds the instruction as a system message with `source: 'memory'` tag
|
|
282
|
+
|
|
283
|
+
### Working memory updates
|
|
284
|
+
Working memory updates happen through the `updateWorkingMemory` tool provided by the Memory class, not through this processor. The processor only handles injecting the current working memory state into conversations.
|
|
285
|
+
|
|
286
|
+
### Default template
|
|
287
|
+
If no template is provided, the processor uses a default markdown template with fields for:
|
|
288
|
+
- First Name, Last Name
|
|
289
|
+
- Location, Occupation
|
|
290
|
+
- Interests, Goals
|
|
291
|
+
- Events, Facts, Projects
|
|
292
|
+
|
|
293
|
+
## Related
|
|
294
|
+
|
|
295
|
+
- [Guardrails](https://mastra.ai/docs/v1/agents/guardrails)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
> Overview of Retrieval-Augmented Generation (RAG) in Mastra, detailing its capabilities for enhancing LLM outputs with relevant context.
|
|
2
|
+
|
|
3
|
+
# RAG (Retrieval-Augmented Generation) in Mastra
|
|
4
|
+
|
|
5
|
+
RAG in Mastra helps you enhance LLM outputs by incorporating relevant context from your own data sources, improving accuracy and grounding responses in real information.
|
|
6
|
+
|
|
7
|
+
Mastra's RAG system provides:
|
|
8
|
+
|
|
9
|
+
- Standardized APIs to process and embed documents
|
|
10
|
+
- Support for multiple vector stores
|
|
11
|
+
- Chunking and embedding strategies for optimal retrieval
|
|
12
|
+
- Observability for tracking embedding and retrieval performance
|
|
13
|
+
|
|
14
|
+
## Example
|
|
15
|
+
|
|
16
|
+
To implement RAG, you process your documents into chunks, create embeddings, store them in a vector database, and then retrieve relevant context at query time.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { embedMany } from "ai";
|
|
20
|
+
import { PgVector } from "@mastra/pg";
|
|
21
|
+
import { MDocument } from "@mastra/rag";
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
|
|
24
|
+
// 1. Initialize document
|
|
25
|
+
const doc = MDocument.fromText(`Your document text here...`);
|
|
26
|
+
|
|
27
|
+
// 2. Create chunks
|
|
28
|
+
const chunks = await doc.chunk({
|
|
29
|
+
strategy: "recursive",
|
|
30
|
+
size: 512,
|
|
31
|
+
overlap: 50,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// 3. Generate embeddings; we need to pass the text of each chunk
|
|
35
|
+
import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
|
|
36
|
+
|
|
37
|
+
const { embeddings } = await embedMany({
|
|
38
|
+
values: chunks.map((chunk) => chunk.text),
|
|
39
|
+
model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small")
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// 4. Store in vector database
|
|
43
|
+
const pgVector = new PgVector({
|
|
44
|
+
id: 'pg-vector',
|
|
45
|
+
connectionString: process.env.POSTGRES_CONNECTION_STRING,
|
|
46
|
+
});
|
|
47
|
+
await pgVector.upsert({
|
|
48
|
+
indexName: "embeddings",
|
|
49
|
+
vectors: embeddings,
|
|
50
|
+
}); // using an index name of 'embeddings'
|
|
51
|
+
|
|
52
|
+
// 5. Query similar chunks
|
|
53
|
+
const results = await pgVector.query({
|
|
54
|
+
indexName: "embeddings",
|
|
55
|
+
queryVector: queryVector,
|
|
56
|
+
topK: 3,
|
|
57
|
+
}); // queryVector is the embedding of the query
|
|
58
|
+
|
|
59
|
+
console.log("Similar chunks:", results);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
This example shows the essentials: initialize a document, create chunks, generate embeddings, store them, and query for similar content.
|
|
63
|
+
|
|
64
|
+
## Document Processing
|
|
65
|
+
|
|
66
|
+
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](./chunking-and-embedding).
|
|
67
|
+
|
|
68
|
+
## Vector Storage
|
|
69
|
+
|
|
70
|
+
Mastra supports multiple vector stores for embedding persistence and similarity search, including pgvector, Pinecone, Qdrant, and MongoDB. See the [vector database doc](./vector-databases).
|
|
71
|
+
|
|
72
|
+
## More resources
|
|
73
|
+
|
|
74
|
+
- [Chain of Thought RAG Example](https://github.com/mastra-ai/mastra/tree/main/examples/basics/rag/cot-rag)
|