Simplified graph retrival by removing inclusive and min_degree parameters
This commit is contained in:
@@ -41,7 +41,7 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
|
|||||||
label (str): Label to get knowledge graph for
|
label (str): Label to get knowledge graph for
|
||||||
max_depth (int, optional): Maximum depth of graph. Defaults to 3.
|
max_depth (int, optional): Maximum depth of graph. Defaults to 3.
|
||||||
inclusive_search (bool, optional): If True, search for nodes that include the label. Defaults to False.
|
inclusive_search (bool, optional): If True, search for nodes that include the label. Defaults to False.
|
||||||
min_degree (int, optional): Minimum degree of nodes. Defaults to 0.
|
min_degree (int, optional): Minimum degree of nodes. Defaults to 0. (Deprecated, always 0)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, List[str]]: Knowledge graph for label
|
Dict[str, List[str]]: Knowledge graph for label
|
||||||
@@ -49,8 +49,6 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
|
|||||||
return await rag.get_knowledge_graph(
|
return await rag.get_knowledge_graph(
|
||||||
node_label=label,
|
node_label=label,
|
||||||
max_depth=max_depth,
|
max_depth=max_depth,
|
||||||
inclusive=inclusive,
|
|
||||||
min_degree=min_degree,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return router
|
return router
|
||||||
|
@@ -614,22 +614,19 @@ class Neo4JStorage(BaseGraphStorage):
|
|||||||
self,
|
self,
|
||||||
node_label: str,
|
node_label: str,
|
||||||
max_depth: int = 3,
|
max_depth: int = 3,
|
||||||
min_degree: int = 0,
|
max_nodes: int = MAX_GRAPH_NODES,
|
||||||
inclusive: bool = False,
|
|
||||||
) -> KnowledgeGraph:
|
) -> KnowledgeGraph:
|
||||||
"""
|
"""
|
||||||
Retrieve a connected subgraph of nodes where the label includes the specified `node_label`.
|
Retrieve a connected subgraph of nodes where the label includes the specified `node_label`.
|
||||||
Maximum number of nodes is constrained by the environment variable `MAX_GRAPH_NODES` (default: 1000).
|
Maximum number of nodes is constrained by the environment variable `MAX_GRAPH_NODES` (default: 1000).
|
||||||
When reducing the number of nodes, the prioritization criteria are as follows:
|
When reducing the number of nodes, the prioritization criteria are as follows:
|
||||||
1. min_degree does not affect nodes directly connected to the matching nodes
|
1. Label matching nodes take precedence
|
||||||
2. Label matching nodes take precedence
|
2. Followed by nodes directly connected to the matching nodes
|
||||||
3. Followed by nodes directly connected to the matching nodes
|
3. Finally, the degree of the nodes
|
||||||
4. Finally, the degree of the nodes
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
node_label: Label of the starting node
|
node_label: Label of the starting node
|
||||||
max_depth: Maximum depth of the subgraph
|
max_depth: Maximum depth of the subgraph
|
||||||
min_degree: Minimum degree of nodes to include. Defaults to 0
|
|
||||||
inclusive: Do an inclusive search if true
|
inclusive: Do an inclusive search if true
|
||||||
Returns:
|
Returns:
|
||||||
KnowledgeGraph: Complete connected subgraph for specified node
|
KnowledgeGraph: Complete connected subgraph for specified node
|
||||||
@@ -647,7 +644,6 @@ class Neo4JStorage(BaseGraphStorage):
|
|||||||
MATCH (n)
|
MATCH (n)
|
||||||
OPTIONAL MATCH (n)-[r]-()
|
OPTIONAL MATCH (n)-[r]-()
|
||||||
WITH n, COALESCE(count(r), 0) AS degree
|
WITH n, COALESCE(count(r), 0) AS degree
|
||||||
WHERE degree >= $min_degree
|
|
||||||
ORDER BY degree DESC
|
ORDER BY degree DESC
|
||||||
LIMIT $max_nodes
|
LIMIT $max_nodes
|
||||||
WITH collect({node: n}) AS filtered_nodes
|
WITH collect({node: n}) AS filtered_nodes
|
||||||
@@ -660,18 +656,14 @@ class Neo4JStorage(BaseGraphStorage):
|
|||||||
"""
|
"""
|
||||||
result_set = await session.run(
|
result_set = await session.run(
|
||||||
main_query,
|
main_query,
|
||||||
{"max_nodes": MAX_GRAPH_NODES, "min_degree": min_degree},
|
{"max_nodes": max_nodes},
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Main query uses partial matching
|
# Main query uses partial matching
|
||||||
main_query = """
|
main_query = """
|
||||||
MATCH (start)
|
MATCH (start)
|
||||||
WHERE
|
WHERE start.entity_id = $entity_id
|
||||||
CASE
|
|
||||||
WHEN $inclusive THEN start.entity_id CONTAINS $entity_id
|
|
||||||
ELSE start.entity_id = $entity_id
|
|
||||||
END
|
|
||||||
WITH start
|
WITH start
|
||||||
CALL apoc.path.subgraphAll(start, {
|
CALL apoc.path.subgraphAll(start, {
|
||||||
relationshipFilter: '',
|
relationshipFilter: '',
|
||||||
@@ -684,13 +676,11 @@ class Neo4JStorage(BaseGraphStorage):
|
|||||||
UNWIND nodes AS node
|
UNWIND nodes AS node
|
||||||
OPTIONAL MATCH (node)-[r]-()
|
OPTIONAL MATCH (node)-[r]-()
|
||||||
WITH node, COALESCE(count(r), 0) AS degree, start, nodes, relationships
|
WITH node, COALESCE(count(r), 0) AS degree, start, nodes, relationships
|
||||||
WHERE node = start OR EXISTS((start)--(node)) OR degree >= $min_degree
|
|
||||||
ORDER BY
|
ORDER BY
|
||||||
CASE
|
CASE
|
||||||
WHEN node = start THEN 3
|
WHEN node = start THEN 0
|
||||||
WHEN EXISTS((start)--(node)) THEN 2
|
ELSE length(shortestPath((start)--(node)))
|
||||||
ELSE 1
|
END ASC,
|
||||||
END DESC,
|
|
||||||
degree DESC
|
degree DESC
|
||||||
LIMIT $max_nodes
|
LIMIT $max_nodes
|
||||||
WITH collect({node: node}) AS filtered_nodes
|
WITH collect({node: node}) AS filtered_nodes
|
||||||
@@ -704,11 +694,9 @@ class Neo4JStorage(BaseGraphStorage):
|
|||||||
result_set = await session.run(
|
result_set = await session.run(
|
||||||
main_query,
|
main_query,
|
||||||
{
|
{
|
||||||
"max_nodes": MAX_GRAPH_NODES,
|
"max_nodes": max_nodes,
|
||||||
"entity_id": node_label,
|
"entity_id": node_label,
|
||||||
"inclusive": inclusive,
|
|
||||||
"max_depth": max_depth,
|
"max_depth": max_depth,
|
||||||
"min_degree": min_degree,
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -759,19 +747,13 @@ class Neo4JStorage(BaseGraphStorage):
|
|||||||
logger.warning(
|
logger.warning(
|
||||||
"Neo4j: falling back to basic Cypher recursive search..."
|
"Neo4j: falling back to basic Cypher recursive search..."
|
||||||
)
|
)
|
||||||
if inclusive:
|
|
||||||
logger.warning(
|
|
||||||
"Neo4j: inclusive search mode is not supported in recursive query, using exact matching"
|
|
||||||
)
|
|
||||||
return await self._robust_fallback(
|
return await self._robust_fallback(
|
||||||
node_label, max_depth, min_degree
|
node_label, max_depth, max_nodes
|
||||||
)
|
)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
async def _robust_fallback(
|
async def _robust_fallback(self, node_label: str, max_depth: int, max_nodes: int) -> KnowledgeGraph:
|
||||||
self, node_label: str, max_depth: int, min_degree: int = 0
|
|
||||||
) -> KnowledgeGraph:
|
|
||||||
"""
|
"""
|
||||||
Fallback implementation when APOC plugin is not available or incompatible.
|
Fallback implementation when APOC plugin is not available or incompatible.
|
||||||
This method implements the same functionality as get_knowledge_graph but uses
|
This method implements the same functionality as get_knowledge_graph but uses
|
||||||
@@ -790,7 +772,7 @@ class Neo4JStorage(BaseGraphStorage):
|
|||||||
if current_depth > max_depth:
|
if current_depth > max_depth:
|
||||||
logger.debug(f"Reached max depth: {max_depth}")
|
logger.debug(f"Reached max depth: {max_depth}")
|
||||||
return
|
return
|
||||||
if len(visited_nodes) >= MAX_GRAPH_NODES:
|
if len(visited_nodes) >= max_nodes:
|
||||||
logger.debug(f"Reached max nodes limit: {MAX_GRAPH_NODES}")
|
logger.debug(f"Reached max nodes limit: {MAX_GRAPH_NODES}")
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -816,8 +798,8 @@ class Neo4JStorage(BaseGraphStorage):
|
|||||||
await results.consume() # Ensure results are consumed
|
await results.consume() # Ensure results are consumed
|
||||||
|
|
||||||
# Nodes not connected to start node need to check degree
|
# Nodes not connected to start node need to check degree
|
||||||
if current_depth > 1 and len(records) < min_degree:
|
# if current_depth > 1 and len(records) < min_degree:
|
||||||
return
|
# return
|
||||||
|
|
||||||
# Add current node to result
|
# Add current node to result
|
||||||
result.nodes.append(node)
|
result.nodes.append(node)
|
||||||
|
Reference in New Issue
Block a user