2026年7月9日 周四晚上19:30,报名腾讯会议了解“如何构建自进化的动态知识库(Brain)”(限30人)
免费POC, 零成本试错
FDE知识库

FDE知识库

学习大模型的前沿技术与行业落地应用


收藏

RAG替代者?深入浅出分析火热的Mem0个性化AI记忆层

发布日期:2024-07-30 19:33:34 浏览次数: 4003
作者:加琪聊AI

微信搜一搜,关注“加琪聊AI”

最近Mem0横空出世,官方称之为PA的记忆层,The memory layer for Personalized AI,有好事者还称这个是RAG的替代者,Mem0究竟为何物,背后的原理是什么,我们今天来一探究竟。

Mem0 介绍

开源地址:https://github.com/mem0ai/mem0

官方介绍为:

Mem0 provides a smart, self-improving memory layer for Large Language Models, enabling personalized AI experiences across applications.

关键点,是为LLM提供的智能的,可自我改进的记忆层,从而可以实现在各种应用中提供更加个性化的和连贯一致的用户体验。

主要特点

  • 多层次记忆:支持用户级、会话级和AI代理级的记忆保留。
  • 自适应个性化:根据用户交互不断改进,提供精准个性化记忆。
  • 开发者友好API:简单易用,易于集成。
  • 跨平台一致性:保持不同设备上的行为一致性。
  • 托管服务:简化部署和维护。

快速开始

安装:通过pip安装mem0ai。

pip install mem0ai

基本用法:

import os
from mem0 import Memory

# 依赖LLM提取记忆,所以需要open ai
os.environ["OPENAI_API_KEY"] = "xxx"

# 吃石化 Mem0
m = Memory()

# 通过add方法,存储非结构化的记忆,metadata提供schema定义
result = m.add("I am working on improving my tennis skills. Suggest some online courses.", user_id="alice", metadata={"category""hobbies"})
print(result)
# Created memory: Improving her tennis skills. Looking for online suggestions.

# Retrieve memories
all_memories = m.get_all()
print(all_memories)

# 搜索记忆  Search memories
related_memories = m.search(query="What are Alice's hobbies?", user_id="alice")
print(related_memories)

# 更新记忆 Update a memory
result = m.update(memory_id="m1", data="Likes to play tennis on weekends")
print(result)

# Get memory history
history = m.history(memory_id="m1")
print(history)

上述的示例代码展示了如何添加记忆、检索记忆、搜索、更新和获取记忆历史。

注意代码里的metadata, 这里相当于定义了一个schema,让LLM从非结构化数据里提取相关的记忆信息。

原理分析

透过上面的示例代码,我们先来猜测下mem0的原理:

  • 通过LLM+制定的metadata,抽取记忆信息,这里雷士知识图谱抽取,重点是定制一个合适的prompt来抽取有效信息
  • 相关记忆信息通过向量化存储,因此可以支持记忆信息检索
  • 记忆支持更新,例如demo里的hobbies更新

我们下载代码一探究竟。

记忆管理

def add(
        self,
        data,
        user_id=None,
        agent_id=None,
        run_id=None,
        metadata=None,
        filters=None,
        prompt=None,
    )
