Skip to content

Commit

Permalink
community: switch to falkordb python client (#20229)
Browse files Browse the repository at this point in the history
  • Loading branch information
efriis authored Apr 9, 2024
1 parent f43b48a commit 37a9e23
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/docs/integrations/graphs/falkordb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
74 changes: 64 additions & 10 deletions libs/community/langchain_community/graphs/falkordb_graph.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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] = {}

Expand All @@ -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"""
Expand Down

0 comments on commit 37a9e23

Please sign in to comment.