-
Notifications
You must be signed in to change notification settings - Fork 6
Blog on "Langchain basic memory types" #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hulksyed07
wants to merge
5
commits into
main
Choose a base branch
from
blog/langchain-memory-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9235dcb
Blog on "Langchain Memory Types"
hulksyed07 1e1e9cd
Renamed to basic in order to add a new blog later for advanced types
hulksyed07 8909376
Merge branch 'main' into blog/langchain-memory-types
hulksyed07 e35c009
wrap in code block
hulksyed07 37e2fc9
showing all output in plaintext to avoid syntax highlighting
hulksyed07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
+++ | ||
title = "LangChain Basic Memory Types: Buffer, BufferWindow, TokenBuffer and Summary" | ||
slug = "langchain-basic-memory-types" | ||
date = 2024-06-30T14:49:39+05:30 | ||
image = "/images/2024/langchain-basic-memory-types.png" | ||
draft = false | ||
authors = ["Syed Mohd Mehndi"] | ||
description = "LangChain Basic Memory Types: Buffer, BufferWindow, TokenBuffer and Summary" | ||
tags = ["AI", "Langchain"] | ||
categories = ["AI", "Langchain"] | ||
type = "" | ||
+++ | ||
|
||
LangChain provides a powerful way to manage conversational memory in chatbots and conversational agents. Among the various memory types it supports, four key ones stand out: ConversationBufferMemory, ConversationBufferWindowMemory, ConversationTokenBufferMemory and ConversationSummaryMemory. Each of these memory types serves a unique purpose and can be used in different scenarios based on the requirements of the conversation. In this blog, we'll explore these memory types and provide short examples to highlight their differences. | ||
|
||
### ConversationBufferMemory | ||
|
||
ConversationBufferMemory stores the entire conversation history. This is useful when you need to retain every part of the conversation for context. | ||
|
||
#### Example: | ||
|
||
```python | ||
from langchain.memory import ConversationBufferMemory | ||
|
||
# Initialize buffer memory | ||
memory = ConversationBufferMemory() | ||
|
||
# Add conversation turns | ||
memory.add_message("User", "Hello, how are you?") | ||
memory.add_message("Assistant", "I'm good, thank you! How can I assist you today?") | ||
memory.add_message("User", "Can you tell me about the weather?") | ||
|
||
# Retrieve memory | ||
print(memory.retrieve()) | ||
``` | ||
|
||
#### Output: | ||
|
||
```vbnet | ||
User: Hello, how are you? | ||
Assistant: I'm good, thank you! How can I assist you today? | ||
User: Can you tell me about the weather? | ||
``` | ||
|
||
### ConversationalBufferWindowMemory | ||
|
||
ConversationalBufferWindowMemory retains only the last N messages of the conversation. This is useful when only the recent context is relevant. | ||
|
||
#### Example: | ||
|
||
```python | ||
from langchain.memory import ConversationBufferWindowMemory | ||
|
||
# Initialize buffer window memory with a window size of 2 | ||
memory = ConversationBufferWindowMemory(window_size=2) | ||
|
||
# Add conversation turns | ||
memory.add_message("User", "Hello, how are you?") | ||
memory.add_message("Assistant", "I'm good, thank you! How can I assist you today?") | ||
memory.add_message("User", "Can you tell me about the weather?") | ||
|
||
# Retrieve memory | ||
print(memory.retrieve()) | ||
``` | ||
|
||
#### Output: | ||
|
||
```vbnet | ||
Assistant: I'm good, thank you! How can I assist you today? | ||
User: Can you tell me about the weather? | ||
``` | ||
|
||
### ConversationalTokenBufferMemory | ||
|
||
ConversationalTokenBufferMemory retains conversation history up to a specified token limit. This is particularly useful when dealing with models that have token limits. | ||
|
||
#### Example: | ||
|
||
```python | ||
from langchain.memory import ConversationTokenBufferMemory | ||
|
||
# Initialize token buffer memory with a token limit of 50 | ||
memory = ConversationTokenBufferMemory(max_tokens=50) | ||
|
||
# Add conversation turns | ||
memory.add_message("User", "Hello, how are you?") | ||
memory.add_message("Assistant", "I'm good, thank you! How can I assist you today?") | ||
memory.add_message("User", "Can you tell me about the weather?") | ||
memory.add_message("Assistant", "Sure, the weather today is sunny with a high of 75°F.") | ||
|
||
# Retrieve memory | ||
print(memory.retrieve()) | ||
``` | ||
|
||
#### Output: | ||
|
||
```vbnet | ||
User: Hello, how are you? | ||
Assistant: I'm good, thank you! How can I assist you today? | ||
User: Can you tell me about the weather? | ||
Assistant: Sure, the weather today is sunny with a high of 75°F. | ||
``` | ||
|
||
(Note: The actual token count will depend on the tokenization model used.) | ||
|
||
### ConversationalSummaryMemory | ||
|
||
ConversationalSummaryMemory maintains a concise summary of the conversation. This is useful for long conversations where retaining every detail is unnecessary, but the gist is important. | ||
|
||
#### Example: | ||
|
||
```python | ||
from langchain.memory import ConversationSummaryMemory | ||
|
||
#### Initialize summary memory | ||
memory = ConversationSummaryMemory() | ||
|
||
#### Add conversation turns | ||
memory.add_message("User", "Hello, how are you?") | ||
memory.add_message("Assistant", "I'm good, thank you! How can I assist you today?") | ||
memory.add_message("User", "Can you tell me about the weather?") | ||
memory.add_message("Assistant", "Sure, the weather today is sunny with a high of 75°F.") | ||
|
||
# Retrieve summary | ||
print(memory.retrieve()) | ||
``` | ||
|
||
#### Output: | ||
|
||
```csharp | ||
The user greeted the assistant and asked about the weather. The assistant responded that the weather is sunny with a high of 75°F. | ||
``` | ||
|
||
## Conclusion | ||
|
||
Each of these memory types offers unique benefits for managing conversational context in LangChain. By choosing the appropriate memory type, you can optimize the performance and relevance of your conversational agents, ensuring they provide the best possible experience for users. | ||
|
||
#### In summary: | ||
|
||
- **ConversationalBufferMemory:** Stores the entire conversation. | ||
- **ConversationalBufferWindowMemory:** Stores the last N messages. | ||
- **ConversationalTokenBufferMemory:** Stores messages up to a token limit. | ||
- **ConversationalSummaryMemory:** Stores a concise summary of the conversation. | ||
|
||
Understanding these memory types and their use cases can significantly enhance the capability of your conversational AI systems. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.