NetworkX
NetworkX 是一個 Python 套件,用於建立、操作和研究複雜網路的結構、動態和功能。
本筆記本說明如何對圖形資料結構進行問答。
設定
我們必須安裝一個 Python 套件。
%pip install --upgrade --quiet networkx
建立圖形
在本節中,我們建構一個範例圖形。目前,這最適合用於小段文字。
from langchain_community.graphs.index_creator import GraphIndexCreator
from langchain_openai import OpenAI
API 參考:GraphIndexCreator | 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
API 參考:GraphQAChain
chain = GraphQAChain.from_llm(OpenAI(temperature=0), graph=graph, verbose=True)
chain.run("what is Intel going to build?")
[1m> Entering new GraphQAChain chain...[0m
Entities Extracted:
[32;1m[1;3m Intel[0m
Full Context:
[32;1m[1;3mIntel is going to build $20 billion semiconductor "mega site"
Intel is building state-of-the-art factories
Intel is creating 10,000 new good-paying jobs
Intel is helping build Silicon Valley[0m
[1m> Finished chain.[0m
' Intel is going to build a $20 billion semiconductor "mega site" with state-of-the-art factories, creating 10,000 new good-paying jobs and helping to build Silicon Valley.'
儲存圖形
我們也可以儲存和載入圖形。
graph.write_to_gml("graph.gml")
from langchain_community.graphs import NetworkxEntityGraph
API 參考: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()