translation/translated/documents/course/03-agent-memory/08-configuring-conversation...

38 lines
1.5 KiB
Markdown

# 配置对话历史
默认情况下,`Memory` 实例在每个新请求中包含当前内存线程中的最后 10 条消息。你可以通过配置 `lastMessages` 选项来自定义这一点:
```typescript
import { Agent } from "@mastra/core/agent";
import { Memory } from "@mastra/memory";
import { openai } from "@ai-sdk/openai";
import { LibSQLStore } from "@mastra/libsql";
// 创建一个具有自定义对话历史设置的内存实例
const memory = new Memory({
storage: new LibSQLStore({
url: "file:../../memory.db",
}),
options: {
lastMessages: 20, // 在上下文中包含最后 20 条消息,而不是默认的 10 条
},
});
// 创建一个具有配置内存的智能体
export const memoryAgent = new Agent({
name: "MemoryAgent",
instructions: `
你是一个具有内存功能的乐于助人的助手。
你可以记住之前的对话和用户偏好。
当用户分享关于他们自己的信息时,请确认并记住它以供将来参考。
如果被问及对话中 earlier 提到的事情,请准确地回忆它。
`,
model: openai("gpt-4o"),
memory: memory,
});
```
`lastMessages` 选项控制有多少最新的消息被包含在智能体的上下文窗口中。这很重要,因为语言模型的上下文窗口大小有限,包含太多消息可能会挤出其他重要信息。
你需要在为智能体提供足够的上下文来理解对话和不要用过多的历史压垮上下文窗口之间取得平衡。