
    3fi                         d dl Z d dlZd dlmZ d dlmZmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZmZmZ erd dlmZ  ej&                  e      Z e
dd	d
       G d de             Zy)    N)time)TYPE_CHECKINGAnyDictListOptional)
deprecated)BaseChatMessageHistory)BaseMessagemessage_to_dictmessages_from_dict)Elasticsearchz0.0.27z#Use langchain-elasticsearch packageT)alternativependingc                   L   e Zd ZdZdddddddddededed   d	ee   d
ee   dee   dee   dee   dee   fdZedefd       Z	eddddddd	ee   dee   dee   dee   dee   dd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y)ElasticsearchChatMessageHistorya  Chat message history that stores history in Elasticsearch.

    Args:
        es_url: URL of the Elasticsearch instance to connect to.
        es_cloud_id: Cloud ID of the Elasticsearch instance to connect to.
        es_user: Username to use when connecting to Elasticsearch.
        es_password: Password to use when connecting to Elasticsearch.
        es_api_key: API key to use when connecting to Elasticsearch.
        es_connection: Optional pre-existing Elasticsearch connection.
        ensure_ascii: Used to escape ASCII symbols in json.dumps. Defaults to True.
        index: Name of the index to use.
        session_id: Arbitrary key that is used to store the messages
            of a single chat session.
    NT)es_connectiones_urles_cloud_ides_user
es_api_keyes_passwordensure_asciiindex
session_idr   r   r   r   r   r   r   r   c                   || _         || _        |	| _        |(|j                  d| j	                         i      | _        n/|| t        j                  |||||      | _        nt        d      | j
                  j                  j                  |      rt        j                  d| d       y t        j                  d| d	       | j
                  j                  j                  |d
ddiddiddidi       y )N
user-agent)headers)r   usernamepasswordcloud_idapi_keyz{Either provide a pre-existing Elasticsearch connection,                 or valid credentials for creating a new connection.)r   zChat history index z# already exists, skipping creation.zCreating index z for storing chat history.
propertiestypekeyworddatetextr   
created_athistory)r   mappings)r   r   r   optionsget_user_agentclientr   connect_to_elasticsearch
ValueErrorindicesexistsloggerdebugcreate)
selfr   r   r   r   r   r   r   r   r   s
             v/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/chat_message_histories/elasticsearch.py__init__z(ElasticsearchChatMessageHistory.__init__%   s&     
)( $'//%t':':'<= 0 DK ;#:9RR $$" S DK G 
 ;;%%E%2LL%eW,OP LL?5'1KLMKK&& '-y&9'-v&6$*F#3# ' 	    returnc                      ddl m}  d|  S )Nr   __version__zlangchain-py-ms/)langchain_communityr=   r<   s    r7   r-   z.ElasticsearchChatMessageHistory.get_user_agent[   s    3!+//r9   )r   r!   r"   r   r    r!   r"   r   r    c                    	 dd l }| r|rt        d      i }| r| g|d<   n|r||d<   nt        d      |r||d<   n|r	|r||f|d<    |j                  di |d	d
t        j                         ii}	 |j                          |S # t        $ r t        d      w xY w# t        $ r}t        j                  d|        |d }~ww xY w)Nr   zbCould not import elasticsearch python package. Please install it with `pip install elasticsearch`.z>Both es_url and cloud_id are defined. Please provide only one.hostsr!   z4Please provide either elasticsearch_url or cloud_id.r"   
basic_authr   r   z#Error connecting to Elasticsearch:  )
elasticsearchImportErrorr0   r   r   r-   info	Exceptionr3   error)	r   r!   r"   r   r    rC   connection_params	es_clienterrs	            r7   r/   z8ElasticsearchChatMessageHistory.connect_to_elasticsearcha   s   	  hP  -/*0g&,4j)STT+2i((/7.Bl+/M// 

!#B#Q#Q#ST
		NN
 G  	F 	>  	LL>seDEI	s#   B 1B B	C$B>>Cc                    	 ddl m} | j                  j                  | j                  dd| j
                  iid      }|rMt        |d	   d	         dkD  r9|d	   d	   D cg c]  }t        j                  |d
   d          }}t        |      S g }t        |      S # $ r}t        j                  d|        |d}~ww xY wc c}w )z(Retrieve the messages from Elasticsearchr   ApiErrortermr   zcreated_at:asc)r   querysortz0Could not retrieve messages from Elasticsearch: Nhits_sourcer*   )rC   rM   r.   searchr   r   r3   rG   lenjsonloadsr   )r6   rM   resultrJ   documentitemss         r7   messagesz(ElasticsearchChatMessageHistory.messages   s    
	.[[''jjdoo>?% ( F c&.01A5 !'vv 6 

8I.y9:E  "%(( E!%((  	LLKC5QRI	
s   <B "CB>B99B>rZ   c                     t        d      )NzODirect assignment to 'messages' is not allowed. Use the 'add_messages' instead.)NotImplementedError)r6   rZ   s     r7   rZ   z(ElasticsearchChatMessageHistory.messages   s    !/
 	
r9   messagec                 V   	 ddl m} | j                  j                  | j                  | j                  t        t               dz        t        j                  t        |      t        | j                              dd       y	# $ r}t        j                  d|        |d	}~ww xY w)
z2Add a message to the chat session in Elasticsearchr   rL   i  )r   r(   T)r   rX   refreshz(Could not add message to Elasticsearch: N)rC   rM   r.   r   r   roundr   rU   dumpsr   boolr   r3   rG   )r6   r]   rM   rJ   s       r7   add_messagez+ElasticsearchChatMessageHistory.add_message   s    	.KKjj"&//"'"6#zz'0%)$*;*;%<      	LLCC5IJI	s   BB B(	B##B(c                     	 ddl m} | j                  j                  | j                  dd| j
                  iid       y# $ r}t        j                  d|        |d}~ww xY w)	z%Clear session memory in Elasticsearchr   rL   rN   r   T)r   rO   r_   z1Could not clear session memory in Elasticsearch: N)rC   rM   r.   delete_by_queryr   r   r3   rG   )r6   rM   rJ   s      r7   clearz%ElasticsearchChatMessageHistory.clear   sg    
	.KK''jjdoo>? ( 
  	LLLSERSI	s   <? A#AA#)r:   N)__name__
__module____qualname____doc__strr   rb   r8   staticmethodr-   r/   propertyr   r   rZ   setterrc   rf   rB   r9   r7   r   r      s   ( 48 $%)!%$(%)'+44 4
  04 4 c]4 #4 SM4 c]4 tn4l 0C 0 0
  !%"&!%"&"&-- 3-- #	-
 3-- 3-- 
- -^ )${+ ) )0 __
k!2 
t 
 
; 4 *r9   r   )rU   loggingr   typingr   r   r   r   r   langchain_core._apir	   langchain_core.chat_historyr
   langchain_core.messagesr   r   r   rC   r   	getLoggerrg   r3   r   rB   r9   r7   <module>ru      si       ; ; * >  +			8	$ H"GQUV}&< } W}r9   