AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


最近爆火的Mem0,但是我认为还不够...
发布日期:2024-08-03 12:25:27 浏览次数: 1706




Mem0为大模型语言模型提供了⼀个自我改进的、智能的内存层,从而实现跨应用程序的、真正个性化的AI 体验。可以帮助大模型更好的了解用户的喜好和需求等。

作者通过了解学习、源码分析、结合业务场景和需求分析,存在一些不同的想法,一起来探讨交流下。

01
mem0介绍

最近霸榜 github trending 的mem0,热度不减,截止到 7 月 23 日晚 9 点,累计 start 15.8k。

mem0 start趋势图

mem0 trending榜一

项目:github.com/mem0ai/mem0

mem0具有广泛的使用场景,常见用例列举如下:

个性化学习助手:长期记忆使学习助手能够记住用户的偏好、过去的互动和进度,从而提供更加定制化、有效的学习体验。

AI Agent客服:通过保留以前交互的信息,可以提供更准确、更具情境感知的帮助,提高客户满意度。

医疗助理:长期记忆使医疗助理能够跟踪患者病史、用药时间表和治疗计划,确保个性化和⼀致的护理。

虚拟伴侣:虚拟伴侣可以利用长期记忆,通过记住个⼈详细信息、偏好和过去的对话来与用户建立更深层次的关系,使互动更有意义。

生产力工具:长期记忆可帮助生产力工具记住用户习惯、常用文档和任务历史记录,从而简化工作流程并提高效率。

游戏 AI:在游戏中,具有长期记忆的 AI 可以通过记住玩家的选择、策略和进度并相应地调整游戏环境来创造更身临其境的体验。 


02
功能梳理


mem0提供了对记忆的一系列的基础操作,总结:记忆增删改查操作。

  • 保存记忆

  • 查询相关记忆

  • 获取记忆

  • 记忆历史

  • 删除记忆


同时提供了一些核心功能

  • 多级记忆:用户、会话和 AI Agent 记忆保留

  • 自适应个性化:基于交互的持续改进

  • 开发友好 API:简单集成到各种应用程序中

  • 跨平台一致性:跨设备的统一行为

  • 托管服务:轻松的托管解决方案


03
如何实现

Mem 0 是如何添加记忆、检索记忆、搜索、更新和获取记忆历史。

```

# Store a memory from any unstructured text  

result = m.add("我今天看了AI无限实验室的一篇文章,觉的还不错.", user_id="alice", metadata={"category": "hobbies"})  

print(result)  

# Created memory: 看了一篇还不错的AI无限实验室的文章。

```


- 塞入带有 metadata 的自然语言, 这⾥相当于定义了⼀个 schema,让 LLM 从⾮结构化数据⾥提取更多有效的记忆信息。

- 通过记忆推演的 prompt(MEMORY_DEDUCTION_PROMPT) 结合⽤户的 data,抽取总结记忆,得到 extracted_memories

- 然后通过 data 查询相关的 existing_memories

- 获取更新后的数据:将老记忆、新记忆拼接到⼀起,通过更新记忆 prompt(UPDATE_MEMORY_PROMPT)让⼤模型调⽤合适的 tool 来更新记忆,tools 包含 :[添加记忆(ADD_MEMORY_TOOL), 更新记忆(UPDATE_MEMORY_TOOL), 删除记忆(DELETE_MEMORY_TOOL)]


最后一步通过 function call 的结果,调⽤tool_calls 对记忆进行相关操作


通过上述流程可以总结得出:Mem 0 记忆的过程本质上全部委托给⼤模型,通过 prompt 工程来实现关键记忆操作,例如记忆推演和记忆更新。


查看源码中的 prompt,会发现大模型经典的 Prompt 起手式:立角色+述问题+定目标+补要求;


你是一个精通 xx 的人,你需要解决什么问题,你的目标是 xxx,下面是我们限定要求,请回答;


更新记忆prompt:


```

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}

```


翻译:


```

您是合并、更新和组织记忆的专家。当提供现有记忆和新信息时,您的任务是合并和更新记忆列表,以反映最准确和最新的信息。您还获得了每个现有记忆与新信息的匹配分数。确保利用这些信息就更新或合并哪些记忆做出最明智的决定。


准则:

-消除重复记忆并合并相关记忆,以确保简洁和更新的列表。

-如果一段记忆与新信息直接矛盾,批判性地评估两条信息: 

-如果新记忆提供了更近期或更准确的更新,用新记忆替换旧记忆。

-如果新记忆看起来不准确或不太详细,保留原始记忆并丢弃旧记忆。

-在所有记忆中保持一致和清晰的风格,确保每个条目都简洁而信息丰富。

-如果新记忆是现有记忆的变体或扩展,更新现有记忆以反映新信息。


以下是任务的细节:

-现有记忆:

{existing_memories}


-新记忆:{memory}

```


记忆演化的Prompt

```

You are an expert at deducing facts, preferences and memories from unstructured text.

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:

```

