ComponentsMemorySQL (SQLAlchemy)On this pageSQL (SQLAlchemy)Structured Query Language (SQL) is a domain-specific language used in programming and designed for managing data held in a relational database management system (RDBMS), or for stream processing in a relational data stream management system (RDSMS). It is particularly useful in handling structured data, i.e., data incorporating relations among entities and variables.SQLAlchemy is an open-source SQL toolkit and object-relational mapper (ORM) for the Python programming language released under the MIT License.This notebook goes over a SQLChatMessageHistory class that allows to store chat history in any database supported by SQLAlchemy.Please note that to use it with databases other than SQLite, you will need to install the corresponding database driver.Basic Usage​To use the storage you need to provide only 2 things:Session Id - a unique identifier of the session, like user name, email, chat id etc.Connection string - a string that specifies the database connection. It will be passed to SQLAlchemy create_engine function.Install SQLAlchemy python package.pip install SQLAlchemyfrom langchain.memory.chat_message_histories import SQLChatMessageHistorychat_message_history = SQLChatMessageHistory(    session_id='test_session',    connection_string='sqlite:///sqlite.db')chat_message_history.add_user_message('Hello')chat_message_history.add_ai_message('Hi')chat_message_history.messages    [HumanMessage(content='Hello', additional_kwargs={}, example=False),     AIMessage(content='Hi', additional_kwargs={}, example=False)]Custom Storage Format​By default, only the session id and message dictionary are stored in the table.However, sometimes you might want to store some additional information, like message date, author, language etc.To do that, you can create a custom message converter, by implementing BaseMessageConverter interface.from datetime import datetimefrom langchain.schema import BaseMessage, HumanMessage, AIMessage, SystemMessagefrom typing import Anyfrom sqlalchemy import Column, Integer, Text, DateTimefrom sqlalchemy.orm import declarative_basefrom langchain.memory.chat_message_histories.sql import BaseMessageConverterBase = declarative_base()class CustomMessage(Base):    __tablename__ = 'custom_message_store'    id = Column(Integer, primary_key=True)    session_id = Column(Text)    type = Column(Text)    content = Column(Text)    created_at = Column(DateTime)    author_email = Column(Text)class CustomMessageConverter(BaseMessageConverter):    def __init__(self, author_email: str):        self.author_email = author_email        def from_sql_model(self, sql_message: Any) -> BaseMessage:        if sql_message.type == 'human':            return HumanMessage(                content=sql_message.content,            )        elif sql_message.type == 'ai':            return AIMessage(                content=sql_message.content,            )        elif sql_message.type == 'system':            return SystemMessage(                content=sql_message.content,            )        else:            raise ValueError(f'Unknown message type: {sql_message.type}')        def to_sql_model(self, message: BaseMessage, session_id: str) -> Any:        now = datetime.now()        return CustomMessage(            session_id=session_id,            type=message.type,            content=message.content,            created_at=now,            author_email=self.author_email        )        def get_sql_model_class(self) -> Any:        return CustomMessagechat_message_history = SQLChatMessageHistory(    session_id='test_session',    connection_string='sqlite:///sqlite.db',    custom_message_converter=CustomMessageConverter(        author_email='test@example.com'    ))chat_message_history.add_user_message('Hello')chat_message_history.add_ai_message('Hi')chat_message_history.messages    [HumanMessage(content='Hello', additional_kwargs={}, example=False),     AIMessage(content='Hi', additional_kwargs={}, example=False)]You also might want to change the name of session_id column. In this case you'll need to specify session_id_field_name parameter.PreviousRocksetNextSQLiteBasic UsageCustom Storage Format