Skip to content


MemEngine:用于开发基于 LLM 的智能体高级记忆的统一模块化库

Abstract

Recently, large language model based (LLM-based) agents have been widely applied across various fields. As a critical part, their memory capabilities have captured significant interest from both industrial and academic communities. Despite the proposal of many advanced memory models in recent research, however, there remains a lack of unified implementations under a general framework. To address this issue, we develop a unified and modular library for developing advanced memory models of LLM-based agents, called MemEngine. Based on our framework, we implement abundant memory models from recent research works. Additionally, our library facilitates convenient and extensible memory development, and offers user-friendly and pluggable memory usage.

近年来,基于大语言模型(LLM)的智能体已广泛应用于各个领域。 作为其中的关键部分,其记忆能力引起了工业界和学术界的广泛关注。 然而,尽管近期研究提出了许多高级记忆模型,但仍然缺少在通用框架下的统一实现。 为解决这一问题,我们开发了一个用于构建基于 LLM 的智能体高级记忆模型的统一模块化库,称为 MemEngine。 基于我们的框架,我们实现了近期研究工作中的大量记忆模型。 此外,我们的库支持便捷且可扩展的记忆开发,并提供用户友好且可插拔的记忆使用方式。

1. Introduction

With the rapid development of large language models (LLMs), LLM-based agents have been widely applied across various fields, due to their capabilities to perform complex tasks and fulfill different roles. Among various internal modules, memory is one of the most critical components for agents, as it determines how they store historical data, reflect on existing knowledge, and recall useful information to support decision-making. Specifically, in complex tasks, memory enables the recording of critical information in the past agent-environment interactions, and provides task-related experiences from previous trajectories. In role-playing and social simulations, it highlights the characteristic and personality of each role, allowing for distinctiveness among different roles.

随着大语言模型(LLM)的快速发展,基于 LLM 的智能体凭借执行复杂任务和承担不同角色的能力,已广泛应用于各个领域。 在各种内部模块中,记忆是智能体最关键的组件之一,因为它决定智能体如何存储历史数据、反思现有知识并召回有用信息来支持决策。 具体而言,在复杂任务中,记忆可以记录过去智能体与环境交互中的关键信息,并从先前轨迹中提供与任务相关的经验。 在角色扮演和社会模拟中,记忆突出每个角色的特征与个性,使不同角色彼此区别开来。

Although some recent works have proposed various memory models for LLM-based agents, they are implemented under different pipelines and lack a unified framework. This inconsistency presents challenges for developers to attempt different models in their experiments. Moreover, many basic functions (such as retrieval and summarization) are duplicated across different models, and researchers often need to implement them repeatedly when developing new models. Besides, many academic models are tightly integrated with agents in a non-pluggable manner, making them difficult to apply across different agents.

尽管近期一些工作为基于 LLM 的智能体提出了不同的记忆模型,但这些模型采用不同流程实现,并且缺少统一框架。 这种不一致使开发者难以在实验中尝试不同模型。 此外,许多基本函数(如检索和总结)在不同模型中重复出现,研究人员开发新模型时往往需要反复实现它们。 同时,许多学术模型以不可插拔的方式与智能体紧密集成,因而难以应用于不同智能体。

In order to address the above problems, we develop a unified and modular library named MemEngine, which facilitates the development of advanced memory models for LLM-based agents. The primary features of our library are summarized as follows:

为解决上述问题,我们开发了名为 MemEngine 的统一模块化库,以支持基于 LLM 的智能体高级记忆模型的开发。 我们的库具有以下主要特性:

Unified and Modular Memory Framework. We propose a unified memory framework composed of three hierarchical levels to organize and implement existing research models under a general structure. The lowest level comprises memory functions, implementing basic functions (e.g., retrieval) as foundational supports for different memory operations. The intermediate level encompasses memory operations, constituting basic operations (e.g., memory recall) to construct different memory models. The highest level involves memory models, implementing various existing research models (e.g., MemoryBank) that can be conveniently applied in different agents. All these three levels are modularized inside our framework, where higher-level modules can reuse lower-level modules, thereby improving efficiency and consistency in implementation. Besides, we provide a configuration module for easy modification of hyper-parameters and prompts at different levels. We also implement a utility module to conveniently save and demonstrate memory contents.

