Fix knowledge graph node not found (#4968) (#4970)

### What problem does this PR solve?

Fix knowledge graph node not found (#4968)

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
zhxlp 2025-02-17 11:49:27 +08:00 committed by GitHub
parent 810f997276
commit 194e8ea696
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -318,6 +318,8 @@ def knowledge_graph(kb_id):
if "nodes" in obj["graph"]:
obj["graph"]["nodes"] = sorted(obj["graph"]["nodes"], key=lambda x: x.get("pagerank", 0), reverse=True)[:256]
if "edges" in obj["graph"]:
obj["graph"]["edges"] = sorted(obj["graph"]["edges"], key=lambda x: x.get("weight", 0), reverse=True)[:128]
if "edges" in obj["graph"]:
node_id_set = { o["id"] for o in obj["graph"]["nodes"] }
filtered_edges = [o for o in obj["graph"]["edges"] if o["source"] != o["target"] and o["source"] in node_id_set and o["target"] in node_id_set]
obj["graph"]["edges"] = sorted(filtered_edges, key=lambda x: x.get("weight", 0), reverse=True)[:128]
return get_json_result(data=obj)