From 1ec8199c8eb07d33f8509620e9b3bc88090a7dd3 Mon Sep 17 00:00:00 2001 From: Raunak Date: Thu, 22 Feb 2024 06:32:56 +0530 Subject: [PATCH] community[patch]: Added more functions in NetworkxEntityGraph class (#17624) - **Description:** 1. Added add_node(), remove_node(), has_node(), remove_edge(), has_edge() and get_neighbors() functions in NetworkxEntityGraph class. 2. Added the above functions in graph_networkx_qa.ipynb documentation. --- .../use_cases/graph/graph_networkx_qa.ipynb | 60 +++++++++++++++++++ .../graphs/networkx_graph.py | 28 +++++++++ 2 files changed, 88 insertions(+) diff --git a/docs/docs/use_cases/graph/graph_networkx_qa.ipynb b/docs/docs/use_cases/graph/graph_networkx_qa.ipynb index a01d7c11276..89325ce8f11 100644 --- a/docs/docs/use_cases/graph/graph_networkx_qa.ipynb +++ b/docs/docs/use_cases/graph/graph_networkx_qa.ipynb @@ -302,6 +302,66 @@ "loaded_graph.get_number_of_nodes()" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "7cc06554", + "metadata": {}, + "outputs": [], + "source": [ + "loaded_graph.add_node(\"NewNode\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f42deb48", + "metadata": {}, + "outputs": [], + "source": [ + "loaded_graph.has_node(\"NewNode\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e91bc6b9", + "metadata": {}, + "outputs": [], + "source": [ + "loaded_graph.remove_node(\"NewNode\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a1d1e745", + "metadata": {}, + "outputs": [], + "source": [ + "loaded_graph.get_neighbors(\"Intel\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eecea586", + "metadata": {}, + "outputs": [], + "source": [ + "loaded_graph.has_edge(\"Intel\", \"Silicon Valley\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1fdc612", + "metadata": {}, + "outputs": [], + "source": [ + "loaded_graph.remove_edge(\"Intel\", \"Silicon Valley\")" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/libs/community/langchain_community/graphs/networkx_graph.py b/libs/community/langchain_community/graphs/networkx_graph.py index ccc0bdad664..7cafed62536 100644 --- a/libs/community/langchain_community/graphs/networkx_graph.py +++ b/libs/community/langchain_community/graphs/networkx_graph.py @@ -139,6 +139,34 @@ class NetworkxEntityGraph: """Clear the graph edges.""" self._graph.clear_edges() + def add_node(self, node: str) -> None: + """Add node in the graph.""" + self._graph.add_node(node) + + def remove_node(self, node: str) -> None: + """Remove node from the graph.""" + if self._graph.has_node(node): + self._graph.remove_node(node) + + def has_node(self, node: str) -> bool: + """Return if graph has the given node.""" + return self._graph.has_node(node) + + def remove_edge(self, source_node: str, destination_node: str) -> None: + """Remove edge from the graph.""" + self._graph.remove_edge(source_node, destination_node) + + def has_edge(self, source_node: str, destination_node: str) -> bool: + """Return if graph has an edge between the given nodes.""" + if self._graph.has_node(source_node) and self._graph.has_node(destination_node): + return self._graph.has_edge(source_node, destination_node) + else: + return False + + def get_neighbors(self, node: str) -> List[str]: + """Return the neighbor nodes of the given node.""" + return self._graph.neighbors(node) + def get_number_of_nodes(self) -> int: """Get number of nodes in the graph.""" return self._graph.number_of_nodes()