parent
72b9fadf89
commit
a1d5a91d71
@ -0,0 +1,40 @@
|
|||||||
|
# conda activate rag
|
||||||
|
# pip install py2neo pyvis
|
||||||
|
from py2neo import Graph
|
||||||
|
from pyvis.network import Network
|
||||||
|
from Config import Config
|
||||||
|
|
||||||
|
# 连接Neo4j
|
||||||
|
graph = Graph(Config.NEO4J_URI, auth=Config.NEO4J_AUTH)
|
||||||
|
|
||||||
|
# 查询所有节点和关系
|
||||||
|
query = """
|
||||||
|
MATCH (n)-[r]->(m)
|
||||||
|
RETURN n, r, m
|
||||||
|
"""
|
||||||
|
data = graph.run(query).data()
|
||||||
|
|
||||||
|
# 创建网络图
|
||||||
|
net = Network(height="750px", width="100%", notebook=True, cdn_resources='in_line')
|
||||||
|
|
||||||
|
# 添加节点和边
|
||||||
|
for item in data:
|
||||||
|
net.add_node(item['n'].identity,
|
||||||
|
label=item['n']['name'],
|
||||||
|
title=item['n'].get('description', ''),
|
||||||
|
group=item['n'].labels[0])
|
||||||
|
net.add_node(item['m'].identity,
|
||||||
|
label=item['m']['name'],
|
||||||
|
title=item['m'].get('description', ''),
|
||||||
|
group=item['m'].labels[0])
|
||||||
|
net.add_edge(item['n'].identity,
|
||||||
|
item['m'].identity,
|
||||||
|
title=type(item['r']).__name__)
|
||||||
|
|
||||||
|
# 生成HTML文件
|
||||||
|
with open("knowledge_graph.html", "w", encoding='utf-8') as f:
|
||||||
|
f.write(net.generate_html())
|
||||||
|
|
||||||
|
# 打开HTML文件
|
||||||
|
import webbrowser
|
||||||
|
webbrowser.open("knowledge_graph.html")
|
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue