
    Ng                         d dl Z d dlmZ d dlmZmZmZ  e j        e          Z	 ed          Z
dZ G d d          ZdS )    N)Template)AnyDictOptionalz
MATCH ()-[e:`$edge_type`]->()
  WITH e limit 1
MATCH (m)-[:`$edge_type`]->(n) WHERE id(m) == src(e) AND id(n) == dst(e)
RETURN "(:" + tags(m)[0] + ")-[:$edge_type]->(:" + tags(n)[0] + ")" AS rels
   c                       e Zd ZdZ	 	 	 	 	 ddeded	ed
edededdfdZdefdZddZ	e
defd            Zddedee         dedefdZddZddededeeef         fdZdS )NebulaGrapha  NebulaGraph wrapper for graph operations.

    NebulaGraph inherits methods from Neo4jGraph to bring ease to the user space.

    *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.
    rootnebula	127.0.0.1%     spaceusernamepasswordaddressportsession_pool_sizereturnNc                 Z   	 ddl }ddl}n# t          $ r t          d          w xY w|| _        || _        || _        || _        || _        || _        | 	                                | _
        d| _        	 |                                  dS # t          $ r}	t          d|	           d}	~	ww xY w)z*Create a new NebulaGraph wrapper instance.r   Nz^Please install NebulaGraph Python client and pandas first: `pip install nebula3-python pandas` z!Could not refresh schema. Error: )nebula3pandasImportErrorr   r   r   r   r   r   _get_session_poolsession_poolschemarefresh_schema	Exception
ValueError)
selfr   r   r   r   r   r   r   r   es
             c/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/graphs/nebula_graph.py__init__zNebulaGraph.__init__$   s    	NNNMMMM 	 	 	6  	 ! 	
!2 2244	F!!!!! 	F 	F 	FDDDEEE	Fs     %3B	 	
B*B%%B*c                    t          | j        | j        | j        | j        | j        g          s
J d            ddlm} ddlm	}m
} ddlm}  |            }| j        |_        	  || j        | j        | j        | j        | j        fg          }n# |$ r t          d          w xY w	 |                    |           n7# |$ r t          d          t"          $ r}t          d|           d }~ww xY w|S )	NzXPlease provide all of the following parameters: username, password, address, port, spacer   )SessionPoolConfig)AuthFailedExceptionInValidHostname)SessionPoolz^Could not connect to NebulaGraph database. Please ensure that the address and port are correctzcCould not connect to NebulaGraph database. Please ensure that the username and password are correctz(Error initializing session pool. Error: )allr   r   r   r   r   nebula3.Configr&   nebula3.Exceptionr'   r(   nebula3.gclient.net.SessionPoolr)   r   max_sizer    initRuntimeError)r!   r&   r'   r(   r)   configr   r"   s           r#   r   zNebulaGraph._get_session_poolF   s   ]DM4<DJO
 
 	
 	
7	
 	
 	
 	544444JJJJJJJJ??????""$$0	&;
,	*+	 LL  	 	 	F  		Mf%%%%" 	 	 	K    	M 	M 	MKKKLLL	M s$   #*B B#'B= =C1C,,C1c                     	 | j                                          d S # t          $ r(}t                              d|            Y d }~d S d }~ww xY w)Nz%Could not close session pool. Error: )r   closer   loggerwarning)r!   r"   s     r#   __del__zNebulaGraph.__del__n   ss    	H##%%%%% 	H 	H 	HNNF1FFGGGGGGGGG	Hs    
AA

Ac                     | j         S )z.Returns the schema of the NebulaGraph database)r   )r!   s    r#   
get_schemazNebulaGraph.get_schemat   s     {    r   queryparamsretryc                 B   ddl m}m} ddlm} |pi }	 | j                            ||          }|                                s3t          	                    d|
                                 d| d           |S # |$ r: t          	                    d| j                    t          d| j                   t          $ rn}|t          k     rL|dz  }t          	                    d	| d