统一模块化记忆框架。 我们提出了一个由三个层级组成的统一记忆框架,在通用结构下组织和实现现有研究模型。 最底层由记忆函数组成,它实现基本函数(例如检索),为不同记忆操作提供基础支持。 中间层包含记忆操作,它构成用于建立不同记忆模型的基本操作(例如记忆召回)。 最高层是记忆模型,它实现了可便捷应用于不同智能体的各种现有研究模型(例如 MemoryBank)。 这三个层级在我们的框架内均采用模块化设计,高层模块可以复用低层模块,从而提升实现效率和一致性。 此外,我们提供配置模块,以便修改不同层级的超参数和提示词。 我们还实现了一个实用工具模块,用于便捷地保存和展示记忆内容。

Abundant Memory Implementation. Based on our unified and modular framework, we implement a wide range of memory models from recent research works, many of which are widely applied in diverse applications. All of these models can be easily switched and tested under our framework, with different configurations of hyper-parameters and prompts that can be adjusted for better application across various agents and tasks.

丰富的记忆实现。 基于我们的统一模块化框架,我们实现了近期研究工作中的多种记忆模型,其中许多模型已广泛用于不同应用。 所有这些模型都可以在我们的框架下轻松切换和测试,并可调整不同的超参数与提示词配置,以更好地应用于各种智能体和任务。

Convenient and Extensible Memory Development. Based on our modular memory operations and memory functions, researchers can conveniently develop their own advanced memory models. They can also extend existing operations and functions to develop their own ones. To better support researchers' development, we provide detailed instructions and examples in our document to guide the customization.

便捷且可扩展的记忆开发。 基于模块化的记忆操作和记忆函数,研究人员可以便捷地开发自己的高级记忆模型。 他们也可以扩展现有操作和函数,以开发自己的模块。 为更好地支持研究人员进行开发,我们在文档中提供了详细说明和示例来指导定制过程。

Pluggable and User-friendly Memory Usage. Our library offers multiple deployment options to empower LLM-based agent with powerful memory capabilities. Besides, we provide various memory usage modes, including default, configurable, and automatic modes. Moreover, our memory modules are pluggable and can be easily utilized across different agent frameworks. Our library is also compatible with some prominent frameworks of LLM-based agents, such as AutoGPT. These features collectively contribute to making our library more user-friendly.

可插拔且用户友好的记忆使用方式。 我们的库提供多种部署选项,为基于 LLM 的智能体赋予强大的记忆能力。 此外,我们提供包括默认、可配置和自动模式在内的多种记忆使用模式。 同时,我们的记忆模块支持插拔,可以轻松用于不同的智能体框架。 我们的库还与 AutoGPT 等一些主流的 LLM 智能体框架兼容。 这些特性共同提升了库的用户友好性。

In summary, MemEngine is the first library that implements a wide variety of memory models from research works under a unified and modular framework, facilitating both convenient development and ease of use. To further benefit the community, we have made our project publicly available at Github repository. Additionally, we have also organized a comprehensive documentation for both application and development purposes.

总而言之,MemEngine 是首个在统一模块化框架下实现多种研究记忆模型的库,既便于开发,也易于使用。 为进一步使社区受益,我们已在 GitHub 仓库中公开项目。 此外,我们还整理了面向应用和开发的完整文档。

2. Comparison with Relevant Libraries

Several existing libraries can also empower memory capabilities for LLM-based agents, including (1) memory modules integrated into agent libraries, and (2) independent memory libraries. Specifically, AutoGen, MetaGPT, CAMEL, AgentScope, LangChain, AgentLite, CrewAI, AutoGPT, and AgentVerse are prominent open-source libraries for building LLM-based AI agent systems. Besides, Memary is an open-source library to empower AI agents with memory for continuous improvement. Cognee provides a scalable and modular pipeline to interconnect and retrieve previous information for AI applications. Mem0 offers an artificial memory layer for LLM-based agents and assistants to make them personalized. Agentmemory implements easy-to-use memory for LLM-based agents with document search and more.

