Skip to main content

NetworkX

NetworkX 是一个用于创建、操作和研究复杂网络的结构、动态和功能的 Python 包。

本笔记本介绍如何在图数据结构上进行问答。

设置

我们需要安装一个 Python 包。

%pip install --upgrade --quiet  networkx

创建图形

在本节中,我们构建一个示例图形。目前,这对于小段文本效果最佳。

from langchain.indexes import GraphIndexCreator
from langchain_openai import OpenAI
index_creator = GraphIndexCreator(llm=OpenAI(temperature=0))
with open("../../../how_to/state_of_the_union.txt") as f:
all_text = f.read()

我们只会使用一个小片段,因为提取知识三元组目前有点费时。

text = "\n".join(all_text.split("\n\n")[105:108])
text
'It won’t look like much, but if you stop and look closely, you’ll see a “Field of dreams,” the ground on which America’s future will be built. \nThis is where Intel, the American company that helped build Silicon Valley, is going to build its $20 billion semiconductor “mega site”. \nUp to eight state-of-the-art factories in one place. 10,000 new good-paying jobs. '
graph = index_creator.from_text(text)

我们可以查看创建的图形。

graph.get_triples()
[('Intel', '$20 billion semiconductor "mega site"', 'is going to build'),
('Intel', 'state-of-the-art factories', 'is building'),
('Intel', '10,000 new good-paying jobs', 'is creating'),
('Intel', 'Silicon Valley', 'is helping build'),
('Field of dreams',
"America's future will be built",
'is the ground on which')]

查询图形

我们现在可以使用图形 QA 链来询问图形

from langchain.chains import GraphQAChain
chain = GraphQAChain.from_llm(OpenAI(temperature=0), graph=graph, verbose=True)
chain.run("Intel 将要建设什么?")


> 进入新的 GraphQAChain 链...
提取的实体:
 Intel
完整上下文:
Intel 将要建设价值 200 亿美元的半导体“超级工厂”
Intel 正在建设最先进的工厂
Intel 正在创造 10,000 个新的高薪工作
Intel 正在帮助建设硅谷

> 完成链。
' Intel 将要建设一个价值 200 亿美元的半导体“超级工厂”,配备最先进的工厂,创造 10,000 个新的高薪工作,并帮助建设硅谷。'

保存图形

我们还可以保存和加载图形。

graph.write_to_gml("graph.gml")
from langchain.indexes.graph import NetworkxEntityGraph
loaded_graph = NetworkxEntityGraph.from_gml("graph.gml")
loaded_graph.get_triples()
[('Intel', '$20 billion semiconductor "mega site"', 'is going to build'),
('Intel', 'state-of-the-art factories', 'is building'),
('Intel', '10,000 new good-paying jobs', 'is creating'),
('Intel', 'Silicon Valley', 'is helping build'),
('Field of dreams',
"America's future will be built",
'is the ground on which')]
loaded_graph.get_number_of_nodes()
loaded_graph.add_node("NewNode")
loaded_graph.has_node("NewNode")
loaded_graph.remove_node("NewNode")
loaded_graph.get_neighbors("Intel")
loaded_graph.has_edge("Intel", "Silicon Valley")
loaded_graph.remove_edge("Intel", "Silicon Valley")
loaded_graph.clear_edges()
loaded_graph.clear()

此页面是否有帮助?


您还可以留下详细的反馈 在 GitHub 上