Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add SQLChatMessageHistory docstring #23978

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion libs/community/langchain_community/chat_message_histories/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"
Expand Down
Loading