
    dh8                         S SK r S SKJrJr  S SKJrJrJrJrJ	r	J
r
  S SKJr   " S S\5      r " S S\5      r\" S	S
SS9 " S S\5      5       r\" S	S
SS9 " S S\5      5       rg)    N)ABCabstractmethod)AnyDictListOptionalTupleUnion)
deprecatedc                   L    \ rS rSrSrS\\\4   4S jrS\4S jr	S\
4S jrSrg	)
NeptuneQueryException   z"Exception for the Neptune queries.	exceptionc                     [        U[        5      (       a%  SU;   a  US   OSU l        SU;   a  US   OSU l        g Xl        SU l        g )Nmessageunknowndetails)
isinstancedictr   r   )selfr   s     `/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/graphs/neptune_graph.py__init__NeptuneQueryException.__init__   sE    i&&3<	3I9Y/yDL3<	3I9Y/yDL$L$DL    returnc                     U R                   $ N)r   r   s    r   get_message!NeptuneQueryException.get_message       ||r   c                     U R                   $ r   )r   r   s    r   get_details!NeptuneQueryException.get_details   r!   r   )r   r   N)__name__
__module____qualname____firstlineno____doc__r
   strr   r   r   r   r#   __static_attributes__ r   r   r   r      s2    ,%%T	"2 %S S r   r   c                       \ rS rSrSr\S\4S j5       r\0 4S\S\	S\	4S jj5       r
\S\4S j5       rS\\\   \\   4   4S	 jrS
\\   S\\   4S jrS\\   S\S\4S jrS
\\   S\\\4   S\4S jrSS jrSrg)BaseNeptuneGraph   z Abstract base class for Neptune.r   c                     U R                   $ )z)Return the schema of the Neptune databaseschemar   s    r   
get_schemaBaseNeptuneGraph.get_schema   s     {{r   queryparamsc                     [        5       er   NotImplementedError)r   r5   r6   s      r   r5   BaseNeptuneGraph.query"       !##r   c                     [        5       er   r8   r   s    r   _get_summaryBaseNeptuneGraph._get_summary&   r;   r   c                 <    U R                  5       nUS   nUS   nX#4$ )z<Get node and edge labels from the Neptune statistics summary
nodeLabels
edgeLabels)r=   )r   summaryn_labelse_labelss       r   _get_labelsBaseNeptuneGraph._get_labels*   s-    ##%<(<(!!r   rD   c                     SnSn/ nU H]  nUR                  US9nU R                  U5      nU H4  nUR                  US   S   US   US   S   S9n	UR                  U	5        M6     M_     U$ )	Nz
        MATCH (a)-[e:`{e_label}`]->(b)
        WITH a,e,b LIMIT 3000
        RETURN DISTINCT labels(a) AS from, type(e) AS edge, labels(b) AS to
        LIMIT 10
        z(:`{a}`)-[:`{e}`]->(:`{b}`)e_labelfromr   edgeto)aeb)formatr5   append)
r   rD   triple_querytriple_templatetriple_schemalabelqdatadtriples
             r   _get_triplesBaseNeptuneGraph._get_triples1   s     8E##E#2A::a=D(//ilai1T71: 0  $$V,	   r   rC   typesc           
      ~   Sn/ nU H  nUR                  US9nXPR                  U5      S.n[        0 5      nUS    HG  n	U	S   R                  5        H-  u  pUR	                  X[        U5      R                     45        M/     MI     U V
Vs/ sH	  u  pXS.PM     snn
US.nUR                  U5        M     U$ s  snn
f )Nz_
        MATCH (a:`{n_label}`)
        RETURN properties(a) AS props
        LIMIT 100
        )n_labelrU   
propertiesr`   propspropertytype)r`   labelsrP   r5   setitemsaddrd   r%   rQ   )r   rC   r\   node_properties_querynode_propertiesrU   rV   rW   spkvnps                r   _get_node_properties%BaseNeptuneGraph._get_node_propertiesF   s    !
 E%,,U,;A"**Q-@DBA,'gJ,,.DAEE1DG$4$4567 / (
 GHHadaA9aHB ""2&   Is   B9c           
      ~   Sn/ nU H  nUR                  US9nXPR                  U5      S.n[        0 5      nUS    HG  n	U	S   R                  5        H-  u  pUR	                  X[        U5      R                     45        M/     MI     UU V
Vs/ sH	  u  pXS.PM     snn
S.nUR                  U5        M     U$ s  snn
f )Nzf
        MATCH ()-[e:`{e_label}`]->()
        RETURN properties(e) AS props
        LIMIT 100
        rH   r_   r`   ra   rb   )rd   r`   rf   )r   rD   r\   edge_properties_queryedge_propertiesrU   rV   rW   rl   rm   rn   ro   eps                r   _get_edge_properties%BaseNeptuneGraph._get_edge_properties]   s    !
 E%,,U,;A"**Q-@DBA,'gJ,,.DAEE1DG$4$4567 / (
 FGHadaA9aHB ""2&  	 Is   B9
Nc                     SSSSSSS.nU R                  5       u  p#U R                  U5      nU R                  X!5      nU R                  X15      nSU S	U S
U S3U l        g)z1
Refreshes the Neptune graph schema information.
STRINGDOUBLEINTEGERLISTMAPBOOLEAN)r*   floatintlistr   boolz4
        Node properties are the following:
        z<
        Relationship properties are the following:
        z6
        The relationships are the following:
        z	
        N)rE   rZ   rq   rw   r2   )r   r\   rC   rD   rT   rk   ru   s          r   _refresh_schema BaseNeptuneGraph._refresh_schemat   s     
 "--/))(333HD33HD		 		 		 	r   r1   )r   N)r%   r&   r'   r(   r)   rc   r*   r3   r   r   r5   r   r=   r	   r   rE   rZ   rq   r   rw   r   r+   r,   r   r   r.   r.      s    *C   /1 $3 $ $d $ $ $d $ $"U49d3i#78 "T#Y 49 *T#Y t  .T#Y tCH~ RV .r   r.   z0.3.15z1.0z#langchain_aws.NeptuneAnalyticsGraph)sinceremovalalternative_importc                       \ rS rSrSr   SS\S\S\\   S\\   SS4
S	 jjr0 4S
\S\	S\
\\4   4S jjrS\
4S jrSrg)NeptuneAnalyticsGraph   a	  Neptune Analytics wrapper for graph operations.

Parameters:
    client: optional boto3 Neptune client
    credentials_profile_name: optional AWS profile name
    region_name: optional AWS region, e.g., us-west-2
    graph_identifier: the graph identifier for a Neptune Analytics graph

Example:
    .. code-block:: python

    graph = NeptuneAnalyticsGraph(
        graph_identifier='<my-graph-id>'
    )

*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.
Ngraph_identifierclientcredentials_profile_nameregion_namer   c                     Ub  X l         OaSSKnUb  UR                  US9nOUR                  5       nXl        U(       a  UR                  SUS9U l         OUR                  S5      U l           U R                  5         g! [         a    [	        S5      e[
         a6  n[        U5      R                  S:X  a  [	        S5      Ue[        S	5      UeSnAff = f! [
         a  n[        S
[        U5      S.5      eSnAff = f)z6Create a new Neptune Analytics graph wrapper instance.Nr   profile_namezneptune-graph)r   RCould not import boto3 python package. Please install it with `pip install boto3`.UnknownServiceErrorzgNeptuneGraph requires a boto3 version 1.34.40 or greater.Please install it with `pip install -U boto3`.Could not load credentials to authenticate with AWS client. Please check that credentials in the specified profile name are valid.)Could not get schema for Neptune databaser   detail)r   boto3Sessionr   ImportError	Exceptionrd   r%   
ValueErrorr   r   r*   )r   r   r   r   r   r   sessionrN   s           r   r   NeptuneAnalyticsGraph.__init__   s   %	!$+7#mm9QmRG $mmoG(8%")..'[ #1 #DK #*.."ADK(	  "'  	>   	Aw#88!E 
 !. 		  	'J!!f 	s;   	A? A	A? A? .C ?C1CC
C< C77C<r5   r6   c                     U R                   R                  U R                  UUSS9n[        R                  " US   R                  5       R                  S5      5      S   $ ! [         a  n[        S[        U5      S.5      eSnAff = f)	Query Neptune database.OPEN_CYPHER)graphIdentifierqueryString
parameterslanguagepayloadzUTF-8results,An error occurred while executing the query.r   r   N)
r   execute_queryr   jsonloadsreaddecoder   r   r*   )r   r5   r6   resprN   s        r   r5   NeptuneAnalyticsGraph.query   s    	;;,, $ 5 5!!&	 - D ::d9o224;;GDEiPP 	'M"1v 	s   AA! !
B+BBc                     U R                   R                  U R                  SS9n US   nU$ ! [         a  n[	        S[        U5      S.5      eS nAff = f! [         a'    [	        SUR                  R                  5       S.5      ef = f)Ndetailed)r   modez/Summary API error occurred on Neptune Analyticsr   graphSummary,Summary API did not return a valid response.)r   get_graph_summaryr   r   r   r*   contentr   r   responserN   rB   s       r   r=   "NeptuneAnalyticsGraph._get_summary   s    
	{{44 $ 5 5J 5 H
	~.G N%  	' Q"1v 	  	'M'//668 	s   $. A 
AAA1B)r   r   )NNN)r%   r&   r'   r(   r)   r*   r   r   r   r   r   r5   r=   r+   r,   r   r   r   r      s    < 26%)88 8 #+3-	8
 c]8 
8t 02 3  d38n $d r   r   zlangchain_aws.NeptuneGraphc                       \ rS rSrSr      SS\S\S\S\S\	\   S	\	\   S
\SS4S jjr
0 4S\S\S\\\4   4S jjrS\4S jrSrg)NeptuneGraphi  a  Neptune wrapper for graph operations.

Parameters:
    host: endpoint for the database instance
    port: port number for the database instance, default is 8182
    use_https: whether to use secure connection, default is True
    client: optional boto3 Neptune client
    credentials_profile_name: optional AWS profile name
    region_name: optional AWS region, e.g., us-west-2
    sign: optional, whether to sign the request payload, default is True

Example:
    .. code-block:: python

    graph = NeptuneGraph(
        host='<my-cluster>',
        port=8182
    )

*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.
Nhostport	use_httpsr   r   r   signr   c           	      l    Ub  X@l         OSSKnUb  UR                  US9n	OUR                  5       n	0 n
U(       a  XjS'   U(       a  SOSnU SU SU 3U
S	'   U(       a  U	R                   " S0 U
D6U l         O-SS
KJn  SSKJn  U	R                   "  S0 U
DSU" US90D6U l           U R                  5         g! [         a    [        S5      e[         a6  n[        U5      R                  S:X  a  [        S5      Ue[        S5      UeSnAff = f! [         a  n[        S[        U5      S.5      eSnAff = f)z,Create a new Neptune graph wrapper instance.Nr   r   r   httpshttpz://:endpoint_url)UNSIGNED)Configconfig)signature_versionr   r   zgNeptuneGraph requires a boto3 version 1.28.38 or greater.Please install it with `pip install -U boto3`.r   r   r   )neptunedata)r   r   r   botocorer   botocore.configr   r   r   rd   r%   r   r   r   r*   )r   r   r   r   r   r   r   r   r   r   client_paramsprotocolr   r   rN   s                  r   r   NeptuneGraph.__init__<  sc   0	!$+7#mm9QmRG $mmoG "3>-0&/7V3;*CvQtf0Mn-").."P-"PDK16")..%#'#  &A#DK0	  "'  	>   	Aw#88!E 
 !. 		  	'J!!f 	s;   	B6 A*B6 7,B6 %D 6D
1DD

D3D..D3r5   r6   c                      U R                   R                  US9S   $ ! [         a  n[        S[	        U5      S.5      eSnAff = f)r   )openCypherQueryr   r   r   N)r   execute_open_cypher_queryr   r   r*   )r   r5   r6   rN   s       r   r5   NeptuneGraph.query  sW    
	;;888O   	'M"1v 	s    
A?Ac                     U R                   R                  5       n US   S   nU$ ! [         a  n[        S[	        U5      S.5      eS nAff = f! [         a'    [        SUR
                  R                  5       S.5      ef = f)Nz`Summary API is not available for this instance of Neptune,ensure the engine version is >=1.2.1.0r   r   r   r   )r   get_propertygraph_summaryr   r   r*   r   r   r   s       r   r=   NeptuneGraph._get_summary  s    	{{<<>H
	y).9G N+  		'A  #1v 		  	'M'//668 	s   ' A 
AAA1B)r   )i  TNNNT)r%   r&   r'   r(   r)   r*   r   r   r   r   r   r   r   r5   r=   r+   r,   r   r   r   r     s    D 26%)FF F 	F
 F #+3-F c]F F 
FP 02 3  d38n d r   r   )r   abcr   r   typingr   r   r   r   r	   r
   langchain_core._api.deprecationr   r   r   r.   r   r   r,   r   r   <module>r      s     # : : 6I $ss sl 
<
, 
D 
3
N# N
Nr   