Ë
    µŒj—*  ã                   óL   — d dl mZ d dlmZmZmZmZ d dlmZm	Z	  G d„ d«      Z
y)é    )Úmd5)ÚAnyÚDictÚListÚTuple)ÚGraphDocumentÚRelationshipc            
       óÎ   — e Zd ZdZ	 ddedededdfd„Zedefd„«       Z	i fd	ed
e
deeeef      fd„Zdd„Zdd„Zdeddfd„Zdeddfd„Z	 ddee   deeeeef      deddfd„Zy)Ú	KuzuGraphu¿  KÃ¹zu wrapper for graph operations.

    *Security note*: Make sure that the database connection uses credentials
        that are narrowly-scoped to only include necessary permissions.
        Failure to do so may result in data corruption or loss, since the calling
        code may attempt commands that would result in deletion, mutation
        of data if appropriately prompted or reading sensitive data if such
        data is present in the database.
        The best way to guard against such negative outcomes is to (as appropriate)
        limit the permissions granted to the credentials used with this tool.

        See https://python.langchain.com/docs/security for more information.
    ÚdbÚdatabaseÚallow_dangerous_requestsÚreturnNc                 óØ   — |durt        d«      ‚	 ddl}|| _        |j	                  | j                  «      | _        || _        | j                  «        y# t        $ r t        d«      ‚w xY w)u0   Initializes the KÃ¹zu graph database connection.TzâThe KuzuGraph class is a powerful tool that can be used to execute arbitrary queries on the database. To enable this functionality, set the `allow_dangerous_requests` parameter to `True` when constructing the KuzuGraph object.r   NuS   Could not import KÃ¹zu python package.Please install KÃ¹zu with `pip install kuzu`.)Ú
ValueErrorÚkuzuÚImportErrorr   Ú
ConnectionÚconnr   Úrefresh_schema)Úselfr   r   r   r   s        úo/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/graphs/kuzu_graph.pyÚ__init__zKuzuGraph.__init__   sz   € ð
 $¨4Ñ/Üð5óð ð	Ûð ˆŒØ—O‘O D§G¡GÓ,ˆŒ	Ø ˆŒØ×ÑÕøô ò 	Üð@óð ð	ús   ‘A ÁA)c                 ó   — | j                   S )u(   Returns the schema of the KÃ¹zu database)Úschema©r   s    r   Ú
get_schemazKuzuGraph.get_schema/   s   € ð �{‰{Ðó    ÚqueryÚparamsc                 ó  — | j                   j                  ||«      }|j                  «       }g }|j                  «       rE|j	                  «       }|j                  t        t        ||«      «      «       |j                  «       rŒE|S )u   Query KÃ¹zu database)r   ÚexecuteÚget_column_namesÚhas_nextÚget_nextÚappendÚdictÚzip)r   r   r    ÚresultÚcolumn_namesÚreturn_listÚrows          r   r   zKuzuGraph.query4   so   € à—‘×"Ñ" 5¨&Ó1ˆØ×.Ñ.Ó0ˆØˆØ�o‰oÔØ—/‘/Ó#ˆCØ×Ñœt¤C¨°cÓ$:Ó;Ô<ð �o‰oÕð Ðr   c           
      ód  — g }| j                   j                  «       }|D ]£  }g |dœ}| j                   j                  |«      }|D ]k  }||   d   }d}||   d   dkD  r9d||   v r||   d   D ]  }	|d|	› d�z  }Œ nt        ||   d   «      D ]  }
|d	z  }Œ	 ||z  }|d
   j	                  ||f«       Œm |j	                  |«       Œ¥ g }| j                   j                  «       }|D ]&  }|j	                  d|d   › d|d   › d|d   › d�«       Œ( g }|D ]�  }|d   }g |dœ}| j                   j                  d|› d�«      }|j                  «       rA|j                  «       }|d   }|d   }|d
   j	                  ||f«       |j                  «       rŒA|j	                  |«       Œ� d|› d|› d|› d�| _	        y)u,   Refreshes the KÃ¹zu graph schema information)Ú
