A-Mem论文阅读
蜂窝煤 Lv3
A-Mem: Agentic Memory for LLM Agents
Conference CCF-A arXiv GitHub Repo
image.png

瞎逼逼

这篇文章最精彩的是用 Zettelkasten(卡片盒笔记法) 这个故事包装了一个由提示驱动的 agentic memory system for LLM agents(这里有点绕,就是这个记忆系统本身就是智能体型的,然后这个记忆系统还是专门设计给智能体用的),公式很清晰简洁(主要是我这种小白也能看懂),提示词设计也很有意思,尤其是那个笔记链接的提示词,感觉很有启发性,值得好好学习一下。

摘要

While large language model (LLM) agents can effectively use external tools for complex real-world tasks, they require memory systems to leverage historical experiences. Current memory systems enable basic storage and retrieval but lack sophisticated memory organization, despite recent attempts to incorporate graph databases. Moreover, these systems’ fixed operations and structures limit their adaptability across diverse tasks. To address this limitation, this paper proposes a novel agentic memory system for LLM agents that can dynamically organize memories in an agentic way. Following the basic principles of the Zettelkasten method, we designed our memory system to create interconnected knowledge networks through dynamic indexing and linking. When a new memory is added, we generate a comprehensive note containing multiple structured attributes, including contextual descriptions, keywords, and tags. The system then analyzes historical memories to identify relevant connections, establishing links where meaningful similarities exist. Additionally, this process enables memory evolution – as new memories are integrated, they can trigger updates to the contextual representations and attributes of existing historical memories, allowing the memory network to continuously refine its understanding. Our approach combines the structured organization principles of Zettelkasten with the flexibility of agent-driven decision making, allowing for more adaptive and context-aware memory management. Empirical experiments on six foundation models show superior improvement against existing SOTA baselines.

虽然大型语言模型(LLM)智能体能够有效利用外部工具来完成复杂的现实任务,但它们仍然需要 记忆系统 来利用历史经验。现有的记忆系统虽然能够实现基本的 存储与检索 功能,但在记忆组织方面仍然较为简单,尽管近期已有一些尝试将图数据库引入其中。此外,这些系统通常依赖 固定的操作方式和结构设计,这限制了它们在不同任务场景中的适应能力。
为了解决这一问题,本文提出了一种新的 面向 LLM 智能体的 agentic 记忆系统,该系统能够以 智能体驱动(agentic)的方式动态组织记忆。借鉴 Zettelkasten(卡片盒笔记法) 的基本思想,我们设计了一种记忆系统,通过 动态索引与链接机制 构建相互连接的知识网络。
当新的记忆被加入时,系统会生成一条包含多种结构化属性的完整笔记,例如 上下文描述、关键词以及标签。随后,系统会分析历史记忆以识别相关连接,并在存在有意义相似性的情况下建立记忆之间的链接。此外,这一过程还 支持记忆演化(memory evolution):当新的记忆被整合进系统时,它们可能会触发对已有历史记忆的 上下文表示和属性进行更新,从而使整个记忆网络能够持续优化其理解。
我们的方法结合了 Zettelkasten 的结构化知识组织原则智能体驱动决策的灵活性,从而实现更加 自适应且具备上下文感知能力的记忆管理机制。在六种基础模型上的实证实验表明,该方法相较于当前 最先进(SOTA)的基线方法 取得了显著性能提升。

方法

生成笔记 Note Construction

记忆 由多个笔记 Note 组成,每个记忆单元由多个属性组成的结构化表示,包含以下元素:

符号 描述
原始交互内容(Original interaction content)
交互的时间戳(Timestamp)
由 LLM 生成的高频关键词,用于捕捉交互中的核心概念
由 LLM 生成的标签集合(tags),用于对记忆进行分类
由 LLM 生成的语境描述(contextual description),提供更丰富的语义理解
记忆内容的语义向量表示(embedding),用于计算记忆之间的语义相似度
与当前记忆具有语义关联的已链接记忆集合(linked memories)

笔记构建过程使用精心设计提示模板 提示 LLM,输入为原始交互内容 、时间戳 和提示模板 ,LLM 输出为结构化记忆单元的属性():

遵循 Zettelkasten 的原子性原则,每条笔记仅捕获一个自包含的知识单元。

为实现高效检索与关联,通过 文本编码器(嵌入模型) 计算一条稠密向量表征,该表征封装了笔记所有文本成分。

A-Mem 系统实现了一个自主链接生成机制,使新的记忆笔记能够在没有预定义规则的情况下形成有意义的连接。

当构建的记忆笔记 被添加到系统中时,我们首先利用其语义嵌入进行基于相似性的检索。对于每个现有的记忆笔记 ,我们计算一个相似性得分(其实就是余弦相似度Cosine Similarity):

系统识别出最相关的 top-k 个记忆笔记:

基于这些候选的最近记忆,我们提示大语言模型(LLM)分析它们之间可能存在的潜在共同属性相关的联系。形式化地,记忆 的更新链接集如下:

基于这些候选最近记忆,我们提示大语言模型(LLM)分析它们可能共有的潜在属性。正式地,记忆 的更新链接集如下:每个生成的链接 的结构为:

通过将基于嵌入的检索作为初始过滤器,我们实现了高效的可扩展性,同时保持了语义相关性。A-MEM 能够快速识别大型记忆集合中的潜在连接,而无需进行详尽比较。更重要的是,由 LLM 驱动的解析能够实现对关系的细致理解,超越简单的相似性度量。该语言模型可以识别微妙的模式、因果关系和概念性关联,而这些可能无法仅从嵌入相似性中显现。我们实现了 Zettelkasten 原则中的灵活链接机制,同时借助现代语言模型。由此形成的网络自然地从记忆内容和上下文中涌现,从而实现自然的知识组织。(用语义嵌入先进行初筛选,然后提示大模型来进行最终的链接生成)

记忆演化 Memory Evolution

在为新记忆建立链接之后,A-MEM 会根据其文本信息和与新记忆的关联关系来演化被检索到的记忆。

对于最近邻居集合 中的每个记忆 ,系统会决定是否需要更新其上下文、关键词和标签。该演化过程可以形式化地表示为:

经过演化的记忆 随后取代集合 中原有的记忆 。这种演化方法能够实现持续更新和新连接的建立,模仿人类学习过程。随着系统随时间处理更多记忆,它会发展出越来越复杂的知识结构,在多个记忆中发现更高阶的模式和概念。这为自主记忆学习奠定基础,使知识的组织通过新体验与已有记忆的持续互动而日益丰富。

检索 Retrieve Relative Memory

在每轮交互中,我们的 A-MEM 执行上下文感知的记忆检索,为智能体提供相关的历史信息。给定当前交互中的查询文本 ,我们首先使用与记忆笔记相同的文本编码器计算其密集向量表示:

系统随后使用余弦相似度计算查询嵌入与 中所有现有记忆笔记之间的相似度分数:

然后我们从历史记忆存储中检索最相关的 条记忆,以构建一个语境恰当的提示。

这些检索到的记忆提供了相关的历史背景,有助于智能体更好地理解和响应当前的交互。检索到的上下文通过将当前交互与记忆系统中存储的相关过去经验联系起来,丰富了智能体的推理过程。

实验

实验结果

LoCoMo 数据集

LoCoMo实验结果
image.png
image.png
image.png

DialSim 数据集

DialSim实验结果

消融实验

“w/o”表示特定模块被移除的实验设置。LG和ME分别代表链路生成模块和记忆演化模块。

image.png

超参数敏感性分析

image.png

关于检索时候取 Top-K 的值