t           d| d|            |                     |||          cY d}~S t          d|           d}~w||f$ rl |t          k     r]|dz  }t          	                    d| d
t           d           |                                 | _        |                     |||          cY S Y dS w xY w)zQuery NebulaGraph database.r   )IOErrorExceptionNoValidSessionException)TTransportExceptionz-Error executing query to NebulaGraph. Error: z
Query: z 
zhNo valid session found in session pool. Please consider increasing the session pool size. Current size:    z0Error executing query to NebulaGraph. Retrying (/z)...
query: z	 
Error: Nz-Connection issue with NebulaGraph. Retrying (z)...
 to recreate session pool)r,   r>   r?   %nebula3.fbthrift.transport.TTransportr@   r   execute_parameteris_succeededr4   r5   	error_msgr   r    r0   RETRY_TIMESexecuter   )	r!   r:   r;   r<   r>   r?   r@   resultr"   s	            r#   rH   zNebulaGraph.executey   s   OOOOOOOOMMMMMM2,	:&88GGF&&(( )$..00) )#) ) )  
 M& 
	 
	 
	NN:!%!7: :  
 :!%!7: :    	V 	V 	V{""
"!&" ")4" "#" "  " "   ||E6599999999 !TQR!T!TUUU#%56 		: 		: 		:{""
V!&V V)4V V V   %)$:$:$<$<!||E6599999 #""		:s-   A#A8 8AF?AD(FD((A0FFc                 n   g g g }}}|                      d                              d          D ]}|                                }|g d}|                      d| d          }|                    d          |                    d          }	}t          |                                          D ]O}
|d                             ||
                                         |	|
                                         f           P|                    |           |                      d	                              d          D ]f}|                                }|g d
}|                      d| d          }|                    d          |                    d          }	}t          |                                          D ]O}
|d                             ||
                                         |	|
                                         f           P|                    |           |                      t                              |                                        d          }t          |          dk    r-|                    |d                                                    hd| d| d| d| _	        dS )z?
        Refreshes the NebulaGraph schema information.
        z	SHOW TAGSName)tag
propertieszDESCRIBE TAG ``FieldTyperM   z
SHOW EDGES)edgerM   zDESCRIBE EDGE `)	edge_typerelsr   zNode properties: z
Edge properties: z
Relationships: 
N)
rH   column_valuescastrangerow_sizeappend	rel_query
substitutelenr   )r!   tags_schemaedge_types_schemarelationshipsrL   tag_name
tag_schemarpropstypesirR   edge_type_nameedge_schemas                 r#   r   zNebulaGraph.refresh_schema   s    9;B&<<,,::6BB 	+ 	+CxxzzH!)<<J9h999::A??733Q__V5L5L5E1::<<(( T T<(//q%(--//0RSSSSz****l33AA&II 	2 	2I&^^--N#1DDK@~@@@AAA??733Q__V5L5L5E1::<<(( U UL)00%(--//58==??1STTTT$$[111 $$~$>> mF##  1vvzz$$QqTYY[[1110 0 0 10 0+0 0 0 	r9   c                     |                      ||          }|                                }i }t          |                                          D ].}||         }|                    |          }d |D             ||<   /|S )N)r<   c                 6    g | ]}|                                 S  )rV   ).0xs     r#   
<listcomp>z%NebulaGraph.query.<locals>.<listcomp>   s     66616688666r9   )rH   keysrW   col_sizerU   )	r!   r:   r<   rI   columnsdcol_numcol_namecol_lists	            r#   r:   zNebulaGraph.query   s    e511++--V__..// 	7 	7Gw'H++H55H66X666AhKKr9   )r
   r   r   r   r   )r   N)Nr   )r   )__name__
__module____qualname____doc__strintr$   r   r   r6   propertyr8   r   dictrH   r   r   r:   rj   r9   r#   r	   r	      sz        &  "!# F  F F  F 	 F
  F  F  F 
 F  F  F  FD&3 & & & &PH H H H C    X2: 2:S 2:(4. 2: 2:TW 2: 2: 2: 2:h!
 !
 !
 !
F 3 s 4S>      r9   r	   )loggingstringr   typingr   r   r   	getLoggerru   r4   rZ   rG   r	   rj   r9   r#   <module>r      s           & & & & & & & & & &		8	$	$H 	 E E E E E E E E E Er9   