From 9bcf8f867dca32e23723dd94fe6535be536a6c28 Mon Sep 17 00:00:00 2001 From: maang-h <55082429+maang-h@users.noreply.github.com> Date: Thu, 11 Jul 2024 22:24:28 +0800 Subject: [PATCH] docs: Add SQLChatMessageHistory docstring (#23978) - **Description:** Add SQLChatMessageHistory docstring. - **Issue:** the issue #21983 Co-authored-by: Eugene Yurtsev --- .../chat_message_histories/sql.py | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/libs/community/langchain_community/chat_message_histories/sql.py b/libs/community/langchain_community/chat_message_histories/sql.py index edfed45466fe4..cb839b4c3aecc 100644 --- a/libs/community/langchain_community/chat_message_histories/sql.py +++ b/libs/community/langchain_community/chat_message_histories/sql.py @@ -113,7 +113,33 @@ def get_sql_model_class(self) -> Any: class SQLChatMessageHistory(BaseChatMessageHistory): - """Chat message history stored in an SQL database.""" + """Chat message history stored in an SQL database. + + Example: + .. code-block:: python + + from langchain_core.messages import HumanMessage + + from langchain_community.chat_message_histories import SQLChatMessageHistory + + # create sync sql message history by connection_string + message_history = SQLChatMessageHistory( + session_id='foo', connection_string='sqlite///:memory.db' + ) + message_history.add_message(HumanMessage("hello")) + message_history.message + + # create async sql message history using aiosqlite + # from sqlalchemy.ext.asyncio import create_async_engine + # + # async_engine = create_async_engine("sqlite+aiosqlite:///memory.db") + # async_message_history = SQLChatMessageHistory( + # session_id='foo', connection=async_engine, + # ) + # await async_message_history.aadd_message(HumanMessage("hello")) + # await async_message_history.aget_messages() + + """ @property @deprecated("0.2.2", removal="0.3.0", alternative="session_maker") @@ -131,6 +157,21 @@ def __init__( engine_args: Optional[Dict[str, Any]] = None, async_mode: Optional[bool] = None, # Use only if connection is a string ): + """Initialize with a SQLChatMessageHistory instance. + + Args: + session_id: Indicates the id of the same session. + connection_string: String parameter configuration for connecting + to the database. + table_name: Table name used to save data. + session_id_field_name: The name of field of `session_id`. + custom_message_converter: Custom message converter for converting + database data and `BaseMessage` + connection: Database connection object, which can be a string containing + connection configuration, Engine object or AsyncEngine object. + engine_args: Additional configuration for creating database engines. + async_mode: Whether it is an asynchronous connection. + """ assert not ( connection_string and connection ), "connection_string and connection are mutually exclusive"