这里是不是有点奇怪,为什么 GPT-4o-mini 和 GPT-4o 的 Top-K 都是 40,而其他模型的 Top-K 都是 10?
理论上 GPT-4o-mini 和 GPT-4o 的性能会更强,所以应该需要更加少的Top-K就可以达到相似的性能;还是说他性能强,增大Top-K还可以进一步取得更加好的效果,直到40才收敛。

Model Multi Hop Temporal Open Domain Single Hop Adversial
GPT-4o-mini 40 40 50 50 40
GPT-4o 40 40 50 50 40
Qwen2.5-1.5b 10 10 10 10 10
Qwen2.5-3b 10 10 50 10 10
Llama3.2-1b 10 10 10 10 10
Llama3.2-3b 10 20 10 10 10

基线方法 Baselines

  • LoCoMo:不使用记忆机制,直接将全部历史对话作为提示输入模型进行问答。

  • ReadAgent:通过“分页 → 记忆摘要 → 交互检索”三阶段流程处理长上下文文档。

  • MemoryBank:基于遗忘曲线动态更新记忆,并通过长期交互构建用户画像。

  • MemGPT:借鉴操作系统内存层级,通过“主上下文 + 外部上下文”管理长期信息.

数据集 Dataset

LoCoMo 数据集

Adyasha Maharana, Dong-Ho Lee, Sergey Tulyakov, Mohit Bansal, Francesco Barbieri, and Yuwei Fang. Evaluating very long-term conversational memory of llm agents. arXiv preprint arXiv:2402.17753, 2024.

LoCoMo 是一个用于评估长对话理解能力的问答数据集,相比传统对话数据集,其对话长度显著更长。以往数据集通常包含约 1K tokens、4–5 个会话的对话,而 LoCoMo 的对话平均长度达到 约 9K tokens,最多可覆盖 35 个会话。因此,该数据集特别适合评估模型在长程依赖建模和跨会话一致性保持方面的能力。

LoCoMo 的问题类型设计较为多样,用于全面评估模型对长对话内容的理解能力,主要包括:

  • Single-hop questions:可在单个会话中直接找到答案的问题

  • Multi-hop questions:需要跨多个会话整合信息才能回答的问题

  • Temporal reasoning questions:测试模型对时间相关信息的理解能力

  • Open-domain knowledge questions:需要结合对话上下文和外部知识进行回答的问题

  • Adversarial questions:用于测试模型识别不可回答问题的能力

LoCoMo 数据集包含 7,512 个问答对,覆盖上述不同类型的问题。

DialSim 数据集

Jiho Kim, Woosog Chay, Hyeonji Hwang, Daeun Kyung, Hyunseung Chung, Eunbyeol Cho, Yohan Jo, and Edward Choi. Dialsim: A real-time simulator for evaluating long-term multi-party dialogue understanding of conversational agents. arXiv preprint arXiv:2406.13144, 2024.

DialSim 是一个用于评估长期对话记忆能力的问答数据集,来源于长期多角色对话场景。该数据集基于热门电视剧(如 Friends、The Big Bang Theory 和 The Office)构建,覆盖 约五年的剧情内容。

DialSim 数据集包含 1,300 个对话会话(sessions),约 350,000 个 tokens。每个会话包含 1000+ 个问题,数据集中的问题主要来源于:

  • 粉丝问答网站中的精炼问题
  • 基于时间知识图谱生成的复杂问题

由于对话跨度长、问题复杂,该数据集特别适合用于评估模型在长期对话记忆、跨时间推理以及多角色对话理解方面的能力。

模型 foundation models

  • Qwen-2.5 1.5B/3B
  • Llama-3.2 1B/3B
  • DeepSeek-R1-32B
  • GPT-4o-mini
  • Claude 3.0 Haiku
  • Claude 3.5 Haiku
  • 文本嵌入 all-minilm-l6-v2

Prompt 设计

生成笔记 附录B.1

提示
原文:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Generate a structured analysis of the following content by:
1. Identifying the most salient keywords (focus on nouns, verbs, and key concepts)
2. Extracting core themes and contextual elements
3. Creating relevant categorical tags