:

        """
        Create a new memory.

        Args:
            data (str): Data to store in the memory.
            user_id (str, optional): ID of the user creating the memory. Defaults to None.
            agent_id (str, optional): ID of the agent creating the memory. Defaults to None.
            run_id (str, optional): ID of the run creating the memory. Defaults to None.
            metadata (dict, optional): Metadata to store with the memory. Defaults to None.
            filters (dict, optional): Filters to apply to the search. Defaults to None.

        Returns:
            str: ID of the created memory.
        """

        if metadata is None:
            metadata = {}
        embeddings = self.embedding_model.embed(data)

        filters = filters or {}
        if user_id:
            filters["user_id"] = metadata["user_id"] = user_id
        if agent_id:
            filters["agent_id"] = metadata["agent_id"] = agent_id
        if run_id:
            filters["run_id"] = metadata["run_id"] = run_id

        if not prompt:
            prompt = MEMORY_DEDUCTION_PROMPT.format(user_input=data, metadata=metadata)
        extracted_memories = self.llm.generate_response(
            messages=[
                {
                    "role""system",
                    "content""You are an expert at deducing facts, preferences and memories from unstructured text.",
                },
                {"role""user""content": prompt},
            ]
        )
        existing_memories = self.vector_store.search(
            name=self.collection_name,
            query=embeddings,
            limit=5,
            filters=filters,
        )
        existing_memories = [
            MemoryItem(
                id=mem.id,
                score=mem.score,
                metadata=mem.payload,
                text=mem.payload["data"],
            )
            for mem in existing_memories
        ]
        serialized_existing_memories = [
            item.model_dump(include={"id""text""score"})
            for item in existing_memories
        ]
        logging.info(f"Total existing memories: {len(existing_memories)}")
        messages = get_update_memory_messages(
            serialized_existing_memories, extracted_memories
        )
        # Add tools for noop, add, update, delete memory.
        tools = [ADD_MEMORY_TOOL, UPDATE_MEMORY_TOOL, DELETE_MEMORY_TOOL]
        response = self.llm.generate_response(messages=messages, tools=tools)
        tool_calls = response["tool_calls"]

        response = []
        if tool_calls:
            # Create a new memory
            available_functions = {
                "add_memory": self._create_memory_tool,
                "update_memory": self._update_memory_tool,
                "delete_memory": self._delete_memory_tool,
            }
            for tool_call in tool_calls:
                function_name = tool_call["name"]
                function_to_call = available_functions[function_name]
                function_args = tool_call["arguments"]
                logging.info(
                    f"[openai_func] func: {function_name}, args: {function_args}"
                )

                # Pass metadata to the function if it requires it
                if function_name in ["add_memory""update_memory"]:
                    function_args["metadata"] = metadata

                function_result = function_to_call(**function_args)
                # Fetch the memory_id from the response
                response.append(
                    {
                        "id": function_result,
                        "event": function_name.replace("_memory"""),
                        "data": function_args.get("data"),
                    }
                )
                capture_event(
                    "mem0.add.function_call",
                    self,
                    {"memory_id": function_result, "function_name": function_name},
                )
        capture_event("mem0.add", self)
        return response

这里的逻辑比较简单

  • 参数的判断、处理
  • 通过 MEMORY_DEDUCTION_PROMPT 结合用户的data,抽取记忆,得到extracted_memories
  • 然后通过data查询相关的existing_memories
  • 然后将extracted_memories、existing_memories 拼接到一起,交予大模型,让大模型调用合适的tool来更新记忆,tools :[ADD_MEMORY_TOOL, UPDATE_MEMORY_TOOL, DELETE_MEMORY_TOOL]
  • 根据function call的结果,调用tool_calls更新记忆

本质上全部委托给大模型,通过prompt做了一定的约束。

相关prompt设计

ps,我们来看下相关度的prompt设计。

