HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/langchain/memory/__pycache__/entity.cpython-310.pyc
o

���gC�@s�dZddlZddlmZmZddlmZddlmZm	Z	m
Z
mZmZddl
mZddlmZddlmZmZdd	lmZdd
lmZmZmZddlmZddlmZdd
lmZm Z ddl!m"Z"e�#e$�Z%edddd�Gdd�dee��Z&edddd�Gdd�de&��Z'edddd�Gdd�de&��Z(edddd�Gdd�de&��Z)edddd�Gdd�de&��Z*edddd�Gdd�de��Z+dS)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_keyz0.3.1z1.0.0z_Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/)�since�removal�messagec	@s�eZdZdZeddedeedeefdd��Zededeeddfd	d
��Zededdfdd��Z	edede
fd
d��Zeddd��ZdS)�BaseEntityStorez%Abstract base class for Entity store.N�key�default�returncC�dS)zGet entity value from store.N���selfrrrr�B/usr/local/lib/python3.10/dist-packages/langchain/memory/entity.py�get$�zBaseEntityStore.get�valuecCr)zSet entity value in store.Nr�r!rr%rrr"�set)r$zBaseEntityStore.setcCr)zDelete entity value from store.Nr�r!rrrr"�delete.r$zBaseEntityStore.deletecCr)z Check if entity exists in store.Nrr(rrr"�exists3r$zBaseEntityStore.existscCr)zDelete all entities from store.Nr�r!rrr"�clear8r$zBaseEntityStore.clear�N�rN)
�__name__�
__module__�__qualname__�__doc__r�strr	r#r'r)�boolr*r,rrrr"rs	"rc@s�eZdZUdZiZeeeefed<ddedeedeefdd�Z	ded	eeddfd
d�Z
deddfdd
�Zdedefdd�Z
ddd�ZdS)�InMemoryEntityStorezIn-memory Entity store.�storeNrrrcCs|j�||�Sr-)r6r#r rrr"r#K�zInMemoryEntityStore.getr%cCs||j|<dSr-�r6r&rrr"r'Nr7zInMemoryEntityStore.setcCs|j|=dSr-r8r(rrr"r)QszInMemoryEntityStore.deletecCs
||jvSr-r8r(rrr"r*T�
zInMemoryEntityStore.existscCs
|j��Sr-)r6r,r+rrr"r,Wr9zInMemoryEntityStore.clearr-r.)r/r0r1r2r6rr3r	�__annotations__r#r'r)r4r*r,rrrr"r5>s
	 r5cs�eZdZdZ						d!deded	ed
edeedeed
edef�fdd�
Ze	defdd��Z
d"dedeedeefdd�Zdedeeddfdd�Zdeddfdd�Z
dedefdd�Zd#dd �Z�ZS)$�UpstashRedisEntityStorez�Upstash 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_store�Q���
session_id�url�token�
key_prefix�ttl�
recall_ttl�args�kwargsc
s�zddlm}	Wntytd��wt�j|i|��z	|	||d�|_Wnty4t�d�Ynw||_	||_
||_|pA||_dS)Nr)�RediszbCould not import upstash_redis python package. Please install it with `pip install upstash_redis`.)rArBz.Upstash Redis instance could not be initiated.)
�
upstash_redisrH�ImportError�super�__init__�redis_client�	Exception�logger�errorr@rCrDrE)
r!r@rArBrCrDrErFrGrH��	__class__rr"rLjs"���z UpstashRedisEntityStore.__init__rcC�|j�d|j��S�N�:�rCr@r+rrr"�full_key_prefix��z'UpstashRedisEntityStore.full_key_prefixNrc	C�J|jj|j�d|��|jd�p|pd}t�d|j�d|�d|�d��|S)NrU��exr<zUpstash Redis MEM get '�': '�'�rM�getexrWrErO�debug�r!rr�resrrr"r#���� zUpstashRedisEntityStore.getr%c
C�X|s|�|�S|jj|j�d|��||jd�t�d|j�d|�d|�d|j���dS)NrUrZzRedis MEM set 'r\�' EX �r)rMr'rWrDrOr`r&rrr"r'��
 �zUpstashRedisEntityStore.setcC�|j�|j�d|���dSrT�rMr)rWr(rrr"r)��zUpstashRedisEntityStore.deletecC�|j�|j�d|���dkS�NrU��rMr*rWr(rrr"r*�rjzUpstashRedisEntityStore.existscs>dtdtf�fdd�}|d�}|dkr||�|dksdSdS)N�cursorrcs*�j�|�j�d��\}}�jj|�|S)N�:*)rM�scanrWr))ro�keys_to_deleter+rr"�scan_and_delete�s
�z6UpstashRedisEntityStore.clear.<locals>.scan_and_deleter)�int)r!rsrorr+r"r,�s
�zUpstashRedisEntityStore.clear)rr<r<r=r>r?r-r.)r/r0r1r2r3r	rtrrL�propertyrWr#r'r)r4r*r,�
__classcell__rrrQr"r;[s@	��������	� 	r;cseZdZUdZeed<dZeed<dZeed<dZ	e
eed<d	Ze
eed
<						d!dededede
ed
e
ed
edef�fdd�
Z
edefdd��Zd"dede
ede
efdd�Zdede
eddfdd�Zdeddfdd�Zdedefdd�Zd#dd �Z�ZS)$�RedisEntityStorez�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.
    rMrr@r=rCr>rDr?rE�redis://localhost:6379/0rArFrGc
s�zddl}Wntytd��wt�j|i|��zddlm}	Wnty.td��wz	|	|dd�|_Wn|jjyQ}
zt	�
|
�WYd}
~
nd}
~
ww||_||_||_
|p^||_dS)NrzRCould 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)�redisrJrKrL�#langchain_community.utilities.redisryrM�
exceptions�ConnectionErrorrOrPr@rCrDrE)r!r@rArCrDrErFrGr|ryrPrQrr"rL�s2
������zRedisEntityStore.__init__rcCrSrTrVr+rrr"rW�rXz RedisEntityStore.full_key_prefixNrc	CrY)NrUrZr<zREDIS MEM get 'r\r]r^rarrr"r#�rczRedisEntityStore.getr%c
Crd)NrUrZzREDIS MEM set 'r\rerfr&rrr"r'�rgzRedisEntityStore.setcCrhrTrir(rrr"r)rjzRedisEntityStore.deletecCrkrlrnr(rrr"r*rjzRedisEntityStore.existscCsNdttdtdttfdd�}||j�|j�d��d�D]}|jj|�qdS)N�iterable�
batch_sizercss<�t|�}tt||��}r|Vtt||��}sdSdSr-)�iter�listr)r�r��iterator�batchrrr"�batched	s
��z'RedisEntityStore.clear.<locals>.batchedrpi�)rrrtrM�	scan_iterrWr))r!r��keybatchrrr"r,s��zRedisEntityStore.clear)rrxr=r>r?r-r.)r/r0r1r2rr:r@r3rCrDr	rtrErLrurWr#r'r)r4r*r,rvrrrQr"rw�sD
	��������& 	rwcs�eZdZUdZdZeed<dZeed<dZe	ed<e
dd	�Z		
	d!dededede	d
e	f
�fdd�
Ze
defdd��Zd"dd�Zd#dedeedeefdd�Zdedeeddfdd�Zdeddfdd�Zdedefdd�Zd"dd �Z�ZS)$�SQLiteEntityStorezSQLite-backed Entity storerr@r=�
table_nameN�connT)�arbitrary_types_allowed�entities.db�db_filerFrGcsZzddl}Wntytd��wt�j|i|��|�|�|_||_||_|��dS)NrzVCould not import sqlite3 python package. Please install it with `pip install sqlite3`.)	�sqlite3rJrKrL�connectr�r@r��_create_table_if_not_exists)r!r@r�r�rFrGr�rQrr"rL's��zSQLiteEntityStore.__init__rcCrS)N�_)r�r@r+rrr"�full_table_name=rXz!SQLiteEntityStore.full_table_namecC�Fd|j�d�}|j�|j�|�Wd�dS1swYdS)Nz(
            CREATE TABLE IF NOT EXISTS zZ (
                key TEXT PRIMARY KEY,
                value TEXT
            )
        �r�r��execute)r!�create_table_queryrrr"r�As�"�z-SQLiteEntityStore._create_table_if_not_existsrcCs>d|j�d�}|j�||f�}|��}|dur|d}|S|S)Nz+
            SELECT value
            FROM �#
            WHERE key = ?
        r�r�r�r��fetchone)r!rr�queryro�resultr%rrr"r#Ks�zSQLiteEntityStore.getr%cCsZ|s|�|�Sd|j�d�}|j�|j�|||f�Wd�dS1s&wYdS)Nz$
            INSERT OR REPLACE INTO z0 (key, value)
            VALUES (?, ?)
        )r)r�r�r�)r!rr%r�rrr"r'Xs
