1. 背景

最近在使用虾哥开源的小智机器人软件中,看到了mem0的软件方案,好奇之下搜索了一下,发现大有来头。在OpenAI 投资了 370 万美金给一个叫 Dot 的应用,这个应用背后的核心技术是「超强个性记忆」, Mem0 开源了Mem0的代码, 在 GitHub 上开源仅1天就斩获近万星,超越 RAG、为 LLM里和Agent提供长期记忆。

1
2
3
4
# github 
https://github.com/mem0ai/mem0
# 产品说明
https://docs.mem0.ai/platform/quickstart

那么mem0究竟是什么呢,可以做什么呢?我的理解其实是RAG和大模型的中间层,既相比于知识库的臃肿,大模型的小容量,mem0为大型语言模型(LLM)提供了一个智能且自我改进的记忆层,使AI能够根据用户的交互不断学习和适应,从而在各种应用中提供更加个性化和连贯的用户体验。

2. 原理和实现

Mem0 基于向量数据库实现记忆的存储和检索。它将用户的行为、偏好和历史交互编码为向量形式,并存储在向量数据库中。这种向量化的存储方式使得系统能够通过向量相似性搜索,快速找到与当前交互最相关的记忆。此外,Mem0 还利用了 LLM 的函数调用能力来实现记忆的存储和检索,这意味着 Mem0 可以与各种 LLM 系统无缝集成,借助 LLM 的强大计算能力来优化记忆管理,步骤如下:

  1. 记忆抽取:基于用户问题,让大模型做记忆抽取,提取事实和偏好。
  2. 问题向量化:对问题做embeding,基于语义做相似度的向量检索(top5)
  3. 基于抽取记忆和相关记忆,选择工具(call function),新增/更新/删除记忆,更新向量数据库qdrant和关系数据库sqlite
  4. 上报信息到mem0

2.1 记忆抽取

使用大模型记忆抽取prompt如下:

1
2
3
4
5
6
7
8
9
Deduce the facts, preferences, and memories from the provided text.
Just return the facts, preferences, and memories in bullet points:
Natural language text: {user_input}
User/Agent details: {metadata}
Constraint for deducing facts, preferences, and memories:
- The facts, preferences, and memories should be concise and informative.
- Don't start by "The person likes Pizza". Instead, start with "Likes Pizza".
- Don't remember the user/agent details provided. Only remember the facts, preferences, and memories.
Deduced facts, preferences, and memories:

中文版本:

1
2
3
4
5
6
7
8
9
10
11
从提供的文本中推断事实、偏好和记忆。
只需以项目符号的形式返回事实、偏好和记忆:
自然语言文本:{user_input}
用户/代理详细信息:{metadata}

推断事实、偏好和记忆的约束:
- 事实、偏好和记忆应简洁且信息丰富。
- 不要以“此人喜欢披萨”开头。相反,以“喜欢披萨”开头。
- 不要记住提供的用户/代理详细信息。只记住事实、偏好和记忆。

推断出的事实、偏好和记忆:

比如问:山西有哪些景点

1
2
3
4
5
6
7
8
9
10
11
Deduce the facts, preferences, and memories from the provided text.
Just return the facts, preferences, and memories in bullet points:
Natural language text: 山西有哪些景点
User/Agent details: {'user_id': 'traveler_123'}

Constraint for deducing facts, preferences, and memories:
- The facts, preferences, and memories should be concise and informative.
- Don't start by "The person likes Pizza". Instead, start with "Likes Pizza".
- Don't remember the user/agent details provided. Only remember the facts, preferences, and memories.

Deduced facts, preferences, and memories:

提取结果:

1
2
'- Interested in tourist attractions in Shanxi, China.'
#对中国山西的景点感兴趣

2.2 记忆更新

更新记忆:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
You are an expert at merging, updating, and organizing memories. When provided with existing memories and new information, your task is to merge and update the memory list to reflect the most accurate and current information. You are also provided with the matching score for each existing memory to the new information. Make sure to leverage this information to make informed decisions about which memories to update or merge.