现有的一些库也可以为基于 LLM 的智能体提供记忆能力,包括:(1)集成在智能体库中的记忆模块;(2)独立记忆库。 具体而言,AutoGen、MetaGPT、CAMEL、AgentScope、LangChain、AgentLite、CrewAI、AutoGPT 和 AgentVerse 是用于构建基于 LLM 的 AI 智能体系统的主流开源库。 此外,Memary 是一个开源库,它通过记忆赋能 AI 智能体,使其持续改进。 Cognee 为 AI 应用提供可扩展且模块化的流程,用于关联和检索先前信息。 Mem0 为基于 LLM 的智能体和助手提供人工记忆层,使其实现个性化。 Agentmemory 为基于 LLM 的智能体实现了易用的记忆,支持文档搜索等功能。

We present a comprehensive comparison between MemEngine and relevant libraries in Table 1. While most of these libraries offer plug-and-play memory components to store and recall information for LLM-based agents, few of them implement advanced memory operations like reflection and optimization. Moreover, compared with other libraries, as a major contribution, MemEngine implements comprehensive research models under a unified framework, along with providing modular operations and functions that assist researchers in customizing advanced memory models.

我们在表1中对 MemEngine 与相关库进行了全面比较。 尽管这些库大多提供即插即用的记忆组件,为基于 LLM 的智能体存储和召回信息,但很少有库实现反思和优化等高级记忆操作。 此外,与其他库相比,MemEngine 的一项主要贡献是在统一框架下实现完整的研究模型,同时提供模块化操作和函数,帮助研究人员定制高级记忆模型。

表1:与相关开源库的比较。我们同时关注集成在主流智能体库中的记忆模块和独立记忆库。除下列库之外,AutoGPT 和 AgentVerse 没有在其库中明确说明记忆支持;AutoGen 以扩展形式支持 MemGPT、Mem0 和 Zep。
Memory FeaturesMemory Integrated in Agent Libraries
AutoGenMetaGPTCAMELAgentScopeLangChainAgentLiteCrewAI
Plug-and-play Integration×
Basic Read and Write Support
Reflection and Optimization Support××××××
Comprehensive Default Models×××××××
Advanced Model Customization××××××
Memory FeaturesIndependent Memory Libraries
MemaryCogneeMem0AgentmemoryMemoryScopeZepMemEngine
(Ours)
Plug-and-play Integration
Basic Read and Write Support
Reflection and Optimization Support×××××
Comprehensive Default Models××××××
Advanced Model Customization×××××

3. MemEngine Library

3.1 Overview

The framework of our library is present in Figure 1. The lowest level implements basic functions as standard support. The intermediate level comprises memory operations for fundamental processes. The highest level features various memory models from previous research works. Additionally, we provide a configuration module and a utility module to facilitate convenient development and usage.

我们的库框架如图1所示。 最底层实现基本函数,作为标准支持。 中间层由用于基础流程的记忆操作组成。 最高层包含先前研究工作中的各种记忆模型。 此外,我们提供配置模块和实用工具模块,以支持便捷开发与使用。

Overview framework of the MemEngine library
图1:MemEngine 库的整体框架。

3.2 Memory Models

We implement a variety of memory models from recent research works under a general structure, allowing seamless switching among them. Specifically, these models are implemented with the interfaces including reset, store, recall, manage, and optimize.

我们在通用结构下实现了近期研究工作中的多种记忆模型,使它们可以无缝切换。 具体而言,这些模型通过 resetstorerecallmanageoptimize 等接口实现。

The implemented memory models are described as follows:

  • FUMemory (Full Memory): Naively concatenates all the information into a single string, also known as long-context memory.
  • LTMemory (Long-term Memory): Calculates semantic similarities with text embeddings to retrieve the most relevant information.
  • STMemory (Short-term Memory): Maintains the most recent information and concatenates them into a single string.
  • GAMemory (Generative Agents): A pioneer memory model with weighted retrieval combination and self-reflection mechanism.
  • MBMemory (MemoryBank): A multi-layered memory model with dynamic summarization and forgetting mechanism.
  • SCMemory (SCM): A self-controlled memory model that can recall minimal but necessary information for inference.
  • MGMemory (MemGPT): A hierarchical memory model that treats the memory system as an operation system.
  • RFMemory (Reflexion): A prominent memory model that can learn to memorize from previous trajectories by optimization.
  • MTMemory (MemTree): A dynamic memory model with a tree-structured semantic representation to organize information.