Format the response as a JSON object:

{
"keywords": [
// several specific, distinct keywords that capture key concepts and terminology
// Order from most to least important
// Don’t include keywords that are the name of the speaker or time
// At least three keywords, but don’t be too redundant.
],
"context":
// one sentence summarizing:
// - Main topic/domain
// - Key arguments/points
// - Intended audience/purpose
,
"tags": [
// several broad categories/themes for classification
// Include domain, format, and type tags
// At least three tags, but don’t be too redundant.
]
}

Content for analysis:

中文翻译:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
请对以下内容进行结构化分析,要求:

1. 识别最重要的关键词
- 重点关注名词、动词以及关键概念。

2. 提取核心主题与上下文要素

3. 生成相关的分类标签

请将输出组织为如下 JSON 对象:

{
"keywords": [
// 若干具体且彼此区分明显的关键词,用于概括核心概念与术语
// 按重要性从高到低排序
// 不要包含说话者姓名或时间信息
// 至少提供 3 个关键词,但避免过度重复
],
"context": "用一句话总结以下内容:主题领域、关键观点以及预期用途或受众",
"tags": [
// 若干较宽泛的分类标签
// 应包含领域、内容形式和类型等维度
// 至少提供 3 个标签,但避免过度重复
]
}

待分析内容:

笔记链接 附录B.2

提示

原文:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
You are an AI memory evolution agent responsible for managing and evolving a knowledge base.
Analyze the new memory note according to keywords and context, also with its several nearest neighbor memories.

The new memory:
- context: {context}
- content: {content}
- keywords: {keywords}

The nearest neighbor memories:
- {nearest_neighbors_memories}

Based on this information, determine:
Should this memory be evolved?
Consider its relationships with other memories.

中文翻译:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
你是一个 AI 记忆演化智能体,负责管理并持续演化一个知识库。
请结合关键词、上下文以及若干条最近邻记忆,对新记忆笔记进行分析。

新记忆信息:
- context: {context}
- content: {content}
- keywords: {keywords}

最近邻记忆:
- {nearest_neighbors_memories}

请基于以上信息判断:
这条记忆是否应该被演化(更新)?
请重点考虑它与其他记忆之间的关系。

记忆演化 附录B.3

提示

原文:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
You are an AI memory evolution agent responsible for managing and evolving a knowledge base.
Analyze the new memory note according to keywords and context, along with several nearest neighbor memories.
Make decisions about its evolution.

The new memory:
- context: {context}
- content: {content}
- keywords: {keywords}

The nearest neighbor memories:
- {nearest_neighbors_memories}

Based on this information, determine:
1. What specific actions should be taken (strengthen, update_neighbor)?
1.1 If choosing to strengthen, which memory should it connect to? Can you provide updated tags for this memory?
1.2 If choosing to update_neighbor, you may update the context and tags of these memories based on improved understanding.
Tags should reflect the content characteristics of these memories for future retrieval and categorization.

All information should be returned in list order:
[[new_memory], [neighbor_memory_1], ... [neighbor_memory_n]]

These actions can be combined.

Return your decision in JSON format with the following structure:
{
"should_evolve": true/false,
"actions": ["strengthen", "merge", "prune"],
"suggested_connections": ["neighbor_memory_ids"],
"tags_to_update": ["tag_1", ..., "tag_n"],
"new_context_neighborhood": ["new context", ..., "new context"],
"new_tags_neighborhood": [["tag_1", ..., "tag_n"], ..., ["tag_1", ..., "tag_n"]]
}

中文翻译:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
你是一个 AI 记忆演化智能体,负责管理并持续演化一个知识库。
请结合关键词、上下文以及若干条最近邻记忆,对新记忆笔记进行分析,并给出其演化决策。

新记忆信息:
- context: {context}
- content: {content}
- keywords: {keywords}

