
    3fi                         d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ  eddd	
       G d de             Zy)    )ListOptionalUnion)
deprecated)BaseChatMessageHistory)BaseMessagemessages_from_dict)get_from_dict_or_env)
Neo4jGraphz0.3.8z1.0z'langchain_neo4j.Neo4jChatMessageHistory)sinceremovalalternative_importc                       e Zd ZdZ	 	 	 	 	 	 ddddeeef   dee   dee   dee   ded	ed
edee   fdZ	e
dee   fd       Zej                  dee   ddfd       ZdeddfdZddZddZy)Neo4jChatMessageHistoryz0Chat message history stored in a Neo4j database.N)graph
session_idurlusernamepassworddatabase
node_labelwindowr   c                   	 dd l }	|st        d      |r#|j                  | _        |j                  | _        nt        d|idd      }t        d|idd      }t        d|idd	      }t        d
|id
dd      }|	j                  j                  |||f      | _        || _        	 | j                  j                          || _        || _        || _        | j                  j                  d| j                   dd| j                  i      j                    y # t        $ r t        d      w xY w# |	j                  j                  $ r t        d      |	j                  j                  $ r t        d      w xY w)Nr   zRCould not import neo4j python package. Please install it with `pip install neo4j`.z7Please ensure that the session_id parameter is providedr   	NEO4J_URIr   NEO4J_USERNAMEr   NEO4J_PASSWORDr   NEO4J_DATABASEneo4j)authzJCould not connect to Neo4j database. Please ensure that the url is correctz]Could not connect to Neo4j database. Please ensure that the username and password are correctz
MERGE (s:`z` {id:$session_id})r   )r   ImportError
ValueError_driver	_databaser
   GraphDatabasedriververify_connectivity
exceptionsServiceUnavailable	AuthError_session_id_node_label_windowexecute_querysummary)
selfr   r   r   r   r   r   r   r   r   s
             n/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/chat_message_histories/neo4j.py__init__z Neo4jChatMessageHistory.__init__   s   	 VWW  ==DL"__DN 's|UKHC+X&
4DH ,X&
4DH ,X&
4DgH !..55c8@T5UDL%DN002 &%""))**?@4++,	
 'c  	> 	@ ##66  <  ##--  O s   D !D) D&)AE1returnc                     d| j                    d| j                  dz   d}| j                  j                  |d| j                  i      \  }}}t        |D cg c]  }|d   	 c}      }|S c c}w )z Retrieve the messages from Neo4j
MATCH (s:`z^`)-[:LAST_MESSAGE]->(last_message) WHERE s.id = $session_id MATCH p=(last_message)<-[:NEXT*0..   z]-() WITH p, length(p) AS length ORDER BY length DESC LIMIT 1 UNWIND reverse(nodes(p)) AS node RETURN {data:{content: node.content}, type:node.type} AS resultr   result)r+   r,   r"   r-   r*   r	   )r/   queryrecords_elmessagess         r0   r;   z Neo4jChatMessageHistory.messagesT   s     ))* +J||a  !NN 	 22L$"2"23
A &g&Fr(|&FG 'Gs   A+r;   c                     t        d      )NzODirect assignment to 'messages' is not allowed. Use the 'add_messages' instead.)NotImplementedError)r/   r;   s     r0   r;   z Neo4jChatMessageHistory.messagese   s    !/
 	
    messagec                     d| j                    d}| j                  j                  ||j                  |j                  | j
                  d      j                   y)z)Append the message to the record in Neo4jr4   a  `) WHERE s.id = $session_id OPTIONAL MATCH (s)-[lm:LAST_MESSAGE]->(last_message) CREATE (s)-[:LAST_MESSAGE]->(new:Message) SET new += {type:$type, content:$content} WITH new, lm, last_message WHERE last_message IS NOT NULL CREATE (last_message)-[:NEXT]->(new) DELETE lm)typecontentr   N)r+   r"   r-   rA   rB   r*   r.   )r/   r?   r7   s      r0   add_messagez#Neo4jChatMessageHistory.add_messagel   s[     ))* +  	 	"""??"..	
 'r>   c                     d| j                    d}| j                  j                  |d| j                  i      j                   y)zClear session memory from Neo4jr4   z`)-[:LAST_MESSAGE]->(last_message) WHERE s.id = $session_id MATCH p=(last_message)<-[:NEXT]-() WITH p, length(p) AS length ORDER BY length DESC LIMIT 1 UNWIND nodes(p) as node DETACH DELETE node;r   N)r+   r"   r-   r*   r.   )r/   r7   s     r0   clearzNeo4jChatMessageHistory.clear   sH     ))* +: : 	 	""5<9I9I*JKSSr>   c                 R    | j                   r| j                   j                          y y )N)r"   close)r/   s    r0   __del__zNeo4jChatMessageHistory.__del__   s    <<LL  r>   )NNNr   Session   )r2   N)__name__
__module____qualname____doc__r   strintr   r   r1   propertyr   r   r;   setterrC   rE   rH    r>   r0   r   r      s     ;
 ""&"&#? '+?#s(O? c]? 3-	?
 3-? ? ? ? 
#?B ${+    __
k!2 
t 
 
; 4 (T!r>   r   N)typingr   r   r   langchain_core._api.deprecationr   langchain_core.chat_historyr   langchain_core.messagesr   r	   langchain_core.utilsr
   langchain_community.graphsr   r   rS   r>   r0   <module>rZ      sG    ( ( 6 > C 5 1 
@
|!4 |!
|!r>   