已实现的记忆模型如下:

  • FUMemory(完整记忆): 直接将所有信息连接成一个字符串,也称为长上下文记忆。
  • LTMemory(长期记忆): 使用文本嵌入计算语义相似度,以检索最相关的信息。
  • STMemory(短期记忆): 保留最近的信息,并将它们连接成一个字符串。
  • GAMemory(生成式智能体): 一种开创性的记忆模型,具有加权检索组合和自反思机制。
  • MBMemory(MemoryBank): 一种具有动态总结和遗忘机制的多层记忆模型。
  • SCMemory(SCM): 一种自我控制的记忆模型,可以召回推理所需的最少但必要的信息。
  • MGMemory(MemGPT): 一种将记忆系统视为操作系统的分层记忆模型。
  • RFMemory(Reflexion): 一种主流记忆模型,可以通过优化从先前轨迹中学习如何记忆。
  • MTMemory(MemTree): 一种使用树结构语义表示来组织信息的动态记忆模型。

All of these memory models are implemented by combining various memory operations, and we make some reasonable adaptations in their implementations. Further details can be found in our project documentation and source code.

所有这些记忆模型都是通过组合不同记忆操作实现的,我们也在实现中进行了一些合理调整。 更多细节可参阅我们的项目文档和源代码。

3.3 Memory Operations

We implement various types of memory operations for constructing memory models, including store, recall, manage, and optimize.

我们实现了用于构建记忆模型的多种记忆操作,包括存储、召回、管理和优化。

Memory Store Operation intends to receive observations from the environment, processing them to obtain memory contents and adding them into memory storage. Another critical function of the memory store operation is to establish foundations for the memory recall operation, such as creating indexes and summaries.

记忆存储操作 旨在接收来自环境的观察,对其进行处理以获得记忆内容,并将其加入记忆存储。 记忆存储操作的另一项关键功能是为记忆召回操作建立基础,例如创建索引和摘要。

Memory Recall Operation intends to obtain useful information to assist agents in their decision-making. Typically, the input is a query or observation representing the current state of agents. Some human-like agents may also endow the memory recall operation with certain retention characteristics like human memory.

记忆召回操作 旨在获得有用信息,以辅助智能体进行决策。 通常,其输入是表示智能体当前状态的查询或观察。 一些类人智能体还可能为记忆召回操作赋予类似人类记忆的某些保留特性。

Memory Manage Operation intends to reorganize existing information for better utilization, such as memory reflection. Besides, simulation-orientated agents may be equipped with a forgetting mechanism during the memory manage operation.

记忆管理操作 旨在重新组织现有信息,以便更好地利用,例如进行记忆反思。 此外,面向模拟的智能体可能在记忆管理操作中配备遗忘机制。

Memory Optimize Operation intends to optimize the memory capability of LLM-based agents by using extra trials and trajectories. It enables agents to extract meta-insight from historical experiences, which can be considered as a learn-to-memorize procedure.

记忆优化操作 旨在利用额外的试验和轨迹来优化基于 LLM 的智能体记忆能力。 它使智能体能够从历史经验中提取元洞见,这可以被视为一种学习如何记忆的过程。

Different memory models may share common memory operations or implement their unique operations according to their requirements. For example, MTMemory and LTMemory share the common memory recall operation LTMemoryRecall, while MTMemory has its own memory store operation MTMemoryStore to implement the tree-structured information update.

不同记忆模型可能共享通用记忆操作,也可能根据需求实现各自独有的操作。 例如,MTMemoryLTMemory 共享通用记忆召回操作 LTMemoryRecall,而 MTMemory 拥有自己的记忆存储操作 MTMemoryStore,用于实现树结构信息更新。

3.4 Memory Functions

We implement various types of memory functions to support the construction of memory operations, which are listed as follows.

我们实现了用于支持记忆操作构建的多种记忆函数,具体如下。

Encoder can transfer textual messages into embeddings to represent in latent space by pre-trained models, such as E5.

