§
    šŠtjÊ#  ã                   óz   — d dl Z d dlZd dlmZmZmZmZmZ d dlm	Z	 d dl
mZmZmZ d dlmZ  G d„ de¦  «        ZdS )é    N)ÚAnyÚDictÚListÚOptionalÚUnion)Úget_from_env)ÚGraphDocumentÚNodeÚRelationship)Ú
GraphStorec                   ód  — e Zd ZdZedeeef         fd„¦   «         Z	 	 	 	 	 	 dde	e         de	e         d	e	e         d
ede	e         de
ddfd„Zedefd„¦   «         Zdd„Zi fdededeeeef                  fd„Z	 ddee         de
ddfd„Zdedefd„Zdedefd„Zi fdededefd„Zdedefd„ZdS ) ÚGremlinGraphaf  Gremlin wrapper for graph operations.

    Parameters:
    url (Optional[str]): The URL of the Gremlin database server or env GREMLIN_URI
    username (Optional[str]): The collection-identifier like '/dbs/database/colls/graph'
                               or env GREMLIN_USERNAME if none provided
    password (Optional[str]): The connection-key for database authentication
                              or env GREMLIN_PASSWORD if none provided
    traversal_source (str): The traversal source to use for queries. Defaults to 'g'.
    message_serializer (Optional[Any]): The message serializer to use for requests.
                                        Defaults to serializer.GraphSONSerializersV2d0()
    include_edge_properties (bool): Whether to include edge properties in
                                    the gremlin graph schema. Defaults to False
    *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.

    *Implementation details*:
        The Gremlin queries are designed to work with Azure CosmosDB limitations
    Úreturnc                 ó   — | j         S ©N)Ústructured_schema©Úselfs    úf/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/graphs/gremlin_graph.pyÚget_structured_schemaz"GremlinGraph.get_structured_schema(   s   € àÔ%Ð%ó    NÚgFÚurlÚusernameÚpasswordÚtraversal_sourceÚmessage_serializerÚinclude_edge_propertiesc           
      óª  — 	 ddl }ddlm}m}	 t          j        dk    r'|                     |                     ¦   «         ¦  «         n# t          $ r t          d¦  «        ‚w xY w| 	                    t          dd|¦  «        |t          dd	|¦  «        t          d
