You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.1 KiB
40 lines
1.1 KiB
# 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") |