2.2 KiB
2.2 KiB
Configuring Working Memory
Let's update our agent with working memory capabilities:
import { Agent } from "@mastra/core/agent";
import { Memory } from "@mastra/memory";
import { openai } from "@ai-sdk/openai";
import { LibSQLStore, LibSQLVector } from "@mastra/libsql";
// Create a memory instance with working memory configuration
const memory = new Memory({
storage: new LibSQLStore({
id: "learning-memory-storage",
url: "file:../../memory.db", // relative path from the `.mastra/output` directory
}), // Storage for message history
vector: new LibSQLVector({
id: "learning-memory-vector",
connectionUrl: "file:../../vector.db", // relative path from the `.mastra/output` directory
}), // Vector database for semantic search
embedder: openai.embedding("text-embedding-3-small"), // Embedder for message embeddings
options: {
semanticRecall: {
topK: 3,
messageRange: {
before: 2,
after: 1,
},
},
workingMemory: {
enabled: true,
},
},
});
// Create an agent with the configured memory
export const memoryAgent = new Agent({
name: "MemoryAgent",
instructions: `
You are a helpful assistant with advanced memory capabilities.
You can remember previous conversations and user preferences.
IMPORTANT: You have access to working memory to store persistent information about the user.
When you learn something important about the user, update your working memory.
This includes:
- Their name
- Their location
- Their preferences
- Their interests
- Any other relevant information that would help personalize the conversation
Always refer to your working memory before asking for information the user has already provided.
Use the information in your working memory to provide personalized responses.
`,
model: openai("gpt-4o"),
memory: memory,
});
The workingMemory configuration has several important options:
enabled: Whether working memory is enabledtemplate: A template for the working memory content
The instructions for the agent are also important. They guide the agent on what information to store in working memory and how to use that information when responding to the user.