Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

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

zhxlp194e8ea696d1
diff --git a/api/apps/kb_app.py b/api/apps/kb_app.py
index fe4538b44e..9aadfa4d31 100644
--- a/api/apps/kb_app.py
+++ b/api/apps/kb_app.py
@@ -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)
\ No newline at end of file