translation/translated/documents/course/01-first-agent/16-adding-memory-to-agent.md

1.2 KiB

为您的代理添加记忆

现在,让我们更新我们的代理以包含记忆功能。打开您的 agents/index.ts 文件并进行以下更改:

  1. 导入 Memory 和 LibSQLStore 类:
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
import { Memory } from "@mastra/memory";
import { LibSQLStore } from "@mastra/libsql";
import { getTransactionsTool } from "../tools";
  1. 通过存储实例配置将记忆添加到您的代理:
export const financialAgent = new Agent({
  name: "Financial Assistant Agent",
  instructions: `角色定义
  // ... 现有说明 ...
  `,
  model: openai("gpt-4o"),
  tools: { getTransactionsTool },
  memory: new Memory({
    storage: new LibSQLStore({
      id: "learning-memory-storage",
      url: "file:../../memory.db", // 本地文件系统数据库。位置相对于输出目录 `.mastra/output`
    }),
  }), // 在这里添加记忆
});

通过在代理配置中添加 memory 属性,您使其能够记住之前的对话。@mastra/memory 包中的 Memory 类提供了一种简单的方式来为您的代理添加记忆功能。@mastra/libsql 中的 LibSQLStore 类将记忆数据持久化到 SQLite 数据库。