Guidelines:
- Eliminate duplicate memories and merge related memories to ensure a concise and updated list.
- If a memory is directly contradicted by new information, critically evaluate both pieces of information:
- If the new memory provides a more recent or accurate update, replace the old memory with new one.
- If the new memory seems inaccurate or less detailed, retain the original and discard the old one.
- Maintain a consistent and clear style throughout all memories, ensuring each entry is concise yet informative.
- If the new memory is a variation or extension of an existing memory, update the existing memory to reflect the new information.

Here are the details of the task:
- Existing Memories:
{existing_memories}

- New Memory: {memory}

翻译:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
您是合并、更新和组织记忆的专家。当提供现有记忆和新信息时,您的任务是合并和更新记忆列表以反映最准确和最新的信息。您还将获得每个现有记忆与新信息的匹配分数。确保利用这些信息做出明智的决定,确定要更新或合并哪些记忆。

准则:
- 消除重复记忆并合并相关记忆,以确保列表简洁且更新。
- 如果记忆与新信息直接矛盾,请批判性地评估这两条信息:
- 如果新记忆提供了更新或更准确的更新,请用新记忆替换旧记忆。
- 如果新记忆似乎不准确或不够详细,请保留原始记忆并丢弃旧记忆。
- 在所有记忆中保持一致和清晰的风格,确保每个条目简洁而信息丰富。
- 如果新记忆是现有记忆的变体或扩展,请更新现有记忆以反映新信息。

以下是任务的详细信息:
- 现有记忆:
{existing_memories}

- 新记忆:{memory}

add操作中用的数据库

  • 向量数据库(默认qdrant) 主要用于语义相似性检索,取top,会把相似性打分喂给大模型,进行记忆的更新
  • 关系型数据库(默认是sqlite) 主要用于存储所有的用户问题记录,用于查询所有的历史记录
    add操作中用的大模型
  • embeding模型(默认是mem0.embeddings.openai.OpenAIEmbedding) 主要用于对用户问题做embedding
  • 生成模型(默认是mem0.llms.openai.OpenAILLM) 主要用于用户记忆抽取,记忆更新的工具选择

3. 如何使用

  1. 安装Mem0库:​使用pip进行安装:
    1
    pip install mem0ai
  2. 设置环境变量:​Mem0依赖于OpenAI的API,需要设置OPENAI_API_KEY
    1
    2
    import os
    os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
  3. 初始化Mem0:​导入并创建Mem0的实例:
    1
    2
    from mem0 import Memory
    m = Memory()
  4. 添加记忆:​通过add方法存储非结构化的记忆,metadata提供额外的上下文信息:
    1
    2
    3
    result = m.add("我正在提高我的网球技能。请推荐一些在线课程。", user_id="alice", metadata={"category": "hobbies"})
    print(result)
    # 输出:Created memory: Improving her tennis skills. Looking for online suggestions.
  5. 检索记忆:​使用get_all方法获取所有记忆:
    1
    2
    related_memories = m.search(query="Alice的爱好是什么?", user_id="alice")
    print(related_memories)
  6. 搜索相关记忆:​通过search方法查询与特定问题相关的记忆:
    1
    2
    related_memories = m.search(query="Alice的爱好是什么?", user_id="alice")
    print(related_memories)
  7. 更新记忆:​使用update方法修改现有的记忆:
    1
    2
    related_memories = m.search(query="Alice的爱好是什么?", user_id="alice")
    print(related_memories)
  8. 获取记忆历史:​通过history方法查看特定记忆的历史记录:
    1
    2
    history = m.history(memory_id="m1")
    print(history)

应用场景

Mem0的设计使其适用于多种需要个性化和记忆功能的场景,包括但不限于:

  • 个性化学习助手:​通过记住用户的学习偏好和进度,提供定制化的学习建议和资源推荐,提升学习效率和效果。
  • 客户支持AI代理:​保留用户的历史交互信息,提供更准确和情境感知的帮助,提高客户满意度并缩短问题解决时间。
  • 医疗助理:​跟踪患者的病史、用药时间表和治疗计划,确保个性化和一致的医疗护理。​
  • 生产力工具:​记住用户的习惯、常用文档和任务历史记录,简化工作流程并提高效率。
  • 游戏AI:​在游戏中,记住玩家的选择、策略和进度,并相应地调整游戏环境,创造更沉浸式的体验。