0% found this document useful (0 votes)
8 views6 pages

L2-Memory - Jupyter Notebook

The document outlines various memory types in LangChain, including ConversationBufferMemory, ConversationBufferWindowMemory, ConversationTokenBufferMemory, and ConversationSummaryMemory. It provides code examples demonstrating how to implement these memory types in a conversation chain with an AI model. Each memory type serves different purposes, such as retaining conversation history or summarizing interactions for efficient context management.

Uploaded by

itsbeenhacked
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

L2-Memory - Jupyter Notebook

The document outlines various memory types in LangChain, including ConversationBufferMemory, ConversationBufferWindowMemory, ConversationTokenBufferMemory, and ConversationSummaryMemory. It provides code examples demonstrating how to implement these memory types in a conversation chain with an AI model. Each memory type serves different purposes, such as retaining conversation history or summarizing interactions for efficient context management.

Uploaded by

itsbeenhacked
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LangChain: Memory

Outline
ConversationBufferMemory
ConversationBufferWindowMemory
ConversationTokenBufferMemory
ConversationSummaryMemory

ConversationBufferMemory
In [1]: import os

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

import warnings
warnings.filterwarnings('ignore')

Note: LLM's do not always produce the same results. When executing the code in your notebook, you may get
slightly different answers that those in the video.

In [2]: # account for deprecation of LLM model


import datetime
# Get the current date
current_date = datetime.datetime.now().date()

# Define the date after which the model should be set to "gpt-3.5-turbo"
target_date = datetime.date(2024, 6, 12)

# Set the model variable based on the current date
if current_date > target_date:
llm_model = "gpt-3.5-turbo"
else:
llm_model = "gpt-3.5-turbo-0301"

In [3]: from langchain.chat_models import ChatOpenAI


from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

In [4]: llm = ChatOpenAI(temperature=0.0, model=llm_model)
memory = ConversationBufferMemory()
conversation = ConversationChain(
llm=llm,
memory = memory,
verbose=True
)

In [5]: conversation.predict(input="Hi, my name is Andrew")

> Entering new ConversationChain chain...


Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is
talkative and provides lots of specific details from its context. If the AI d
oes not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: Hi, my name is Andrew


AI:

> Finished chain.

"Hello Andrew, it's nice to meet you. My name is AI. How can I assist you tod
ay?"

In [6]: conversation.predict(input="What is 1+1?")

> Entering new ConversationChain chain...


Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is
talkative and provides lots of specific details from its context. If the AI d
oes not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: Hi, my name is Andrew
AI: Hello Andrew, it's nice to meet you. My name is AI. How can I assist you
today?
Human: What is 1+1?
AI:

> Finished chain.

'The answer to 1+1 is 2.'


In [7]: conversation.predict(input="What is my name?")

> Entering new ConversationChain chain...


Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is
talkative and provides lots of specific details from its context. If the AI d
oes not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: Hi, my name is Andrew
AI: Hello Andrew, it's nice to meet you. My name is AI. How can I assist you
today?
Human: What is 1+1?
AI: The answer to 1+1 is 2.
Human: What is my name?
AI:

> Finished chain.

'Your name is Andrew, as you mentioned earlier.'

In [8]: print(memory.buffer)

Human: Hi, my name is Andrew


AI: Hello Andrew, it's nice to meet you. My name is AI. How can I assist you
today?
Human: What is 1+1?
AI: The answer to 1+1 is 2.
Human: What is my name?
AI: Your name is Andrew, as you mentioned earlier.

In [9]: memory.load_memory_variables({})

