By default, LLMs, Chains and Agents are stateless. They operate independently on each incoming query, without retaining any memory of previous interactions. However, in certain applications like chatbots, it is crucial to remember past conversations in both the short and long term. This is where the Memory feature comes into play.
LangChain offers two ways to incorporate memory components. Firstly, it provides helpful tools for managing and manipulating previous chat messages in a modular and versatile manner. These utilities can be used in various contexts. Secondly, LangChain facilitates the integration of these utilities into chains, making it easier to incorporate memory functionality.
Memory involves maintaining the state of a user’s interactions with a language model. These interactions are captured in ChatMessages, which are sequences of messages. The process of memory entails ingesting, capturing, transforming, and extracting knowledge from these chat message sequences.
Different types of memory
There are different types of memory, each with its own approach. For each memory type, there are standalone functions that extract information from a message sequence, as well as ways to use this memory type within a chain.
There are seven ways to interact with Memory in LangChain and Large Language Models. These seven approaches are outlined below:
- Conversation BUFFER Memory : Entire history
- Conversation BUFFER WINDOW Memory : k past interaction
- conversation TOKEN BUFFER memory : n tokens of memory
- Conversation ENTITY memory: Extracts entity
- Conversation KNOWLEDGE GRAPH memory: return graph
- Conversation SUMMARY memory : summarizes
- Conversation SUMMARY BUFFER memory : summarizes the k interactions
Memory can provide multiple pieces of information, such as the most recent N messages and a summary of all previous messages. The information can be returned as either a string or a list of messages. In this notebook, we will focus on the simplest form of memory known as “buffer” memory, which involves keeping a buffer of all prior messages. We will demonstrate how to utilize the modular utility functions and then showcase its application in a chain, both returning a string and a list of messages.