Commit
·
578a211
1
Parent(s):
d874b77
add pagination to get_neighbours
Browse files- gradio_mcp_space.py +37 -6
gradio_mcp_space.py
CHANGED
|
@@ -415,7 +415,7 @@ def list_nodes_by_type(node_type: str, limit: int = 20, page: int = 1) -> str:
|
|
| 415 |
|
| 416 |
|
| 417 |
@observe(as_type="tool")
|
| 418 |
-
def get_neighbors(node_id: str) -> str:
|
| 419 |
"""
|
| 420 |
Get all nodes directly connected to a given node.
|
| 421 |
|
|
@@ -434,14 +434,42 @@ def get_neighbors(node_id: str) -> str:
|
|
| 434 |
if node_id not in knowledge_graph.graph:
|
| 435 |
return f"Error: Node '{node_id}' not found in knowledge graph"
|
| 436 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
neighbors = knowledge_graph.get_neighbors(node_id)
|
| 438 |
if not neighbors:
|
| 439 |
return f"No neighbors found for node '{node_id}'"
|
| 440 |
|
| 441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
result += "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n"
|
| 443 |
|
| 444 |
-
for i, neighbor in enumerate(
|
| 445 |
result += f"{i}. {neighbor.id}\n"
|
| 446 |
result += f" Name: {getattr(neighbor, 'name', 'Unknown')}\n"
|
| 447 |
result += f" Type: {neighbor.node_type}\n"
|
|
@@ -454,8 +482,9 @@ def get_neighbors(node_id: str) -> str:
|
|
| 454 |
result += f" ← Relation: {edge_data.get('relation', 'Unknown')}\n"
|
| 455 |
result += "\n"
|
| 456 |
|
| 457 |
-
|
| 458 |
-
|
|
|
|
| 459 |
|
| 460 |
return result
|
| 461 |
except Exception as e:
|
|
@@ -1944,10 +1973,12 @@ def create_gradio_app():
|
|
| 1944 |
with gr.Row():
|
| 1945 |
with gr.Column():
|
| 1946 |
neighbor_node_id = gr.Textbox(label="Node ID", placeholder="Enter node ID...")
|
|
|
|
|
|
|
| 1947 |
neighbor_btn = gr.Button("Get Neighbors", variant="primary")
|
| 1948 |
with gr.Column():
|
| 1949 |
neighbor_output = gr.Textbox(label="Neighbors", lines=20, max_lines=30)
|
| 1950 |
-
neighbor_btn.click(fn=get_neighbors, inputs=neighbor_node_id, outputs=neighbor_output)
|
| 1951 |
gr.Markdown(_tool_doc_md(get_neighbors))
|
| 1952 |
|
| 1953 |
gr.Markdown("---")
|
|
|
|
| 415 |
|
| 416 |
|
| 417 |
@observe(as_type="tool")
|
| 418 |
+
def get_neighbors(node_id: str, limit: int = 20, page: int = 1) -> str:
|
| 419 |
"""
|
| 420 |
Get all nodes directly connected to a given node.
|
| 421 |
|
|
|
|
| 434 |
if node_id not in knowledge_graph.graph:
|
| 435 |
return f"Error: Node '{node_id}' not found in knowledge graph"
|
| 436 |
|
| 437 |
+
# Convert limit/page to int if they're strings
|
| 438 |
+
if isinstance(limit, str):
|
| 439 |
+
try:
|
| 440 |
+
limit = int(limit)
|
| 441 |
+
except ValueError:
|
| 442 |
+
return f"Error: 'limit' must be an integer, got '{limit}'"
|
| 443 |
+
|
| 444 |
+
if isinstance(page, str):
|
| 445 |
+
try:
|
| 446 |
+
page = int(page)
|
| 447 |
+
except ValueError:
|
| 448 |
+
return f"Error: 'page' must be an integer, got '{page}'"
|
| 449 |
+
|
| 450 |
+
if limit <= 0:
|
| 451 |
+
return "Error: limit must be a positive integer"
|
| 452 |
+
if page < 1:
|
| 453 |
+
return "Error: 'page' must be a positive integer (1 or greater)"
|
| 454 |
+
|
| 455 |
neighbors = knowledge_graph.get_neighbors(node_id)
|
| 456 |
if not neighbors:
|
| 457 |
return f"No neighbors found for node '{node_id}'"
|
| 458 |
|
| 459 |
+
total = len(neighbors)
|
| 460 |
+
# Pagination
|
| 461 |
+
total_pages = (total + limit - 1) // limit
|
| 462 |
+
if page > total_pages:
|
| 463 |
+
return f"Error: Page {page} does not exist. Total pages: {total_pages} (with {total} neighbors at {limit} per page)"
|
| 464 |
+
|
| 465 |
+
start_idx = (page - 1) * limit
|
| 466 |
+
end_idx = start_idx + limit
|
| 467 |
+
page_slice = neighbors[start_idx:end_idx]
|
| 468 |
+
|
| 469 |
+
result = f"Neighbors of '{node_id}' (Page {page}/{total_pages}, {total} total):\n"
|
| 470 |
result += "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n"
|
| 471 |
|
| 472 |
+
for i, neighbor in enumerate(page_slice, start=start_idx + 1):
|
| 473 |
result += f"{i}. {neighbor.id}\n"
|
| 474 |
result += f" Name: {getattr(neighbor, 'name', 'Unknown')}\n"
|
| 475 |
result += f" Type: {neighbor.node_type}\n"
|
|
|
|
| 482 |
result += f" ← Relation: {edge_data.get('relation', 'Unknown')}\n"
|
| 483 |
result += "\n"
|
| 484 |
|
| 485 |
+
# Pagination hint
|
| 486 |
+
if page < total_pages:
|
| 487 |
+
result += f"Use page={page + 1} to see the next page\n"
|
| 488 |
|
| 489 |
return result
|
| 490 |
except Exception as e:
|
|
|
|
| 1973 |
with gr.Row():
|
| 1974 |
with gr.Column():
|
| 1975 |
neighbor_node_id = gr.Textbox(label="Node ID", placeholder="Enter node ID...")
|
| 1976 |
+
neighbor_limit = gr.Slider(1, 100, value=20, step=1, label="Max Results")
|
| 1977 |
+
neighbor_page = gr.Slider(1, 100, value=1, step=1, label="Page")
|
| 1978 |
neighbor_btn = gr.Button("Get Neighbors", variant="primary")
|
| 1979 |
with gr.Column():
|
| 1980 |
neighbor_output = gr.Textbox(label="Neighbors", lines=20, max_lines=30)
|
| 1981 |
+
neighbor_btn.click(fn=get_neighbors, inputs=[neighbor_node_id, neighbor_limit, neighbor_page], outputs=neighbor_output)
|
| 1982 |
gr.Markdown(_tool_doc_md(get_neighbors))
|
| 1983 |
|
| 1984 |
gr.Markdown("---")
|