langchain_experimental.graph_transformers.llm.LLMGraphTransformer

class langchain_experimental.graph_transformers.llm.LLMGraphTransformer(llm: BaseLanguageModel, allowed_nodes: List[str] = [], allowed_relationships: List[str] = [], prompt: Optional[ChatPromptTemplate] = None, strict_mode: bool = True, node_properties: Union[bool, List[str]] = False)[source]

Transform documents into graph-based documents using a LLM.

It allows specifying constraints on the types of nodes and relationships to include in the output graph. The class doesn’t support neither extract and node or relationship properties

Parameters
  • llm (BaseLanguageModel) – An instance of a language model supporting structured output.

  • allowed_nodes (List[str], optional) – Specifies which node types are allowed in the graph. Defaults to an empty list, allowing all node types.

  • allowed_relationships (List[str], optional) – Specifies which relationship types are allowed in the graph. Defaults to an empty list, allowing all relationship types.

  • prompt (Optional[ChatPromptTemplate], optional) – The prompt to pass to the LLM with additional instructions.

  • strict_mode (bool, optional) – Determines whether the transformer should apply filtering to strictly adhere to allowed_nodes and allowed_relationships. Defaults to True.

  • node_properties (Union[bool, List[str]]) –

Example

Methods

__init__(llm[, allowed_nodes, ...])

aconvert_to_graph_documents(documents)

Asynchronously convert a sequence of documents into graph documents.

aprocess_response(document)

Asynchronously processes a single document, transforming it into a graph document.

convert_to_graph_documents(documents)

Convert a sequence of documents into graph documents.

process_response(document)

Processes a single document, transforming it into a graph document using an LLM based on the model's schema and constraints.

__init__(llm: BaseLanguageModel, allowed_nodes: List[str] = [], allowed_relationships: List[str] = [], prompt: Optional[ChatPromptTemplate] = None, strict_mode: bool = True, node_properties: Union[bool, List[str]] = False) None[source]
Parameters
  • llm (BaseLanguageModel) –

  • allowed_nodes (List[str]) –

  • allowed_relationships (List[str]) –

  • prompt (Optional[ChatPromptTemplate]) –

  • strict_mode (bool) –

  • node_properties (Union[bool, List[str]]) –

Return type

None

async aconvert_to_graph_documents(documents: Sequence[Document]) List[GraphDocument][source]

Asynchronously convert a sequence of documents into graph documents.

Parameters

documents (Sequence[Document]) –

Return type

List[GraphDocument]

async aprocess_response(document: Document) GraphDocument[source]

Asynchronously processes a single document, transforming it into a graph document.

Parameters

document (Document) –

Return type

GraphDocument

convert_to_graph_documents(documents: Sequence[Document]) List[GraphDocument][source]

Convert a sequence of documents into graph documents.

Parameters
  • documents (Sequence[Document]) – The original documents.

  • **kwargs – Additional keyword arguments.

Returns

The transformed documents as graphs.

Return type

Sequence[GraphDocument]

process_response(document: Document) GraphDocument[source]

Processes a single document, transforming it into a graph document using an LLM based on the model’s schema and constraints.

Parameters

document (Document) –

Return type

GraphDocument

Examples using LLMGraphTransformer