Summary Buffer Memory¶
In [1]:
Copied!
from langchain.memory import CassandraChatMessageHistory
from langchain.memory import ConversationSummaryBufferMemory
from langchain.chains import ConversationChain
from langchain.memory import CassandraChatMessageHistory
from langchain.memory import ConversationSummaryBufferMemory
from langchain.chains import ConversationChain
In [2]:
Copied!
from cqlsession import getCQLSession, getCQLKeyspace
astraSession = getCQLSession()
astraKeyspace = getCQLKeyspace()
from cqlsession import getCQLSession, getCQLKeyspace
astraSession = getCQLSession()
astraKeyspace = getCQLKeyspace()
/home/stefano/.virtualenvs/langchain-cassio-3.10/lib/python3.10/site-packages/cassandra/io/asyncorereactor.py:34: DeprecationWarning: The asyncore module is deprecated and will be removed in Python 3.12. The recommended replacement is asyncio import asyncore /home/stefano/.virtualenvs/langchain-cassio-3.10/lib/python3.10/site-packages/cassandra/datastax/cloud/__init__.py:173: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated ssl_context = SSLContext(PROTOCOL_TLS) /home/stefano/.virtualenvs/langchain-cassio-3.10/lib/python3.10/site-packages/cassandra/io/asyncorereactor.py:347: DeprecationWarning: ssl.match_hostname() is deprecated self._connect_socket()
In [3]:
Copied!
message_history = CassandraChatMessageHistory(
session_id='summarized-conversation-4321',
session=astraSession,
keyspace='langchain',
ttl_seconds = 3600,
)
# message_history.clear()
message_history = CassandraChatMessageHistory(
session_id='summarized-conversation-4321',
session=astraSession,
keyspace='langchain',
ttl_seconds = 3600,
)
# message_history.clear()
In [4]:
Copied!
from langchain.llms import OpenAI
llm = OpenAI()
from langchain.llms import OpenAI
llm = OpenAI()
In [5]:
Copied!
memory = ConversationSummaryBufferMemory(
llm=llm,
chat_memory=message_history,
max_token_limit=180,
)
memory = ConversationSummaryBufferMemory(
llm=llm,
chat_memory=message_history,
max_token_limit=180,
)
In [6]:
Copied!
summaryConversation = ConversationChain(
llm=llm,
memory=memory,
verbose=True
)
summaryConversation = ConversationChain(
llm=llm,
memory=memory,
verbose=True
)
In [7]:
Copied!
summaryConversation.predict(input='Tell me about William Tell')
summaryConversation.predict(input='Tell me about William Tell')
> 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 does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Tell me about William Tell AI: > Finished chain.
Out[7]:
" William Tell is a Swiss folk hero from the late 13th century. He is best known for his legendary feat of shooting an arrow through an apple placed on his son's head. He is often portrayed as an expert marksman and archer. He is also known for his role in the formation of the Swiss Confederation, a revolutionary alliance against the Habsburgs."
In [8]:
Copied!
summaryConversation.predict(
input="Any relation to the apple from Cupertino?"
)
summaryConversation.predict(
input="Any relation to the apple from Cupertino?"
)
> 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 does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Tell me about William Tell AI: William Tell is a Swiss folk hero from the late 13th century. He is best known for his legendary feat of shooting an arrow through an apple placed on his son's head. He is often portrayed as an expert marksman and archer. He is also known for his role in the formation of the Swiss Confederation, a revolutionary alliance against the Habsburgs. Human: Any relation to the apple from Cupertino? AI: > Finished chain.
Out[8]:
" No, I don't believe there is any relation between William Tell's apple and the apple from Cupertino. The apple from Cupertino is the logo of Apple Inc., an American technology company, while William Tell's apple is a symbol of courage and heroism in Swiss folklore."
In [9]:
Copied!
memory.moving_summary_buffer
memory.moving_summary_buffer
Out[9]:
''
In [10]:
Copied!
summaryConversation.predict(
input="I heard the two apples may be in fact the very same fruit."
)
summaryConversation.predict(
input="I heard the two apples may be in fact the very same fruit."
)
> 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 does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Tell me about William Tell AI: William Tell is a Swiss folk hero from the late 13th century. He is best known for his legendary feat of shooting an arrow through an apple placed on his son's head. He is often portrayed as an expert marksman and archer. He is also known for his role in the formation of the Swiss Confederation, a revolutionary alliance against the Habsburgs. Human: Any relation to the apple from Cupertino? AI: No, I don't believe there is any relation between William Tell's apple and the apple from Cupertino. The apple from Cupertino is the logo of Apple Inc., an American technology company, while William Tell's apple is a symbol of courage and heroism in Swiss folklore. Human: I heard the two apples may be in fact the very same fruit. AI: > Finished chain.
Out[10]:
" That is an interesting theory, but I'm not aware of any evidence to support this claim. The apple from Cupertino is a modern logo, while William Tell's apple has been a part of Swiss folklore for centuries."
In [11]:
Copied!
memory.moving_summary_buffer
memory.moving_summary_buffer
Out[11]:
"\nThe human asks what the AI knows about William Tell. The AI responds that William Tell is a Swiss folk hero from the late 13th century, best known for his legendary feat of shooting an arrow through an apple placed on his son's head. He is also known for his role in the formation of the Swiss Confederation, a revolutionary alliance against the Habsburgs."
In [12]:
Copied!
print(memory.predict_new_summary(
memory.chat_memory.messages,
memory.moving_summary_buffer,
))
print(memory.predict_new_summary(
memory.chat_memory.messages,
memory.moving_summary_buffer,
))
The human asks what the AI knows about William Tell. The AI responds that William Tell is a Swiss folk hero from the late 13th century, best known for his legendary feat of shooting an arrow through an apple placed on his son's head. He is also known for his role in the formation of the Swiss Confederation, a revolutionary alliance against the Habsburgs. The human further inquires about a possible relation to the apple from Cupertino, to which the AI responds there is no relation between the two apples, as the apple from Cupertino is the logo of Apple Inc., an American technology company, while William Tell's apple is a symbol of courage and heroism in Swiss folklore. The human suggests the two apples may be the same fruit, to which the AI responds that there is no evidence to support this claim.
In [13]:
Copied!
summaryConversation.predict(
input='Ok, enough with apples. Let\'s talk starfish now.'
)
summaryConversation.predict(
input='Ok, enough with apples. Let\'s talk starfish now.'
)
> 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 does not know the answer to a question, it truthfully says it does not know. Current conversation: System: The human asks what the AI knows about William Tell. The AI responds that William Tell is a Swiss folk hero from the late 13th century, best known for his legendary feat of shooting an arrow through an apple placed on his son's head. He is also known for his role in the formation of the Swiss Confederation, a revolutionary alliance against the Habsburgs. Human: Tell me about William Tell AI: William Tell is a Swiss folk hero from the late 13th century. He is best known for his legendary feat of shooting an arrow through an apple placed on his son's head. He is often portrayed as an expert marksman and archer. He is also known for his role in the formation of the Swiss Confederation, a revolutionary alliance against the Habsburgs. Human: Any relation to the apple from Cupertino? AI: No, I don't believe there is any relation between William Tell's apple and the apple from Cupertino. The apple from Cupertino is the logo of Apple Inc., an American technology company, while William Tell's apple is a symbol of courage and heroism in Swiss folklore. Human: I heard the two apples may be in fact the very same fruit. AI: That is an interesting theory, but I'm not aware of any evidence to support this claim. The apple from Cupertino is a modern logo, while William Tell's apple has been a part of Swiss folklore for centuries. Human: Ok, enough with apples. Let's talk starfish now. AI: > Finished chain.
Out[13]:
" Sure! Starfish are a type of marine animal belonging to the phylum Echinodermata. They have a unique body structure with five or more arms and no backbone. They are found in most of the world's oceans and can live in shallow waters, deep sea trenches, and even intertidal zones."
In [14]:
Copied!
memory.moving_summary_buffer
memory.moving_summary_buffer
Out[14]:
"\nThe human asks what the AI knows about William Tell. The AI responds that William Tell is a Swiss folk hero from the late 13th century, best known for his legendary feat of shooting an arrow through an apple placed on his son's head. He is often portrayed as an expert marksman and archer, and is also known for his role in the formation of the Swiss Confederation, a revolutionary alliance against the Habsburgs. The human then inquires about any relation between William Tell's apple and the logo of Apple Inc., to which the AI responds there is no relation."
In [15]:
Copied!
print(memory.predict_new_summary(
memory.chat_memory.messages,
memory.moving_summary_buffer,
))
print(memory.predict_new_summary(
memory.chat_memory.messages,
memory.moving_summary_buffer,
))
The human asks what the AI knows about William Tell. The AI responds that William Tell is a Swiss folk hero from the late 13th century, best known for his legendary feat of shooting an arrow through an apple placed on his son's head. He is often portrayed as an expert marksman and archer, and is also known for his role in the formation of the Swiss Confederation, a revolutionary alliance against the Habsburgs. The human then inquires about any relation between William Tell's apple and the logo of Apple Inc., to which the AI responds there is no relation, as the apple from Cupertino is a modern logo while William Tell's apple has been a part of Swiss folklore for centuries. The conversation then shifts to starfish, which the AI explains are a type of marine animal belonging to the phylum Echinodermata with five or more arms and no backbone, found in most of the world's oceans.