Skip to main content

如何迁移到 LangGraph 内存

从 LangChain v0.3 版本开始,我们建议 LangChain 用户利用 LangGraph 持久化来整合memory添加到他们的 LangChain 应用程序中。

  • 依赖RunnableWithMessageHistoryBaseChatMessageHistory不需要进行任何更改,但建议考虑将 LangGraph 用于更复杂的用例。
  • 依赖 LangChain 0.0.x 中已弃用的内存抽象的用户应按照本指南升级到 LangChain 0.3.x 中新的 LangGraph 持久化功能。

为什么使用 LangGraph 进行内存处理?

LangGraph 持久化的主要优点是:

  • 对多个用户和对话的内置支持,这是实际对话式 AI 应用程序的典型要求。
  • 能够随时保存和恢复复杂的对话。这有助于:
    • 错误恢复
    • 允许人工干预 AI 工作流
    • 探索不同的对话路径(“时间旅行”)
  • 与传统语言模型和现代聊天模型完全兼容。LangChain 中的早期内存实现不是为较新的聊天模型 API 设计的,这会导致工具调用等功能出现问题。LangGraph 内存可以持久化任何自定义状态。
  • 高度可定制,允许您完全控制内存的工作方式并使用不同的存储后端。

LangChain 中内存的演变

自首次发布以来,内存的概念在 LangChain 中发生了重大变化。

LangChain 0.0.x 内存

从广义上讲,LangChain 0.0.x 内存用于处理三个主要用例:

用例
Managing conversation historyKeep only the last n turns of the conversation between the user and the AI.
Extraction of structured informationExtract structured information from the conversation history, such as a list of facts learned about the user.
Composite memory implementationsCombine multiple memory sources, e.g., a list of known facts about the user along with facts learned during a given conversation.

虽然 LangChain 0.0.x 内存抽象很有用,但它们的功能有限,不太适合现实世界的对话式 AI 应用程序。这些内存抽象缺乏对多用户、多对话场景的内置支持,而这对于实际的对话式 AI 系统至关重要。

这些实现中的大多数已在 LangChain 0.3.x 中被正式弃用,以支持 LangGraph 持久化。

RunnableWithMessageHistory 和 BaseChatMessageHistory

注意

如果您想使用 BaseChatMessageHistory 和 LangGraph,请参阅 How to use BaseChatMessageHistory with LangGraphBaseChatMessageHistory(带或不带RunnableWithMessageHistory) 中。

从 LangChain v0.1 开始,我们开始建议用户主要依赖 BaseChatMessageHistoryBaseChatMessageHistory服务 作为在对话中存储和检索消息的简单持久性。

当时,编排 LangChain 链的唯一选择是通过 LCEL。要将内存与LCEL,用户必须使用 RunnableWithMessageHistory 接口。虽然对于基本的聊天应用程序来说已经足够了,但许多用户发现 API 不直观且难以使用。

从 LangChain v0.3 开始,我们建议代码将 LangGraph 用于编排和持久化:

  • 编排:在 LangGraph 中,用户定义指定应用程序流程的图形。这允许用户继续使用LCEL在单个节点中,当LCEL,同时可以轻松定义更具可读性和可维护性的复杂编排逻辑。
  • 持久化:用户可以依靠 LangGraph 的持久化来存储和检索数据。LangGraph 持久化非常灵活,可以支持比RunnableWithMessageHistory接口。
信息

如果您一直在使用RunnableWithMessageHistoryBaseChatMessageHistory,则无需进行任何更改。我们不打算在不久的将来弃用任何一项功能。此功能对于简单的聊天应用程序和任何使用RunnableWithMessageHistory将继续按预期工作。

迁移

先决条件

1. 管理对话历史记录

管理对话历史记录的目标是以最适合聊天模型使用的方式存储和检索历史记录。

这通常涉及修剪和/或总结对话历史记录,以保留对话中最相关的部分,同时使对话适合聊天模型的上下文窗口。

属于此类别的内存类包括:

内存类型如何迁移描述
ConversationTokenBufferMemoryLink to Migration GuideKeeps only the most recent messages in the conversation under the constraint that the total number of tokens in the conversation does not exceed a certain limit.
ConversationSummaryMemoryLink to Migration GuideContinually summarizes the conversation history. The summary is updated after each conversation turn. The abstraction returns the summary of the conversation history.
ConversationSummaryBufferMemoryLink to Migration GuideProvides a running summary of the conversation together with the most recent messages in the conversation under the constraint that the total number of tokens in the conversation does not exceed a certain limit.

2. 从对话历史中提取结构化信息

属于此类别的内存类包括:

内存类型描述
BaseEntityStoreAn abstract interface that resembles a key-value store. It was used for storing structured information learned during the conversation. The information had to be represented as an object of key-value pairs.

以及抽象的特定后端实现:

内存类型描述
InMemoryEntityStoreAn implementation of BaseEntityStore that stores the information in the literal computer memory (RAM).

这些抽象自首次发布以来没有得到太多发展。原因 是要使这些抽象有用,它们通常需要对特定应用程序进行大量专业化,因此这些 抽象不像 Conversation History Management 抽象那样被广泛使用。

因此,没有针对这些抽象的迁移指南。如果您正在努力迁移应用程序 依赖于这些抽象,请在 LangChain GitHub 仓库上写下 issue,解释您的用例,我们将尝试提供有关如何迁移这些抽象的更多指导。

从对话历史记录中提取结构化信息的一般策略是使用具有工具调用功能的聊天模型从对话历史记录中提取结构化信息。 然后,可以将提取的信息保存到适当的数据结构(例如,对象)中,并且可以根据需要检索其中的信息并将其添加到提示中。

3. 在一个或多个内存实现之上提供复合逻辑的实现

属于此类别的内存类包括:

内存类型描述
CombinedMemoryThis abstraction accepted a list of BaseMemory and fetched relevant memory information from each of them based on the input.

这些实现似乎没有得到广泛使用或提供重要价值。用户应该能够 重新实现这些,而不会在自定义代码中遇到太大困难。

使用 LangGraph 探索持久化:

使用简单的 LCEL 添加持久性(对于更复杂的用例,首选 langgraph):

使用消息历史记录: