如何迁移到 LangGraph 内存
从 LangChain v0.3 版本开始,我们建议 LangChain 用户利用 LangGraph 持久化来整合memory添加到他们的 LangChain 应用程序中。
- 依赖
RunnableWithMessageHistory或BaseChatMessageHistory不需要进行任何更改,但建议考虑将 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 history | Keep only the last n turns of the conversation between the user and the AI. |
| Extraction of structured information | Extract structured information from the conversation history, such as a list of facts learned about the user. |
| Composite memory implementations | Combine 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 开始,我们开始建议用户主要依赖 BaseChatMessageHistory。BaseChatMessageHistory服务
作为在对话中存储和检索消息的简单持久性。
当时,编排 LangChain 链的唯一选择是通过 LCEL。要将内存与LCEL,用户必须使用 RunnableWithMessageHistory 接口。虽然对于基本的聊天应用程序来说已经足够了,但许多用户发现 API 不直观且难以使用。
从 LangChain v0.3 开始,我们建议新代码将 LangGraph 用于编排和持久化:
- 编排:在 LangGraph 中,用户定义指定应用程序流程的图形。这允许用户继续使用
LCEL在单个节点中,当LCEL,同时可以轻松定义更具可读性和可维护性的复杂编排逻辑。 - 持久化:用户可以依靠 LangGraph 的持久化来存储和检索数据。LangGraph 持久化非常灵活,可以支持比
RunnableWithMessageHistory接口。
如果您一直在使用RunnableWithMessageHistory或BaseChatMessageHistory,则无需进行任何更改。我们不打算在不久的将来弃用任何一项功能。此功能对于简单的聊天应用程序和任何使用RunnableWithMessageHistory将继续按预期工作。
迁移
这些指南假定您熟悉以下概念:
1. 管理对话历史记录
管理对话历史记录的目标是以最适合聊天模型使用的方式存储和检索历史记录。
这通常涉及修剪和/或总结对话历史记录,以保留对话中最相关的部分,同时使对话适合聊天模型的上下文窗口。
属于此类别的内存类包括:
| 内存类型 | 如何迁移 | 描述 |
|---|---|---|
ConversationBufferMemory | Link to Migration Guide | A basic memory implementation that simply stores the conversation history. |
ConversationStringBufferMemory | Link to Migration Guide | A special case of ConversationBufferMemory designed for LLMs and no longer relevant. |
ConversationBufferWindowMemory | Link to Migration Guide | Keeps the last n turns of the conversation. Drops the oldest turn when the buffer is full. |
ConversationTokenBufferMemory | Link to Migration Guide | Keeps 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. |
ConversationSummaryMemory | Link to Migration Guide | Continually summarizes the conversation history. The summary is updated after each conversation turn. The abstraction returns the summary of the conversation history. |
ConversationSummaryBufferMemory | Link to Migration Guide | Provides 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. |
VectorStoreRetrieverMemory | See related long-term memory agent tutorial | Stores the conversation history in a vector store and retrieves the most relevant parts of past conversation based on the input. |
2. 从对话历史中提取结构化信息
请参阅 long-term memory agent tutorial 实现可以从对话历史记录中提取结构化信息的代理。
属于此类别的内存类包括:
| 内存类型 | 描述 |
|---|---|
BaseEntityStore | An 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 a dictionary of key-value pairs. |
ConversationEntityMemory | Combines the ability to summarize the conversation while extracting structured information from the conversation history. |
以及抽象的特定后端实现:
| 内存类型 | 描述 |
|---|---|
InMemoryEntityStore | An implementation of BaseEntityStore that stores the information in the literal computer memory (RAM). |
RedisEntityStore | A specific implementation of BaseEntityStore that uses Redis as the backend. |
SQLiteEntityStore | A specific implementation of BaseEntityStore that uses SQLite as the backend. |
UpstashRedisEntityStore | A specific implementation of BaseEntityStore that uses Upstash as the backend. |
这些抽象自首次发布以来一直受到有限的开发。这是因为它们通常需要大量定制才能使特定应用程序有效,这使得 它们的使用不如 Conversation History Management 抽象。
因此,没有针对这些抽象的迁移指南。如果您正在努力迁移应用程序 依赖于这些抽象,请:
- 请查看此 Long-term memory agent 教程,该教程应该为如何从对话历史记录中提取结构化信息提供一个很好的起点。
- 如果您仍在苦苦挣扎,请在 LangChain GitHub 存储库上打开一个问题,解释您的用例,我们将尝试提供有关如何迁移这些抽象的更多指导。
从对话历史记录中提取结构化信息的一般策略是使用具有工具调用功能的聊天模型从对话历史记录中提取结构化信息。 然后,可以将提取的信息保存到适当的数据结构(例如,字典)中,并且可以根据需要检索其中的信息并将其添加到提示中。
3. 在一个或多个内存实现之上提供复合逻辑的实现
属于此类别的内存类包括:
| 内存类型 | 描述 |
|---|---|
CombinedMemory | This abstraction accepted a list of BaseMemory and fetched relevant memory information from each of them based on the input. |
SimpleMemory | Used to add read-only hard-coded context. Users can simply write this information into the prompt. |
ReadOnlySharedMemory | Provided a read-only view of an existing BaseMemory implementation. |
这些实现似乎没有得到广泛使用或提供重要价值。用户应该能够 重新实现这些,而不会在自定义代码中遇到太大困难。
相关资源
使用 LangGraph 探索持久化:
使用简单的 LCEL 添加持久性(对于更复杂的用例,首选 langgraph):
使用消息历史记录: