Ë
    µŒjÙ  ã                  ón   — d Z ddlmZ ddlmZmZmZmZmZ dZ	 G d„ de«      Z
dd„Zdd„Z G d	„ d
«      Zy)z&Networkx wrapper for graph operations.é    )Úannotations)ÚAnyÚListÚ
NamedTupleÚOptionalÚTuplez<|>c                  óB   — e Zd ZU dZded<   ded<   ded<   edd„«       Zy)	ÚKnowledgeTriplezKnowledge triple in the graph.ÚstrÚsubjectÚ	predicateÚobject_c                óp   — |j                  «       j                  d«      \  }}}|dd }|dd } | |||«      S )z'Create a KnowledgeTriple from a string.z, é   Néÿÿÿÿ©ÚstripÚsplit)ÚclsÚtriple_stringr   r   r   s        ús/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/graphs/networkx_graph.pyÚfrom_stringzKnowledgeTriple.from_string   sI   € ð '4×&9Ñ&9Ó&;×&AÑ&AÀ$Ó&GÑ#ˆ�˜GØ˜!˜"�+ˆØ˜#˜2�,ˆÙ�7˜I wÓ/Ð/ó    N)r   r   Úreturnz'KnowledgeTriple')Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__Úclassmethodr   © r   r   r
   r
   
   s&   … Ù(àƒLØƒNØƒLàò0ó ñ0r   r
   c                óà   — | j                  «       } | r| dk(  rg S | j                  t        «      }g }|D ])  }	 t        j	                  |«      }|j                  |«       Œ+ |S # t
        $ r Y Œ9w xY w)z2Parse knowledge triples from the knowledge string.ÚNONE)r   r   ÚKG_TRIPLE_DELIMITERr
   r   Ú
ValueErrorÚappend)Úknowledge_strÚtriple_strsÚresultsÚ
triple_strÚ	kg_triples        r   Úparse_triplesr,      s}   € à!×'Ñ'Ó)€MÙ˜M¨VÒ3Øˆ	Ø×%Ñ%Ô&9Ó:€KØ€GÛ!ˆ
ð	Ü'×3Ñ3°JÓ?ˆIð 	�‰�yÕ!ð "ð €Nøô ò 	Ùð	ús   ·A!Á!	A-Á,A-c                ó�   — | j                  «       dk(  rg S | j                  d«      D �cg c]  }|j                  «       ‘Œ c}S c c}w )z$Extract entities from entity string.r#   Ú,r   )Ú
entity_strÚws     r   Úget_entitiesr1   *   sC   € à×ÑÓ˜VÒ#Øˆ	à#-×#3Ñ#3°CÔ#8Ó9Ñ#8˜a�—‘•	Ð#8Ñ9Ð9ùÒ9s   ©Ac                  ó®   — e Zd ZdZddd„Zedd„«       Zdd„Zdd„Zdd„Z	ddd„Z
dd	„Zdd
„Zdd„Zdd„Zdd„Zdd„Zd d„Zd!d„Zd"d„Zd#d„Zd$d„Zd%d„Zy)&ÚNetworkxEntityGraphaÉ  Networkx wrapper for entity 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.
    Nc                ó¾   — 	 ddl }|�)t        ||j                  «      st	        d«      ‚|| _        y|j                  «       | _        y# t        $ r t        d«      ‚w xY w)zCreate a new graph.r   NúXCould not import networkx python package. Please install it with `pip install networkx`.z'Passed in graph is not of correct shape)ÚnetworkxÚImportErrorÚ
