Source code for langchain_community.graphs.index_creator
fromtypingimportOptional,TypefrompydanticimportBaseModelfromlangchain_core.language_modelsimportBaseLanguageModelfromlangchain_core.promptsimportBasePromptTemplatefromlangchain_core.prompts.promptimportPromptTemplatefromlangchain_community.graphsimportNetworkxEntityGraphfromlangchain_community.graphs.networkx_graphimportKG_TRIPLE_DELIMITERfromlangchain_community.graphs.networkx_graphimportparse_triples# flake8: noqa_DEFAULT_KNOWLEDGE_TRIPLE_EXTRACTION_TEMPLATE=("You are a networked intelligence helping a human track knowledge triples"" about all relevant people, things, concepts, etc. and integrating"" them with your knowledge stored within your weights"" as well as that stored in a knowledge graph."" Extract all of the knowledge triples from the text."" A knowledge triple is a clause that contains a subject, a predicate,"" and an object. The subject is the entity being described,"" the predicate is the property of the subject that is being"" described, and the object is the value of the property.\n\n""EXAMPLE\n""It's a state in the US. It's also the number 1 producer of gold in the US.\n\n"f"Output: (Nevada, is a, state){KG_TRIPLE_DELIMITER}(Nevada, is in, US)"f"{KG_TRIPLE_DELIMITER}(Nevada, is the number 1 producer of, gold)\n""END OF EXAMPLE\n\n""EXAMPLE\n""I'm going to the store.\n\n""Output: NONE\n""END OF EXAMPLE\n\n""EXAMPLE\n""Oh huh. I know Descartes likes to drive antique scooters and play the mandolin.\n"f"Output: (Descartes, likes to drive, antique scooters){KG_TRIPLE_DELIMITER}(Descartes, plays, mandolin)\n""END OF EXAMPLE\n\n""EXAMPLE\n""{text}""Output:")KNOWLEDGE_TRIPLE_EXTRACTION_PROMPT=PromptTemplate(input_variables=["text"],template=_DEFAULT_KNOWLEDGE_TRIPLE_EXTRACTION_TEMPLATE,)
[docs]classGraphIndexCreator(BaseModel):"""Functionality to create graph index."""llm:Optional[BaseLanguageModel]=Nonegraph_type:Type[NetworkxEntityGraph]=NetworkxEntityGraph
[docs]deffrom_text(self,text:str,prompt:BasePromptTemplate=KNOWLEDGE_TRIPLE_EXTRACTION_PROMPT)->NetworkxEntityGraph:"""Create graph index from text."""ifself.llmisNone:raiseValueError("llm should not be None")graph=self.graph_type()# Temporary local scoped import while community does not depend on# langchain explicitlytry:fromlangchain.chainsimportLLMChainexceptImportError:raiseImportError("Please install langchain to use this functionality. ""You can install it with `pip install langchain`.")chain=LLMChain(llm=self.llm,prompt=prompt)output=chain.predict(text=text)knowledge=parse_triples(output)fortripleinknowledge:graph.add_triple(triple)returngraph
[docs]asyncdefafrom_text(self,text:str,prompt:BasePromptTemplate=KNOWLEDGE_TRIPLE_EXTRACTION_PROMPT)->NetworkxEntityGraph:"""Create graph index from text asynchronously."""ifself.llmisNone:raiseValueError("llm should not be None")graph=self.graph_type()# Temporary local scoped import while community does not depend on# langchain explicitlytry:fromlangchain.chainsimportLLMChainexceptImportError:raiseImportError("Please install langchain to use this functionality. ""You can install it with `pip install langchain`.")chain=LLMChain(llm=self.llm,prompt=prompt)output=awaitchain.apredict(text=text)knowledge=parse_triples(output)fortripleinknowledge:graph.add_triple(triple)returngraph