Mem0:新一代AI Agent的持久化记忆体系

一、Mem0简介

Mem0 是一个轻量级、可扩展的长期记忆框架,支持本地部署和云端使用。其设计初衷是为 LLM 提供结构化的记忆支持,帮助智能体记住用户偏好、背景信息等,从而提供更个性化、更连贯的回答。

1. 基本特性

记忆处理:利用大型语言模型自动从对话中提取并存储关键信息,同时保持完整的上下文语境

记忆管理:持续更新存储信息并消除矛盾点,确保数据准确性

双重存储架构:结合向量数据库(用于记忆存储)和图数据库(用于关系追踪)的混合存储方案

智能检索系统:采用语义搜索与图查询技术,根据信息重要性和时效性检索相关记忆

便捷API集成:提供简单易用的记忆添加(add)与检索(search)接口端点

2. 安装方式

安装只需一行命令:

bash 复制代码
pip install mem0ai

二、初始化与基本操作

1. 初始化

python 复制代码
from mem0 import Memory
import os

os.environ["OPENAI_API_KEY"] = "your-api-key"  # 用于 embedding
m = Memory()

或者使用异步版本:

python 复制代码
from mem0 import AsyncMemory
m = AsyncMemory()

2. 添加记忆

添加记忆将利用大型语言模型自动从对话中提取并存储关键信息:

python 复制代码
messages = [
    {"role": "user", "content": "我今晚想看电影,有什么推荐?"},
]

m.add(messages, user_id="alice", metadata={"category": "电影推荐"})

也可关闭自动推理,不做关键信息的提取,直接保存原始对话:

python 复制代码
m.add(messages, user_id="alice", infer=False)

3. 检索记忆

● 获取所有记忆:

python 复制代码
all_memories = m.get_all(user_id="alice")

● 查询特定记忆:

python 复制代码
memory = m.get("memory_id")

● 搜索相关记忆:

python 复制代码
related = m.search("我喜欢什么电影?", user_id="alice")

4. 更新、删除与重置记忆

● 更新:

python 复制代码
m.update(memory_id="xxx", data="我爱看动作电影")

● 删除单条或全部记忆:

python 复制代码
m.delete("memory_id")
m.delete_all(user_id="alice")

● 重置所有记忆:

python 复制代码
m.reset()

5. 兼容OpenAI客户端的完整示例

Mem0 提供兼容 OpenAI 的 chat.completions.create()接口,方便无缝集成,每次调用都会自动添加记忆以及查找记忆

python 复制代码
from mem0.proxy.main import Mem0

client = Mem0(api_key="your-api-key")
messages = [{"role": "user", "content": "我喜欢印度菜,但不能吃奶酪。"}]
user_id = "alice"

# 第一次交互
client.chat.completions.create(messages=messages, model="gpt-4o-mini", user_id=user_id)

# 第二次交互将自动利用记忆生成更合理的答案
messages = [{"role": "user", "content": "推荐旧金山好吃的餐厅"}]
reply = client.chat.completions.create(messages=messages, model="gpt-4o-mini", user_id=user_id)
print(reply.choices[0].message.content)

输出示例中将结合用户"不能吃奶酪"的偏好,避免推荐含奶的餐厅。


三、核心概念

1. 两种主要的记忆类型

1.1 短期记忆

AI系统中最基础的记忆形式,用于存储即时上下文信息------类似于人类记住对话中刚刚提及的内容。主要包括:

对话历史:最近的交互消息及其顺序

工作记忆:临时变量和状态信息

注意力上下文:当前对话的焦点内容

1.2 长期记忆

更先进的AI应用会部署长期记忆系统,实现跨对话的信息留存。具体包含:

事实性记忆:关于用户特征、偏好及领域知识的存储

情景记忆:过往交互记录与经历

语义记忆:对概念及其关联关系的理解

Mem0通过以下方式构建长期记忆系统:

● 采用向量嵌入技术存储和检索语义信息

● 跨会话维持用户专属上下文

● 建立高效检索机制,精准调用相关历史交互

2. Mem0的两个主要接口

add:用于存储对话内容并转化为记忆

search:基于查询条件获取相关记忆

2.1 记忆写入流程

写入操作通过多阶段处理对话内容:

  1. 信息提取

● 大语言模型从对话中提取关键记忆

● 识别重要实体及其关联关系

  1. 冲突处理

● 系统比对新旧数据

● 自动检测并解决信息矛盾

  1. 记忆存储

● 向量数据库存储记忆本体

● 图数据库维护关系网络

● 每次交互后实时更新记忆库

2.2 记忆检索流程

检索操作通过智能分层处理实现精准召回:

  1. 查询优化

● 大语言模型解析并优化搜索语句

