
    Ng'                     r    d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZ dZdZdZ G d	 d
e          ZdS )    N)AnyDictListOptional)
deprecated)GraphDocument)
GraphStorez
MATCH (n)
WITH keys(n) as keys, labels(n) AS labels
WITH CASE WHEN keys = [] THEN [NULL] ELSE keys END AS keys, labels
UNWIND labels AS label
UNWIND keys AS key
WITH label, collect(DISTINCT key) AS keys
RETURN {label:label, keys:keys} AS output
z
MATCH ()-[r]->()
WITH keys(r) as keys, type(r) AS types
WITH CASE WHEN keys = [] THEN [NULL] ELSE keys END AS keys, types 
UNWIND types AS type
UNWIND keys AS key WITH type,
collect(DISTINCT key) AS keys 
RETURN {types:type, keys:keys} AS output
z
MATCH (n)-[r]->(m)
UNWIND labels(n) as src_label
UNWIND labels(m) as dst_label
UNWIND type(r) as rel_type
RETURN DISTINCT {start: src_label, type: rel_type, end: dst_label} AS output
c                      e Zd ZdZ	 	 	 	 	 ddededed	ee         d
ee         deddfdZ	 	 	 	 	 ddededed	ee         d
ee         deddfdZ	 e
dd          	 	 	 	 	 ddededed	ee         d
ee         deddfd            Zedefd            Zedeeef         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S )FalkorDBGrapha  FalkorDB 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.
    	localhost  NFdatabasehostportusernamepasswordsslreturnc           	      ^   	 |                      ||||||           nI# t          $ r< 	 |                     ||||||           n# t          $ r t          d          w xY wY nw xY wd| _        i | _        	 |                                  dS # t          $ r}t          d|           d}~ww xY w)z-Create a new FalkorDB graph wrapper instance.zXCould not import falkordb python package. Please install it with `pip install falkordb`. z!Could not refresh schema. Error: N)(_FalkorDBGraph__init_falkordb_connectionImportError%_FalkorDBGraph__init_redis_connectionschemastructured_schemarefresh_schema	Exception
ValueError)selfr   r   r   r   r   r   es           e/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/graphs/falkordb_graph.py__init__zFalkorDBGraph.__init__5   s%   	++$h#     
	 
	 
		,,dD(Hc       !E   
	 13	F!!!!! 	F 	F 	FDDDEEE	Fs>    
A#AA#AA#"A#5B 
B,B''B,c                     ddl m} 	  ||||||          | _        n$# t          $ r}t	          d|           d }~ww xY w| j                            |          | _        d S )Nr   )FalkorDBr   r   r   r   r   zFailed to connect to FalkorDB: )falkordbr$   _driverr   ConnectionErrorselect_graph_graph)	r   r   r   r   r   r   r   r$   r    s	            r!   __init_falkordb_connectionz(FalkorDBGraph.__init_falkordb_connectionX   s     	&%%%%%	I#8x(PS  DLL  	I 	I 	I!"GA"G"GHHH	I l//99s    
?:?z0.0.31r+   )alternativec                     dd l }ddlm} t          j        dt
                     |                    |||||          | _         || j        |          | _        d S )Nr   )GraphzwUsing the redis package is deprecated. Please use the falkordb package instead, install it with `pip install falkordb`.r%   )	redisredis.commands.graphr.   warningswarnDeprecationWarningRedisr'   r*   )	r   r   r   r   r   r   r   r/   r.   s	            r!   __init_redis_connectionz%FalkorDBGraph.__init_redis_connectionl   s     	...... 	6 		
 	
 	
 {{D8hC # 
 
 eDL(33    c                     | j         S )z+Returns the schema of the FalkorDB database)r   r   s    r!   
get_schemazFalkorDBGraph.get_schema   s     {r6   c                     | j         S )z*Returns the structured schema of the Graph)r   r8   s    r!   get_structured_schemaz#FalkorDBGraph.get_structured_schema   s     %%r6   c                    |                      t                    }|                      t                    }|                      t                    }d |D             d |D             d |D             d| _        d| d| d| d| _        d	S )
z-Refreshes the schema of the FalkorDB databasec                 D    i | ]}|d          d         |d          d         S )r   labelkeys .0els     r!   
<dictcomp>z0FalkorDBGraph.refresh_schema.<locals>.<dictcomp>   s*    SSSR2a5>2a5=SSSr6   c                 D    i | ]}|d          d         |d          d         S )r   typesr?   r@   rA   s     r!   rD   z0FalkorDBGraph.refresh_schema.<locals>.<dictcomp>   s*    QQQB"Q%."Q%-QQQr6   c                     g | ]
}|d          S )r   r@   rA   s     r!   
<listcomp>z0FalkorDBGraph.refresh_schema.<locals>.<listcomp>   s    <<<be<<<r6   )
node_props	rel_propsrelationshipszNode properties: z
Relationships properties: z
Relationships: 
N)querynode_properties_queryrel_properties_query	rel_queryr   r   )r   node_propertiesrel_propertiesrK   s       r!   r   zFalkorDBGraph.refresh_schema   s    %)ZZ0E%F%F$(JJ/C$D$D#'::i#8#8 TS?SSSQQ.QQQ<<m<<<"
 "
0 0 0)70 0+0 0 0 	r6   rM   paramsc                     	 | j                             ||          }|j        S # t          $ r}t	          d|           d}~ww xY w)zQuery FalkorDB database.z(Generated Cypher Statement is not valid
N)r*   rM   
result_setr   r   )r   rM   rS   datar    s        r!   rM   zFalkorDBGraph.query   sa    	Q;$$UF33D?" 	Q 	Q 	QOAOOPPP	Qs   !$ 
AA  Agraph_documentsinclude_sourcec                    |D ]}|j         D ]0}|                     d|j         d|j         dd|j        i           1|j        D ]}|                     d|j        j         d|j        j         d|j        j         d|j        j         d|j                            dd	          	                                 d
d|j        i           dS )zN
        Take GraphDocument as input as uses it to construct a graph.
        z	MERGE (n:z {id:'z9'}) SET n += $properties RETURN distinct 'done' AS result
propertiesz	MATCH (a:z'}), (b:z'}) MERGE (a)-[r: _z<]->(b) SET r += $properties RETURN distinct 'done' AS resultN)
nodesrM   typeidrZ   rK   sourcetargetreplaceupper)r   rW   rX   documentnoderels         r!   add_graph_documentsz!FalkorDBGraph.add_graph_documents   s2    ( 	 	H   

;DI ; ;dg ; ; ; "4?3     - 
 


;CJO ; ;CJM ; ;!jo; ;69jm; ;),)9)9#s)C)C)I)I)K)K; ; ; "3>2	 	 	 	
	 	r6   )r   r   NNF)r   N)F)__name__
__module____qualname____doc__strintr   boolr"   r   r   r   propertyr9   r   r   r;   r   dictr   rM   r   rg   r@   r6   r!   r   r   &   s        "  "&"&!F !F!F !F 	!F
 3-!F 3-!F !F 
!F !F !F !FL  "&"&: :: : 	:
 3-: 3-: : 
: : : :( Z&BCCC  "&"&4 44 4 	4
 3-4 3-4 4 
4 4 4 DC44 C    X &tCH~ & & & X&
 
 
 
$ 02 Q Q3 Q Qd4S>6J Q Q Q Q LQ #M2DH	     r6   r   )r1   typingr   r   r   r   langchain_core._apir   )langchain_community.graphs.graph_documentr   &langchain_community.graphs.graph_storer	   rN   rO   rP   r   r@   r6   r!   <module>ru      s     , , , , , , , , , , , , * * * * * * C C C C C C = = = = = =  	c c c c cJ c c c c cr6   