最近邻记忆:
- {nearest_neighbors_memories}

请基于以上信息判断:
1. 应采取哪些具体动作(strengthen、update_neighbor)?
1.1 如果选择 strengthen,应连接到哪条记忆?并给出该记忆更新后的 tags。
1.2 如果选择 update_neighbor,可基于新的理解更新邻居记忆的 context 和 tags。
这些 tags 应体现记忆内容特征,以便后续检索和分类。

所有信息按以下列表顺序返回:
[[new_memory], [neighbor_memory_1], ... [neighbor_memory_n]]

动作可以组合执行。

请按以下 JSON 结构输出:
{
"should_evolve": true/false,
"actions": ["strengthen", "merge", "prune"],
"suggested_connections": ["neighbor_memory_ids"],
"tags_to_update": ["tag_1", ..., "tag_n"],
"new_context_neighborhood": ["new context", ..., "new context"],
"new_tags_neighborhood": [["tag_1", ..., "tag_n"], ..., ["tag_1", ..., "tag_n"]]
}

一个具体使用记忆的例子

原文:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Example:

Question 686: Which hobby did Dave pick up in October 2023?
Prediction: photography
Reference: photography

talk start time: 10:54 am on 17 November, 2023
memory content:
Speaker Dave says: Hey Calvin, long time no talk! A lot has happened. I've taken up photography and it's been great, been taking pics of the scenery around here which is really cool.
memory context:
The main topic is the speaker's new hobby of photography, highlighting their enjoyment of capturing local scenery, aimed at engaging a friend in conversation about personal experiences.
memory keywords: ['photography', 'scenery', 'conversation', 'experience', 'hobby']
memory tags: ['hobby', 'photography', 'personal development', 'conversation', 'leisure']

talk start time: 6:38 pm on 21 July, 2023
memory content:
Speaker Calvin says: Thanks, Dave! It feels great having my own space to work in. I've been experimenting with different genres lately, pushing myself out of my comfort zone. Adding electronic elements to my songs gives them a fresh vibe. It's been an exciting process of self-discovery and growth!
memory context:
The speaker discusses their creative process in music, highlighting experimentation with genres and the incorporation of electronic elements for personal growth and artistic evolution.
memory keywords: ['space', 'experimentation', 'genres', 'electronic', 'self-discovery', 'growth']
memory tags: ['music', 'creativity', 'self-improvement', 'artistic expression']

中文翻译:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
示例:

问题 686:Dave 在 2023 年 10 月开始了哪项爱好?
预测:photography
参考答案:photography

对话开始时间:2023 年 11 月 17 日 上午 10:54
记忆内容:
说话人 Dave:Hey Calvin,好久没聊了!最近发生了很多事。我开始玩摄影了,感觉很棒。我一直在拍这附近的风景,真的很酷。
记忆上下文:
这条记忆的主题是说话人开始了摄影这一新爱好,强调了其对拍摄本地风景的兴趣,意图是在与朋友交流中分享个人近况与体验。
记忆关键词:['photography', 'scenery', 'conversation', 'experience', 'hobby']
记忆标签:['hobby', 'photography', 'personal development', 'conversation', 'leisure']

对话开始时间:2023 年 7 月 21 日 下午 6:38
记忆内容:
说话人 Calvin:谢谢你,Dave!有了自己的创作空间感觉太好了。最近我一直在尝试不同的音乐风格,逼自己走出舒适区。给歌曲加入电子元素让作品更有新鲜感。这是一个令人兴奋的自我探索与成长过程!
记忆上下文:
说话人讨论了其音乐创作过程,重点在于风格实验以及电子元素的引入,并体现了个人成长和艺术演进。
记忆关键词:['space', 'experimentation', 'genres', 'electronic', 'self-discovery', 'growth']
记忆标签:['music', 'creativity', 'self-improvement', 'artistic expression']
 Comments
Comment plugin failed to load
Loading comment plugin