MEMORY_DEDUCTION_PROMPT = """
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:


从提供的文本中推断出事实、偏好和记忆。
仅以项目符号形式返回事实、偏好和记忆:
自然语言文本:{用户输入}
用户/代理详细信息:{元数据}

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

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

再来看更新记忆的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}


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

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

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

- 新记忆:{记忆} 

Mem0 点评

Mem0 是RAG的杀手?  

  • NO, Mem0 是RAG的助手,可以帮助提供更个性化的内容。

Mem0 有什么用处?

  • Mem0可以显著提升个性化AI的能力。通过记住用户的偏好等用户画像信息,AI产品就可以提供更加个性化服务,有较好的想象空间。传统的用户画像依赖于产研去设计schema,只能挖掘存储设计好的一些特征,而Mem0通过大模型,可以提供schame base和大模型自己挖掘的记忆,提供了一条更通用的方案
  • PA产品、泛娱乐、教育等各个领域,都可以发挥出作用

Mem0 有什么不足?

  • mem0当前未看到提供一些通用的schema,提供graph base的管理支持
  • 用户的memory应该区分短中长期记忆,mem0当前是未区分的,需要有解决方案
  • 自定义的记忆管理规则
  • 支持结合RAG 和用户对话历史,初始化记忆
  • 完全依赖LLM,成本较大

我们也可以看下mem0的roadmap,有规划提供一些自定义规则支持:

  • Integration with various LLM providers
  • Support for LLM frameworks
  • Integration with AI Agents frameworks
  • Customizable memory creation/update rules
  • Hosted platform support

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

产品:场景落地咨询+大模型应用平台+行业解决方案

承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业

联系我们

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

微信扫码

添加专属顾问

回到顶部

加载中...

扫码咨询

扫码登录
登录即表示您同意《53AI网站服务协议》
服务协议

欢迎您使用【53AI 官方网站】(以下简称“本网站”或“我们”)。本《会员服务协议》(以下简称“本协议”)是您(以下简称“会员”或“用户”)与【深圳市博思协创网络科技有限公司】之间关于注册、登录及使用本网站会员服务所订立的法律协议。

在您注册或登录前,请务必审慎阅读、充分理解各条款内容,特别是免除或限制责任的条款、知识产权条款、争议解决条款等。此类条款将以加粗形式提示您注意。 当您通过微信公众号授权、手机验证码验证或其他方式成功登录本网站时,即视为您已完全理解并同意接受本协议的全部内容。

一、 定义

本网站:指由【深圳市博思协创网络科技有限公司】运营的,域名为【53ai.com】的网站及相关移动端页面。

会员服务:指本网站向注册会员提供的知识库文章查阅、内容检索及其他相关增值服务。

知识库内容:指本网站发布的包括但不限于文字、图表、数据、研究报告、行业分析等数字化内容资源。

二、 账号注册与登录

登录方式:本网站支持以下登录方式,您可根据实际情况选择:

微信公众号授权登录:您同意将您的微信OpenID信息授权给本网站,用于创建或关联会员账号。

手机验证码登录:您需提供真实有效的手机号码,并通过短信验证码完成身份验证与登录/注册。

账号安全:您的账号仅限您本人使用,禁止赠与、借用、租用、转让或售卖。因您保管不善导致的账号被盗、密码泄露等损失,由您自行承担。

实名认证:根据相关法律法规要求,我们可能要求您在特定功能下完成实名认证。如您拒绝提供,可能无法使用部分或全部服务。

未成年人保护:若您未满18周岁,请在法定监护人的陪同下阅读本协议,并在征得监护人同意后使用本服务。

三、 服务内容与规范

知识库查阅权限:会员登录后,有权按照其会员等级对应的权限范围,在线浏览、检索本网站知识库中的相关文章及内容。

服务变更:我们有权根据业务发展需要,调整、变更或终止部分服务内容,并将以网站公告、公众号消息等方式提前通知。

禁止行为:您在使用服务时不得实施以下行为:

利用技术手段批量爬取、下载、转存知识库内容;

将知识库内容用于商业目的或未经授权地向第三方传播;

干扰本网站正常运行或侵犯其他用户合法权益;

发布违法违规信息或从事违反公序良俗的活动。

四、 知识产权声明

权利归属:本网站知识库中的排版设计、软件代码等内容的知识产权均归【公司全称】或原权利人所有,受《中华人民共和国著作权法》等法律保护。

有限许可:本网站授予会员一项非独占、不可转让、不可转授权的普通许可,仅限于个人学习、研究之目的在线查阅知识库内容。

侵权追责:未经书面许可,任何单位或个人不得以任何形式复制、转载、摘编、镜像、汇编或以其他方式使用上述内容。一经发现,我们保留追究其法律责任的权利。

五、 个人信息保护

我们重视对您个人信息的保护。关于我们如何收集、使用、存储和保护您的个人信息,请单独阅读 《隐私政策》。

您通过微信公众号授权或手机号验证所提供的信息,我们将严格按照《个人信息保护法》的规定处理,仅用于身份识别、服务提供及安全验证等必要用途。

您可以随时通过网站设置或联系客服行使查阅、更正、删除个人信息及撤回授权同意的权利。

六、 免责声明

内容准确性:知识库内容仅供参考,不构成专业建议。我们不对其完整性、准确性、时效性作任何明示或暗示的保证,您应自行判断并承担使用风险。

不可抗力:因自然灾害、政策法规变化、网络故障、第三方平台接口异常(如微信接口维护、运营商短信通道故障)等不可抗力导致的服务中断或延迟,我们不承担违约责任。

第三方链接:本网站可能包含指向第三方网站的链接,该等网站的内容和服务不受我们控制,请您自行甄别风险。

七、 违约责任

如您违反本协议约定,我们有权视情节采取警告、限制功能、暂停服务、注销账号等措施,并保留要求赔偿损失的权利。

如因您的违约行为导致我们遭受行政处罚、第三方索赔或商誉损失,您应承担全部赔偿责任(包括但不限于罚款、赔偿金、律师费、公证费等)。

八、 法律适用与争议解决

本协议的订立、执行和解释均适用中华人民共和国大陆地区法律。

因本协议产生的或与本协议有关的任何争议,双方应友好协商解决;协商不成的,任何一方均可向【公司所在地】有管辖权的人民法院提起诉讼。

九、 其他

本协议构成双方就本服务达成的完整协议,取代此前任何口头或书面约定。

本协议任一条款被认定为无效或不可执行的,不影响其他条款的效力。

我们对本协议享有最终解释权,并在法律允许的范围内保留随时修改的权利。修改后的协议一经公布即生效,继续使用服务即视为同意修订内容。


已查阅