翻译:

```

您是从非结构化文本中推断事实、偏好和记忆的专家。

从提供的文本中推断出事实、偏好和记忆。

只需在要点中返回事实、偏好和记忆:

自然语言文本:{user_input} 

用户/Agents详细信息:{metadata}


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

-事实、偏好和记忆应该简洁且内容丰富。

-不要从“这个人喜欢披萨”开始。相反,从“喜欢披萨”开始。陈述事实

-不要关注用户/Agent细节信息。只记住事实、偏好和记忆。


推断的事实、偏好和记忆应该是:

```

Mem 0 是如何添加记忆、检索记忆、搜索、更新和获取记忆历史。可以通过官方提供的示例入手,下面是官方提供的示例Demo:

# Store a memory from any unstructured text  result = m.add("我今天看了AI无限实验室的一篇文章,觉的还不错.", user_id="alice", metadata={"category": "hobbies"})  print(result)  # Created memory: 看了一篇还不错的AI无限实验室的文章。

通过源码分析,可以看出记忆 add 背后的逻辑如下:

  1. 塞入带有 metadata 的自然语言, 相当于定义了⼀个 schema,让 LLM 从非结构化数据里提取更多有效的记忆信息。

  2. 通过记忆推演的 prompt + 用户的 data,抽取总结记忆,得到新记忆

  3. 然后通过 data 查询相关的老记忆

  4. 获取更新后的数据:将老记忆、新记忆拼接到⼀起,通过更新记忆 prompt 让大模型调用合适的 tool 来更新记忆,tools 包含 :[添加记忆更新记忆删除记忆]

最后一步通过 function call 的结果,调⽤tool_calls 对记忆进行具体操作

通过上述流程可以总结得出:mem0 记忆的关键过程本质上全部委托大模型,通过 prompt 工程来实现关键记忆操作,例如记忆推演和记忆更新。

查看源码中的 prompt,会发现大模型经典的 Prompt 起手式立角色+述问题+定目标+补要求

你是一个精通 xx 的人,你需要解决什么问题,你的目标是 xxx,下面是我们限定要求,请回答;

更新记忆prompt

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}

翻译如下:

您是合并、更新和组织记忆的专家。当提供现有记忆和新信息时,您的任务是合并和更新记忆列表,以反映最准确和最新的信息。您还获得了每个现有记忆与新信息的匹配分数。确保利用这些信息就更新或合并哪些记忆做出最明智的决定。准则:-消除重复记忆并合并相关记忆,以确保简洁和更新的列表。-如果一段记忆与新信息直接矛盾,批判性地评估两条信息:	-如果新记忆提供了更近期或更准确的更新,用新记忆替换旧记忆。	-如果新记忆看起来不准确或不太详细,保留原始记忆并丢弃旧记忆。-在所有记忆中保持一致和清晰的风格,确保每个条目都简洁而信息丰富。-如果新记忆是现有记忆的变体或扩展,更新现有记忆以反映新信息。以下是任务的细节:-现有记忆:{existing_memories}-新记忆:{memory}

记忆演化的Prompt

You are an expert at deducing facts, preferences and memories from unstructured text.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:

翻译如下:

您是从非结构化文本中推断事实、偏好和记忆的专家。从提供的文本中推断出事实、偏好和记忆。只需在要点中返回事实、偏好和记忆:自然语言文本:{user_input} 用户/Agents详细信息:{metadata}推断事实、偏好和记忆的约束:-事实、偏好和记忆应该简洁且内容丰富。-不要从“这个人喜欢披萨”开始。相反,从“喜欢披萨”开始。陈述事实-不要关注用户/Agent细节信息。只记住事实、偏好和记忆。推断的事实、偏好和记忆应该是:


04
总结

mem0 提供了面向记忆增删改查操作,同时提供了若干周边好用的生态工具,可以帮助提供更个性化的内容。但本质上还是调用大模型进行记忆的提取更新这一类的操作;

Mem0可以显著提升个性化AI的能力。通过记住用户的偏好等用户画像信息,AI产品就可以提供更加个性化服务,有较大的想象空间,未来可期,但存在如下问题:

  • 过分依赖大模型本身效果

  • 每次add操作都会调用大模型处理,token花费大

  • 两类prompt并不能满足业务需求,实际业务场景需要细化和处理

作者认为的mem0想要落到业务系统中还需要如下考虑,mem0是一个通用内存设计框架,但具体到具体的业务场景中是需要进行补充完善的。

例如:如果你想模拟一个阿尔茨海默病的患者,他的记忆是复杂多变的;又比如你可能记得昨天吃了什么饭,但是一年前的饭菜是什么样子你会忘记,但是你又会记得三年前你生日那天你的女朋友给你送的礼物;这需要的复杂的模拟算法支撑;

  • 细化的记忆组件,针对于业务需求,需要细化场景定制prompt

  • 前置分类器,判定是否需要进行记忆,减少整体花费


53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询