mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-12 06:13:36 +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",
|
"You can run the `falkordb` Docker container locally:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"```bash\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",
|
||||||
"\n",
|
"\n",
|
||||||
"Once launched, you create a database on the local machine and connect to it."
|
"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 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_document import GraphDocument
|
||||||
from langchain_community.graphs.graph_store import GraphStore
|
from langchain_community.graphs.graph_store import GraphStore
|
||||||
|
|
||||||
@ -58,18 +61,22 @@ class FalkorDBGraph(GraphStore):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Create a new FalkorDB graph wrapper instance."""
|
"""Create a new FalkorDB graph wrapper instance."""
|
||||||
try:
|
try:
|
||||||
import redis
|
self.__init_falkordb_connection(
|
||||||
from redis.commands.graph import Graph
|
database, host, port, username, password, ssl
|
||||||
except ImportError:
|
|
||||||
raise ImportError(
|
|
||||||
"Could not import redis python package. "
|
|
||||||
"Please install it with `pip install redis`."
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self._driver = redis.Redis(
|
except ImportError:
|
||||||
host=host, port=port, username=username, password=password, ssl=ssl
|
try:
|
||||||
)
|
# Falls back to using the redis package just for backwards compatibility
|
||||||
self._graph = Graph(self._driver, database)
|
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.schema: str = ""
|
||||||
self.structured_schema: Dict[str, Any] = {}
|
self.structured_schema: Dict[str, Any] = {}
|
||||||
|
|
||||||
@ -78,6 +85,53 @@ class FalkorDBGraph(GraphStore):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValueError(f"Could not refresh schema. Error: {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
|
@property
|
||||||
def get_schema(self) -> str:
|
def get_schema(self) -> str:
|
||||||
"""Returns the schema of the FalkorDB database"""
|
"""Returns the schema of the FalkorDB database"""
|
||||||
|
Loading…
Reference in New Issue
Block a user