{'history': "Human: Hi, my name is Andrew\nAI: Hello Andrew, it's nice to mee
t you. My name is AI. How can I assist you today?\nHuman: What is 1+1?\nAI: T
he answer to 1+1 is 2.\nHuman: What is my name?\nAI: Your name is Andrew, as
you mentioned earlier."}

In [10]: memory = ConversationBufferMemory()

In [11]: memory.save_context({"input": "Hi"},


{"output": "What's up"})

In [12]: print(memory.buffer)

Human: Hi
AI: What's up

In [13]: memory.load_memory_variables({})

{'history': "Human: Hi\nAI: What's up"}


In [14]: memory.save_context({"input": "Not much, just hanging"},
{"output": "Cool"})

In [15]: memory.load_memory_variables({})

{'history': "Human: Hi\nAI: What's up\nHuman: Not much, just hanging\nAI: Coo
l"}

ConversationBufferWindowMemory
In [16]: from langchain.memory import ConversationBufferWindowMemory

In [17]: memory = ConversationBufferWindowMemory(k=1)

In [18]: memory.save_context({"input": "Hi"},


{"output": "What's up"})
memory.save_context({"input": "Not much, just hanging"},
{"output": "Cool"})

In [19]: memory.load_memory_variables({})

{'history': 'Human: Not much, just hanging\nAI: Cool'}

In [20]: llm = ChatOpenAI(temperature=0.0, model=llm_model)


memory = ConversationBufferWindowMemory(k=1)
conversation = ConversationChain(
llm=llm,
memory = memory,
verbose=False
)

In [21]: conversation.predict(input="Hi, my name is Andrew")

"Hello Andrew, it's nice to meet you. My name is AI. How can I assist you tod
ay?"

In [22]: conversation.predict(input="What is 1+1?")

'The answer to 1+1 is 2.'

In [23]: conversation.predict(input="What is my name?")

"I'm sorry, I don't have access to that information. Could you please tell me
your name?"
ConversationTokenBufferMemory
In [24]: #!pip install tiktoken

In [25]: from langchain.memory import ConversationTokenBufferMemory


from langchain.llms import OpenAI
llm = ChatOpenAI(temperature=0.0, model=llm_model)

In [26]: memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=50)


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!"})

In [27]: memory.load_memory_variables({})

{'history': 'AI: Amazing!\nHuman: Backpropagation is what?\nAI: Beautiful!\nH


uman: Chatbots are what?\nAI: Charming!'}

ConversationSummaryMemory
In [28]: from langchain.memory import ConversationSummaryBufferMemory

In [29]: # 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."

memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=100)
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}"})

In [30]: memory.load_memory_variables({})

{'history': "System: The human and AI engage in small talk before discussing
the day's schedule. The AI informs the human of a morning meeting with the pr
oduct team, time to work on the LangChain project, and a lunch meeting with a
customer interested in the latest AI developments."}
In [31]: conversation = ConversationChain(
llm=llm,
memory = memory,
verbose=True
)

In [32]: conversation.predict(input="What would be a good demo to show?")

> Entering new ConversationChain chain...


Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is
talkative and provides lots of specific details from its context. If the AI d
oes not know the answer to a question, it truthfully says it does not know.

Current conversation:
System: The human and AI engage in small talk before discussing the day's sch
edule. The AI informs the human of a morning meeting with the product team, t
ime to work on the LangChain project, and a lunch meeting with a customer int
erested in the latest AI developments.
Human: What would be a good demo to show?
AI:

> Finished chain.

"Based on the customer's interest in AI developments, I would suggest showcas


ing our latest natural language processing capabilities. We could demonstrate
how our AI can understand and respond to complex language queries, and even p
rovide personalized recommendations based on the user's preferences. Addition
ally, we could highlight our machine learning algorithms and how they continu
ously improve the accuracy and efficiency of our AI. Would you like me to pre
pare a demo for the meeting?"

In [33]: memory.load_memory_variables({})

{'history': "System: The human and AI engage in small talk before discussing
the day's schedule. The AI informs the human of a morning meeting with the pr
oduct team, time to work on the LangChain project, and a lunch meeting with a
customer interested in the latest AI developments. The human asks what would
be a good demo to show.\nAI: Based on the customer's interest in AI developme
nts, I would suggest showcasing our latest natural language processing capabi
lities. We could demonstrate how our AI can understand and respond to complex
language queries, and even provide personalized recommendations based on the
user's preferences. Additionally, we could highlight our machine learning alg
orithms and how they continuously improve the accuracy and efficiency of our
AI. Would you like me to prepare a demo for the meeting?"}

Reminder: Download your notebook to you local computer to save your work.

You might also like