1.1 图数据库的创建
首先对大模型进行加载
from langchain.indexes import GraphIndexCreator
from langchain.document_loaders import TextLoader
from bigdl.llm.langchain.llms import TransformersLLM
llm = TransformersLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 1024, "trust_remote_code": True},
)
index_creator = GraphIndexCreator(llm)
其次加载图数据库:
with open("../../state_of_the_union.txt") as f:
all_text = f.read()
text = "\n".join(all_text.split("\n\n")[105:108])
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')]
1.2 图数据库对话
from langchain.chains import GraphQAChain
chain = GraphQAChain.from_llm(llm, graph=graph, verbose=True)
chain.run("what is Intel going to build?")
1.3 保存图
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')]