propertiesÚlabelÚtypeÚ Ú	dimensionr   ÚshapeÚ[Ú]z[]r.   z(:Úsrcz)-[:Únamez]->(:ÚdstÚ)zCALL table_info('z') RETURN *;é   é   zNode properties: z
Relationships properties: z
Relationships: Ú
N)
r   Ú_get_node_table_namesÚ_get_node_property_namesÚranger&   Ú_get_rel_table_namesr"   r$   r%   r   )r   Únode_propertiesÚnode_table_namesÚ
table_nameÚcurrent_table_schemar.   Úproperty_nameÚproperty_typeÚlist_type_flagÚsÚiÚrelationshipsÚ
rel_tablesÚtableÚrel_propertiesÚquery_resultr,   Ú	prop_nameÚ	prop_types                      r   r   zKuzuGraph.refresh_schema>   s2  € àˆØŸ9™9×:Ñ:Ó<ÐÛ*ˆJØ24¸zÑ#JÐ ØŸ™×;Ñ;¸JÓGˆJÛ!+�Ø *¨=Ñ 9¸&Ñ A�Ø!#�Ø˜mÑ,¨[Ñ9¸AÒ=Ø *¨]Ñ";Ñ;Ø!+¨MÑ!:¸7Ô!C˜AØ*°°!°°A¨hÑ6™Nñ "Dô "' z°-Ñ'@ÀÑ'MÖ!N˜AØ*¨dÑ2™Nð "Oà Ñ/�Ø$ \Ñ2×9Ñ9à%Ø%ðõð ",ð" ×"Ñ"Ð#7Õ8ð) +ð, ˆØ—Y‘Y×3Ñ3Ó5ˆ
ÛˆEØ× Ñ Ø�U˜5‘\�N $ u¨V¡} o°U¸5À¹<¸.ÈÐJõð  ð
 ˆÛˆEØ˜v™ˆJØ24¸zÑ#JÐ ØŸ9™9×,Ñ,Ø# J <¨|Ð<óˆLð ×'Ñ'Ô)Ø"×+Ñ+Ó-�Ø ™F�	Ø ™F�	Ø$ \Ñ2×9Ñ9¸9ÀiÐ:PÔQð	 ×'Ñ'Õ)ð
 ×!Ñ!Ð"6Õ7ð  ð   Ð0ð 1)Ø)7Ð(8ð 9Ø+˜_¨Bð0ð 	�r   c                 ó:   — | j                   j                  d«       y )NzÅ
            CREATE NODE TABLE IF NOT EXISTS Chunk (
                id STRING,
                text STRING,
                type STRING,
                PRIMARY KEY(id)
            );
            ©r   r"   r   s    r   Ú_create_chunk_node_tablez"KuzuGraph._create_chunk_node_tables   s   € Ø�	‰	×Ñðõ		
r   Ú
node_labelc                 óB   — | j                   j                  d|› d�«       y )Nz-
            CREATE NODE TABLE IF NOT EXISTS zv (
                id STRING,
                type STRING,
                PRIMARY KEY(id)
            );
            rR   )r   rT   s     r   Ú_create_entity_node_tablez#KuzuGraph._create_entity_node_table   s(   € Ø�	‰	×Ñð-Ø-7¨Lð 9ðõ	
r   Úrelc           	      ó²   — | j                   j                  d|j                  › d|j                  j                  › d|j                  j                  › d�«       y )Nz,
            CREATE REL TABLE IF NOT EXISTS z (
                FROM z TO z
            );
            )r   r"   r0   ÚsourceÚtarget)r   rW   s     r   Ú!_create_entity_relationship_tablez+KuzuGraph._create_entity_relationship_tableŠ   sN   € Ø�	‰	×Ñð,Ø,/¯H©H¨:ð 6Ø—j‘j—o‘oÐ& d¨3¯:©:¯?©?Ð*;ð <ðõ	
r   Úgraph_documentsÚallowed_relationshipsÚinclude_sourcec                 ó  — t        |D ��ch c]  }|j                  D ]  }|j                  ’Œ Œ! c}}«      }|D �]Â  }|rÓ| j                  «        |j                  j
                  j                  d«      sSt        |j                  j                  j                  d«      «      j                  «       |j                  j
                  d<   | j                  j                  d|j                  j
                  d   |j                  j                  dœ¬«       |D ]  }| j                  |«       Œ |j                  D �]  }| j                  j                  d|j                  › d�d|j                  i¬«       |sŒ>| j                  «        d}g }	|D ]  }|	j                  d	|› �«       Œ t        t!        |	«      «      }	|d
j#                  |	«      z  }|dz  }|r| j                  j                  |«       |j                  |v sŒÆ| j                  j                  d|j                  › d�|j                  j
                  d   |j                  dœ¬«       �Œ |j$                  D ]Ÿ  }