● 系统预置定向筛选条件

  1. 向量搜索

● 执行基于语义的向量相似度匹配

● 按关联度对结果排序

● 支持多维筛选(用户/代理/元数据等)

  1. 结果合成

● 聚合并优化搜索结果

● 返回带相关性评分的记忆内容

● 包含完整元数据及时间戳

四、其他特性

1. 完全兼容OpenAI规范

可以像操作OpenAI客户端一样的方式来操作Mem0客户端,接口完全通用:

python 复制代码
from mem0.proxy.main import Mem0

client = Mem0(api_key="m0-xxx")

# First interaction: Storing user preferences
messages = [
    {
        "role": "user",
        "content": "I love indian food but I cannot eat pizza since allergic to cheese."
    },
]
user_id = "alice"
chat_completion = client.chat.completions.create(
    messages=messages,
    model="gpt-4o-mini",
    user_id=user_id
)
# Memory saved after this will look like: "Loves Indian food. Allergic to cheese and cannot eat pizza."

# Second interaction: Leveraging stored memory
messages = [
    {
        "role": "user",
        "content": "Suggest restaurants in San Francisco to eat.",
    }
]

chat_completion = client.chat.completions.create(
    messages=messages,
    model="gpt-4o-mini",
    user_id=user_id
)
print(chat_completion.choices[0].message.content)
# Answer: You might enjoy Indian restaurants in San Francisco, such as Amber India, Dosa, or Curry Up Now, which offer delicious options without cheese.

如需自定义相关配置(如向量数据库信息),则可以在初始化Mem0客户端的时候加入config参数:

python 复制代码
config = {
    "vector_store": {
        "provider": "qdrant",
        "config": {
            "host": "localhost",
            "port": 6333,
        }
    },
}

client = Mem0(config=config)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "What's the capital of France?",
        }
    ],
    model="gpt-4o",
)

2. 提示词配置

用户可以根据实际应用自行修改或者配置以下两种提示词。

2.1 关键信息提取Prompt

关键信息提取允许根据特定使用场景或专业领域,定制Mem0的信息处理行为。通过定义该提示,可以精确控制如何从用户消息中提取关键信息。

建议遵循以下原则

● 明确指定需要提取的信息类型

● 提供少量示例(few-shot)引导语言模型

● 确保示例遵循标准格式规范

以下是官方给出的示例:

python 复制代码
custom_fact_extraction_prompt = """
Please only extract entities containing customer support information, order details, and user information. 
Here are some few shot examples:

Input: Hi.
Output: {{"facts" : []}}

Input: The weather is nice today.
Output: {{"facts" : []}}

Input: My order #12345 hasn't arrived yet.
Output: {{"facts" : ["Order #12345 not received"]}}

Input: I'm John Doe, and I'd like to return the shoes I bought last week.
Output: {{"facts" : ["Customer name: John Doe", "Wants to return shoes", "Purchase made last week"]}}

Input: I ordered a red shirt, size medium, but received a blue one instead.
Output: {{"facts" : ["Ordered red shirt, size medium", "Received blue shirt instead"]}}

Return the facts and customer information in a json format as shown above.
"""

2.2 记忆更新Prompt

记忆更新是用于决定对记忆执行何种操作的控制指令。通过自定义该提示,可以精确控制记忆的更新机制。

Mem0会将新提取的事实与现有记忆进行比对,并自动决定采取以下操作之一:

新增(Add):将新事实添加至记忆库

更新(Update):用新事实修正现有记忆内容

删除(Delete):移除现有记忆条目

保持(No Change):不执行任何记忆变更

以下是官方给出的示例:

python 复制代码
UPDATE_MEMORY_PROMPT = """You are a smart memory manager which controls the memory of a system.
You can perform four operations: (1) add into the memory, (2) update the memory, (3) delete from the memory, and (4) no change.

Based on the above four operations, the memory will change.

Compare newly retrieved facts with the existing memory. For each new fact, decide whether to:
- ADD: Add it to the memory as a new element
- UPDATE: Update an existing memory element
- DELETE: Delete an existing memory element
- NONE: Make no change (if the fact is already present or irrelevant)

There are specific guidelines to select which operation to perform:

1. **Add**: If the retrieved facts contain new information not present in the memory, then you have to add it by generating a new ID in the id field.
- **Example**:
    - Old Memory:
        [
            {
                "id" : "0",
                "text" : "User is a software engineer"
            }
        ]
    - Retrieved facts: ["Name is John"]
    - New Memory:
        {
            "memory" : [
                {
                    "id" : "0",
                    "text" : "User is a software engineer",
                    "event" : "NONE"
                },
                {
                    "id" : "1",
                    "text" : "Name is John",
                    "event" : "ADD"
                }
            ]

        }

2. **Update**: If the retrieved facts contain information that is already present in the memory but the information is totally different, then you have to update it. 
If the retrieved fact contains information that conveys the same thing as the elements present in the memory, then you have to keep the fact which has the most information. 
Example (a) -- if the memory contains "User likes to play cricket" and the retrieved fact is "Loves to play cricket with friends", then update the memory with the retrieved facts.
Example (b) -- if the memory contains "Likes cheese pizza" and the retrieved fact is "Loves cheese pizza", then you do not need to update it because they convey the same information.
If the direction is to update the memory, then you have to update it.
Please keep in mind while updating you have to keep the same ID.
Please note to return the IDs in the output from the input IDs only and do not generate any new ID.
- **Example**:
    - Old Memory:
        [
            {
                "id" : "0",
                "text" : "I really like cheese pizza"
            },
            {
                "id" : "1",
                "text" : "User is a software engineer"
            },
            {
                "id" : "2",
                "text" : "User likes to play cricket"
            }
        ]
    - Retrieved facts: ["Loves chicken pizza", "Loves to play cricket with friends"]
    - New Memory:
        {
        "memory" : [
                {
                    "id" : "0",
                    "text" : "Loves cheese and chicken pizza",
                    "event" : "UPDATE",
                    "old_memory" : "I really like cheese pizza"
                },
                {
                    "id" : "1",
                    "text" : "User is a software engineer",
                    "event" : "NONE"
                },
                {
                    "id" : "2",
                    "text" : "Loves to play cricket with friends",
                    "event" : "UPDATE",
                    "old_memory" : "User likes to play cricket"
                }
            ]
        }


3. **Delete**: If the retrieved facts contain information that contradicts the information present in the memory, then you have to delete it. Or if the direction is to delete the memory, then you have to delete it.
Please note to return the IDs in the output from the input IDs only and do not generate any new ID.
- **Example**:
    - Old Memory:
        [
            {
                "id" : "0",
                "text" : "Name is John"
            },
            {
                "id" : "1",
                "text" : "Loves cheese pizza"
            }
        ]
    - Retrieved facts: ["Dislikes cheese pizza"]
    - New Memory:
        {
        "memory" : [
                {
                    "id" : "0",
                    "text" : "Name is John",
                    "event" : "NONE"
                },
                {
                    "id" : "1",
                    "text" : "Loves cheese pizza",
                    "event" : "DELETE"
                }
        ]
        }

4. **No Change**: If the retrieved facts contain information that is already present in the memory, then you do not need to make any changes.
- **Example**:
    - Old Memory:
        [
            {
                "id" : "0",
                "text" : "Name is John"
            },
            {
                "id" : "1",
                "text" : "Loves cheese pizza"
            }
        ]
    - Retrieved facts: ["Name is John"]
    - New Memory:
        {
        "memory" : [
                {
                    "id" : "0",
                    "text" : "Name is John",
                    "event" : "NONE"
                },
                {
                    "id" : "1",
                    "text" : "Loves cheese pizza",
                    "event" : "NONE"
                }
            ]
        }
"""

五、总结

Mem0 作为一个易用、灵活且功能强大的记忆增强框架,极大地扩展了大语言模型的上下文记忆能力。无论是构建常见的多轮对话机器人、智能搜索系统还是个性化推荐服务,Mem0 都是一个非常值得尝试的工具。

相关推荐
智能砖头2 分钟前
本地文档AI助手:基于LangChain和Qwen2.5的智能问答系统
人工智能·python
Alang3 分钟前
Mac Mini M4 16G 内存本地大模型性能横评:9 款模型实测对比
前端·llm·aigc
聚客AI2 小时前
🛫AI大模型训练到发布一条龙:Hugging Face终极工作流
人工智能·llm·掘金·日新计划
ZackSock2 小时前
自己开发 MCP 服务器
llm·ollama·mcp
新智元4 小时前
刚刚,谷歌 AI 路线图曝光:竟要抛弃注意力机制?Transformer 有致命缺陷!
人工智能·openai
Maynor9964 小时前
我是如何使用Claude Code
人工智能
知舟不叙4 小时前
基于OpenCV的图像增强技术:直方图均衡化与自适应直方图均衡化
人工智能·opencv·计算机视觉·图像增强
speop4 小时前
【datawhale组队学习】共读AI新圣经
人工智能·学习
Blossom.1184 小时前
基于深度学习的智能图像增强技术:原理、实现与应用
人工智能·python·深度学习·神经网络·机器学习·tensorflow·sklearn
moonsims5 小时前
高开放性具身智能AIBOX平台—专为高校实验室与科研项目打造的边缘计算基座(让高校和科研院所聚焦核心算法)
人工智能