NetworkX
NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.
This notebook goes over how to do question answering over a graph data structure.
Setting up
We have to install a Python package.
%pip install --upgrade --quiet networkx
Create the graph
In this section, we construct an example graph. At the moment, this works best for small pieces of text.
from langchain_community.graphs.index_creator import GraphIndexCreator
from langchain_openai import OpenAI
API Reference:GraphIndexCreator | OpenAI
index_creator = GraphIndexCreator(llm=OpenAI(temperature=0))
with open("../../../how_to/state_of_the_union.txt") as f:
all_text = f.read()
We will use just a small snippet, because extracting the knowledge triplets is a bit intensive at the moment.
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)
We can inspect the created graph.
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')]