
    NgC                        d Z ddlZddlmZm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 dd	lmZ 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"  ej#        e$          Z% eddd           G d dee                      Z& eddd           G d de&                      Z' eddd           G d de&                      Z( eddd           G d de&                      Z) eddd           G d de&                      Z* eddd           G d de                      Z+dS )zJDeprecated as of LangChain v0.3.4 and will be removed in LangChain v1.0.0.    N)ABCabstractmethod)islice)AnyDictIterableListOptional)
deprecated)BaseLanguageModel)BaseMessageget_buffer_string)BasePromptTemplate)	BaseModel
ConfigDictField)LLMChain)BaseChatMemory)ENTITY_EXTRACTION_PROMPTENTITY_SUMMARIZATION_PROMPT)get_prompt_input_keyz0.3.1z1.0.0z_Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/)sinceremovalmessagec            	           e Zd ZdZeddedee         dee         fd            Zededee         ddfd            Zededdfd	            Z	edede
fd
            Zedd            ZdS )BaseEntityStorez%Abstract base class for Entity store.Nkeydefaultreturnc                     dS )zGet entity value from store.N selfr   r   s      S/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/memory/entity.pygetzBaseEntityStore.get$   	     	    valuec                     dS )zSet entity value in store.Nr!   r#   r   r(   s      r$   setzBaseEntityStore.set)   r&   r'   c                     dS )zDelete entity value from store.Nr!   r#   r   s     r$   deletezBaseEntityStore.delete.   r&   r'   c                     dS )z Check if entity exists in store.Nr!   r-   s     r$   existszBaseEntityStore.exists3   r&   r'   c                     dS )zDelete all entities from store.Nr!   r#   s    r$   clearzBaseEntityStore.clear8   r&   r'   Nr   N)__name__
__module____qualname____doc__r   strr
   r%   r+   r.   boolr0   r3   r!   r'   r$   r   r      s        0/ s Xc] hsm    ^ s 8C= T    ^ # $    ^ # $    ^    ^  r'   r   c                       e Zd ZU dZi Zeeee         f         ed<   ddedee         dee         fdZ	dedee         ddfd	Z
deddfd
ZdedefdZddZdS )InMemoryEntityStorezIn-memory Entity store.storeNr   r   r   c                 8    | j                             ||          S r4   )r>   r%   r"   s      r$   r%   zInMemoryEntityStore.getK   s    z~~c7+++r'   r(   c                     || j         |<   d S r4   r>   r*   s      r$   r+   zInMemoryEntityStore.setN   s    
3r'   c                     | j         |= d S r4   rA   r-   s     r$   r.   zInMemoryEntityStore.deleteQ   s    JsOOOr'   c                     || j         v S r4   rA   r-   s     r$   r0   zInMemoryEntityStore.existsT   s    dj  r'   c                 4    | j                                         S r4   )r>   r3   r2   s    r$   r3   zInMemoryEntityStore.clearW   s    z!!!r'   r4   r5   )r6   r7   r8   r9   r>   r   r:   r
   __annotations__r%   r+   r.   r;   r0   r3   r!   r'   r$   r=   r=   >   s          "!&(E4Xc]"#(((, ,s ,Xc] ,hsm , , , , s  8C=  T        # $    !# !$ ! ! ! !" " " " " "r'   r=   c                        e Zd ZdZ	 	 	 	 	 	 ddeded	ed
edee         dee         dedef fdZe	defd            Z
ddedee         dee         fdZdedee         ddfdZdeddfdZdedefdZddZ xZS )UpstashRedisEntityStorezUpstash Redis backed Entity store.

    Entities get a TTL of 1 day by default, and
    that TTL is extended by 3 days every time the entity is read back.
    r    memory_storeQ  
