ConversationSummaryMemory

Open in ColabOpen in GitHub

Overview

This tutorial covers how to summarize and manage conversation history using LangChain.

ConversationSummaryMemory optimizes memory usage by summarizing conversation content, allowing efficient management of long conversation histories.

In this tutorial, we will demonstrate how to implement conversation summarization functionality using LangChain's ConversationSummaryMemory.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can alternatively set OPENAI_API_KEY in .env file and load it.

[Note] This is not necessary if you've already set OPENAI_API_KEY in previous steps.

Conversation Summary Memory

Let's now explore how to use a more complex type of memory: ConversationSummaryMemory.

This type of memory generates a summary of the conversation over time , which can be useful for compressing conversational information as the conversation progresses.

ConversationSummaryMemory summarizes the conversation as it continues and stores the current summary in memory .

This memory can then be used to insert the summarized conversation history into prompts or chains.

It is particularly useful for longer conversations, where retaining the entire conversation history in the prompt would take up too many tokens.

Let's create a ConversationSummaryMemory.

You can store multiple conversations.

You can check the history of the stored memory.

It provides a concise summary of all previous conversations.

Conversation Summary Buffer Memory

ConversationSummaryBufferMemory combines two key ideas:

It retains a buffer of the recent conversation history in memory while compiling older interactions into a summary without completely flushing them.

It determines when to flush the conversation based on the token length, instead of the number of interactions.

First, let's save a single conversation, and then check the memory.

Check the conversation stored in memory.

At this point, the conversation is not yet summarized because it hasn't reached the 200-token threshold.

Let's add more conversations to exceed the 200-token limit.

Check the stored conversation history.

The most recent conversation remains unsummarized, while the previous conversations are stored as a summary.

Last updated