跳转到主要内容
AG-Kit 的内存系统旨在与 AI agents 无缝集成,提供自动上下文管理、持久化知识存储和智能内存操作。本指南涵盖集成模式、特定框架实现和最佳实践。

集成概述

内存-Agent 工作流

核心集成模式

1. 自动内存管理
  • Agent 自动存储对话事件
  • 上下文检索透明进行
  • Token 限制自动管理
2. 双层内存
  • 短期:近期对话上下文
  • 长期:持久化用户知识和偏好
3. 智能上下文工程
  • 长对话自动摘要
  • Token 感知的上下文裁剪
  • 对话分支用于实验

框架集成

配置与最佳实践

内存配置

// 生产环境配置
const memory = MySQLMemory.create({
  host: process.env.DB_HOST,
  database: process.env.DB_NAME,
  sessionId: userId,

  // 上下文工程
  maxTokens: 8000,
  enableContextManagement: true
});

const agent = new Agent({
  name: 'production-assistant',
  model: provider,
  memory: memory,

  // 内存感知配置
  maxContextTokens: 8000,
  memoryRetrievalLimit: 10
});

故障排查

常见集成问题

内存未持久化
// ❌ 错误:内存实例未共享
const agent1 = new Agent({ memory: new InMemoryMemory() });
const agent2 = new Agent({ memory: new InMemoryMemory() });

// ✅ 正确:共享内存实例
const sharedMemory = new InMemoryMemory();
const agent1 = new Agent({ memory: sharedMemory });
const agent2 = new Agent({ memory: sharedMemory });
会话隔离问题
// ❌ 错误:不同用户使用相同会话
const agent = new Agent({ memory, sessionId: 'default' });

// ✅ 正确:每个用户使用唯一会话
const agent = new Agent({
  memory,
  sessionId: `user-${userId}`
});

下一步