a
    ”˜ëjé-  ã                   @   s@  d Z 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
mZ ddlmZmZ ddlmZ ddlmZmZ e e¡ZG dd	„ d	e	ƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZeeeedœdd„Zeeedœdd„ZG dd„ deeƒZeeeedœdd„Z e!eef e!eef e!eef dœdd„Z"e#edœdd „Z$dS )!zSerializable base class.é    N)ÚABC)ÚAnyÚLiteralÚOptionalÚ	TypedDictÚUnionÚcast)Ú	BaseModelÚ
ConfigDict)Ú	FieldInfo)ÚNotRequiredÚoverridec                   @   sF   e Zd ZU dZeed< ee ed< ee ed< ee	ee
f  ed< dS )ÚBaseSerializedz"Base class for serialized objects.ÚlcÚidÚnameÚgraphN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚintÚ__annotations__ÚlistÚstrr   Údictr   © r   r   ú_/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/load/serializable.pyr      s   
r   c                   @   s.   e Zd ZU dZed ed< eeef ed< dS )ÚSerializedConstructorzSerialized constructor.ÚconstructorÚtypeÚkwargsN)	r   r   r   r   r   r   r   r   r   r   r   r   r   r   #   s   
r   c                   @   s   e Zd ZU dZed ed< dS )ÚSerializedSecretzSerialized secret.Úsecretr    N)r   r   r   r   r   r   r   r   r   r   r"   ,   s   
r"   c                   @   s*   e Zd ZU dZed ed< ee ed< dS )ÚSerializedNotImplementedzSerialized not implemented.Únot_implementedr    ÚreprN)r   r   r   r   r   r   r   r   r   r   r   r   r$   3   s   
r$   )ÚvalueÚkeyÚmodelÚreturnc                 C   s   t |ƒj| }t| |ƒS )zäTry to determine if a value is different from the default.

    Args:
        value: The value.
        key: The key.
        model: The pydantic model.

    Returns:
        Whether the value is different from the default.
    )r    Úmodel_fieldsÚ_try_neq_default)r'   r(   r)   Úfieldr   r   r   Útry_neq_default<   s    r.   )r'   r-   r*   c                 C   sà   zt | ¡ | kƒW S  tyÚ } z°zt| ¡ | kƒW W  Y d }~S  tyÄ } zlz$| |juW W  Y d }~W  Y d }~S  ty® } z"W Y d }~W Y d }~W Y d }~dS d }~0 0 W Y d }~n
d }~0 0 W Y d }~n
d }~0 0 d S )NF)ÚboolÚget_defaultÚ	ExceptionÚallÚdefault)r'   r-   Ú_r   r   r   r,   K   s    $r,   c                       sÜ   e Zd ZdZeeddœ‡ fdd„Zeedœdd„ƒZee	e
 dœd	d
„ƒZeee
e
f dœdd„ƒZeedœdd„ƒZee	e
 dœdd„ƒZedd�Zeedœ‡ fdd„ƒZeeef dœ‡ fdd„Zedœdd„Z‡  ZS )ÚSerializableaâ  Serializable base class.

    This class is used to serialize objects to JSON.

    It relies on the following methods and properties:

    - ``is_lc_serializable``: Is this class serializable?
      By design, even if a class inherits from Serializable, it is not serializable by
      default. This is to prevent accidental serialization of objects that should not
      be serialized.
    - ``get_lc_namespace``: Get the namespace of the langchain object.
      During deserialization, this namespace is used to identify
      the correct class to instantiate.
      Please see the ``Reviver`` class in ``langchain_core.load.load`` for more details.
      During deserialization an additional mapping is handle
      classes that have moved or been renamed across package versions.
    - ``lc_secrets``: A map of constructor argument names to secret ids.
    - ``lc_attributes``: List of additional attribute names that should be included
      as part of the serialized representation.
    N)Úargsr!   r*   c                    s   t ƒ j|i |¤Ž dS )Ú N)ÚsuperÚ__init__)Úselfr6   r!   ©Ú	__class__r   r   r9   q   s    zSerializable.__init__)r*   c                 C   s   dS )a@  Is this class serializable?

        By design, even if a class inherits from Serializable, it is not serializable by
        default. This is to prevent accidental serialization of objects that should not
        be serialized.

        Returns:
            Whether the class is serializable. Default is False.
        Fr   ©Úclsr   r   r   Úis_lc_serializableu   s    zSerializable.is_lc_serializablec                 C   s   | j  d¡S )zùGet the namespace of the langchain object.

        For example, if the class is `langchain.llms.openai.OpenAI`, then the
        namespace is ["langchain", "llms", "openai"]

        Returns:
            The namespace as a list of strings.
        Ú.)r   Úsplitr=   r   r   r   Úget_lc_namespace‚   s    
zSerializable.get_lc_namespacec                 C   s   i S )z‚A map of constructor argument names to secret ids.

        For example,
            {"openai_api_key": "OPENAI_API_KEY"}
        r   ©r:   r   r   r   Ú
