Skip to main content

如何迁移到 LangGraph 内存

¥How to migrate to LangGraph memory

从 LangChain v0.3 版本开始,我们建议 LangChain 用户利用 LangGraph 持久性将 memory 集成到他们的 LangChain 应用中。

¥As of the v0.3 release of LangChain, we recommend that LangChain users take advantage of LangGraph persistence to incorporate memory into their LangChain application.

  • 依赖 RunnableWithMessageHistoryBaseChatMessageHistory 的用户无需进行任何更改,但建议在更复杂的用例中考虑使用 LangGraph。

    ¥Users that rely on RunnableWithMessageHistory or BaseChatMessageHistory do not need to make any changes, but are encouraged to consider using LangGraph for more complex use cases.

  • 依赖 LangChain 0.0.x 中已弃用的内存抽象的用户应遵循本指南升级到 LangChain 0.3.x 中新的 LangGraph 持久性功能。

    ¥Users that rely on deprecated memory abstractions from LangChain 0.0.x should follow this guide to upgrade to the new LangGraph persistence feature in LangChain 0.3.x.

为什么使用 LangGraph 来存储内存?

¥Why use LangGraph for memory?

LangGraph 持久化的主要优势在于:

¥The main advantages of persistence in LangGraph are:

  • 内置对多用户和多对话的支持,这是现实世界对话式 AI 应用的典型要求。

    ¥Built-in support for multiple users and conversations, which is a typical requirement for real-world conversational AI applications.

  • 能够随时保存和恢复复杂的对话。这有助于:

    ¥Ability to save and resume complex conversations at any point. This helps with:

    • 错误恢复

      ¥Error recovery

    • 允许人工干预 AI 工作流

      ¥Allowing human intervention in AI workflows

    • 探索不同的对话路径 ("时间旅行")

      ¥Exploring different conversation paths ("time travel")

  • 与传统 语言模型 和现代 聊天模型 完全兼容。LangChain 早期的内存实现并非为较新的聊天模型 API 而设计,这导致工具调用等功能存在问题。LangGraph 内存可以持久化任何自定义状态。

    ¥Full compatibility with both traditional language models and modern chat models. Early memory implementations in LangChain weren't designed for newer chat model APIs, causing issues with features like tool-calling. LangGraph memory can persist any custom state.

  • 高度可定制,允许你完全控制内存的工作方式并使用不同的存储后端。

    ¥Highly customizable, allowing you to fully control how memory works and use different storage backends.

LangChain 中内存的演变

¥Evolution of memory in LangChain

自 LangChain 首次发布以来,其内存概念已得到显著发展。

¥The concept of memory has evolved significantly in LangChain since its initial release.

LangChain 0.0.x 内存

¥LangChain 0.0.x memory

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

¥Broadly speaking, LangChain 0.0.x memory was used to handle three main use cases:

Use CaseExample
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 系统至关重要。

¥While the LangChain 0.0.x memory abstractions were useful, they were limited in their capabilities and not well suited for real-world conversational AI applications. These memory abstractions lacked built-in support for multi-user, multi-conversation scenarios, which are essential for practical conversational AI systems.

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

¥Most of these implementations have been officially deprecated in LangChain 0.3.x in favor of LangGraph persistence.

RunnableWithMessageHistory 和 BaseChatMessageHistory

¥RunnableWithMessageHistory and BaseChatMessageHistory

note

如果你想在 LangGraph 中使用 BaseChatMessageHistory(无论是否使用 RunnableWithMessageHistory),请参阅 如何在 LangGraph 中使用 BaseChatMessageHistory

¥Please see How to use BaseChatMessageHistory with LangGraph, if you would like to use BaseChatMessageHistory (with or without RunnableWithMessageHistory) in LangGraph.

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

¥As of LangChain v0.1, we started recommending that users rely primarily on BaseChatMessageHistory. BaseChatMessageHistory serves as a simple persistence for storing and retrieving messages in a conversation.

当时,编排 LangChain 链的唯一选项是通过 LCEL。要将内存与 LCEL 合并,用户必须使用 RunnableWithMessageHistory 接口。虽然该 API 对于基本的聊天应用来说已经足够,但许多用户发现它不够直观,使用起来也很困难。

¥At that time, the only option for orchestrating LangChain chains was via LCEL. To incorporate memory with LCEL, users had to use the RunnableWithMessageHistory interface. While sufficient for basic chat applications, many users found the API unintuitive and challenging to use.

从 LangChain v0.3 开始,我们建议新代码利用 LangGraph 进行编排和持久化:

¥As of LangChain v0.3, we recommend that new code takes advantage of LangGraph for both orchestration and persistence:

  • 编排:在 LangGraph 中,用户定义 graphs 来指定应用的流程。这允许用户在需要 LCEL 时,在各个节点内继续使用 LCEL,同时轻松定义更易读、更易维护的复杂编排逻辑。

    ¥Orchestration: In LangGraph, users define graphs that specify the flow of the application. This allows users to keep using LCEL within individual nodes when LCEL is needed, while making it easy to define complex orchestration logic that is more readable and maintainable.

  • 持久性:用户可以依赖 LangGraph 的持久性来存储和检索数据。LangGraph 持久化功能极其灵活,可以支持比 RunnableWithMessageHistory 接口更广泛的用例。

    ¥Persistence: Users can rely on LangGraph's persistence to store and retrieve data. LangGraph persistence is extremely flexible and can support a much wider range of use cases than the RunnableWithMessageHistory interface.

info

如果你一直在使用 RunnableWithMessageHistoryBaseChatMessageHistory,则无需进行任何更改。我们近期不打算弃用任何一项功能。此功能对于简单的聊天应用来说已经足够,任何使用 RunnableWithMessageHistory 的代码都将继续按预期工作。

¥If you have been using RunnableWithMessageHistory or BaseChatMessageHistory, you do not need to make any changes. We do not plan on deprecating either functionality in the near future. This functionality is sufficient for simple chat applications and any code that uses RunnableWithMessageHistory will continue to work as expected.

迁移

¥Migrations

Prerequisites

这些指南假设读者熟悉以下概念:

¥These guides assume some familiarity with the following concepts:

1. 管理对话历史记录

¥ Managing conversation history

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

¥The goal of managing conversation history is to store and retrieve the history in a way that is optimal for a chat model to use.

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

¥Often this involves trimming and / or summarizing the conversation history to keep the most relevant parts of the conversation while having the conversation fit inside the context window of the chat model.

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

¥Memory classes that fall into this category include:

Memory TypeHow to MigrateDescription
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. 从对话历史记录中提取结构化信息

¥ Extraction of structured information from the conversation history

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

¥Memory classes that fall into this category include:

Memory TypeDescription
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.

抽象的具体后端实现:

¥And specific backend implementations of abstractions:

Memory TypeDescription
InMemoryEntityStoreAn implementation of BaseEntityStore that stores the information in the literal computer memory (RAM).

自首次发布以来,这些抽象并没有得到太多开发。原因是,这些抽象要发挥作用,通常需要针对特定应用进行大量的专业化,因此这些抽象不像对话历史管理抽象那样被广泛使用。

¥These abstractions have not received much development since their initial release. The reason is that for these abstractions to be useful they typically require a lot of specialization for a particular application, so these abstractions are not as widely used as the conversation history management abstractions.

因此,这些抽象没有迁移指南。如果你在迁移依赖于这些抽象的应用时遇到困难,请在 LangChain GitHub 代码库上提交一个问题,解释你的用例,我们将尽力提供更多关于如何迁移这些抽象的指导。

¥For this reason, there are no migration guides for these abstractions. If you're struggling to migrate an application that relies on these abstractions, please pen an issue on the LangChain GitHub repository, explain your use case, and we'll try to provide more guidance on how to migrate these abstractions.

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

¥The general strategy for extracting structured information from the conversation history is to use a chat model with tool calling capabilities to extract structured information from the conversation history. The extracted information can then be saved into an appropriate data structure (e.g., an object), and information from it can be retrieved and added into the prompt as needed.

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

¥ Implementations that provide composite logic on top of one or more memory implementations

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

¥Memory classes that fall into this category include:

Memory TypeDescription
CombinedMemoryThis abstraction accepted a list of BaseMemory and fetched relevant memory information from each of them based on the input.

这些实现似乎并未得到广泛使用,也没有提供显著的价值。用户应该能够在自定义代码中轻松重新实现这些功能。

¥These implementations did not seem to be used widely or provide significant value. Users should be able to re-implement these without too much difficulty in custom code.

¥Related Resources

使用 LangGraph 探索持久性:

¥Explore persistence with LangGraph:

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

¥Add persistence with simple LCEL (favor langgraph for more complex use cases):

使用消息历史记录:

¥Working with message history: