Skip to main content

SQL (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:

  1. Session Id - a unique identifier of the session, like user name, email, chat id etc.
  2. Connection string - a string that specifies the database connection. It will be passed to SQLAlchemy create_engine function.
  3. Install SQLAlchemy python package.
!pip install SQLAlchemy
from langchain.memory.chat_message_histories import SQLChatMessageHistory

chat_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 datetime
from typing import Any

from langchain.memory.chat_message_histories.sql import BaseMessageConverter
from langchain.schema import AIMessage, BaseMessage, HumanMessage, SystemMessage
from sqlalchemy import Column, DateTime, Integer, Text
from sqlalchemy.orm import declarative_base

Base = 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 CustomMessage


chat_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.