�"�zSQLiteEntityStore.setcCsJd|j�d�}|j�|j�||f�Wd�dS1swYdS)N�
            DELETE FROM r�r�)r!rr�rrr"r)bs�"�zSQLiteEntityStore.deletecCs.d|j�d�}|j�||f�}|��}|duS)Nz'
            SELECT 1
            FROM z7
            WHERE key = ?
            LIMIT 1
        r�)r!rr�ror�rrr"r*js�zSQLiteEntityStore.existscCr�)Nr�z	
        r�)r!r�rrr"r,us�"�zSQLiteEntityStore.clear)rr�r=r.r-)r/r0r1r2r@r3r:r�r�rr�model_configrLrur�r�r	r#r'r)r4r*r,rvrrrQr"r�s<
	�������
 


r�cs
eZdZUdZdZeed<dZeed<eed<e	Z
eed<eZ
eed<gZeeed	<d
Zeed<dZeed
<eed�Zeed<edeefdd��Zedeefdd��Zdeeefdeeeffdd�Zdeeefdeeefddf�fdd�Zddd�Z�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_storercCs|jjS)zAccess chat memory messages.)�chat_memory�messagesr+rrr"�buffer�szConversationEntityMemory.buffercCs
d|jgS)zMWill always return list of memory variables.

        :meta private:
        �entities)r�r+rrr"�memory_variables�s
z)ConversationEntityMemory.memory_variables�inputsc
Cs�t|j|jd�}|jdurt||j�}n|j}t|j|jdd�|j	|j
d�}|j|||d�}|��dkr;g}n
dd�|�
d	�D�}i}|D]}|j�|d
�||<qI||_|jrg|j|jdd�}	n|}	|j|	d|iS)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�NONEcSsg|]}|���qSr)�strip)�.0�wrrr"�
<listcomp>�szBConversationEntityMemory.load_memory_variables.<locals>.<listcomp>�,r<r�)rr�r��	input_keyrr�r
r�r�r�r��predictr��splitr�r#r��return_messagesr�)
r!r��chain�prompt_input_key�
buffer_string�outputr��entity_summaries�entityr�rrr"�load_memory_variables�s4
�	��z.ConversationEntityMemory.load_memory_variables�outputsNc
s�t��||�|jdurt||j�}n|j}t|j|jdd�|j|j	d�}||}t
|j|jd�}|j
D]}|j�|d�}|j||||d�}	|j�||	���q7dS)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�r<)�summaryr�r�r�)rK�save_contextr�rr�r
r�r�r�r�rr�r�r�r�r#r�r'r�)
r!r�r�r�r��
input_datar�r��existing_summaryr�rQrr"r��s*
�
��z%ConversationEntityMemory.save_contextcCs"|j��|j��|j��dS)zClear memory contents.N)r�r,r�r�r+rrr"r,s

zConversationEntityMemory.clearr.)!r/r0r1r2r�r3r:r�rrr�rrr�r�rr�rtr�rr5r�rrurr�r�rrr�r�r,rvrrrQr"r�}s$
	"*A*r�),r2�logging�abcrr�	itertoolsr�typingrrrrr	�langchain_core._apir
�langchain_core.language_modelsr�langchain_core.messagesrr
�langchain_core.promptsr�pydanticrrr�langchain.chains.llmr�langchain.memory.chat_memoryr�langchain.memory.promptrr�langchain.memory.utilsr�	getLoggerr/rOrr5r;rwr�r�rrrr"�<module>sf
���N�[�a