langchain[patch],community[minor]: Move graph index creator (#20795)

Move graph index creator to community
This commit is contained in:
Eugene Yurtsev
2024-05-01 10:04:30 -04:00
committed by GitHub
parent aa0bc7467c
commit 1ce1a10f2b
5 changed files with 116 additions and 48 deletions

View File

@@ -11,10 +11,10 @@ Importantly, Index keeps on working even if the content being written is derived
via a set of transformations from some source content (e.g., indexing children
documents that were derived from parent documents by chunking.)
"""
from langchain_community.graphs.index_creator import GraphIndexCreator
from langchain_core.indexing.api import IndexingResult, aindex, index
from langchain.indexes._sql_record_manager import SQLRecordManager
from langchain.indexes.graph import GraphIndexCreator
from langchain.indexes.vectorstore import VectorstoreIndexCreator
__all__ = [

View File

@@ -1,47 +1,5 @@
"""Graph Index Creator."""
from typing import Optional, Type
from langchain_community.graphs.index_creator import GraphIndexCreator
from langchain_community.graphs.networkx_graph import NetworkxEntityGraph
from langchain_community.graphs.networkx_graph import NetworkxEntityGraph, parse_triples
from langchain_core.language_models import BaseLanguageModel
from langchain_core.prompts import BasePromptTemplate
from langchain_core.pydantic_v1 import BaseModel
from langchain.chains.llm import LLMChain
from langchain.indexes.prompts.knowledge_triplet_extraction import (
KNOWLEDGE_TRIPLE_EXTRACTION_PROMPT,
)
class GraphIndexCreator(BaseModel):
"""Functionality to create graph index."""
llm: Optional[BaseLanguageModel] = None
graph_type: Type[NetworkxEntityGraph] = NetworkxEntityGraph
def from_text(
self, text: str, prompt: BasePromptTemplate = KNOWLEDGE_TRIPLE_EXTRACTION_PROMPT
) -> NetworkxEntityGraph:
"""Create graph index from text."""
if self.llm is None:
raise ValueError("llm should not be None")
graph = self.graph_type()
chain = LLMChain(llm=self.llm, prompt=prompt)
output = chain.predict(text=text)
knowledge = parse_triples(output)
for triple in knowledge:
graph.add_triple(triple)
return graph
async def afrom_text(
self, text: str, prompt: BasePromptTemplate = KNOWLEDGE_TRIPLE_EXTRACTION_PROMPT
) -> NetworkxEntityGraph:
"""Create graph index from text asynchronously."""
if self.llm is None:
raise ValueError("llm should not be None")
graph = self.graph_type()
chain = LLMChain(llm=self.llm, prompt=prompt)
output = await chain.apredict(text=text)
knowledge = parse_triples(output)
for triple in knowledge:
graph.add_triple(triple)
return graph
__all__ = ["GraphIndexCreator", "NetworkxEntityGraph"]

View File

@@ -1 +1,12 @@
"""Relevant prompts for constructing indexes."""
from langchain_core._api import warn_deprecated
warn_deprecated(
since="0.1.47",
message=(
"langchain.indexes.prompts will be removed in the future."
"If you're relying on these prompts, please open an issue on "
"GitHub to explain your use case."
),
pending=True,
)

View File

@@ -3,8 +3,7 @@ from langchain.indexes import __all__
def test_all() -> None:
"""Use to catch obvious breaking changes."""
assert __all__ == sorted(__all__, key=str.lower)
assert __all__ == [
expected = [
"aindex",
"GraphIndexCreator",
"index",
@@ -12,3 +11,4 @@ def test_all() -> None:
"SQLRecordManager",
"VectorstoreIndexCreator",
]
assert __all__ == sorted(expected, key=lambda x: x.lower())