translation/translated/documents/course/02-agent-tools-mcp/31-enhancing-memory-configu...

79 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 增强内存配置
最后,让我们增强我们的内存配置,让我们的智能体更加有用:
```typescript
import { LibSQLStore, LibSQLVector } from "@mastra/libsql";
const memory = new Memory({
storage: new LibSQLStore({
id: "learning-memory-storage",
url: "file:../../memory.db",
}),
vector: new LibSQLVector({
id: "learning-memory-vector",
connectionUrl: "file:../../memory.db",
}),
embedder: openai.embedding("text-embedding-3-small"),
options: {
// 在上下文中保留最后 20 条消息
lastMessages: 20,
// 启用语义搜索以查找相关的过去对话
semanticRecall: {
topK: 3,
messageRange: {
before: 2,
after: 1,
},
},
// 启用工作记忆来记住用户信息
workingMemory: {
enabled: true,
template: `
<user>
<first_name></first_name>
<username></username>
<preferences></preferences>
<interests></interests>
<conversation_style></conversation_style>
</user>`,
},
},
});
```
并更新智能体指令以使用此增强内存:
```typescript
export const personalAssistantAgent = new Agent({
name: "Personal Assistant",
instructions: `
// ... 现有指令 ...
你可以访问对话记忆,并可以记住关于用户的详细信息。
当你了解有关用户的信息时,使用适当的工具更新他们的工作记忆。
这包括:
- 他们的兴趣
- 他们的偏好
- 他们的对话风格(正式、非正式等)
- 任何其他有助于个性化对话的相关信息
始终保持有用和专业的语调。
使用存储的信息提供更个性化的响应。
`,
model: openai("gpt-4o"),
tools: { ...mcpTools },
memory,
});
```
这种增强的内存配置为你的智能体提供了更复杂的记忆能力:
1. **对话历史**`lastMessages` 选项在上下文中保留最后 20 条消息,允许你的智能体引用最近的对话。
2. **语义回忆**`semanticRecall` 选项使你的智能体能够使用语义搜索找到相关的过去对话,即使它们发生在很久以前。要使 `semanticRecall` 工作,你需要配置向量存储和嵌入器。
3. **工作记忆**`workingMemory` 选项允许你的智能体记住关于用户的特定信息,如他们的偏好和兴趣,并使用这些信息提供更个性化的响应。
通过更新智能体的指令以包含关于这些记忆能力的信息,你帮助它理解如何有效地使用它们来提供更好的用户体验。