Added search mode and min degree filtering for NetworkX

- Implemented exact and inclusive search modes
- Added min degree filtering for nodes
- Updated API to parse label for search options
This commit is contained in:
yangdx
2025-03-04 16:08:05 +08:00
parent 735231d851
commit 002948d342
3 changed files with 70 additions and 7 deletions

View File

@@ -34,6 +34,11 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
2. Followed by nodes directly connected to the matching nodes
3. Finally, the degree of the nodes
Maximum number of nodes is limited to env MAX_GRAPH_NODES(default: 1000)
Control search mode by label content:
1. only label-name : exact search with the label name (selecting from the label list return previously)
2. label-name follow by '>n' : exact search of nodes with degree more than n
3. label-name follow by* : inclusive search of nodes with degree more than n
4. label-name follow by '>n*' : inclusive search
Args:
label (str): Label to get knowledge graph for
@@ -42,6 +47,37 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
Returns:
Dict[str, List[str]]: Knowledge graph for label
"""
return await rag.get_knowledge_graph(node_label=label, max_depth=max_depth)
# Parse label to extract search mode and min degree if specified
search_mode = "exact" # Default search mode
min_degree = 0 # Default minimum degree
original_label = label
# First check if label ends with *
if label.endswith("*"):
search_mode = "inclusive" # Always set to inclusive if ends with *
label = label[:-1].strip() # Remove trailing *
# Try to parse >n if it exists
if ">" in label:
try:
degree_pos = label.rfind(">")
degree_str = label[degree_pos + 1:].strip()
min_degree = int(degree_str) + 1
label = label[:degree_pos].strip()
except ValueError:
# If degree parsing fails, just remove * and keep the rest as label
label = original_label[:-1].strip()
# If no *, check for >n pattern
elif ">" in label:
try:
degree_pos = label.rfind(">")
degree_str = label[degree_pos + 1:].strip()
min_degree = int(degree_str) + 1
label = label[:degree_pos].strip()
except ValueError:
# If degree parsing fails, treat the whole string as label
label = original_label
return await rag.get_knowledge_graph(node_label=label, max_depth=max_depth, search_mode=search_mode, min_degree=min_degree)
return router