Documentation Index
Fetch the complete documentation index at: https://tech.illasoft.com/llms.txt
Use this file to discover all available pages before exploring further.
Phase 5: 智能提取
目标:替换启发式提取,让系统自主决定”什么值得记住”
现状(Phase 2):行为统计 + LLM 一次性提取,固定规则决定存什么。
问题:一次性提取遗漏上下文关系,固定规则无法自适应。
5.1 预测-校准提取(NEMORI)
NEMORI 的核心思想来自自由能原理:系统预测即将发生什么,当预测失败时,才触发记忆存储。
Kira 实现:
interface PredictionGap {
predicted: string; // "用户通常选 vintage 滤镜"
actual: string; // "用户选了 cyberpunk 滤镜"
gap_type: 'preference_shift' | 'new_behavior' | 'contradiction';
importance: number; // 偏差越大越重要
}
async function extractByPredictionGap(
task: MemoryExtractTask,
existingProfile: Profile
): Promise<Memory[]> {
// 1. 基于已有画像预测行为
const predictions = await predictBehavior(existingProfile, task.messages[0]);
// 2. 对比实际 tool_calls
const gaps = detectGaps(predictions, task.tool_calls);
// 3. 只为有意义的偏差创建记忆
return gaps
.filter(g => g.importance > 0.3)
.map(g => createMemoryFromGap(g));
}
关键收益:已知信息不重复存储,大幅减少冗余记忆写入。
5.2 主动提取(ProMem)
ProMem 的关键洞察:通过自问式迭代探测(recurrent self-questioning)主动从对话历史中挖掘值得记忆的信息,而不是被动存储再寄希望于检索能找到。
const extractionPrompt = `
分析这段对话,提取值得记住的信息。
对每条提取的记忆,回答:
1. 未来什么场景会需要检索到它?
2. 用户可能怎么表达相关需求?
3. 如果缺少这条记忆,Agent 会犯什么错?
只提取"缺少会导致 Agent 犯错"的信息。
`;
关键收益:提取质量从”存了很多但检索不到”提升到”存的少但每条都能被正确检索”。
5.3 惊奇分割(HiMem)
用话题连贯性 + 惊奇度双通道检测对话边界,替代固定 turn 数分割:
function segmentConversation(messages: Message[]): Episode[] {
const episodes: Episode[] = [];
let currentEpisode: Message[] = [];
for (const msg of messages) {
const topicShift = detectTopicShift(currentEpisode, msg); // 话题通道
const surprise = calculateSurprise(currentEpisode, msg); // 惊奇通道
if (topicShift > 0.7 || surprise > 0.8) {
episodes.push(createEpisode(currentEpisode));
currentEpisode = [msg];
} else {
currentEpisode.push(msg);
}
}
return episodes;
}
关键收益:每个记忆单元语义完整,不会因为固定窗口切割而丢失上下文。
Phase 6: 深度固化
目标:从简单的”每天跑一次 Consolidator”升级为多层次离线记忆加工
现状(Phase 3):每天凌晨 3:00 跑一次 LLM 更新画像。
问题:无法处理矛盾记忆,固化粒度太粗。
6.1 睡眠时固化(LightMem)
受 Atkinson-Shiffrin 模型启发,将固化完全解耦为离线 Worker:
Kira 实现:固化 Worker 作为独立 Inngest function,session 结束时触发而非每天定时:
// Inngest event: memory/consolidate.requested
// 触发时机:session 结束(最后一条消息后 30 分钟无活动)
interface ConsolidateEventData {
entity_id: string;
entity_type: 'user' | 'group';
session_memories: string[]; // 本 session 产生的 memory IDs
}
关键收益:固化不再是每天一次的批处理,而是每个 session 结束后自动触发。LightMem 实测在线推理时 token 消耗降低 117x(仅计在线推理成本,含离线固化后约 22x;注入 Agent 的上下文更精简)。
6.2 冲突感知再固化(HiMem)
当检索到的记忆与新信息矛盾时,触发再固化:
async function injectWithConflictDetection(
query: string,
memories: Memory[]
): Promise<{ memories: Memory[]; conflicts: Conflict[] }> {
const conflicts: Conflict[] = [];
for (let i = 0; i < memories.length; i++) {
for (let j = i + 1; j < memories.length; j++) {
if (isContradictory(memories[i], memories[j])) {
conflicts.push({
memory_a: memories[i],
memory_b: memories[j],
resolution: 'prefer_newer',
});
}
}
}
if (conflicts.length > 0) {
await inngest.send({ name: 'memory/reconsolidate.requested', data: { conflicts } });
}
return { memories, conflicts };
}
关键收益:记忆不再”只增不改”。用户改变偏好时,旧记忆被更新而不是共存造成矛盾。
6.3 叙事记忆构建(Amory)
关键洞察:在离线时用 Agent 推理构建记忆,比在线时检索更值得投入算力。
碎片记忆:
- "用户用了 vintage 滤镜"
- "用户裁剪为 1:1"
- "用户导出了"
- "用户说'这张很适合发 Instagram'"
叙事记忆:
"用户正在为 Instagram 准备方形图片,偏好 vintage 风格。
导出行为表明对结果满意。"
关键收益:叙事记忆保留了因果关系和上下文,碎片记忆不行。
Phase 7: 自适应遗忘
目标:从简单指数衰减升级为认知科学级遗忘机制
现状(Phase 4):单一指数衰减 + access_count 加成。
问题:所有记忆用相同衰减速率,已固化记忆仍占用空间。
7.1 ACT-R 激活公式
用认知科学的 ACT-R 模型替代简单指数衰减:
function calculateActivation(memory: Memory, queryEmbedding?: Float32Array): number {
// 1. 基础水平激活(幂律遗忘)
const ageDays = daysSince(memory.created_at);
const recencyActivation = -0.5 * Math.log(ageDays + 1);
// 2. 频率激活
const frequencyActivation = Math.log(memory.access_count + 1);
// 3. 语义扩散激活
let spreadingActivation = 0;
if (queryEmbedding) {
const similarity = cosineSimilarity(memory.embedding, queryEmbedding);
spreadingActivation = similarity * 2.0;
}
// 4. 随机噪声(防止确定性遗忘杀死罕见但重要的记忆)
const noise = gaussianRandom() * 0.25;
return recencyActivation + frequencyActivation + spreadingActivation + noise;
}
关键收益:核心是时间衰减、频率和语义相似度三者协同。随机噪声作为辅助防止”确定性遗忘陷阱”——罕见但重要的记忆不会因为访问频率低被杀死。
7.2 差分衰减率(FadeMem)
不同类型的记忆用不同的衰减速率:
const DECAY_RATES: Record<Memory['type'], number> = {
instruction: 0.01, // 显式指令衰减最慢
preference: 0.03, // 偏好中等衰减
workflow: 0.05, // 工作流习惯衰减较快
episodic: 0.10, // 情景记忆衰减最快
};
关键收益:45% 存储节省。情景记忆快速衰减释放空间,显式指令几乎永不过期。
7.3 混合遗忘策略
基于 Forgetful but Faithful 的六策略框架,Kira 选用其中五个(去掉与 LRU 重叠的 FIFO),以 Hybrid 为默认:
| 策略 | 说明 | 场景 |
|---|
| Priority Decay | 重要度加权衰减 | 默认混合策略的核心 |
| Reflection-Summary | 低激活记忆摘要合并 | Consolidator 中使用 |
| LRU | 最久未访问优先淘汰 | 缓存层(Dragonfly)清理 |
| Random-Drop | 概率随机丢弃 | 防止过拟合 |
| Hybrid | Priority Decay + Reflection-Summary | 默认策略 |