编码器 可以使用 E5 等预训练模型将文本消息转换为嵌入,以在潜在空间中表示这些消息。

Retrieval is utilized to find the most useful information for the current query or observation, commonly by different aspects like semantic relevance, importance, recency, and so on.

检索 用于根据语义相关性、重要性和时近性等不同方面,为当前查询或观察查找最有用的信息。

Reflector aims to draw new insights in a higher level from existing information, commonly for reflection and optimization.

反思器 旨在从现有信息中提取更高层次的新洞见,通常用于反思和优化。

Summarizer can summarize texts into a brief summary, which can decrease the lengths of texts and emphasize critical points.

总结器 可以将文本概括为简短摘要,从而缩短文本长度并突出要点。

Trigger is designed to call functions or tools in extensible manners. One significant instance is utilizing LLMs to determine which function should be called with certain arguments.

触发器 旨在以可扩展的方式调用函数或工具。 一个重要示例是利用 LLM 确定应使用特定参数调用哪个函数。

Utilization aims to deal with several different parts of memory contents, formulating this information into a unified output.

利用器 旨在处理记忆内容中的多个不同部分,并将这些信息组织成统一输出。

Forget is typically applied in simulation-oriented agents, such as role-playing and social simulations. It empowers agents with features of human cognitive psychology, aligning with human roles.

遗忘器 通常用于角色扮演和社会模拟等面向模拟的智能体。 它赋予智能体人类认知心理特征,使其与人类角色保持一致。

Truncation helps to formulate memory contexts under the limitations of token numbers by certain LLMs.

截断器 有助于在某些 LLM 的 token 数量限制下组织记忆上下文。

Judge intends to assess given observations or intermediate messages on certain aspects. For example, GAMemory judges the importance score of each observation when stored into memory, as an auxiliary criterion for the retrieval process.

评判器 旨在从特定方面评估给定观察或中间消息。 例如,GAMemory 在将每条观察存入记忆时评判其重要性分数,并将该分数作为检索过程的辅助标准。

LLM provides a convenient interface to utilize the powerful capability of different large language models.

LLM 提供了一个便捷接口,用于调用不同大语言模型的强大能力。

All these memory functions are designed to conveniently construct different memory operations for various models. For example, GAMemoryStore utilizes LLMJudge to provide the importance score on each observation.

所有这些记忆函数都用于便捷地为不同模型构建各种记忆操作。 例如,GAMemoryStore 使用 LLMJudge 为每条观察提供重要性分数。

3.5 Memory Configurations

In order to improve convenience for developers and facilitate parameter tuning by researchers, we have developed a unified memory configuration module. First, we design a hierarchical memory configuration module corresponding to our three-level memory framework, enabling adjustments to both hyper-parameters and prompts within the memory models. Second, we provide a comprehensive set of default hyper-parameters and prompts, where developers and researchers can adjust only the specific parts without altering others. Finally, our configuration supports both statistic manners (e.g., files) and dynamic manners (e.g., dictionaries).

为提高开发者使用的便利性,并方便研究人员调节参数,我们开发了统一的记忆配置模块。 首先,我们设计了与三层记忆框架对应的分层记忆配置模块,使记忆模型中的超参数和提示词均可调整。 其次,我们提供一套完整的默认超参数和提示词,开发者和研究人员只需调整特定部分,而不必改动其他内容。 最后,我们的配置既支持静态方式(例如文件),也支持动态方式(例如字典)。

3.6 Memory Utilities

We implement extra utilities as auxiliary components, which are loosely coupled with the above modules. We implement a storage module as a database to retain the contents of information. We also provide a display module to visualize the specific contents within the memory. Besides, we offer a client module to utilize MemEngine through remote deployment on a server implemented by FastAPI. We also implement an automatic selector to assist developers to choose memory models with hyper-parameters for their own tasks.

我们实现了额外的实用工具作为辅助组件,它们与上述模块松耦合。 我们实现了一个充当数据库的存储模块,用于保留信息内容。 我们还提供展示模块,用于可视化记忆中的具体内容。 此外,我们提供客户端模块,以便通过 FastAPI 实现的服务器远程部署来使用 MemEngine。 我们还实现了自动选择器,帮助开发者为自己的任务选择记忆模型及其超参数。

