a
    •˜ëj‡  ã                   @   s  d Z ddlmZmZmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ eedœdd	„Zeedœd
d„Zeedœdd„Zeedœdd„Zeedœdd„Zddœeeeegef  eeef dœdd„Zddœeeeegef  eeef dœdd„ZdS )z?Create a key-value store for any langchain serializable object.é    )ÚAnyÚCallableÚOptional)ÚDocument)ÚSerializableÚdumpsÚloads)Ú	BaseStoreÚ	ByteStore)ÚEncoderBackedStore)ÚobjÚreturnc                 C   s   t | ƒ d¡S )ú,Return a bytes representation of a document.úutf-8)r   Úencode)r   © r   úZ/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/storage/_lc_store.pyÚ_dump_as_bytes   s    r   c                 C   s$   t | tƒsd}t|ƒ‚t| ƒ d¡S )r   zExpected a Document instancer   )Ú
isinstancer   Ú	TypeErrorr   r   )r   Úmsgr   r   r   Ú_dump_document_as_bytes   s    
r   )Ú
serializedr   c                 C   s8   t |  d¡tgd�}t|tƒs4dt|ƒ› �}t|ƒ‚|S )z.Return a document from a bytes representation.r   )Zallowed_objectsz"Expected a Document instance. Got )r   Údecoder   r   Útyper   )r   r   r   r   r   r   Ú_load_document_from_bytes   s
    
r   c                 C   s   t |  d¡ƒS )z6Return a ``Serializable`` from a bytes representation.r   )r   r   )r   r   r   r   Ú_load_from_bytes"   s    	r   )Úxr   c                 C   s   | S )zReturn the same object.r   )r   r   r   r   Ú	_identity.   s    r   N)Úkey_encoder)Ústorer   r   c                C   s   t | |p
tttƒS )a¯  Create a store for langchain serializable objects from a bytes store.

    .. danger::

        Treat the underlying byte store as a trust boundary.

        Reads from this store are deserialized with
        ``langchain_core.load.loads``, which instantiates Python objects
        from the stored payload. The same threat model applies: a payload
        can carry constructor kwargs (custom ``base_url``, headers, model
        name, etc.) that get applied during ``__init__``, so the bytes are
        effectively executable configuration rather than plain data.

        **Never back this store with anything an attacker can write to** -
        for example a shared cache that other tenants can populate, an
        S3 bucket without strict write controls, or a Redis instance
        reused across trust boundaries. A single tampered value will
        instantiate attacker-controlled classes the next time the store
        is read.

        If you cannot guarantee the store is write-restricted to your own
        process, use ``create_kv_docstore`` instead - it pins
        ``allowed_objects=[Document]`` so a tampered value can at worst
        produce a ``Document``, never a chat model or LLM with a
        redirected endpoint.

    Args:
        store: A bytes store to use as the underlying store.
        key_encoder: A function to encode keys; if None uses identity function.

    Returns:
        A key-value store for documents.
    )r   r   r   r   ©r    r   r   r   r   Úcreate_lc_store6   s    &ür"   c                C   s   t | |p
tttƒS )au  Create a store for langchain Document objects from a bytes store.

    This store does run time type checking to ensure that the values are
    Document objects.

    Args:
        store: A bytes store to use as the underlying store.
        key_encoder: A function to encode keys; if None uses identity function.

    Returns:
        A key-value store for documents.
    )r   r   r   r   r!   r   r   r   Úcreate_kv_docstored   s    ür#   )Ú__doc__Útypingr   r   r   Zlangchain_core.documentsr   Zlangchain_core.loadr   r   r   Zlangchain_core.storesr	   r
   Z langchain.storage.encoder_backedr   Úbytesr   r   r   r   Ústrr   r"   r#   r   r   r   r   Ú<module>   s,   	ý
ü1ý
ü