Commit
·
a0b7cd2
1
Parent(s):
c69669a
add all option to get_related chunks
Browse files- gradio_mcp_space.py +16 -6
gradio_mcp_space.py
CHANGED
|
@@ -670,14 +670,24 @@ def get_related_chunks(chunk_id: str, relation_type: str = "calls") -> str:
|
|
| 670 |
return f"Error: Chunk '{chunk_id}' not found in knowledge graph"
|
| 671 |
|
| 672 |
related = []
|
| 673 |
-
|
| 674 |
-
|
|
|
|
| 675 |
target_node = knowledge_graph.graph.nodes[target]['data']
|
| 676 |
related.append({
|
| 677 |
"id": target,
|
| 678 |
"file_path": getattr(target_node, 'path', 'Unknown'),
|
| 679 |
"entity_name": attrs.get('entity_name')
|
| 680 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
|
| 682 |
if not related:
|
| 683 |
return f"No chunks found with '{relation_type}' relationship from '{chunk_id}'"
|
|
@@ -1311,13 +1321,13 @@ def find_path(source_id: str, target_id: str, max_depth: int = 5) -> str:
|
|
| 1311 |
@observe(as_type="tool")
|
| 1312 |
def get_subgraph(node_id: str, depth: int = 2, edge_types: Optional[str] = None) -> str:
|
| 1313 |
"""
|
| 1314 |
-
|
| 1315 |
|
| 1316 |
Optionally filters by edge types (comma-separated).
|
| 1317 |
|
| 1318 |
Args:
|
| 1319 |
node_id: The ID of the central node
|
| 1320 |
-
depth: The depth/radius of the subgraph to
|
| 1321 |
edge_types: Optional comma-separated list of edge types (e.g., 'calls,contains')
|
| 1322 |
|
| 1323 |
Returns:
|
|
@@ -1997,7 +2007,7 @@ def create_gradio_app():
|
|
| 1997 |
with gr.Row():
|
| 1998 |
with gr.Column():
|
| 1999 |
related_chunk_id = gr.Textbox(label="Chunk ID", placeholder="Enter chunk ID...")
|
| 2000 |
-
relation_type = gr.Dropdown(choices=["calls", "contains", "declares", "uses"], label="Relation Type", value="calls")
|
| 2001 |
related_btn = gr.Button("Get Related Chunks", variant="primary")
|
| 2002 |
with gr.Column():
|
| 2003 |
related_output = gr.Textbox(label="Related Chunks", lines=20, max_lines=30)
|
|
@@ -2057,7 +2067,7 @@ def create_gradio_app():
|
|
| 2057 |
subgraph_node = gr.Textbox(label="Center Node ID", placeholder="Enter node ID...")
|
| 2058 |
subgraph_depth = gr.Slider(1, 5, value=2, step=1, label="Depth")
|
| 2059 |
subgraph_edge_types = gr.Textbox(label="Edge Types (comma-separated, optional)", placeholder="e.g., calls,contains")
|
| 2060 |
-
subgraph_btn = gr.Button("
|
| 2061 |
with gr.Column():
|
| 2062 |
subgraph_output = gr.Textbox(label="Subgraph", lines=20, max_lines=30)
|
| 2063 |
subgraph_btn.click(fn=get_subgraph, inputs=[subgraph_node, subgraph_depth, subgraph_edge_types], outputs=subgraph_output)
|
|
|
|
| 670 |
return f"Error: Chunk '{chunk_id}' not found in knowledge graph"
|
| 671 |
|
| 672 |
related = []
|
| 673 |
+
if relation_type == "" or relation_type == "all":
|
| 674 |
+
# Get all outgoing edges regardless of relation type
|
| 675 |
+
for _, target, attrs in knowledge_graph.graph.out_edges(chunk_id, data=True):
|
| 676 |
target_node = knowledge_graph.graph.nodes[target]['data']
|
| 677 |
related.append({
|
| 678 |
"id": target,
|
| 679 |
"file_path": getattr(target_node, 'path', 'Unknown'),
|
| 680 |
"entity_name": attrs.get('entity_name')
|
| 681 |
})
|
| 682 |
+
else:
|
| 683 |
+
for _, target, attrs in knowledge_graph.graph.out_edges(chunk_id, data=True):
|
| 684 |
+
if attrs.get('relation') == relation_type:
|
| 685 |
+
target_node = knowledge_graph.graph.nodes[target]['data']
|
| 686 |
+
related.append({
|
| 687 |
+
"id": target,
|
| 688 |
+
"file_path": getattr(target_node, 'path', 'Unknown'),
|
| 689 |
+
"entity_name": attrs.get('entity_name')
|
| 690 |
+
})
|
| 691 |
|
| 692 |
if not related:
|
| 693 |
return f"No chunks found with '{relation_type}' relationship from '{chunk_id}'"
|
|
|
|
| 1321 |
@observe(as_type="tool")
|
| 1322 |
def get_subgraph(node_id: str, depth: int = 2, edge_types: Optional[str] = None) -> str:
|
| 1323 |
"""
|
| 1324 |
+
Retrieve a subgraph around a node up to a specified depth.
|
| 1325 |
|
| 1326 |
Optionally filters by edge types (comma-separated).
|
| 1327 |
|
| 1328 |
Args:
|
| 1329 |
node_id: The ID of the central node
|
| 1330 |
+
depth: The depth/radius of the subgraph to Retrieve (default: 2)
|
| 1331 |
edge_types: Optional comma-separated list of edge types (e.g., 'calls,contains')
|
| 1332 |
|
| 1333 |
Returns:
|
|
|
|
| 2007 |
with gr.Row():
|
| 2008 |
with gr.Column():
|
| 2009 |
related_chunk_id = gr.Textbox(label="Chunk ID", placeholder="Enter chunk ID...")
|
| 2010 |
+
relation_type = gr.Dropdown(choices=["" , "calls", "contains", "declares", "uses"], label="Relation Type", value="calls")
|
| 2011 |
related_btn = gr.Button("Get Related Chunks", variant="primary")
|
| 2012 |
with gr.Column():
|
| 2013 |
related_output = gr.Textbox(label="Related Chunks", lines=20, max_lines=30)
|
|
|
|
| 2067 |
subgraph_node = gr.Textbox(label="Center Node ID", placeholder="Enter node ID...")
|
| 2068 |
subgraph_depth = gr.Slider(1, 5, value=2, step=1, label="Depth")
|
| 2069 |
subgraph_edge_types = gr.Textbox(label="Edge Types (comma-separated, optional)", placeholder="e.g., calls,contains")
|
| 2070 |
+
subgraph_btn = gr.Button("Retrieve Subgraph", variant="primary")
|
| 2071 |
with gr.Column():
|
| 2072 |
subgraph_output = gr.Textbox(label="Subgraph", lines=20, max_lines=30)
|
| 2073 |
subgraph_btn.click(fn=get_subgraph, inputs=[subgraph_node, subgraph_depth, subgraph_edge_types], outputs=subgraph_output)
|