a
    dg                     @   sV   d dl Z d dlmZmZmZmZ d dlmZmZm	Z	 d dl
mZ dZG dd dZdS )    N)AnyDictListSequence)GraphDocumentNodeRelationship)Documentr   c                   @   s`   e Zd ZdZddi dfeeeeef eddddZ	e
ed	d
dZee
 ee dddZdS )RelikGraphTransformera  
    A transformer class for converting documents into graph structures
    using the Relik library and models.

    This class leverages relik models for extracting relationships
    and nodes from text documents and converting them into a graph format.
    The relationships are filtered based on a specified confidence threshold.

    For more details on the Relik library, visit their GitHub repository:
      https://github.com/SapienzaNLP/relik

    Args:
        model (str): The name of the pretrained Relik model to use.
          Default is "relik-ie/relik-relation-extraction-small-wikipedia".
        relationship_confidence_threshold (float): The confidence threshold for
          filtering relationships. Default is 0.1.
        model_config (Dict[str, any]): Additional configuration options for the
          Relik model. Default is an empty dictionary.
        ignore_self_loops (bool): Whether to ignore relationships where the
          source and target nodes are the same. Default is True.
    z(relik-ie/relik-relation-extraction-smallg?TN)model!relationship_confidence_thresholdmodel_configignore_self_loopsreturnc                 C   s`   zdd l }tdtj W n ty8   tdY n0 |jj|fi || _|| _	|| _
d S )Nr   relikzRCould not import relik python package. Please install it with `pip install relik`.)r   logging	getLoggersetLevelWARNINGImportErrorZRelikZfrom_pretrainedrelik_modelr   r   )selfr   r   r   r   r    r   }/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_experimental/graph_transformers/relik.py__init__!   s    
zRelikGraphTransformer.__init__)documentr   c           
      C   s
  |  |j}g }|jD ]0}|t|j|j dkr6tn|j d qg }|j	D ]}|j
| jk rdqR| jr||jj|jjkr|qRt|jj|jj dkrtn
|jj d}t|jj|jj dkrtn
|jj d}t|||jdd d}	||	 qRt|||dS )Nz--NME--)idtype _)sourcetargetr   )nodesrelationshipsr    )r   Zpage_contentspansappendr   textlabelstripDEFAULT_NODE_TYPEZtriplets
confidencer   r   subjectobjectr   replaceupperr   )
r   r   Z	relik_outr"   noder#   ZtripleZsource_nodeZtarget_nodeZrelationshipr   r   r   process_document6   sH    
	


z&RelikGraphTransformer.process_document)	documentsr   c                 C   s&   g }|D ]}|  |}|| q|S )a  Convert a sequence of documents into graph documents.

        Args:
            documents (Sequence[Document]): The original documents.
            kwargs: Additional keyword arguments.

        Returns:
            Sequence[GraphDocument]: The transformed documents as graphs.
        )r0   r%   )r   r1   resultsr   Zgraph_documentr   r   r   convert_to_graph_documentsc   s
    
z0RelikGraphTransformer.convert_to_graph_documents)__name__
__module____qualname____doc__strfloatr   r   boolr   r	   r   r0   r   r   r3   r   r   r   r   r
   
   s    
.r
   )r   typingr   r   r   r   Z)langchain_community.graphs.graph_documentr   r   r   Zlangchain_core.documentsr	   r)   r
   r   r   r   r   <module>   s
   