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.

Anxiety and confusion icon. Credit: https://commons.wikimedia.org/wiki/File:Anxiety_confusion_2686190.svg
Anxiety and confusion icon. Credit: https://commons.wikimedia.org/wiki/File:Anxiety_confusion_2686190.svg

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.

Message Reaction Feature for Chatrooms Credit: https://linecorp.com/th/pr/news/en/2021/3845
Message Reaction Feature for Chatrooms Credit: https://linecorp.com/th/pr/news/en/2021/3845

There are seven ways to interact with Memory in LangChain and Large Language Models. These seven approaches are outlined below:

  1. Conversation BUFFER Memory : Entire history
  2. Conversation BUFFER WINDOW Memory : k past interaction
  3. conversation TOKEN BUFFER memory : n tokens of memory
  4. Conversation ENTITY memory: Extracts entity
  5. Conversation KNOWLEDGE GRAPH memory: return graph
  6. Conversation SUMMARY memory : summarizes
  7. 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.

Let’s get started

Open In Colab

แชร์ให้เพื่อน:

Surapong Kanoktipsatharporn on FacebookSurapong Kanoktipsatharporn on LinkedinSurapong Kanoktipsatharporn on Rss
Surapong Kanoktipsatharporn
Solutions Architect at Bua Labs
The ultimate test of your knowledge is your capacity to convey it to another.

Published by Surapong Kanoktipsatharporn

The ultimate test of your knowledge is your capacity to convey it to another.