mirror of
https://github.com/hwchase17/langchain
synced 2024-11-18 09:25:54 +00:00
community: switch to falkordb python client (#20229)
This commit is contained in:
parent
f43b48aebc
commit
37a9e23c05
@ -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."
|
||||
|
@ -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 @@ class FalkorDBGraph(GraphStore):
|
||||
) -> 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
|
||||
except ImportError:
|
||||
try:
|
||||
# Falls back to using the redis package just for backwards compatibility
|
||||
self.__init_redis_connection(
|
||||
database, host, port, username, password, ssl
|
||||
)
|
||||
self._graph = Graph(self._driver, database)
|
||||
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 @@ class FalkorDBGraph(GraphStore):
|
||||
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"""
|
||||
|
Loading…
Reference in New Issue
Block a user