lc_secretsŽ   s    zSerializable.lc_secretsc                 C   s   i S )z¹List of attribute names that should be included in the serialized kwargs.

        These attributes must be accepted by the constructor.
        Default is an empty dictionary.
        r   rC   r   r   r   Úlc_attributes—   s    zSerializable.lc_attributesc                 C   s<   d| j v r&| j d dur&| j d j}n| j}g |  ¡ ¢|‘S )a1  Return a unique identifier for this class for serialization purposes.

        The unique identifier is a list of strings that describes the path
        to the object.
        For example, for the class `langchain.llms.openai.OpenAI`, the id is
        ["langchain", "llms", "openai", "OpenAI"].
        ÚoriginN)Z__pydantic_generic_metadata__r   rB   )r>   Úoriginal_namer   r   r   Úlc_id    s    ÿþzSerializable.lc_idÚignore)Úextrac                    s   ‡ fdd„t ƒ  ¡ D ƒS )Nc                    s2   g | ]*\}}|t ˆ ƒjvs&t||ˆ ƒr||f‘qS r   )r    r+   r.   )Ú.0ÚkÚvrC   r   r   Ú
<listcomp>¹   s   þz.Serializable.__repr_args__.<locals>.<listcomp>)r8   Ú__repr_args__rC   r;   rC   r   rO   ·   s    
þzSerializable.__repr_args__c                    sŒ  |   ¡ s|  ¡ S t| ƒj}i }i }| D ]:\}}t| ||ƒs<q&||v rP|| jrPq&t| ||ƒ||< q&dg| j ¡ ¢D ]º}|t	u r„ �q.|rÀddg}|D ]*}t
||ƒr”d| j› d|› d�}	t|	ƒ‚q”td|du rÐ| nt|| ƒƒ}
| |
j¡ t|ƒD ].}|| }||v rð|| j }durð|||< qð| |
j¡ qr|D ]6}t| |dƒ�pL| |¡}|du�r2| ||i¡ �q2dd	|  ¡ |�s~|nt||ƒd
œS )zÖSerialize the object to JSON.

        Raises:
            ValueError: If the class has deprecated attributes.

        Returns:
            A json serializable object or a SerializedNotImplemented object.
        NZlc_namespaceZlc_serializablezClass z has a deprecated attribute z3. Please use the corresponding classmethod instead.r5   é   r   )r   r    r   r!   )r?   Úto_json_not_implementedr    r+   Ú_is_field_usefulÚexcludeÚgetattrr<   Úmror5   ÚhasattrÚ
ValueErrorr   r8   ÚupdaterD   r   ÚaliasrE   ÚgetrH   Ú_replace_secrets)r:   r+   ZsecretsZ	lc_kwargsrL   rM   r>   Zdeprecated_attributesÚattrÚmsgÚthisr(   r'   rY   Zsecret_valuer;   r   r   Úto_json¿   s\    	
þ

ÿÿ
þ

ÿúzSerializable.to_jsonc                 C   s   t | ƒS )zfSerialize a "not implemented" object.

        Returns:
            SerializedNotImplemented.
        )rQ   rC   r   r   r   rQ     s    z$Serializable.to_json_not_implemented)r   r   r   r   r   r9   Úclassmethodr/   r?   r   r   rB   Úpropertyr   rD   rE   rH   r
   Zmodel_configr   rO   r   r   r$   r_   rQ   Ú__classcell__r   r   r;   r   r5   Z   s&   ÿNr5   )Úinstr(   r'   r*   c              
   C   s¤   t | ƒj |¡}|sdS | ¡ r$dS zt|ƒ}W n( tyX } zd}W Y d}~n
d}~0 0 |rbdS |jtu rzt|tƒrzdS |jt	u r’t|t	ƒr’dS t
||ƒ}|p¢|S )a¼  Check if a field is useful as a constructor argument.

    Args:
        inst: The instance.
        key: The key.
        value: The value.

    Returns:
        Whether the field is useful. If the field is required, it is useful.
        If the field is not required, it is useful if the value is not None.
        If the field is not required and the value is None, it is useful if the
        default value is different from the value.
    FTN)r    r+   rZ   Zis_requiredr/   r1   Údefault_factoryr   Ú
isinstancer   r,   )rc   r(   r'   r-   Zvalue_is_truthyr4   Zvalue_neq_defaultr   r   r   rR     s"    
rR   )ÚrootÚsecrets_mapr*   c           	      C   sz   |   ¡ }| ¡ D ]d\}}| d¡�^ }}|}|D ](}||vr@ qZ||   ¡ ||< || }q0||v rdd|gdœ||< q|S )Nr@   rP   r#   )r   r    r   )ÚcopyÚitemsrA   )	rf   rg   ÚresultÚpathZ	secret_idÚpartsÚlastÚcurrentÚpartr   r   r   r[   C  s    
ýr[   )Úobjr*   c                 C   s¸   g }zJt | dƒr(g | j d¡¢| j‘}n$t | dƒrLg | jj d¡¢| jj‘}W n  tyn   tjddd� Y n0 dd|d	d
œ}t 	t¡� t
| ƒ|d< W d	  ƒ n1 sª0    Y  |S )z†Serialize a "not implemented" object.

    Args:
        obj: object to serialize.

    Returns:
        SerializedNotImplemented
    r   r@   r<   zFailed to serialize objectT)Úexc_inforP   r%   N)r   r    r   r&   r&   )rV   r   rA   r   r<   r1   ÚloggerÚdebugÚ
contextlibÚsuppressr&   )rp   Zid_rj   r   r   r   rQ   X  s     	

ü*rQ   )%r   rt   ÚloggingÚabcr   Útypingr   r   r   r   r   r   Zpydanticr	   r
   Zpydantic.fieldsr   Ztyping_extensionsr   r   Ú	getLoggerr   rr   r   r   r"   r$   r   r/   r.   r,   r5   rR   r   r[   ÚobjectrQ   r   r   r   r   Ú<module>   s*    	
		 =.
þ