Skip to content

The agent engineering platform.

community[patch]: Added more functions in NetworkxEntityGraph class (#17624)

Raunak1ec8199c8eb0
diff --git a/docs/docs/use_cases/graph/graph_networkx_qa.ipynb b/docs/docs/use_cases/graph/graph_networkx_qa.ipynb
index a01d7c1127..89325ce8f1 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 ccc0bdad66..7cafed6253 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()