LangChain手册(JSTS版)12内存
Memory is the concept of storing and retrieving data in the process of a conversation. There are two main methods, loadMemoryVariables and saveContext. The first method is used to retrieve data from memory (optionally using the current input values), and the second method is used to store data in memory.
内存是在对话过程中存储和检索数据的概念。有两种主要方法, loadMemoryVariables 和 saveContext 。第一种方法用于从内存中检索数据(可选地使用当前输入值),第二种方法用于在内存中存储数据。
export type InputValues = Record<string, any>;export type OutputValues = Record<string, any>;interface BaseMemory { loadMemoryVariables(values: InputValues): Promise<MemoryVariables>; saveContext( inputValues: InputValues, outputValues: OutputValues ): Promise<void>;}
Do not share the same memory instance between two different chains, a memory instance represents the history of a single conversation
不要在两个不同的链之间共享相同的内存实例,内存实例表示单个会话的历史记录
If you deploy your LangChain app on a serverless environment do not store memory instances in a variable, as your hosting provider may have reset it by the next time the function is called.
如果您在无服务器环境中部署 LangChain 应用程序,请不要将内存实例存储在变量中,因为您的托管服务提供商可能在下次调用函数时重置它。
BufferMemory is the simplest type of memory - it just remembers previous conversational back and forths directly.
BufferMemory 是最简单的内存类型 - 它只是直接记住以前的对话来回。
This example covers how to use chat-specific memory classes with chat models.
此示例介绍如何将特定于聊天的内存类与聊天模型一起使用。
BufferWindowMemory keeps track of the back-and-forths in conversation, and then uses a window of size k to surface the last k back-and-forths to use as memory.
BufferWindowMemory 跟踪对话中的来回,然后使用大小为 k 的窗口显示最后 k 个来回以用作内存。
The Conversation Summary Memory summarizes the conversation as it happens and stores the current summary in memory. This memory can then be used to inject the summary of the conversation so far into a prompt/chain. This memory is most useful for longer conversations, where keeping the past message history in the prompt verbatim would take up too many tokens.
对话摘要记忆在对话发生时对其进行汇总,并将当前摘要存储在内存中。然后,可以使用此内存将到目前为止的对话摘要注入到提示/链中。此内存对于较长的对话最有用,其中将过去的消息历史记录逐字保留在提示符中会占用太多标记。
For longer-term persistence across chat sessions, you can swap out the default in-memory chatHistory that backs chat memory classes like BufferMemory for a DynamoDB instance.
为了跨聊天会话实现长期持久性,您可以换出默认的内存中聊天历史记录,该聊天历史记录支持 DynamoDB 实例的聊天内存类(如 BufferMemory)。
Entity Memory remembers given facts about specific entities in a conversation.
实体内存记住对话中有关特定实体的给定事实。
For distributed, serverless persistence across chat sessions, you can swap in a Momento-backed chat message history.
对于跨聊天会话的分布式无服务器持久性,您可以交换 Momento 支持的聊天消息历史记录。
Mot?rhead is a memory server implemented in Rust. It automatically handles incremental summarization in the background and allows for stateless applications.
Mot?rhead 是一个用 Rust 实现的内存服务器。它在后台自动处理增量汇总,并允许无状态应用程序。
For longer-term persistence across chat sessions, you can swap out the default in-memory chatHistory that backs chat memory classes like BufferMemory for a Redis instance.
为了跨聊天会话实现长期持久性,您可以换出默认的内存中聊天历史记录,该聊天历史记录支持 Redis 实例的聊天内存类(如 BufferMemory)。
Because Upstash Redis works via a REST API, you can use this with Vercel Edge, Cloudflare Workers and other Serverless environments.
由于Upstash Redis通过REST API工作,因此您可以将其用于Vercel Edge,Cloudflare Workers和其他无服务器环境。
VectorStoreRetrieverMemory stores memories in a VectorDB and queries the top-K most "salient" docs every time it is called.
VectorStoreRetrieverMemory将内存存储在VectorDB中,并在每次调用时查询top-K最“突出”的文档。
Zep is a memory server that stores, summarizes, embeds, indexes, and enriches conversational AI chat histories, autonomous agent histories, document Q&A histories and exposes them via simple, low-latency APIs.
Zep 是一个内存服务器,用于存储、汇总、嵌入、索引和丰富对话式 AI 聊天历史记录、自主代理历史记录、文档问答历史记录,并通过简单、低延迟的 API 公开它们。
To implement your own memory class you have two options:
要实现自己的内存类,您有两个选择:
This is the easiest way to implement your own memory class. You can subclass BaseChatMemory, which takes care of saveContext by saving inputs and outputs as Chat Messages, and implement only the loadMemoryVariables method. This method is responsible for returning the memory variables that are relevant for the current input values.
这是实现自己的内存类的最简单方法。您可以子类 BaseChatMemory ,它通过将输入和输出另存为聊天消息来处理 saveContext ,并仅实现 loadMemoryVariables 方法。此方法负责返回与当前输入值相关的内存变量。
abstract class BaseChatMemory extends BaseMemory { chatHistory: ChatMessageHistory; abstract loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;}SubclassingBaseMemory? 子类BaseMemory
If you want to implement a more custom memory class, you can subclass BaseMemory and implement both loadMemoryVariables and saveContext methods. The saveContext method is responsible for storing the input and output values in memory. The loadMemoryVariables method is responsible for returning the memory variables that are relevant for the current input values.
如果要实现更自定义的内存类,可以子类 BaseMemory 并实现 loadMemoryVariables 和 saveContext 方法。 saveContext 方法负责将输入和输出值存储在内存中。 loadMemoryVariables 方法负责返回与当前输入值相关的内存变量。
abstract class BaseMemory { abstract loadMemoryVariables(values: InputValues): Promise<MemoryVariables>; abstract saveContext( inputValues: InputValues, outputValues: OutputValues ): Promise<void>;}