diff --git a/docs/docs/integrations/graphs/falkordb.ipynb b/docs/docs/integrations/graphs/falkordb.ipynb index 397d2b2d36e42..917830fd2d5f7 100644 --- a/docs/docs/integrations/graphs/falkordb.ipynb +++ b/docs/docs/integrations/graphs/falkordb.ipynb @@ -22,7 +22,7 @@ "You can run the `falkordb` Docker container locally:\n", "\n", "```bash\n", - "docker run -p 6379:6379 -it --rm falkordb/falkordb:edge\n", + "docker run -p 6379:6379 -it --rm falkordb/falkordb\n", "```\n", "\n", "Once launched, you create a database on the local machine and connect to it." diff --git a/libs/community/langchain_community/graphs/falkordb_graph.py b/libs/community/langchain_community/graphs/falkordb_graph.py index e23d01d4ed6f2..99e3e4592b3e9 100644 --- a/libs/community/langchain_community/graphs/falkordb_graph.py +++ b/libs/community/langchain_community/graphs/falkordb_graph.py @@ -1,5 +1,8 @@ +import warnings from typing import Any, Dict, List, Optional +from langchain_core._api import deprecated + from langchain_community.graphs.graph_document import GraphDocument from langchain_community.graphs.graph_store import GraphStore @@ -58,18 +61,22 @@ def __init__( ) -> None: """Create a new FalkorDB graph wrapper instance.""" try: - import redis - from redis.commands.graph import Graph - except ImportError: - raise ImportError( - "Could not import redis python package. " - "Please install it with `pip install redis`." + self.__init_falkordb_connection( + database, host, port, username, password, ssl ) - self._driver = redis.Redis( - host=host, port=port, username=username, password=password, ssl=ssl - ) - self._graph = Graph(self._driver, database) + except ImportError: + try: + # Falls back to using the redis package just for backwards compatibility + self.__init_redis_connection( + database, host, port, username, password, ssl + ) + except ImportError: + raise ImportError( + "Could not import falkordb python package. " + "Please install it with `pip install falkordb`." + ) + self.schema: str = "" self.structured_schema: Dict[str, Any] = {} @@ -78,6 +85,53 @@ def __init__( except Exception as e: raise ValueError(f"Could not refresh schema. Error: {e}") + def __init_falkordb_connection( + self, + database: str, + host: str = "localhost", + port: int = 6379, + username: Optional[str] = None, + password: Optional[str] = None, + ssl: bool = False, + ) -> None: + from falkordb import FalkorDB + + try: + self._driver = FalkorDB( + host=host, port=port, username=username, password=password, ssl=ssl + ) + except Exception as e: + raise ConnectionError(f"Failed to connect to FalkorDB: {e}") + + self._graph = self._driver.select_graph(database) + + @deprecated("0.0.31", alternative="__init_falkordb_connection") + def __init_redis_connection( + self, + database: str, + host: str = "localhost", + port: int = 6379, + username: Optional[str] = None, + password: Optional[str] = None, + ssl: bool = False, + ) -> None: + import redis + from redis.commands.graph import Graph + + # show deprecation warning + warnings.warn( + "Using the redis package is deprecated. " + "Please use the falkordb package instead, " + "install it with `pip install falkordb`.", + DeprecationWarning, + ) + + self._driver = redis.Redis( + host=host, port=port, username=username, password=password, ssl=ssl + ) + + self._graph = Graph(self._driver, database) + @property def get_schema(self) -> str: """Returns the schema of the FalkorDB database"""