session_idurltoken
key_prefixttl
recall_ttlargskwargsc                 <   	 ddl m}	 n# t          $ r t          d          w xY w t                      j        |i | 	  |	||          | _        n*# t          $ r t                              d           Y nw xY w|| _	        || _
        || _        |p|| _        d S )Nr   )RediszbCould not import upstash_redis python package. Please install it with `pip install upstash_redis`.)rM   rN   z.Upstash Redis instance could not be initiated.)upstash_redisrU   ImportErrorsuper__init__redis_client	ExceptionloggererrorrL   rO   rP   rQ   )r#   rL   rM   rN   rO   rP   rQ   rR   rS   rU   	__class__s             r$   rY   z UpstashRedisEntityStore.__init__j   s    	+++++++ 	 	 	F  	 	$)&)))	K %#U ; ; ;D 	K 	K 	KLLIJJJJJ	K %$$+s   
 $A $A;:A;r   c                 $    | j          d| j         S N:rO   rL   r2   s    r$   full_key_prefixz'UpstashRedisEntityStore.full_key_prefix       /55DO555r'   Nr   c           	          | j                             | j         d| | j                  p|pd}t                              d| j         d| d| d           |S )Nra   exrH   zUpstash Redis MEM get '': ''rZ   getexrc   rQ   r\   debugr#   r   r   ress       r$   r%   zUpstashRedisEntityStore.get   s{    ##t';$C$Cc$C$C#XX  	
 	Ut/CUUcUUsUUUVVV
r'   r(   c           
          |s|                      |          S | j                            | j         d| || j                   t
                              d| j         d| d| d| j                    d S )Nra   rf   zRedis MEM set 'rh   ' EX r.   rZ   r+   rc   rP   r\   rl   r*   s      r$   r+   zUpstashRedisEntityStore.set        	$;;s###!5====uRRRTd2TTSTTeTT$(TT	
 	
 	
 	
 	
r'   c                 N    | j                             | j         d|            d S r`   rZ   r.   rc   r-   s     r$   r.   zUpstashRedisEntityStore.delete   0      D$8!@!@3!@!@AAAAAr'   c                 R    | j                             | j         d|           dk    S Nra      rZ   r0   rc   r-   s     r$   r0   zUpstashRedisEntityStore.exists   /     ''4+?(G(G#(G(GHHAMMr'   c                 x     dt           dt           f fd} |d          }|dk    r ||           |dk    d S d S )Ncursorr   c                 r    j                             | j         d          \  } } j         j        |  | S )N:*)rZ   scanrc   r.   )r|   keys_to_deleter#   s     r$   scan_and_deletez6UpstashRedisEntityStore.clear.<locals>.scan_and_delete   sK    %)%6%;%;4/333& &"FN %D$n55Mr'   r   )int)r#   r   r|   s   `  r$   r3   zUpstashRedisEntityStore.clear   sl    	C 	C 	 	 	 	 	 	 !##kkOF### kkkkkkr'   )r   rH   rH   rI   rJ   rK   r4   r5   )r6   r7   r8   r9   r:   r
   r   r   rY   propertyrc   r%   r+   r.   r;   r0   r3   __classcell__r^   s   @r$   rG   rG   [   s         $()$4, ,, , 	,
 , c], SM, , , , , , , ,> 6 6 6 6 X6 s Xc] hsm    
s 
8C= 
T 
 
 
 
B# B$ B B B BN# N$ N N N N
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$r'   rG   c                   R    e Zd ZU dZeed<   dZeed<   dZeed<   dZ	e
e         ed<   d	Ze
e         ed
<   	 	 	 	 	 ddededede
e         d
e
e         dedef fdZedefd            Zddede
e         de
e         fdZdede
e         ddfdZdeddfdZdedefdZddZ xZS )RedisEntityStorezRedis-backed Entity store.

    Entities get a TTL of 1 day by default, and
    that TTL is extended by 3 days every time the entity is read back.
    rZ   r   rL   rI   rO   rJ   rP   rK   rQ   redis://localhost:6379/0rM   rR   rS   c                    	 dd l }n# t          $ r t          d          w xY w t                      j        |i | 	 ddlm}	 n# t          $ r t          d          w xY w	  |	|d          | _        n6# |j        j        $ r$}
t          
                    |
           Y d }
~
nd }
~
ww xY w|| _        || _        || _        |p|| _        d S )Nr   zRCould not import redis python package. Please install it with `pip install redis`.)
get_clientzzCould not import langchain_community.utilities.redis.get_client. Please install it with `pip install langchain-community`.T)	redis_urldecode_responses)redisrW   rX   rY   #langchain_community.utilities.redisr   rZ   
exceptionsConnectionErrorr\   r]   rL   rO   rP   rQ   )r#   rL   rM   rO   rP   rQ   rR   rS   r   r   r]   r^   s              r$   rY   zRedisEntityStore.__init__   s<   	LLLL 	 	 	>  	 	$)&)))	FFFFFFF 	 	 	L  		  *
S4 P P PD/ 	  	  	 LL	  %$$+s+    "A A $A7 7B*B%%B*r   c                 $    | j          d| j         S r`   rb   r2   s    r$   rc   z RedisEntityStore.full_key_prefix   rd   r'   Nr   c           	          | j                             | j         d| | j                  p|pd}t                              d| j         d| d| d           |S )Nra   rf   rH   zREDIS MEM get 'rh   ri   rj   rm   s       r$   r%   zRedisEntityStore.get   s{    ##t';$C$Cc$C$C#XX  	
 	Mt';MMcMMsMMMNNN
