LLMGraphTransformer#

class langchain_experimental.graph_transformers.llm.LLMGraphTransformer(llm: BaseLanguageModel, allowed_nodes: List[str] = [], allowed_relationships: List[str] | List[Tuple[str, str, str]] = [], prompt: ChatPromptTemplate | None = None, strict_mode: bool = True, node_properties: bool | List[str] = False, relationship_properties: bool | List[str] = False, ignore_tool_usage: bool = False, additional_instructions: str = '')[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 supports extracting properties for both nodes and relationships.

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]]) – If True, the LLM can extract any node properties from text. Alternatively, a list of valid properties can be provided for the LLM to extract, restricting extraction to those specified.

  • relationship_properties (Union[bool, List[str]]) – If True, the LLM can extract any relationship properties from text. Alternatively, a list of valid properties can be provided for the LLM to extract, restricting extraction to those specified.

  • ignore_tool_usage (bool) – Indicates whether the transformer should bypass the use of structured output functionality of the language model. If set to True, the transformer will not use the language model’s native function calling capabilities to handle structured output. Defaults to False.

  • additional_instructions (str) – Allows you to add additional instructions to the prompt without having to change the whole prompt.

Example

Methods

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

aconvert_to_graph_documents(documents[, config])

Asynchronously convert a sequence of documents into graph documents.

aprocess_response(document[, config])

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

convert_to_graph_documents(documents[, config])

Convert a sequence of documents into graph documents.

process_response(document[, config])

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] | List[Tuple[str, str, str]] = [], prompt: ChatPromptTemplate | None = None, strict_mode: bool = True, node_properties: bool | List[str] = False, relationship_properties: bool | List[str] = False, ignore_tool_usage: bool = False, additional_instructions: str = '') None[source]#
Parameters:
  • llm (BaseLanguageModel)

  • allowed_nodes (List[str])

  • allowed_relationships (List[str] | List[Tuple[str, str, str]])

  • prompt (ChatPromptTemplate | None)

  • strict_mode (bool)

  • node_properties (bool | List[str])

  • relationship_properties (bool | List[str])

  • ignore_tool_usage (bool)

  • additional_instructions (str)

Return type:

None

async aconvert_to_graph_documents(documents: Sequence[Document], config: RunnableConfig | None = None) List[GraphDocument][source]#

Asynchronously convert a sequence of documents into graph documents.

Parameters:
Return type:

List[GraphDocument]

async aprocess_response(document: Document, config: RunnableConfig | None = None) GraphDocument[source]#

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

Parameters:
Return type:

GraphDocument

convert_to_graph_documents(documents: Sequence[Document], config: RunnableConfig | None = None) List[GraphDocument][source]#

Convert a sequence of documents into graph documents.

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

  • kwargs – Additional keyword arguments.

  • config (RunnableConfig | None)

Returns:

The transformed documents as graphs.

Return type:

Sequence[GraphDocument]

process_response(document: Document, config: RunnableConfig | None = None) GraphDocument[source]#

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

Parameters:
Return type:

GraphDocument

Examples using LLMGraphTransformer