isinstanceÚDiGraphr%   Ú_graph)ÚselfÚgraphÚnxs      r   Ú__init__zNetworkxEntityGraph.__init__A   sd   € ð	Û!ð ÐÜ˜e R§Z¡ZÔ0Ü Ð!JÓKÐKØˆD�KàŸ*™*›,ˆD�Køô ò 	ÜðAóð ð	ús   ‚A ÁAc                ón   — 	 dd l }|j                  |«      } | |«      S # t        $ r t        d«      ‚w xY w)Nr   r5   )r6   r7   Úread_gml)r   Úgml_pathr=   r<   s       r   Úfrom_gmlzNetworkxEntityGraph.from_gmlQ   sH   € ð	Û!ð —‘˜HÓ%ˆÙ�5‹zÐøô ò 	ÜðAóð ð	ús   ‚ Ÿ4c                ó¤  — | j                   j                  |j                  «      s%| j                   j                  |j                  «       | j                   j                  |j                  «      s%| j                   j                  |j                  «       | j                   j                  |j                  |j                  |j                  ¬«       y)zAdd a triple to the graph.)ÚrelationN)r:   Úhas_noder   Úadd_noder   Úadd_edger   ©r;   Úknowledge_triples     r   Ú
add_triplezNetworkxEntityGraph.add_triple]   sš   € ð �{‰{×#Ñ#Ð$4×$<Ñ$<Ô=Ø�K‰K× Ñ Ð!1×!9Ñ!9Ô:Ø�{‰{×#Ñ#Ð$4×$<Ñ$<Ô=Ø�K‰K× Ñ Ð!1×!9Ñ!9Ô:Ø�‰×ÑØ×$Ñ$Ø×$Ñ$Ø%×/Ñ/ð 	õ 	
r   c                óÆ   — | j                   j                  |j                  |j                  «      r1| j                   j	                  |j                  |j                  «       yy)zDelete a triple from the graph.N)r:   Úhas_edger   r   Úremove_edgerH   s     r   Údelete_triplez!NetworkxEntityGraph.delete_triplek   sL   € à�;‰;×ÑÐ 0× 8Ñ 8Ð:J×:RÑ:RÔSØ�K‰K×#Ñ#Ð$4×$<Ñ$<Ð>N×>VÑ>VÕWð Tr   c           	     ó€   — | j                   j                  d¬«      D ���cg c]  \  }}}|||d   f‘Œ c}}}S c c}}}w )zGet all triples in the graph.T)ÚdatarD   )r:   Úedges)r;   ÚuÚvÚds       r   Úget_tripleszNetworkxEntityGraph.get_triplesp   s?   € à59·[±[×5FÑ5FÈDÐ5FÔ5QÕRÑ5Q©'¨!¨Q°��A�q˜‘}Ò%Ð5QÓRÐRùÔRs   ¡9c                óô   — ddl }| j                  j                  |«      sg S g }|j                  | j                  ||¬«      D ]3  \  }}| j                  |   |   d   }|j	                  |› d|› d|› �«       Œ5 |S )z Get information about an entity.r   N)Údepth_limitrD   Ú )r6   r:   rE   Ú	dfs_edgesr&   )r;   ÚentityÚdepthr=   r)   ÚsrcÚsinkrD   s           r   Úget_entity_knowledgez(NetworkxEntityGraph.get_entity_knowledget   s�   € ãð �{‰{×#Ñ# FÔ+ØˆIàˆØŸ™ d§k¡k°6Àu˜ÖM‰IˆC�Ø—{‘{ 3Ñ'¨Ñ-¨jÑ9ˆHØ�N‰N˜c˜U ! H :¨Q¨t¨fÐ5Õ6ð Nð ˆr   c                óD   — dd l }|j                  | j                  |«       y )Nr   )r6   Ú	write_gmlr:   )r;   Úpathr=   s      r   Úwrite_to_gmlz NetworkxEntityGraph.write_to_gml‚   s   € Ûà
�‰�T—[‘[ $Õ'r   c                ó8   — | j                   j                  «        y)zClear the graph.N)r:   Úclear©r;   s    r   rd   zNetworkxEntityGraph.clear‡   s   € à�‰×ÑÕr   c                ó8   — | j                   j                  «        y)zClear the graph edges.N)r:   Úclear_edgesre   s    r   rg   zNetworkxEntityGraph.clear_edges‹   s   € à�‰×ÑÕ!r   c                ó:   — | j                   j                  |«       y)zAdd node in the graph.N)r:   rF   ©r;   Únodes     r   rF   zNetworkxEntityGraph.add_node�   s   € à�‰×Ñ˜TÕ"r   c                ór   — | j                   j                  |«      r| j                   j                  |«       yy)zRemove node from the graph.N)r:   rE   Úremove_noderi   s     r   rl   zNetworkxEntityGraph.remove_node“   s+   € à�;‰;×Ñ Ô%Ø�K‰K×#Ñ# DÕ)ð &r   c                ó8   — | j                   j                  |«      S )z#Return if graph has the given node.)r:   rE   ri   s     r   rE   zNetworkxEntityGraph.has_node˜   s   € à�{‰{×#Ñ# DÓ)Ð)r   c                ó<   — | j                   j                  ||«       y)zRemove edge from the graph.N)r:   rM   ©r;   Úsource_nodeÚdestination_nodes      r   rM   zNetworkxEntityGraph.remove_edgeœ   s   € à�‰×Ñ Ð-=Õ>r   c                ó¨   — | j                   j                  |«      r7| j                   j                  |«      r| j                   j                  ||«      S y)z4Return if graph has an edge between the given nodes.F)r:   rE   rL   ro   s      r   rL   zNetworkxEntityGraph.has_edge    sA   € à�;‰;×Ñ Ô,°·±×1EÑ1EÐFVÔ1WØ—;‘;×'Ñ'¨Ð5EÓFÐFàr   c                ó8   — | j                   j                  |«      S )z,Return the neighbor nodes of the given node.)r:   Ú	neighborsri   s     r   Úget_neighborsz!NetworkxEntityGraph.get_neighbors§   s   € à�{‰{×$Ñ$ TÓ*Ð*r   c                ó6   — | j                   j                  «       S )z!Get number of nodes in the graph.)r:   Únumber_of_nodesre   s    r   Úget_number_of_nodesz'NetworkxEntityGraph.get_number_of_nodes«   s   € à�{‰{×*Ñ*Ó,Ð,r   c                óR   — ddl }t        |j                  | j                  «      «      S )zDGet a list of entity names in the graph sorted by causal dependence.r   N)r6   ÚlistÚtopological_sortr:   )r;   r=   s     r   Úget_topological_sortz(NetworkxEntityGraph.get_topological_sort¯   s   € ãä�B×'Ñ'¨¯©Ó4Ó5Ð5r   c                ó2  — ddl m} 	 ddl} || j
                  «      }|j                  |j                  dd«      ¬	«       |j                  |j                  d
