Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Graph sampling with Neo4j (#766)

Wendong-Fan3c88d418a08d
diff --git a/camel/storages/graph_storages/neo4j_graph.py b/camel/storages/graph_storages/neo4j_graph.py
index 201f80a49..8ab6623d9 100644
--- a/camel/storages/graph_storages/neo4j_graph.py
+++ b/camel/storages/graph_storages/neo4j_graph.py
@@ -583,3 +583,141 @@ class Neo4jGraph(BaseGraphStorage):
]
},
)
+
+ def random_walk_with_restarts(
+ self,
+ graph_name: str,
+ sampling_ratio: float,
+ start_node_ids: List[int],
+ restart_probability: float = 0.1,
+ node_label_stratification: bool = False,
+ relationship_weight_property: Optional[str] = None,
+ ) -> Dict[str, Any]:
+ r"""Runs the Random Walk with Restarts (RWR) sampling algorithm.
+
+ Args:
+ graph_name (str): The name of the original graph in the graph
+ catalog.
+ sampling_ratio (float): The fraction of nodes in the original
+ graph to be sampled.
+ start_node_ids (List[int]): IDs of the initial set of nodes of the
+ original graph from which the sampling random walks will start.
+ restart_probability (float, optional): The probability that a
+ sampling random walk restarts from one of the start nodes.
+ Defaults to `0.1`.
+ node_label_stratification (bool, optional): If true, preserves the
+ node label distribution of the original graph. Defaults to
+ `False`.
+ relationship_weight_property (Optional[str], optional): Name of
+ the relationship property to use as weights. If unspecified,
+ the algorithm runs unweighted. Defaults to `None`.
+
+ Returns:
+ Dict[str, Any]: A dictionary with the results of the RWR sampling.
+ """
+ from neo4j.exceptions import ClientError, CypherSyntaxError
+
+ try:
+ self.query(query="CALL gds.version() YIELD version RETURN version")
+ except ClientError:
+ raise ValueError(
+ "Graph Data Science (GDS) library is not installed or not"
+ " available. Reference: https://neo4j.com/docs/graph-data-science/current/installation/"
+ )
+
+ query = """
+ CALL gds.graph.sample.rwr($graphName, $fromGraphName, {
+ samplingRatio: $samplingRatio,
+ startNodes: $startNodes,
+ restartProbability: $restartProbability,
+ nodeLabelStratification: $nodeLabelStratification,
+ relationshipWeightProperty: $relationshipWeightProperty
+ })
+ YIELD graphName, fromGraphName, nodeCount,
+ relationshipCount, startNodeCount, projectMillis
+ RETURN graphName, fromGraphName, nodeCount,
+ relationshipCount, startNodeCount, projectMillis
+ """
+
+ params = {
+ "graphName": f"{graph_name}_sampled",
+ "fromGraphName": graph_name,
+ "samplingRatio": sampling_ratio,
+ "startNodes": start_node_ids,
+ "restartProbability": restart_probability,
+ "nodeLabelStratification": node_label_stratification,
+ "relationshipWeightProperty": relationship_weight_property,
+ }
+
+ try:
+ result = self.query(query, params)
+ return result[0] if result else {}
+ except CypherSyntaxError as e:
+ raise ValueError(f"Generated Cypher Statement is not valid\n{e}")
+
+ def common_neighbour_aware_random_walk(
+ self,
+ graph_name: str,
+ sampling_ratio: float,
+ start_node_ids: List[int],
+ node_label_stratification: bool = False,
+ relationship_weight_property: Optional[str] = None,
+ ) -> Dict[str, Any]:
+ r"""Runs the Common Neighbour Aware Random Walk (CNARW) sampling
+ algorithm.
+
+ Args:
+ graph_name (str): The name of the original graph in the graph
+ catalog.
+ sampling_ratio (float): The fraction of nodes in the original
+ graph to be sampled.
+ start_node_ids (List[int]): IDs of the initial set of nodes of the
+ original graph from which the sampling random walks will start.
+ node_label_stratification (bool, optional): If true, preserves the
+ node label distribution of the original graph. Defaults to
+ `False`.
+ relationship_weight_property (Optional[str], optional): Name of
+ the relationship property to use as weights. If unspecified,
+ the algorithm runs unweighted. Defaults to `None`.
+
+ Returns:
+ Dict[str, Any]: A dictionary with the results of the CNARW
+ sampling.
+ """
+ from neo4j.exceptions import ClientError, CypherSyntaxError
+
+ try:
+ self.query(query="CALL gds.version() YIELD version RETURN version")
+ except ClientError:
+ raise ValueError(
+ "Graph Data Science (GDS) library is not installed or not"
+ " available. Reference: https://neo4j.com/docs/graph-data-science/current/installation/"
+ )
+
+ query = """
+ CALL gds.graph.sample.cnarw($graphName, $fromGraphName, {
+ samplingRatio: $samplingRatio,
+ startNodes: $startNodes,
+ nodeLabelStratification: $nodeLabelStratification,
+ relationshipWeightProperty: $relationshipWeightProperty
+ })
+ YIELD graphName, fromGraphName, nodeCount,
+ relationshipCount, startNodeCount, projectMillis
+ RETURN graphName, fromGraphName, nodeCount,
+ relationshipCount, startNodeCount, projectMillis
+ """
+
+ params = {
+ "graphName": f"{graph_name}_sampled_cnarw",
+ "fromGraphName": graph_name,
+ "samplingRatio": sampling_ratio,
+ "startNodes": start_node_ids,
+ "nodeLabelStratification": node_label_stratification,
+ "relationshipWeightProperty": relationship_weight_property,
+ }
+
+ try:
+ result = self.query(query, params)
+ return result[0] if result else {}
+ except CypherSyntaxError as e:
+ raise ValueError(f"Generated Cypher Statement is not valid\n{e}")
diff --git a/test/storages/graph_storages/test_neo4j_graph.py b/test/storages/graph_storages/test_neo4j_graph.py
index 696b012b9..0fe0d475c 100644
--- a/test/storages/graph_storages/test_neo4j_graph.py
+++ b/test/storages/graph_storages/test_neo4j_graph.py
@@ -13,6 +13,7 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
+import pytest
from unstructured.documents.elements import Element
from camel.storages import Neo4jGraph
@@ -28,9 +29,9 @@ from camel.storages.graph_storages.neo4j_graph import (
REL_QUERY,
)
-url = os.environ.get("NEO4J_URI", "default_url")
-username = os.environ.get("NEO4J_USERNAME", "default_username")
-password = os.environ.get("NEO4J_PASSWORD", "default_password")
+url = os.environ.get("NEO4J_URI", "Your_URI")
+username = os.environ.get("NEO4J_USERNAME", "Your_Username")
+password = os.environ.get("NEO4J_PASSWORD", "Your_Password")
test_data = [
@@ -264,3 +265,54 @@ def test_neo4j_filtering_labels() -> None:
# Assert both are empty
assert graph.structured_schema["node_props"] == {}
assert graph.structured_schema["relationships"] == []
+
+
[email protected](reason="Skipping since need GDS installation")
+def test_random_walk_with_restarts() -> None:
+ r"""Test the random walk with restarts sampling algorithm."""
+ graph = Neo4jGraph(
+ url=url, username=username, password=password, truncate=True
+ )
+ # Delete all nodes in the graph
+ graph.query("MATCH (n) DETACH DELETE n")
+ # Create sample graph
+ graph.query("""
+ CREATE (n1:Node {id: 1})
+ CREATE (n2:Node {id: 2})
+ CREATE (n3:Node {id: 3})
+ CREATE (n1)-[:REL_TYPE]->(n2)
+ CREATE (n2)-[:REL_TYPE]->(n3)
+ """)
+ # Run random walk with restarts
+ result = graph.random_walk_with_restarts(
+ graph_name="sample_graph",
+ sampling_ratio=0.5,
+ start_node_ids=[1],
+ restart_probability=0.15,
+ )
+ assert "graphName" in result
+ assert result["nodeCount"] > 0
+
+
[email protected](reason="Skipping since need GDS installation")
+def test_common_neighbour_aware_random_walk() -> None:
+ r"""Test the common neighbour aware random walk sampling algorithm."""
+ graph = Neo4jGraph(
+ url=url, username=username, password=password, truncate=True
+ )
+ # Delete all nodes in the graph
+ graph.query("MATCH (n) DETACH DELETE n")
+ # Create sample graph
+ graph.query("""
+ CREATE (n1:Node {id: 1})
+ CREATE (n2:Node {id: 2})
+ CREATE (n3:Node {id: 3})
+ CREATE (n1)-[:REL_TYPE]->(n2)
+ CREATE (n2)-[:REL_TYPE]->(n3)
+ """)
+ # Run common neighbour aware random walk
+ result = graph.common_neighbour_aware_random_walk(
+ graph_name="sample_graph", sampling_ratio=0.5, start_node_ids=[1]
+ )
+ assert "graphName" in result
+ assert result["nodeCount"] > 0