Memory

LangChain Basics 2

Jan Kirenz

Memory

ConversationBufferMemory

ConversationBufferWindowMemory

ConversationTokenBufferMemory

ConversationSummaryMemory

Setup

Python

from langchain.memory import ConversationSummaryBufferMemory
from langchain.llms import OpenAI
from langchain.memory import ConversationTokenBufferMemory
from langchain.memory import ConversationBufferWindowMemory
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
import datetime
import warnings
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())  # read local .env file

warnings.filterwarnings('ignore')

Conversation Buffer Memory

Setup

llm_model = "gpt-3.5-turbo"

llm = ChatOpenAI(temperature=0.0, model=llm_model)
memory = ConversationBufferMemory()

conversation = ConversationChain(
    llm=llm,
    memory=memory,
    verbose=True  # shows conversation history
)

Some examples

conversation.predict(input="Hi, my name is Jan")
  • “Hello Jan! It’s nice to meet you. How can I assist you today?”
conversation.predict(input="What is 1+1?")
  • ‘1+1 is equal to 2.’
conversation.predict(input="What is my name?")
  • ‘Your name is Jan.’

Memory buffer

print(memory.buffer)
Human: Hi, my name is Jan
AI: Hello Jan! It's nice to meet you. How can I assist you today?
Human: What is 1+1?
AI: 1+1 is equal to 2.
Human: What is my name?
AI: Your name is Jan.
memory.load_memory_variables({})
  • {‘history’: “Human: Hi, my name is Jan: Hello Jan! It’s nice to meet you. How can I assist you today?: What is 1+1?: 1+1 is equal to 2.: What is my name?: Your name is Jan.”}

Conversational example

memory = ConversationBufferMemory()
memory.save_context({"input": "Hi"},
                    {"output": "What's up"})
print(memory.buffer)
Human: Hi
AI: What's up
memory.load_memory_variables({})
  • {‘history’: “Human: Hi: What’s up”}

Conversational example continued

memory.save_context({"input": "Not much, just hanging"},
                    {"output": "Cool"})
memory.load_memory_variables({})
  • {‘history’: “Human: Hi: What’s up: Not much, just hanging: Cool”}

Conversation Buffer Window Memory

Conversation Buffer Window Memory

memory = ConversationBufferWindowMemory(k=1)

Example

memory.save_context({"input": "Hi"},
                    {"output": "What's up"})

memory.save_context({"input": "Not much, just hanging"},
                    {"output": "Cool"})
memory.load_memory_variables({})
  • {‘history’: ‘Human: Not much, just hanging: Cool’}

Setup

llm = ChatOpenAI(temperature=0.0, model=llm_model)

memory = ConversationBufferWindowMemory(k=1)

conversation = ConversationChain(
    llm=llm,
    memory=memory,
    verbose=False
)

Example

conversation.predict(input="Hi, my name is Jan")
  • “Hello Jan! It’s nice to meet you. How can I assist you today?”
conversation.predict(input="What is 1+1?")
  • ‘1+1 is equal to 2.’
conversation.predict(input="What is my name?")
  • “I’m sorry, but I don’t have access to personal information about individuals unless it has been shared with me in the course of our conversation.”

Conversation TokenBuffer Memory

Setup

llm = ChatOpenAI(temperature=0.0, model=llm_model)
memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=50)

Example

memory.save_context({"input": "AI is what?!"},
                    {"output": "Amazing!"})

memory.save_context({"input": "Backpropagation is what?"},
                    {"output": "Beautiful!"})
memory.save_context({"input": "Chatbots are what?"},
                    {"output": "Charming!"})
memory.load_memory_variables({})
  • {‘history’: ‘AI: Amazing!: Backpropagation is what?: Beautiful!: Chatbots are what?: Charming!’}

Conversation Summary Memory

Example

# create a long string
schedule = "There is a meeting at 8am with your product team. \
You will need your powerpoint presentation prepared. \
9am-12pm have time to work on your LangChain \
project which will go quickly because Langchain is such a powerful tool. \
At Noon, lunch at the italian resturant with a customer who is driving \
from over an hour away to meet you to understand the latest in AI. \
Be sure to bring your laptop to show the latest LLM demo."

Setup

memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=100)

Example

memory.save_context({"input": "Hello"}, {"output": "What's up"})
memory.save_context({"input": "Not much, just hanging"},
                    {"output": "Cool"})
memory.save_context({"input": "What is on the schedule today?"},
                    {"output": f"{schedule}"})

History

memory.load_memory_variables({})
  • {‘history’: ‘System: The human and AI exchange greetings. The human asks about the schedule for the day. The AI provides a detailed schedule, including a meeting with the product team, work on the LangChain project, and a lunch meeting with a customer interested in AI. The AI emphasizes the importance of bringing a laptop to showcase the latest LLM demo during the lunch meeting.’}

ConversationChain

conversation = ConversationChain(
    llm=llm,
    memory=memory,
    verbose=True
)

Conversation predict

conversation.predict(input="What would be a good demo to show?")
  • “A good demo to show during the lunch meeting with the customer interested in AI would be the latest LLM (Language Model) demo. The LLM is a cutting-edge AI model that can generate human-like text based on a given prompt. It has been trained on a vast amount of data and can generate coherent and contextually relevant responses. By showcasing the LLM demo, you can demonstrate the capabilities of AI in natural language processing and generate interest in potential applications for the customer’s business.”

Memory

memory.load_memory_variables({})
  • {‘history’: “System: The human and AI exchange greetings and discuss the schedule for the day. The AI provides a detailed schedule, including a meeting with the product team, work on the LangChain project, and a lunch meeting with a customer interested in AI. The AI emphasizes the importance of bringing a laptop to showcase the latest LLM demo during the lunch meeting. The human asks what would be a good demo to show, and the AI suggests showcasing the latest LLM (Language Model) demo. The LLM is a cutting-edge AI model that can generate human-like text based on a given prompt. By showcasing the LLM demo, the AI can demonstrate the capabilities of AI in natural language processing and generate interest in potential applications for the customer’s business.”}

Acknowledgments

This tutorial is mainly based on the excellent course “LangChain for LLM Application Development” provided by Harrison Chase and Andrew Ng

What’s next?

Congratulations! You have completed this tutorial 👍

Next, you may want to go back to the lab’s website