r'   r(   c           
          |s|                      |          S | j                            | j         d| || j                   t
                              d| j         d| d| d| j                    d S )Nra   rf   zREDIS MEM set 'rh   rp   rq   r*   s      r$   r+   zRedisEntityStore.set   rr   r'   c                 N    | j                             | j         d|            d S r`   rt   r-   s     r$   r.   zRedisEntityStore.delete  ru   r'   c                 R    | j                             | j         d|           dk    S rw   ry   r-   s     r$   r0   zRedisEntityStore.exists  rz   r'   c                     dt           t                   dt          dt           t                   fd} || j                            | j         d          d          D ]} | j        j        |  d S )Niterable
batch_sizer   c              3      K   t          |           }t          t          ||                    x}r%|V  t          t          ||                    x}#d S d S r4   )iterlistr   )r   r   iteratorbatchs       r$   batchedz'RedisEntityStore.clear.<locals>.batched	  sn      H~~Hx < <===%   x < <===%     r'   r~   i  )r   r   r   rZ   	scan_iterrc   r.   )r#   r   keybatchs      r$   r3   zRedisEntityStore.clear  s    	hsm 	 	# 	 	 	 	
  ''4+?(C(C(CDDc
 
 	0 	0H %D$h///	0 	0r'   )r   r   rI   rJ   rK   r4   r5   )r6   r7   r8   r9   r   rE   rL   r:   rO   rP   r
   r   rQ   rY   r   rc   r%   r+   r.   r;   r0   r3   r   r   s   @r$   r   r      s          J$J$$$%C#%%% 0J000 $-()$4$, $,$, $, 	$,
 c]$, SM$, $, $, $, $, $, $, $,L 6 6 6 6 X6 s Xc] hsm    
s 
8C= 
T 
 
 
 
B# B$ B B B BN# N$ N N N N
0 
0 
0 
0 
0 
0 
0 
0r'   r   c                       e Zd ZU dZdZeed<   dZeed<   dZe	ed<    e
d	          Z	 	 	 ddededede	de	f
 fdZedefd            ZddZddedee         dee         fdZdedee         ddfdZdeddfdZdedefdZddZ xZS )SQLiteEntityStorezSQLite-backed Entity storer   rL   rI   
table_nameNconnT)arbitrary_types_allowedentities.dbdb_filerR   rS   c                     	 dd l }n# t          $ r t          d          w xY w t                      j        |i | |                    |          | _        || _        || _        |                                  d S )Nr   zVCould not import sqlite3 python package. Please install it with `pip install sqlite3`.)	sqlite3rW   rX   rY   connectr   rL   r   _create_table_if_not_exists)r#   rL   r   r   rR   rS   r   r^   s          r$   rY   zSQLiteEntityStore.__init__'  s    	NNNN 	 	 	@  	
 	$)&)))OOG,,	$$((*****s    "r   c                 $    | j          d| j         S )N_)r   rL   r2   s    r$   full_table_namez!SQLiteEntityStore.full_table_name=  rd   r'   c                     d| j          d}| j        5  | j                            |           d d d            d S # 1 swxY w Y   d S )Nz(
            CREATE TABLE IF NOT EXISTS zZ (
                key TEXT PRIMARY KEY,
                value TEXT
            )
        r   r   execute)r#   create_table_querys     r$   r   z-SQLiteEntityStore._create_table_if_not_existsA  s    (,(<   Y 	2 	2I0111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2   ;??r   c                     d| j          d}| j                            ||f          }|                                }|
|d         }|S |S )Nz+
            SELECT value
            FROM #
            WHERE key = ?
        r   r   r   r   fetchone)r#   r   r   queryr|   resultr(   s          r$   r%   zSQLiteEntityStore.getK  sa    &  
 ""53&11""1IELr'   r(   c                     |s|                      |          S d| j         d}| j        5  | j                            |||f           d d d            d S # 1 swxY w Y   d S )Nz$
            INSERT OR REPLACE INTO z0 (key, value)
            VALUES (?, ?)
        )r.   r   r   r   )r#   r   r(   r   s       r$   r+   zSQLiteEntityStore.setX  s     	$;;s###$($8   Y 	3 	3Iec5\222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3s   AAAc                     d| j          d}| j        5  | j                            ||f           d d d            d S # 1 swxY w Y   d S )N
            DELETE FROM r   r   )r#   r   r   s      r$   r.   zSQLiteEntityStore.deleteb  s    -   Y 	- 	-IecV,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   =AAc                     d| j          d}| j                            ||f          }|                                }|d uS )Nz'
            SELECT 1
            FROM z7
            WHERE key = ?
            LIMIT 1
        r   )r#   r   r   r|   r   s        r$   r0   zSQLiteEntityStore.existsj  sS    &   ""53&11""T!!r'   c                     d| j          d}| j        5  | j                            |           d d d            d S # 1 swxY w Y   d S )Nr   z	
        r   )r#   r   s     r$   r3   zSQLiteEntityStore.clearu  s    -   Y 	% 	%Ie$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%r   )r   r   rI   r5   r4   )r6   r7   r8   r9   rL   r:   rE   r   r   r   r   model_configrY   r   r   r   r
   r%   r+   r.   r;   r0   r3   r   r   s   @r$   r   r     s         %$J$J$$$D#: $  L $$(	+ ++ + 	+
 + + + + + + +, 6 6 6 6 X62 2 2 2 s Xc] hsm    3s 38C= 3T 3 3 3 3-# -$ - - - -	"# 	"$ 	" 	" 	" 	"% % % % % % % %r'   r   c                   t    e Zd ZU dZdZeed<   dZeed<   eed<   e	Z
eed<   eZeed<   g Zee         ed	<   d
Zeed<   dZeed<    ee          Zeed<   edee         fd            Zedee         fd            Zdeeef         deeef         fdZdeeef         deeef         ddf fdZddZ xZ S )ConversationEntityMemorya<  Entity extractor & summarizer memory.

    Extracts named entities from the recent chat history and generates summaries.
    With a swappable entity store, persisting entities across conversations.
    Defaults to an in-memory entity store, and can be swapped out for a Redis,
    SQLite, or other entity store.
    Humanhuman_prefixAI	ai_prefixllmentity_extraction_promptentity_summarization_promptentity_cache   khistorychat_history_key)default_factoryentity_storer   c                     | j         j        S )zAccess chat memory messages.)chat_memorymessagesr2   s    r$   bufferzConversationEntityMemory.buffer  s     ((r'   c                     d| j         gS )zMWill always return list of memory variables.

        :meta private:
        entities)r   r2   s    r$   memory_variablesz)ConversationEntityMemory.memory_variables  s     D122r'   inputsc                 D   t          | j        | j                  }| j        t	          || j                  }n| j        }t          | j        | j         dz  d         | j	        | j
                  }|                    |||                   }|                                dk    rg }nd |                    d          D             }i }|D ] }| j                            |d	          ||<   !|| _        | j        r| j        | j         dz  d         }	n|}	| j        |	d
|iS )ae  
        Returns chat history and all generated entities with summaries if available,
        and updates or clears the recent entity cache.

        New entity name can be found when calling this method, before the entity
        summaries are generated, so the entity cache values may be empty if no entity
        descriptions are generated yet.
        r   promptN   r   r   )r   inputNONEc                 6    g | ]}|                                 S r!   )strip).0ws     r$   
<listcomp>zBConversationEntityMemory.load_memory_variables.<locals>.<listcomp>  s     ===a		===r'   ,rH   r   )r   r   r   	input_keyr   r   r   r   r   r   r   predictr   splitr   r%   r   return_messagesr   )
r#   r   chainprompt_input_keybuffer_stringoutputr   entity_summariesentityr   s
             r$   load_memory_variablesz.ConversationEntityMemory.load_memory_variables  sg    TXd.KLLL>!3FD<QRR#~
 *K!&*n
 
 
 !)*  
 
 <<>>V##HH >=6<<+<+<===H  	I 	IF'+'8'<'<VR'H'HV$$ %  	#+tvgkmm4FF #F !6(
 	
r'   outputsNc                    t                                          ||           | j        t          || j                  }n| j        }t          | j        | j         dz  d         | j        | j	                  }||         }t          | j        | j                  }| j        D ]c}| j                            |d          }|                    ||||          }	| j                            ||	                                           ddS )z
        Save context from this conversation history to the entity store.

        Generates a summary for each entity in the entity cache by prompting
        the model, and saves these summaries to the entity store.
        Nr   r   r   rH   )summaryr   r   r   )rX   save_contextr   r   r   r   r   r   r   r   r   r   r   r   r   r%   r   r+   r   )r#   r   r   r   r   
input_datar   r   existing_summaryr   r^   s             r$   r   z%ConversationEntityMemory.save_context  s#    	VW--->!3FD<QRR#~
 *K!&*n
 
 
 ,-
 TXd.NOOO ' 
	: 
	:F#044VR@@]](% 	 #  F !!&&,,..9999
	: 
	:r'   c                     | j                                          | j                                         | j                                         dS )zClear memory contents.N)r   r3   r   r   r2   s    r$   r3   zConversationEntityMemory.clear  sG       !!!!!!!!r'   r5   )!r6   r7   r8   r9   r   r:   rE   r   r   r   r   r   r   r   r   r	   r   r   r   r   r=   r   r   r   r   r   r   r   r   r   r   r3   r   r   s   @r$   r   r   }  s           L#Is	3K0KKK6Q!3QQQ !L$s)    AsJJJ%c%%% %*E:M$N$N$NL/NNN)[) ) ) ) X) 3$s) 3 3 3 X3?
DcN ?
tCH~ ?
 ?
 ?
 ?
B(:4S> (:DcN (:t (: (: (: (: (: (:T" " " " " " " "r'   r   ),r9   loggingabcr   r   	itertoolsr   typingr   r   r   r	   r
   langchain_core._apir   langchain_core.language_modelsr   langchain_core.messagesr   r   langchain_core.promptsr   pydanticr   r   r   langchain.chains.llmr   langchain.memory.chat_memoryr   langchain.memory.promptr   r   langchain.memory.utilsr   	getLoggerr6   r\   r   r=   rG   r   r   r   r!   r'   r$   <module>r     s{   P P  # # # # # # # #       6 6 6 6 6 6 6 6 6 6 6 6 6 6 * * * * * * < < < < < < B B B B B B B B 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 1 ) ) ) ) ) ) 7 7 7 7 7 7        8 7 7 7 7 7		8	$	$ 
	G	      i   : 
	G	  " " " " "/ " " "* 
	G	  K$ K$ K$ K$ K$o K$ K$ K$\ 
	G	  X0 X0 X0 X0 X0 X0 X0 X0v 
	G	  ^% ^% ^% ^% ^% ^% ^% ^%B 
	G	  W" W" W" W" W"~ W" W" W" W" W"r'   