LLMGraphTransformer#
- class langchain_experimental.graph_transformers.llm.LLMGraphTransformer(llm: BaseLanguageModel, allowed_nodes: List[str] = [], allowed_relationships: List[str] = [], prompt: ChatPromptTemplate | None = None, strict_mode: bool = True, node_properties: bool | List[str] = False, relationship_properties: 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 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.
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] = [], prompt: ChatPromptTemplate | None = None, strict_mode: bool = True, node_properties: bool | List[str] = False, relationship_properties: bool | List[str] = False) None [source]#
- Parameters:
llm (BaseLanguageModel) –
allowed_nodes (List[str]) –
allowed_relationships (List[str]) –
prompt (ChatPromptTemplate | None) –
strict_mode (bool) –
node_properties (bool | List[str]) –
relationship_properties (bool | List[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:
documents (Sequence[Document]) –
config (RunnableConfig | None) –
- 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:
document (Document) –
config (RunnableConfig | None) –
- Return type:
- 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:
document (Document) –
config (RunnableConfig | None) –
- Return type:
Examples using LLMGraphTransformer