From 0b1370b13e5007ad7207ef7b82c73ad0fedc1f82 Mon Sep 17 00:00:00 2001 From: benx13 <32802278+benx13@users.noreply.github.com> Date: Thu, 3 Oct 2024 05:11:22 -0700 Subject: [PATCH] Update networkx_graph.py added support for pickle networkx graph import --- .../langchain_community/graphs/networkx_graph.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libs/community/langchain_community/graphs/networkx_graph.py b/libs/community/langchain_community/graphs/networkx_graph.py index 28e78fcc1a7ac..35fa34664cbae 100644 --- a/libs/community/langchain_community/graphs/networkx_graph.py +++ b/libs/community/langchain_community/graphs/networkx_graph.py @@ -89,7 +89,19 @@ def from_gml(cls, gml_path: str) -> NetworkxEntityGraph: ) graph = nx.read_gml(gml_path) return cls(graph) - + + @classmethod + def from_pickle(cls, pickle_path: str) -> NetworkxEntityGraph: + try: + import pickle + except ImportError: + raise ImportError( + "Could not import pickle python package. " + "Please install it with `pip install pickle`." + ) + graph = pickle.load(open(pickle_path, 'rb')) + return cls(graph) + def add_triple(self, knowledge_triple: KnowledgeTriple) -> None: """Add a triple to the graph.""" # Creates nodes if they don't exist