mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 17:35:28 +03:00
7.0 KiB
7.0 KiB
In [1]:
from langchain.indexes import GraphIndexCreator
from langchain.llms import OpenAI
from langchain.document_loaders import TextLoaderIn [2]:
index_creator = GraphIndexCreator(llm=OpenAI(temperature=0))In [3]:
with open("../../state_of_the_union.txt") as f:
all_text = f.read()In [4]:
text = "\n".join(all_text.split("\n\n")[105:108])In [5]:
textOut [5]:
'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. '
In [6]:
graph = index_creator.from_text(text)In [7]:
graph.get_triples()Out [7]:
[('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')]In [8]:
from langchain.chains import GraphQAChainIn [9]:
chain = GraphQAChain.from_llm(OpenAI(temperature=0), graph=graph, verbose=True)In [10]:
chain.run("what is Intel going to build?")Out [10]:
[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.'
In [7]:
graph.write_to_gml("graph.gml")In [8]:
from langchain.indexes.graph import NetworkxEntityGraphIn [9]:
loaded_graph = NetworkxEntityGraph.from_gml("graph.gml")In [10]:
loaded_graph.get_triples()Out [10]:
[('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')]In [ ]: