|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import json |
| 15 | +from typing import List |
| 16 | + |
| 17 | +import sqlalchemy |
| 18 | +from langchain_core.chat_history import BaseChatMessageHistory |
| 19 | +from langchain_core.messages import BaseMessage, messages_from_dict |
| 20 | + |
| 21 | +from langchain_google_cloud_sql_mysql.mysql_engine import MySQLEngine |
| 22 | + |
| 23 | + |
| 24 | +class MySQLChatMessageHistory(BaseChatMessageHistory): |
| 25 | + """Chat message history stored in a Cloud SQL MySQL database.""" |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + engine: MySQLEngine, |
| 30 | + session_id: str, |
| 31 | + table_name: str = "message_store", |
| 32 | + ) -> None: |
| 33 | + self.engine = engine |
| 34 | + self.session_id = session_id |
| 35 | + self.table_name = table_name |
| 36 | + self._create_table_if_not_exists() |
| 37 | + |
| 38 | + def _create_table_if_not_exists(self) -> None: |
| 39 | + create_table_query = f"""CREATE TABLE IF NOT EXISTS {self.table_name} ( |
| 40 | + id INT AUTO_INCREMENT PRIMARY KEY, |
| 41 | + session_id TEXT NOT NULL, |
| 42 | + data JSON NOT NULL, |
| 43 | + type TEXT NOT NULL |
| 44 | + );""" |
| 45 | + |
| 46 | + with self.engine.connect() as conn: |
| 47 | + conn.execute(sqlalchemy.text(create_table_query)) |
| 48 | + conn.commit() |
| 49 | + |
| 50 | + @property |
| 51 | + def messages(self) -> List[BaseMessage]: # type: ignore |
| 52 | + """Retrieve the messages from Cloud SQL""" |
| 53 | + query = f"SELECT data, type FROM {self.table_name} WHERE session_id = '{self.session_id}' ORDER BY id;" |
| 54 | + with self.engine.connect() as conn: |
| 55 | + results = conn.execute(sqlalchemy.text(query)).fetchall() |
| 56 | + # load SQLAlchemy row objects into dicts |
| 57 | + items = [ |
| 58 | + {"data": json.loads(result[0]), "type": result[1]} for result in results |
| 59 | + ] |
| 60 | + messages = messages_from_dict(items) |
| 61 | + return messages |
| 62 | + |
| 63 | + def add_message(self, message: BaseMessage) -> None: |
| 64 | + """Append the message to the record in Cloud SQL""" |
| 65 | + query = f"INSERT INTO {self.table_name} (session_id, data, type) VALUES (:session_id, :data, :type);" |
| 66 | + with self.engine.connect() as conn: |
| 67 | + conn.execute( |
| 68 | + sqlalchemy.text(query), |
| 69 | + { |
| 70 | + "session_id": self.session_id, |
| 71 | + "data": json.dumps(message.dict()), |
| 72 | + "type": message.type, |
| 73 | + }, |
| 74 | + ) |
| 75 | + conn.commit() |
| 76 | + |
| 77 | + def clear(self) -> None: |
| 78 | + """Clear session memory from Cloud SQL""" |
| 79 | + query = f"DELETE FROM {self.table_name} WHERE session_id = :session_id;" |
| 80 | + with self.engine.connect() as conn: |
| 81 | + conn.execute(sqlalchemy.text(query), {"session_id": self.session_id}) |
| 82 | + conn.commit() |
0 commit comments