4. Usage of MemEngine

In this section, we describe the usage of MemEngine to empower LLM-based agents with advanced memory capabilities. We divide our usage into two aspects: (1) utilize pre-implemented memory models, and (2) customize new memory models.

本节介绍如何使用 MemEngine 为基于 LLM 的智能体赋予高级记忆能力。 我们将使用方式分为两个方面:(1)使用预先实现的记忆模型;(2)定制新的记忆模型。

4.1 Utilize Pre-implemented Memory Models

There are two primary ways to deploy our library as follows.

我们的库主要有以下两种部署方式。

Local Deployment. Developers can easily install our library in their Python environment via pip, conda, or from source code. Then, they can create memory modules for their agents, and utilize unified interfaces to perform memory operations within programs.

本地部署。 开发者可以通过 pip、conda 或源代码,轻松地在 Python 环境中安装我们的库。 随后,他们可以为智能体创建记忆模块,并使用统一接口在程序中执行记忆操作。

Remote Deployment. Alternatively, developers can install our library on computing servers and launch the service through a port. Then, they can initiate a client to perform memory operations by sending HTTP requests remotely from their lightweight devices.

远程部署。 或者,开发者可以在计算服务器上安装我们的库,并通过端口启动服务。 随后,他们可以启动客户端,从轻量级设备远程发送 HTTP 请求来执行记忆操作。

After deployment, there are three modes available for utilizing pre-implemented memory models. In the default mode, the library provides a comprehensive set of hyper-parameters and prompts for default usage. In the configurable mode, developers can adjust certain hyper-parameters and prompts to better adapt to their applications. In the automatic mode, the library automatically selects the appropriate memory models, hyper-parameters, and prompts from the provided ranges, based on a specific task's criteria.

部署完成后,可以通过三种模式使用预先实现的记忆模型。 在默认模式下,库提供一套完整的超参数和提示词供默认使用。 在可配置模式下,开发者可以调整特定超参数和提示词,以更好地适配其应用。 在自动模式下,库根据特定任务的标准,从给定范围中自动选择合适的记忆模型、超参数和提示词。

Additionally, our library offers compatibility with several well-known tools and frameworks, such as vllm and AutoGPT.

此外,我们的库还兼容 vllm 和 AutoGPT 等一些知名工具与框架。

4.2 Customize New Memory Models

Our library provides support for developers to customize advanced memory models, offering comprehensive documentation and examples. There are three major aspects to customizing new models.

我们的库支持开发者定制高级记忆模型,并提供完整的文档和示例。 定制新模型主要包括三个方面。

Customize Memory Functions. Researchers may need to implement new functions in their models to extend existing ones for additional features. For example, they may extend LLMJudge to design a BiasJudge for poisoning detection.

定制记忆函数。 研究人员可能需要在模型中实现新函数,在现有函数基础上扩展额外特性。 例如,他们可以扩展 LLMJudge,设计用于投毒检测的 BiasJudge

Customize Memory Operations. In developing a new model, customizing memory operations is crucial as they constitute the major pipelines of the detailed processes. For instance, a new memory recall operation can be implemented with a series of memory functions with advanced design and combination.

定制记忆操作。 开发新模型时,定制记忆操作至关重要,因为它们构成了具体处理流程的主要管线。 例如,可以通过一系列经过高级设计与组合的记忆函数实现新的记忆召回操作。

Customize Memory Models. By integrating newly customized memory operations with existing ones, researchers can design their models with various combinations to best suit their applications.

定制记忆模型。 通过将新定制的记忆操作与现有操作集成,研究人员可以使用不同组合设计模型,使其最适合自己的应用。

5. Conclusion

In this paper, we introduce a unified and modular library for developing advanced memory of LLM-based agents. In the future, we plan to provide support for multi-modal memory (such as visual and audio memory) to further enrich and enhance the memory capabilities of LLM-based agents for wider applications.

本文介绍了一个用于开发基于 LLM 的智能体高级记忆的统一模块化库。 未来,我们计划支持多模态记忆(例如视觉记忆和音频记忆),进一步丰富和增强基于 LLM 的智能体记忆能力,使其适用于更广泛的应用。