a
    •˜ëjÆ  ã                   @   sŽ   d 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 ddlmZ ddlmZ dd	lmZ ed
ddd�G dd„ deƒƒZdS )z-Class for a VectorStore-backed memory object.é    )ÚSequence)ÚAnyÚOptionalÚUnion)Ú
deprecated)ÚDocument)Ú
BaseMemory)ÚVectorStoreRetriever)ÚField)Úget_prompt_input_keyz0.3.1z1.0.0z_Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/)ZsinceZremovalÚmessagec                   @   s–  e Zd ZU dZedd�Zeed< dZe	ed< dZ
ee	 ed< d	Zeed
< eed�Zee	 ed< eee	 dœdd„ƒZee	ef e	dœdd„Zee ee	eee e	f f dœdd„Zee	ef ee	eee e	f f dœdd„Zee	ef ee	eee e	f f dœdd„Zee	ef ee	e	f ee dœdd„Zee	ef ee	e	f ddœdd„Zee	ef ee	e	f ddœdd „Zddœd!d"„Zddœd#d$„Z dS )%ÚVectorStoreRetrieverMemoryz§Vector Store Retriever Memory.

    Store the conversation history in a vector store and retrieves the relevant
    parts of past conversation based on the input.
    T©ÚexcludeÚ	retrieverÚhistoryÚ
memory_keyNÚ	input_keyFÚreturn_docs)Údefault_factoryÚexclude_input_keys)Úreturnc                 C   s   | j gS )z?The list of keys emitted from the load_memory_variables method.)r   ©Úself© r   ú[/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/memory/vectorstore.pyÚmemory_variables-   s    z+VectorStoreRetrieverMemory.memory_variables)Úinputsr   c                 C   s   | j du rt|| jƒS | j S )z!Get the input key for the prompt.N)r   r   r   )r   r   r   r   r   Ú_get_prompt_input_key2   s    
z0VectorStoreRetrieverMemory._get_prompt_input_key)Údocsr   c                 C   s*   | j sd dd„ |D ƒ¡}n|}| j|iS )NÚ
c                 S   s   g | ]
}|j ‘qS r   ©Úpage_content)Ú.0Údocr   r   r   Ú
<listcomp>>   ó    zMVectorStoreRetrieverMemory._documents_to_memory_variables.<locals>.<listcomp>)r   Újoinr   )r   r   Úresultr   r   r   Ú_documents_to_memory_variables8   s    z9VectorStoreRetrieverMemory._documents_to_memory_variablesc                 C   s(   |   |¡}|| }| j |¡}|  |¡S )úReturn history buffer.)r   r   Zinvoker)   ©r   r   r   Úqueryr   r   r   r   Úload_memory_variablesC   s    
z0VectorStoreRetrieverMemory.load_memory_variablesc                 Ã   s.   |   |¡}|| }| j |¡I dH }|  |¡S )r*   N)r   r   Zainvoker)   r+   r   r   r   Úaload_memory_variablesM   s    
z1VectorStoreRetrieverMemory.aload_memory_variables)r   Úoutputsr   c                    sd   t | jƒ‰ ˆ  | j¡ ‡ fdd„| ¡ D ƒ}dd„ t| ¡ ƒt| ¡ ƒ D ƒ}d |¡}t|d�gS )z0Format context from this conversation to buffer.c                    s   i | ]\}}|ˆ vr||“qS r   r   ©r#   ÚkÚvr   r   r   Ú
<dictcomp>`   r&   z>VectorStoreRetrieverMemory._form_documents.<locals>.<dictcomp>c                 S   s   g | ]\}}|› d |› �‘qS )z: r   r0   r   r   r   r%   a   s   ÿz>VectorStoreRetrieverMemory._form_documents.<locals>.<listcomp>r    r!   )Úsetr   Úaddr   ÚitemsÚlistr'   r   )r   r   r/   Zfiltered_inputsZtextsr"   r   r   r   Ú_form_documentsW   s    
þ
z*VectorStoreRetrieverMemory._form_documentsc                 C   s   |   ||¡}| j |¡ dS ©z.Save context from this conversation to buffer.N)r8   r   Zadd_documents©r   r   r/   Z	documentsr   r   r   Úsave_contexth   s    z'VectorStoreRetrieverMemory.save_contextc                 Ã   s"   |   ||¡}| j |¡I dH  dS r9   )r8   r   Zaadd_documentsr:   r   r   r   Úasave_contextm   s    z(VectorStoreRetrieverMemory.asave_contextc                 C   s   dS ©zNothing to clear.Nr   r   r   r   r   Úclearv   s    z VectorStoreRetrieverMemory.clearc                 Ã   s   dS r=   r   r   r   r   r   Úacleary   s    z!VectorStoreRetrieverMemory.aclear)!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r	   Ú__annotations__r   Ústrr   r   r   ÚboolÚtupler   r   Úpropertyr7   r   Údictr   r   r   r   r)   r-   r.   r8   r;   r<   r>   r?   r   r   r   r   r      s:   
	ý
ý
ý

ü"

ü	r   N)rC   Úcollections.abcr   Útypingr   r   r   Zlangchain_core._apir   Zlangchain_core.documentsr   Zlangchain_core.memoryr   Zlangchain_core.vectorstoresr	   Zpydanticr
   Zlangchain.memory.utilsr   r   r   r   r   r   Ú<module>   s   ü