2023-12-11 21:53:30 +00:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from langchain_core.chat_history import BaseChatMessageHistory
|
|
|
|
from langchain_core.messages import BaseMessage
|
|
|
|
|
|
|
|
|
|
|
|
class StreamlitChatMessageHistory(BaseChatMessageHistory):
|
|
|
|
"""
|
|
|
|
Chat message history that stores messages in Streamlit session state.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
key: The key to use in Streamlit session state for storing messages.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, key: str = "langchain_messages"):
|
|
|
|
try:
|
|
|
|
import streamlit as st
|
|
|
|
except ImportError as e:
|
|
|
|
raise ImportError(
|
|
|
|
"Unable to import streamlit, please run `pip install streamlit`."
|
|
|
|
) from e
|
|
|
|
|
|
|
|
if key not in st.session_state:
|
|
|
|
st.session_state[key] = []
|
2024-03-01 20:17:18 +00:00
|
|
|
self._key = key
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
|
|
@property
|
2024-03-01 20:17:18 +00:00
|
|
|
def messages(self) -> List[BaseMessage]:
|
2023-12-11 21:53:30 +00:00
|
|
|
"""Retrieve the current list of messages"""
|
2024-03-01 20:17:18 +00:00
|
|
|
import streamlit as st
|
|
|
|
|
|
|
|
return st.session_state[self._key]
|
|
|
|
|
|
|
|
@messages.setter
|
|
|
|
def messages(self, value: List[BaseMessage]) -> None:
|
|
|
|
"""Set the messages list with a new value"""
|
|
|
|
import streamlit as st
|
|
|
|
|
|
|
|
st.session_state[self._key] = value
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
|
|
def add_message(self, message: BaseMessage) -> None:
|
|
|
|
"""Add a message to the session memory"""
|
2024-03-01 20:17:18 +00:00
|
|
|
self.messages.append(message)
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
|
|
def clear(self) -> None:
|
|
|
|
"""Clear session memory"""
|
2024-03-01 20:17:18 +00:00
|
|
|
self.messages.clear()
|