Add graph_query skill script
Browse files
openclaw/skills/graph_query/graph_query.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
OpenClaw Skill: graph_query
|
| 4 |
+
Query the TigerGraph knowledge graph using natural language.
|
| 5 |
+
"""
|
| 6 |
+
import json
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
import requests
|
| 10 |
+
|
| 11 |
+
API_BASE = os.getenv("GRAPHRAG_API_BASE", "http://localhost:3000")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def graph_query(query: str, depth: int = 2, top_k: int = 5) -> dict:
|
| 15 |
+
"""Query the knowledge graph via the GraphRAG API."""
|
| 16 |
+
try:
|
| 17 |
+
response = requests.post(
|
| 18 |
+
f"{API_BASE}/api/compare",
|
| 19 |
+
json={
|
| 20 |
+
"query": query,
|
| 21 |
+
"adaptiveRouting": True,
|
| 22 |
+
"hops": depth,
|
| 23 |
+
"topK": top_k,
|
| 24 |
+
},
|
| 25 |
+
timeout=60,
|
| 26 |
+
)
|
| 27 |
+
response.raise_for_status()
|
| 28 |
+
data = response.json()
|
| 29 |
+
|
| 30 |
+
return {
|
| 31 |
+
"query": query,
|
| 32 |
+
"entities": data.get("graphrag", {}).get("entities", []),
|
| 33 |
+
"relations": data.get("graphrag", {}).get("relations", []),
|
| 34 |
+
"answer": data.get("graphrag", {}).get("answer", ""),
|
| 35 |
+
"tokens_used": data.get("graphrag", {}).get("tokens", 0),
|
| 36 |
+
"cost_usd": data.get("graphrag", {}).get("costUsd", 0),
|
| 37 |
+
"complexity": data.get("complexity", 0),
|
| 38 |
+
"query_type": data.get("queryType", "unknown"),
|
| 39 |
+
"recommended_pipeline": data.get("recommended", ""),
|
| 40 |
+
}
|
| 41 |
+
except requests.exceptions.ConnectionError:
|
| 42 |
+
return {"error": f"Cannot connect to GraphRAG API at {API_BASE}. Is the server running?"}
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return {"error": str(e)}
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
query = " ".join(sys.argv[1:]) if len(sys.argv) > 1 else "What is GraphRAG?"
|
| 49 |
+
result = graph_query(query)
|
| 50 |
+
print(json.dumps(result, indent=2))
|