d|¦  «        |r|n|	                     ¦   «         ¬¦  «        | _        d| _        || _        dS )z,Create a new Gremlin graph wrapper instance.r   N)ÚclientÚ
serializerÚwin32z@Please install gremlin-python first: `pip3 install gremlinpythonr   ÚGREMLIN_URIr   ÚGREMLIN_USERNAMEr   ÚGREMLIN_PASSWORD)r   r   r   r   r   Ú )ÚasyncioÚgremlin_python.driverr    r!   ÚsysÚplatformÚset_event_loop_policyÚWindowsSelectorEventLoopPolicyÚImportErrorÚClientr   ÚGraphSONSerializersV2d0Úschemar   )
r   r   r   r   r   r   r   r'   r    r!   s
             r   Ú__init__zGremlinGraph.__init__,   s  € ð
	ØˆNˆNˆNà@Ð@Ð@Ð@Ð@Ð@Ð@Ð@åŒ|˜wÒ&Ð&Ø×-Ò-¨g×.TÒ.TÑ.VÔ.VÑWÔWÐWøøÝð 	ð 	ð 	ÝØRñô ð ð	øøøð
 —m’mÝ˜U M°3Ñ7Ô7Ø-Ý! *Ð.@À(ÑKÔKÝ! *Ð.@À(ÑKÔKà!ð 6Ð1Ð1à×3Ò3Ñ5Ô5ð $ñ 
ô 
ˆŒð ˆŒØ'>ˆÔ$Ð$Ð$s   ‚AA ÁA c                 óh   — t          | j        ¦  «        dk    r|                      ¦   «          | j        S )z*Returns the schema of the Gremlin databaser   )Úlenr0   Úrefresh_schemar   s    r   Ú
get_schemazGremlinGraph.get_schemaN   s3   € õ ˆtŒ{ÑÔ˜qÒ Ð Ø×ÒÑ!Ô!Ð!ØŒ{Ðr   c                 ó   — | j                              d¦  «                             ¦   «                              ¦   «         }| j                              d¦  «                             ¦   «                              ¦   «         }| j                              d¦  «                             ¦   «                              ¦   «         d         }|||dœ| _        d                     dd                     |¦  «        d	d                     |¦  «        d
|› �g¦  «        | _        | j        rc| j                              d¦  «                             ¦   «                              ¦   «         d         }|| j        d<   | xj        d|› �z  c_        dS dS )zA
        Refreshes the Gremlin graph schema information.
        zg.V().label().dedup()zg.E().label().dedup()z?g.V().group().by(label).by(properties().label().dedup().fold())r   )Úvertex_labelsÚedge_labelsÚvertice_propsú
z Vertex labels are the following:ú,zEdge labels are the following:z$Vertices have following properties:
z£g.E().group().by(label).by(project('inVLabel', 'outVLabel','properties').by(inV().label()).by(outV().label()).by(properties().key().dedup().fold()).dedup().fold())Ú
edge_propsz^
Edges have the following properties, grouped by label and the distinct inV and outV labels:
 N)r    ÚsubmitÚallÚresultr   Újoinr0   r   )r   Úvertex_schemaÚedge_schemaÚvertex_propertiesÚedge_propertiess        r   r4   zGremlinGraph.refresh_schemaU   sˆ  € ð œ×*Ò*Ð+BÑCÔC×GÒGÑIÔI×PÒPÑRÔRˆØ”k×(Ò(Ð)@ÑAÔA×EÒEÑGÔG×NÒNÑPÔPˆàŒK×ÒØQñô ÷ ŠS‰UŒUßŠV‰XŒX�aô	ð 	ð +Ø&Ø.ð"
ð "
ˆÔð —i’ià2Ø—’˜Ñ'Ô'Ø0Ø—’˜Ñ%Ô%ØKÐ8IÐKÐKðñ
ô 
ˆŒð Ô'ð 	à”×"Ò"ð/ñô ÷ ’‘”ß’‘”˜!ôð ð 4CˆDÔ" <Ñ0ØˆKŒKðJØ8GðJð JñˆKŒKˆKˆKð	ð 	r   ÚqueryÚparamsc                 ó‚   — | j                              |¦  «        }|                     ¦   «                              ¦   «         S r   )r    r=   r>   r?   )r   rE   rF   Úqs       r   rE   zGremlinGraph.queryƒ   s/   € ØŒK×Ò˜uÑ%Ô%ˆØ�uŠu‰wŒw�~Š~ÑÔÐr   Úgraph_documentsÚinclude_sourcec           
      ó¢  — i }|D �]H}|r�|j         j        |j         j        dœ}t          j        |j         j                             ¦   «         ¦  «                             ¦   «         }|                      t          |d|¬¦  «        |¦  «        }|j	        D ]e}|                      |¦  «        }	|rL|  
                    t          d||	i ¬¦  «        ¦  «         |  
                    t          d|	|i ¬¦  «        ¦  «         Œf|j        D ]M}
|                      |
j         |¦  «         |                      |
j        |¦  «         |  
                    |
¦  «         ŒN�ŒJdS )zN
        Take GraphDocument as input as uses it to construct a graph.
        )Úpage_contentÚmetadataÚDocument)ÚidÚtypeÚ
propertieszcontains information about)rP   ÚsourceÚtargetrQ   zis extracted fromN)rR   rL   rM   ÚhashlibÚmd5ÚencodeÚ	hexdigestÚadd_noder
   ÚnodesÚadd_edger   ÚrelationshipsrS   )r   rI   rJ   Ú
node_cacheÚdocumentÚ	doc_propsÚdoc_idÚdoc_nodeÚnÚnodeÚels              r   Úadd_graph_documentsz GremlinGraph.add_graph_documents‡   s�  € ð 35ˆ
Ø'ð )	"ñ )	"ˆHØð 	ð %-¤OÔ$@Ø (¤Ô 8ðð �	õ !œ X¤_Ô%A×%HÒ%HÑ%JÔ%JÑKÔK×UÒUÑWÔW�ØŸ=š=Ý˜F¨À	ÐJÑJÔJÈJñô �ð
 ”^ð ð �Ø—}’} QÑ'Ô'�Ø!ð à—M’MÝ$Ø!=Ø#+Ø#'Ø')ð	ñ ô ñô ð ð —M’MÝ$Ø!4Ø#'Ø#+Ø')ð	ñ ô ñô ð øð Ô,ð "ð "�à—’˜bœi¨Ñ4Ô4Ð4à—’˜bœi¨Ñ4Ô4Ð4à—’˜bÑ!Ô!Ð!Ð!ñ"ðG)	"ð )	"r   rb   c                 óÀ   — d|j         › d�d|j        › d�z   d|j         › d�z   d|j        › d�z   }|j                             ¦   «         D ]\  }}|d|› d|› d�z  }Œ|d	z   S )
Nzg.V().has('id','z	').fold()z.coalesce(unfold(),addV('ú')z.property('id','z.property('type','ú.property('ú', 'ú))rO   rP   rQ   Úitems)r   rb   Ú
base_queryÚkeyÚvalues        r   Úbuild_vertex_queryzGremlinGraph.build_vertex_query¹   s¦   € à1˜tœwÐ1Ð1Ð1Ø7¨$¬)Ð7Ð7Ð7ñ8à, ¤Ð,Ð,Ð,ñ-ð 1 4¤9Ð0Ð0Ð0ñ1ð 	ð œ/×/Ò/Ñ1Ô1ð 	;ð 	;‰JˆC�ØÐ:¨Ð:Ð:°Ð:Ð:Ð:Ñ:ˆJˆJà˜CÑÐr   Úrelationshipc           	      ó  — d|j         j        › d�}d|j        j        › d�}d|› d|› d|j        › d|j        › d�	                     dd	¦  «                             d
d	¦  «        }|j                             ¦   «         D ]\  }}|d|› d|› d�z  }Œ|S )Nz.has('id','rf   z"g.V()z.as('a')  
            .V()z7.as('b') 
            .choose(
                __.inE('zR').where(outV().as('a')),
                __.identity(),
                __.addE('z7').from('a').to('b')
            )        
            r:   r&   ú	rg   rh   )rR   rO   rS   rP   ÚreplacerQ   rj   )r   ro   Úsource_queryÚtarget_queryrk   rl   rm   s          r   Úbuild_edge_queryzGremlinGraph.build_edge_queryÅ   sã   € Ø? \Ô%8Ô%;Ð?Ð?Ð?ˆØ? \Ô%8Ô%;Ð?Ð?Ð?ˆð ð ð Øðð ð &Ô*ðð ð
 'Ô+ðð ð ÷ ’˜˜bÑ!Ô!§'¢'¨$°Ñ"3Ô"3ð 	ð 'Ô1×7Ò7Ñ9Ô9ð 	;ð 	;‰JˆC�ØÐ:¨Ð:Ð:°Ð:Ð:Ð:Ñ:ˆJˆJàÐr   r\   c                 ó(  — d|j         vr|j        |j         d<   |j        |v r||j                 S |                      |¦  «        }| j                             |¦  «                             ¦   «                              ¦   «         d         }|||j        <   |S )NÚlabelr   )rQ   rP   rO   rn   r    r=   r>   r?   )r   rb   r\   rE   Ú_s        r   rX   zGremlinGraph.add_nodeÖ   s�   € à˜$œ/Ð)Ð)Ø'+¤yˆDŒO˜GÑ$ØŒ7�jÐ Ð Ø˜dœgÔ&Ð&à×+Ò+¨DÑ1Ô1ˆEØ”×"Ò" 5Ñ)Ô)×-Ò-Ñ/Ô/×6Ò6Ñ8Ô8¸Ô;ˆAØ"&ˆJ�t”wÑØˆKr   c                 ó¨   — |                       |¦  «        }| j                             |¦  «                             ¦   «                              ¦   «         S r   )ru   r    r=   r>   r?   )r   ro   rE   s      r   rZ   zGremlinGraph.add_edgeâ   sC   € Ø×%Ò% lÑ3Ô3ˆØŒ{×!Ò! %Ñ(Ô(×,Ò,Ñ.Ô.×5Ò5Ñ7Ô7Ð7r   )NNNr   NF)r   N)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr   Ústrr   r   r   Úboolr1   r5   r4   Údictr   rE   r	   rd   r
   rn   r   ru   rX   rZ   © r   r   r   r      s"  € € € € € ðð ð8 ð& t¨C°¨H¤~ð &ð &ð &ñ „Xð&ð
 "Ø"&Ø"&Ø #Ø,0Ø(-ð ?ð  ?à�cŒ]ð ?ð ˜3”-ð ?ð ˜3”-ð	 ?ð
 ð ?ð % SœMð ?ð "&ð ?ð 
ð ?ð  ?ð  ?ð  ?ðD ð˜Cð ð ð ñ „Xðð,ð ,ð ,ð ,ð\ 02ð  ð  ˜3ð  ¨ð  °d¸4ÀÀSÀ¼>Ô6Jð  ð  ð  ð  ð
 LQð0"ð 0"Ø# MÔ2ð0"ØDHð0"à	ð0"ð 0"ð 0"ð 0"ðd
  tð 
 °ð 
 ð 
 ð 
 ð 
 ð¨\ð ¸cð ð ð ð ð" 79ð 
ð 
˜Tð 
¨tð 
¸Tð 
ð 
ð 
ð 
ð8 \ð 8°cð 8ð 8ð 8ð 8ð 8ð 8r   r   )rT   r)   Útypingr   r   r   r   r   Úlangchain_core.utilsr   Ú)langchain_community.graphs.graph_documentr	   r
   r   Ú&langchain_community.graphs.graph_storer   r   r‚   r   r   ú<module>r‡      sÁ   ðØ €€€Ø 
€
€
€
Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3à -Ð -Ð -Ð -Ð -Ð -à WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WØ =Ð =Ð =Ð =Ð =Ð =ðY8ð Y8ð Y8ð Y8ð Y8�:ñ Y8ô Y8ð Y8ð Y8ð Y8r   