d«      «       y# t        $ r+}|j                  dk(  r	 t        d«      ‚t        d«      ‚d}~ww xY w)zè
        Provides better drawing

        Usage in a jupyter notebook:

            >>> from IPython.display import SVG
            >>> self.draw_graphviz_svg(layout="dot", filename="web.svg")
            >>> SVG('web.svg')
        r   )Ú	to_agraphNÚ	_graphvizz‚Could not import graphviz debian package. Please install it with:`sudo apt-get update``sudo apt-get install graphviz graphviz-dev`z\Could not import pygraphviz python package. Please install it with:`pip install pygraphviz`.ÚprogÚdot)r€   ra   z	graph.svg)	Únetworkx.drawing.nx_agraphr~   Ú
pygraphvizr7   Únamer:   ÚlayoutÚgetÚdraw)r;   Úkwargsr~   rƒ   Úer<   s         r   Údraw_graphvizz!NetworkxEntityGraph.draw_graphvizµ   s›   € õ 	9ð	Ûñ* ˜$Ÿ+™+Ó&ˆà�‰˜&Ÿ*™* V¨UÓ3ˆÔ4Ø�
‰
�6—:‘:˜f kÓ2Õ3øô- ò 	Ø�v‰v˜Ò$ðô "ðCóð ô "ð0óð ûð	ús   ˆA" Á"	BÁ+&BÂB)N)r<   zOptional[Any]r   ÚNone)rA   r   r   r3   )rI   r
   r   r‹   )r   zList[Tuple[str, str, str]])r   )rZ   r   r[   Úintr   ú	List[str])ra   r   r   r‹   )r   r‹   )rj   r   r   r‹   )rj   r   r   Úbool)rp   r   rq   r   r   r‹   )rp   r   rq   r   r   rŽ   )rj   r   r   r�   )r   rŒ   )r   r�   )rˆ   r   r   r‹   )r   r   r   r   r>   r    rB   rJ   rN   rU   r^   rb   rd   rg   rF   rl   rE   rM   rL   ru   rx   r|   rŠ   r!   r   r   r3   r3   2   sr   „ ñô'ð  ò	ó ð	ó
óXó
Sôó(ó
ó"ó#ó*ó
*ó?óó+ó-ó6ô%4r   r3   N)r'   r   r   zList[KnowledgeTriple])r/   r   r   r�   )r   Ú
__future__r   Útypingr   r   r   r   r   r$   r
   r,   r1   r3   r!   r   r   Ú<module>r‘      s9   ðÙ ,å "ç 9Õ 9àÐ ô0�jô 0ó ó :÷h4ò h4r   