diff --git a/libs/community/langchain_community/chat_message_histories/falkordb.py b/libs/community/langchain_community/chat_message_histories/falkordb.py index bbe9072841fb6..0ef7901a27537 100644 --- a/libs/community/langchain_community/chat_message_histories/falkordb.py +++ b/libs/community/langchain_community/chat_message_histories/falkordb.py @@ -1,7 +1,7 @@ -from typing import List, Optional, Union import os -import redis.exceptions +from typing import List, Optional, Union +import redis.exceptions from langchain_core.chat_history import BaseChatMessageHistory from langchain_core.messages import ( AIMessage, @@ -26,14 +26,20 @@ class FalkorDBChatMessageHistory(BaseChatMessageHistory): password (Optional[str]): Password for authenticating with FalkorDB. host (str): Host where the FalkorDB is running. Defaults to 'localhost'. port (int): Port number where the FalkorDB is running. Defaults to 6379. - node_label (str): Label for the session node in the graph. Defaults to "Session". - window (int): The number of messages to retrieve when querying the history. Defaults to 3. - ssl (bool): Whether to use SSL for connecting to the database. Defaults to False. - graph (Optional[FalkorDBGraph]): Optionally provide an existing FalkorDBGraph object for connecting. + node_label (str): Label for the session node + in the graph. Defaults to "Session". + window (int): The number of messages to retrieve when querying + the history. Defaults to 3. + ssl (bool): Whether to use SSL for connecting + to the database. Defaults to False. + graph (Optional[FalkorDBGraph]): Optionally provide an existing + FalkorDBGraph object for connecting. Example: .. code-block:: python - from langchain_community.chat_message_histories import FalkorDBChatMessageHistory + from langchain_community.chat_message_histories import ( + FalkorDBChatMessageHistory + ) history = FalkorDBChatMessageHistory( session_id="1234", @@ -56,7 +62,10 @@ def __init__( *, graph: Optional[FalkorDBGraph] = None, ) -> None: - """Initialize the FalkorDBChatMessageHistory class with the session and connection details.""" + """ + Initialize the FalkorDBChatMessageHistory + class with the session and connection details. + """ try: import falkordb except ImportError: @@ -115,21 +124,23 @@ def __init__( if "already indexed" in e: raise ValueError(f"{self._node_label} has already been indexed") - def _process_records(self, records: list) -> list: + def _process_records(self, records: list) -> List[BaseMessage]: """Process the records from FalkorDB and convert them into BaseMessage objects. Args: records (list): The raw records fetched from the FalkorDB query. Returns: - list: A list of `BaseMessage` objects. + List[BaseMessage]: A list of `BaseMessage` objects. """ - messages = [] + # Explicitly set messages as a list of BaseMessage + messages: List[BaseMessage] = [] for record in records: content = record[0].get("data", {}).get("content", "") message_type = record[0].get("type", "").lower() + # Append the correct message type to the list if message_type == "human": messages.append( HumanMessage( @@ -143,7 +154,7 @@ def _process_records(self, records: list) -> list: ) ) else: - print(f"Unknown message type: {message_type}") + raise ValueError(f"Unknown message type: {message_type}") return messages diff --git a/libs/community/tests/integration_tests/chat_message_histories/test_falkordb_chat_message_history.py b/libs/community/tests/integration_tests/chat_message_histories/test_falkordb_chat_message_history.py index 998faa9f8c1cf..5482b527c5fee 100644 --- a/libs/community/tests/integration_tests/chat_message_histories/test_falkordb_chat_message_history.py +++ b/libs/community/tests/integration_tests/chat_message_histories/test_falkordb_chat_message_history.py @@ -2,9 +2,9 @@ Integration tests for FalkorDB Chat History/Memory functionality. Note: -These tests are conducted using a local FalkorDB instance but can also +These tests are conducted using a local FalkorDB instance but can also be run against a Cloud FalkorDB instance. Ensure that appropriate host,port -cusername, and password configurations are set up +cusername, and password configurations are set up before running the tests. Test Cases: @@ -14,11 +14,15 @@ when passing the FalkorDB driver through a graph object. """ + from langchain_core.messages import AIMessage, HumanMessage -from langchain_community.chat_message_histories.falkordb import FalkorDBChatMessageHistory +from langchain_community.chat_message_histories.falkordb import ( + FalkorDBChatMessageHistory, +) from langchain_community.graphs import FalkorDBGraph + def test_add_messages() -> None: """Basic testing: add messages to the FalkorDBChatMessageHistory.""" message_store = FalkorDBChatMessageHistory("500daysofSadiya")