translation/translated/documents/course/03-agent-memory/22-custom-working-memory-te...

90 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

# 自定义工作记忆模板
模板指导智能体在工作记忆中跟踪和更新什么信息。如果未提供模板,则使用默认模板,但您通常希望定义一个针对智能体特定用例的自定义模板。
让我们使用自定义工作记忆模板更新我们的智能体:
```typescript
import { Agent } from "@mastra/core/agent";
import { Memory } from "@mastra/memory";
import { openai } from "@ai-sdk/openai";
// 创建具有自定义工作记忆模板的内存实例
const memory = new Memory({
storage: new LibSQLStore({
id: "learning-memory-storage",
url: "file:../../memory.db", // 相对于 `.mastra/output` 目录的相对路径
}), // 消息历史记录的存储
vector: new LibSQLVector({
connectionUrl: "file:../../vector.db", // 相对于 `.mastra/output` 目录的相对路径
}), // 用于语义搜索的向量数据库
embedder: openai.embedding("text-embedding-3-small"), // 用于消息嵌入的嵌入器
options: {
semanticRecall: {
topK: 3,
messageRange: {
before: 2,
after: 1,
},
},
workingMemory: {
enabled: true,
},
workingMemory: {
enabled: true,
template: `
# 用户档案
## 个人信息
- 姓名:
- 位置:
- 时区:
## 偏好
- 沟通风格:[例如,正式、非正式]
- 兴趣:
- 喜爱话题:
## 会话状态
- 当前主题:
- 未解决问题:
- [问题 1]
- [问题 2]
`,
},
},
});
// 创建具有配置内存的智能体
export const memoryAgent = new Agent({
name: "MemoryAgent",
instructions: `
您是一个具有高级记忆能力的助手。
您可以记住之前的对话和用户偏好。
重要提示:您有权访问工作记忆来存储关于用户的持久信息。
当您了解用户的任何重要信息时,请根据模板更新您的工作记忆。
在询问用户已提供的信息之前,请始终参考您的工作记忆。
使用工作记忆中的信息提供个性化响应。
当用户分享个人信息(如姓名、位置或偏好)时,请承认并相应地更新您的工作记忆。
`,
model: openai("gpt-4o"),
memory: memory,
});
```
模板是一个定义工作记忆结构的 Markdown 文档。它包含不同类型信息的部分,如个人信息、偏好和会话状态。
模板服务于几个重要目的:
1. 它指导智能体跟踪什么信息以及如何组织它
2. 它在跨对话中提供一致的工作记忆结构
3. 它使智能体更容易找到和更新特定信息
您应该根据智能体的特定需求以及它需要记住的用户或任务类型的信息来设计您的模板。