| j'                  |
«       |
j                  j                  }|
j                  j                  }|
j(                  j                  }|
j(                  j                  }| j                  j                  d|› d|› d|
j                  › d�||dœ¬«       Œ¡ �ŒÅ yc c}}w )u/  
        Adds a list of `GraphDocument` objects that represent nodes and relationships
        in a graph to a KÃ¹zu backend.

        Parameters:
          - graph_documents (List[GraphDocument]): A list of `GraphDocument` objects
            that contain the nodes and relationships to be added to the graph. Each
            `GraphDocument` should encapsulate the structure of part of the graph,
            including nodes, relationships, and the source document information.

          - allowed_relationships (List[Tuple[str, str, str]]): A list of allowed
            relationships that exist in the graph. Each tuple contains three elements:
            the source node type, the relationship type, and the target node type.
            Required for KÃ¹zu, as the names of the relationship tables that need to
            pre-exist are derived from these tuples.

          - include_source (bool): If True, stores the source document
            and links it to nodes in the graph using the `MENTIONS` relationship.
            This is useful for tracing back the origin of data. Merges source
            documents based on the `id` property from the source document metadata
            if available; otherwise it calculates the MD5 hash of `page_content`
            for merging process. Defaults to False.
        Úidzutf-8z¢
                    MERGE (c:Chunk {id: $id}) 
                        SET c.text = $text,
                            c.type = "text_chunk"
                    )r`   Útext)Ú
parametersz
                    MERGE (e:zN {id: $id})
                        SET e.type = "entity"
                    z/CREATE REL TABLE GROUP IF NOT EXISTS MENTIONS (zFROM Chunk TO z, z), label STRING, triplet_source_id STRING)z]
                            MATCH (c:Chunk {id: $id}),
                                  (e:z� {id: $node_id})
                            MERGE (c)-[m:MENTIONS]->(e)
                          SET m.triplet_source_id = $id
                            )r`   Únode_idz
                    MATCH (e1:z4 {id: $source_id}),
                            (e2:z4 {id: $target_id})
                    MERGE (e1)-[:z]->(e2)
                    )Ú	source_idÚ	target_idN)ÚlistÚnodesr0   rS   rY   ÚmetadataÚgetr   Úpage_contentÚencodeÚ	hexdigestr   r"   rV   r`   r&   ÚsetÚjoinrJ   r[   rZ   )r   r\   r]   r^   ÚdocumentÚnodeÚnode_labelsrT   ÚddlÚtable_namesrW   Úsource_labelrd   Útarget_labelre   s                  r   Úadd_graph_documentszKuzuGraph.add_graph_documents“   sÑ  € ô< Ù'6ÔR¡˜8À8Ç>Ä>¸4ˆT�Y‹YÀ>ˆY ÒRó
ˆô (ˆHñ Ø×-Ñ-Ô/Ø—‘×/Ñ/×3Ñ3°DÔ9ä58Ø Ÿ™×4Ñ4×;Ñ;¸GÓDó6ç‘i“kð —O‘O×,Ñ,¨TÑ2ð —	‘	×!Ñ!ðð 'Ÿo™o×6Ñ6°tÑ<Ø (§¡× <Ñ <ñ ð "ô 
ó *�
Ø×.Ñ.¨zÕ:ð *ð !Ÿ��Ø—	‘	×!Ñ!ðØ"Ÿi™i˜[ð )ðð !% d§g¡g˜ð "ô ò "ð ×1Ñ1Ô3ØK�CØ"$�KÛ&1˜
Ø#×*Ñ*¨^¸J¸<Ð+HÕIð '2ä"&¤s¨;Ó'7Ó"8�KØ˜4Ÿ9™9 [Ó1Ñ1�CàÐFÑF�CÙØŸ	™	×)Ñ)¨#Ô.ð —y‘y KÒ/ØŸ	™	×)Ñ)ð!&à&*§i¡i [ð 1ð ð '/§o¡o×&>Ñ&>¸tÑ&DØ+/¯7©7ñ(ð *ö ð3 'ðN  ×-Ô-�Ø×6Ñ6°sÔ;à"Ÿz™zŸ™�ØŸJ™JŸM™M�	Ø"Ÿz™zŸ™�ØŸJ™JŸM™M�	Ø—	‘	×!Ñ!ðØ+˜nð -!Ø!- ð /"Ø"%§(¡( ð ,ðð &/Ø%.ñ ð "õ 
ò .ñE (ùó Ss   ‹$L 
)r   F)r   N)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚstrÚboolr   Úpropertyr   r'   r   r   r   r   rS   rV   r	   r[   r   r   rv   © r   r   r   r      sõ   „ ñð QVñØðØ!$ðØIMðà	óð2 ð˜Cò ó ðð 02ñ ˜3ð ¨ð °d¸4ÀÀSÀ¹>Ñ6Jó ó3
ój

ð	
°Cð 	
¸Dó 	
ð
°\ð 
Àdó 
ð  %ñ	uà˜mÑ,ðuð  $ E¨#¨s°C¨-Ñ$8Ñ9ðuð ð	uð
 
ôur   r   N)Úhashlibr   Útypingr   r   r   r   Ú)langchain_community.graphs.graph_documentr   r	   r   r~   r   r   Ú<module>r‚      s   ðÝ ß )Ó )ç Q÷Aò Ar   