Documentation Index
Fetch the complete documentation index at: https://docs.ag-kit.dev/llms.txt
Use this file to discover all available pages before exploring further.
AG-Kit 的内存系统旨在与 AI agents 无缝集成,提供自动上下文管理、持久化知识存储和智能内存操作。本指南涵盖集成模式、特定框架实现和最佳实践。
集成概述
内存-Agent 工作流
核心集成模式
1. 自动内存管理
- Agent 自动存储对话事件
- 上下文检索透明进行
- Token 限制自动管理
2. 双层内存
3. 智能上下文工程
- 长对话自动摘要
- Token 感知的上下文裁剪
- 对话分支用于实验
框架集成
import { LlamaIndexAgent } from '@ag-kit/adapter-llamaindex';
import { ConversationMemory } from '@ag-kit/adapter-llamaindex';
import { createMemory } from 'llamaindex';
// 创建对话内存管理器
const conversationMemory = new ConversationMemory({
memoryFactory: () => createMemory()
});
// 创建集成了 AG-Kit 的 LlamaIndex agent
const agent = new LlamaIndexAgent({
name: 'llamaindex-assistant',
model: provider,
conversationMemory: conversationMemory,
tools: []
});
// 使用对话 ID 进行内存隔离
const response = await agent.run({
input: 'Hello, remember my preferences',
conversationId: 'user-123'
});
配置与最佳实践
内存配置
// 生产环境配置
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}`
});
下一步