Memory, base class¶
This implements the Cassandra-specific I/O facilities, and is then used by LangChain to power every other, more sophisticated, abstraction around keeping chat memory.
In [1]:
Copied!
from langchain.memory import CassandraChatMessageHistory
from langchain.memory import CassandraChatMessageHistory
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='test-session',
session=astraSession,
keyspace='langchain',
ttl_seconds = 3600,
)
message_history.clear()
message_history = CassandraChatMessageHistory(
session_id='test-session',
session=astraSession,
keyspace='langchain',
ttl_seconds = 3600,
)
message_history.clear()
In [4]:
Copied!
message_history.messages
message_history.messages
Out[4]:
[]
In [5]:
Copied!
message_history.add_user_message('Hello, I am human.')
message_history.add_user_message('Hello, I am human.')
In [6]:
Copied!
message_history.add_ai_message('Hello, I am the bot.')
message_history.add_ai_message('Hello, I am the bot.')
In [7]:
Copied!
message_history.messages
message_history.messages
Out[7]:
[HumanMessage(content='Hello, I am human.', additional_kwargs={}, example=False), AIMessage(content='Hello, I am the bot.', additional_kwargs={}, example=False)]
In [8]:
Copied!
message_history.clear()
message_history.clear()
In [9]:
Copied!
message_history.messages
message_history.messages
Out[9]:
[]