a
    ”˜ëjçH  ã                   @  sl  d Z ddlmZ ddlZddlZddl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mZmZmZmZm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  ddl!m"Z# ddl$m%Z%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/m0Z0 e�r"ddl1m2Z2 e 3ej4¡Z5e/dƒddœdd„ƒZ6e5j7Z8e5j9Z:e5j7dkZ;e5j7dkZ<eZ=e>e Z?ede=d�Z@dddœdd„ZAdddœdd „ZBdddœd!d"„ZCd#dd$œd%d&„ZDd'd#d(œd)d*„ZEG d+d,„ d,e&ƒZFddd-œd.d/d0d1d2d3d4œd5d6„ZGddd-œd.d3d7d1d2d3d4œd8d9„ZHddd-œd.d:d7d1d2d3d4œd;d<„ZIed3d=d>œd?d@„ƒZJedAd=d>œdBd@„ƒZJed/dCd>œdDd@„ƒZJedEdCd>œdFd@„ƒZJdGdHd>œdId@„ZJedJdJdKdL�ZKeLƒ ZMdeMfd.d#d2dMd3dNœdOdP„ZNe	dQdR�deMdSœd.d#d2dMd3dTœdUdV„ƒZOe	dQdR�d.d#d3dWœdXdY„ƒZPdid.d2d#d3dZœd[d\„Zd]d^„ eQeƒD ƒZRd_d_d`œdadb„ZSddddcœd.d2ddded3dfœdgdh„ZTdS )jzUtilities for pydantic.é    )ÚannotationsN)Únullcontext)Ú	lru_cacheÚwraps)ÚGenericAlias)ÚTYPE_CHECKINGÚAnyÚCallableÚOptionalÚTypeVarÚUnionÚcastÚoverload)Úversion)Ú	BaseModelÚ
ConfigDictÚFieldÚPydanticDeprecationWarningÚ	RootModelÚroot_validator)Úcreate_model)Ú	FieldInfo)ÚDEFAULT_REF_TEMPLATEÚGenerateJsonSchemaÚJsonSchemaModeÚJsonSchemaValue)r   )Ú
ModelField)Ú
deprecatedÚoverride)Úcore_schemaz#Use PYDANTIC_VERSION.major instead.Úint)Úreturnc                   C  s   t jS )z’DEPRECATED - Get the major version of Pydantic.

    Use PYDANTIC_VERSION.major instead.

    Returns:
        The major version of Pydantic.
    )ÚPYDANTIC_VERSIONÚmajor© r$   r$   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/utils/pydantic.pyÚget_pydantic_major_version7   s    	r&   é   é   Ú
TBaseModel)ÚboundÚtypeÚbool)Úclsr!   c                 C  s
   t | tƒS )z�Check if the given class is Pydantic v1-like.

    Returns:
        True if the given class is a subclass of Pydantic ``BaseModel`` 1.x.
    )Ú
issubclassÚBaseModelV1©r-   r$   r$   r%   Úis_pydantic_v1_subclassO   s    r1   c                 C  s
   t | tƒS )z‰Check if the given class is Pydantic v2-like.

    Returns:
        True if the given class is a subclass of Pydantic BaseModel 2.x.
    )r.   r   r0   r$   r$   r%   Úis_pydantic_v2_subclassX   s    r2   c                 C  s&   t  | ¡rt| tƒrdS t| ttfƒS )a4  Check if the given class is a subclass of Pydantic BaseModel.

    Check if the given class is a subclass of any of the following:

    * pydantic.BaseModel in Pydantic 2.x
    * pydantic.v1.BaseModel in Pydantic 2.x

    Returns:
        True if the given class is a subclass of Pydantic ``BaseModel``.
    F)ÚinspectÚisclassÚ
isinstancer   r.   r   r/   r0   r$   r$   r%   Úis_basemodel_subclassa   s    r6   r   )Úobjr!   c                 C  s   t | ttfƒS )a7  Check if the given class is an instance of Pydantic BaseModel.

    Check if the given class is an instance of any of the following:

    * pydantic.BaseModel in Pydantic 2.x
    * pydantic.v1.BaseModel in Pydantic 2.x

    Returns:
        True if the given class is an instance of Pydantic ``BaseModel``.
    )r5   r   r/   )r7   r$   r$   r%   Úis_basemodel_instances   s    r8   r	   )Úfuncr!   c                   sb   t  ¡ �F t jdtd� tdd�tˆ ƒddddœ‡ fdd	„ƒƒ}W d
  ƒ n1 sT0    Y  |S )zÅDecorator to run a function before model initialization.

    Args:
        func (Callable): The function to run before model initialization.

    Returns:
        Any: The decorated function.
    Úignore©ÚactionÚcategoryT)Úpreútype[BaseModel]údict[str, Any])r-   Úvaluesr!   c                   sÈ   | j }| ¡ D ]®\}}t| dƒrNt| jdƒrN| jjrN|j|v rN| |j¡||< t| dƒr~| j d¡r~|j|v r~| |j¡||< ||vs’|| du r| 	¡ s|j
dur²| 
¡ ||< q|j||< qˆ | |ƒS )aA  Decorator to run a function before model initialization.

            Args:
                cls (Type[BaseModel]): The model class.
                values (dict[str, Any]): The values to initialize the model with.

            Returns:
                dict[str, Any]: The values to initialize the model with.
            ÚConfigÚallow_population_by_field_nameÚmodel_configZpopulate_by_nameN)Úmodel_fieldsÚitemsÚhasattrrB   rC   ÚaliasÚpoprD   ÚgetZis_requiredÚdefault_factoryÚdefault)r-   rA   ÚfieldsÚnameÚ
field_info©r9   r$   r%   Úwrapper‘   s8    ÿ
þýüÿ
þýÿ
ÿþ
zpre_init.<locals>.wrapperN)ÚwarningsÚcatch_warningsÚfilterwarningsr   r   r   )r9   rQ   r$   rP   r%   Úpre_init‚   s    	
8(rU   c                   @  s&   e Zd ZdZeddddœdd„ƒZdS )	Ú_IgnoreUnserializablez£A JSON schema generator that ignores unknown types.

    https://docs.pydantic.dev/latest/concepts/json_schema/#customizing-the-json-schema-generation-process
    zcore_schema.CoreSchemaÚstrr   )ÚschemaÚ
error_infor!   c                 C  s   i S ©Nr$   )ÚselfrX   rY   r$   r$   r%   Úhandle_invalid_for_json_schemaÄ   s    z4_IgnoreUnserializable.handle_invalid_for_json_schemaN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r\   r$   r$   r$   r%   rV   ¾   s   rV   ©ÚdescriptionsÚfn_descriptionrW   ztype[BaseModelV1]ÚlistzOptional[dict]zOptional[str]r?   )rN   ÚmodelÚfield_namesrb   rc   r!   c          
      C  s„   i }|D ]P}|j | }|jr(|js(|jnt|j }|rJ||v rJ|| |j_||jf||< qt| fi |¤Ž}	t 	|pz|j
pzd¡|	_
|	S )z=Create a pydantic model with only a subset of model's fields.Ú )Ú
__fields__ÚrequiredÚ
allow_noneZouter_type_r
   rO   ÚdescriptionÚcreate_model_v1ÚtextwrapÚdedentr`   )
rN   re   rf   rb   rc   rM   Ú
field_nameÚfieldÚtÚrtnr$   r$   r%   Ú_create_subset_model_v1Ë   s    	
ÿÿürs   z	list[str]c                  s²   |pi }i }ˆ D ]F}|j | }| ||j¡}	t|	|jd�}
|jrH|j|
_|j|
f||< qt| fi |¤dtdd�i¤Ž}‡ fdd„|j	 
¡ D ƒ}t|ƒ|_	t |p¨|jp¨d¡|_|S )z:Create a pydantic model with a subset of the model fields.)rk   rL   Ú
__config__T©Úarbitrary_types_allowedc                   s    g | ]\}}|ˆ v r||f‘qS r$   r$   )Ú.0rN   Ú
annotation©rf   r$   r%   Ú
<listcomp>  s   þz+_create_subset_model_v2.<locals>.<listcomp>rg   )rE   rJ   rk   ÚFieldInfoV2rL   Úmetadatarx   Ú_create_model_baser   Ú__annotations__rF   Údictrm   rn   r`   )rN   re   rf   rb   rc   Zdescriptions_rM   ro   rp   rk   rO   rr   Zselected_annotationsr$   ry   r%   Ú_create_subset_model_v2è   s,    	
ÿÿÿ
þ
r€   ÚTypeBaseModelc                C  s.   t |tƒrt| ||||d�S t| ||||d�S )z|Create subset model using the same pydantic version as the input model.

    Returns:
        The created subset model.
    ra   )r.   r/   rs   r€   )rN   re   rf   rb   rc   r$   r$   r%   Ú_create_subset_model  s    
ûûr‚   zdict[str, FieldInfoV2])re   r!   c                 C  s   d S rZ   r$   ©re   r$   r$   r%   Ú
get_fields0  s    r„   r   c                 C  s   d S rZ   r$   rƒ   r$   r$   r%   r„   4  s    zdict[str, ModelField]c                 C  s   d S rZ   r$   rƒ   r$   r$   r%   r„   8  s    r/   c                 C  s   d S rZ   r$   rƒ   r$   r$   r%   r„   <  s    zBUnion[type[Union[BaseModel, BaseModelV1]], BaseModel, BaseModelV1]z4Union[dict[str, FieldInfoV2], dict[str, ModelField]]c                 C  sH   t | tƒst| ƒ} t| tƒr"| jS t| tƒr2| jS d| › �}t|ƒ‚dS )z°Return the field names of a Pydantic model.

    Args:
        model: The Pydantic model or instance.

    Raises:
        TypeError: If the model is not a Pydantic model.
    zExpected a Pydantic model. Got N)r5   r+   r.   r   rE   r/   rh   Ú	TypeError)re   Úmsgr$   r$   r%   r„   @  s    



Tr$   )rv   ÚfrozenZprotected_namespacesÚobject)rN   Útype_Úmodule_nameÚdefault_r!   c              	     sþ   dt fdddddœ‡ fdd„}dt td	fdddd
dddœ‡ fdd„}d|itdd�t|ƒt|ƒ|pdddœ}|turz||d< t ¡ �b z0t|tƒr²t|t	ƒs²t
|tƒr²tjdtd� W n tyÆ   Y n0 tˆ tf|ƒ}W d  ƒ n1 sê0    Y  td|ƒS )zCreate a base class.Tr?   r,   rW   r@   )r-   Úby_aliasÚref_templater!   c                   s    t | | ƒj||d�}ˆ |d< |S )N)rŒ   r�   Útitle)ÚsuperrX   )r-   rŒ   r�   Úschema_©rN   r$   r%   rX   d  s
    
ÿz"_create_root_model.<locals>.schemaZ
validationztype[GenerateJsonSchema]r   )r-   rŒ   r�   Úschema_generatorÚmoder!   c                   s$   t | | ƒj||||d�}ˆ |d< |S )N)rŒ   r�   r’   r“   rŽ   )r�   Úmodel_json_schema)r-   rŒ   r�   r’   r“   r�   r‘   r$   r%   r”   p  s    
üz-_create_root_model.<locals>.model_json_schemaÚrootru   zlangchain_core.runnables.utils)r~   rD   rX   r”   r^   r:   r;   N)r   r   r   ÚclassmethodÚ
NO_DEFAULTrR   rS   r5   r+   r   r.   r/   rT   r   r…   r   r   )rN   r‰   rŠ   r‹   rX   r”   Zbase_class_attributesZcustom_root_typer$   r‘   r%   Ú_create_root_model\  s>    
ýûû
ÿþýÿ
,r˜   é   )Úmaxsize)rŠ   r‹   )Ú
model_namer‰   rŠ   r‹   r!   c                C  s   t | |||d�S )N)r‹   rŠ   )r˜   )r›   r‰   rŠ   r‹   r$   r$   r%   Ú_create_root_model_cached›  s    ÿrœ   )r›   Úfield_definitionsr!   c                K  s   t | fdtit|ƒ¤ŽS )Nrt   )r}   Ú_SchemaConfigÚ_remap_field_definitions)r›   r�   r$   r$   r%   Ú_create_model_cached¨  s    ÿþýr    )r›   rŠ   r�   r!   c                K  s0   i }d|v r|  d¡|d< t| f||dœ|¤ŽS )aº  Create a pydantic model with the given field definitions.

    Please use create_model_v2 instead of this function.

    Args:
        model_name: The name of the model.
        module_name: The name of the module where the model is defined.
            This is used by Pydantic to resolve any forward references.
        **field_definitions: The field definitions for the model.

    Returns:
        Type[BaseModel]: The created model.
    Z__root__r•   )rŠ   r�   )rI   Úcreate_model_v2)r›   rŠ   r�   Úkwargsr$   r$   r%   r   µ  s    ÿýür   c                 C  s   h | ]}|  d ¡s|’qS )Ú_)Ú
startswith)rw   Úkeyr$   r$   r%   Ú	<setcomp>Þ  ó    r¦   r@   )r�   r!   c              
   C  s’   i }|   ¡ D ]€\}}| d¡s&|tv r„t|tƒrJdt› d|› d�}t|ƒ‚|\}}|t|||| d¡ dd¡ 	¡ d�f|d|› �< q|||< q|S )zDThis remaps fields to avoid colliding with internal pydantic fields.r£   zVRemapping for fields starting with '_' or fields with a name matching a reserved name zB is not supported if  the field is a pydantic Field instance. Got Ú.ú )rL   rH   Zserialization_aliasrŽ   Zprivate_)
rF   r¤   Ú_RESERVED_NAMESr5   r{   ÚNotImplementedErrorr   ÚlstripÚreplacerŽ   )r�   Zremappedr¥   Úvaluer†   r‰   r‹   r$   r$   r%   rŸ   á  s,    
ÿþÿüþ

rŸ   )rŠ   r�   r•   zOptional[dict[str, Any]]zOptional[Any])r›   rŠ   r�   r•   r!   c          
   	   C  sX  |pi }|rŒ|r"d|› �}t |ƒ‚t|tƒr@|d |d dœ}nd|i}zt| fd|i|¤Ž}W n& ty†   t| fd|i|¤Ž}Y n0 |S t| ¡ ƒ}d}|D ]}	|	 d¡r d	}q |rÀt	 
¡ ntƒ �| |rØt	jd
d� z t| fi |¤ŽW W  d  ƒ S  t�y2   t| fdtit|ƒ¤Ž Y W  d  ƒ S 0 W d  ƒ n1 �sJ0    Y  dS )a*  Create a pydantic model with the given field definitions.

    Attention:
        Please do not use outside of langchain packages. This API
        is subject to change at any time.

    Args:
        model_name: The name of the model.
        module_name: The name of the module where the model is defined.
            This is used by Pydantic to resolve any forward references.
        field_definitions: The field definitions for the model.
        root: Type for a root model (RootModel)

    Returns:
        Type[BaseModel]: The created model.
    zAWhen specifying __root__ no other fields should be provided. Got r   r'   )r‰   r‹   r‰   rŠ   Fre   Tr:   )r<   Nrt   )r«   r5   Útuplerœ   r…   r˜   ÚsetÚkeysr¤   rR   rS   r   rT   r    r}   rž   rŸ   )
r›   rŠ   r�   r•   r†   r¢   Znamed_root_modelÚnamesZcapture_warningsrN   r$   r$   r%   r¡   ý  s\    ÿÿ
ÿÿÿ
ÿþý
 ÿþýr¡   )N)Ur`   Ú
__future__r   r3   rm   rR   Ú
contextlibr   Ú	functoolsr   r   Útypesr   Útypingr   r   r	   r
   r   r   r   r   ZpydanticÚ	packagingr   r   r   r   r   r   r   r   r}   Zpydantic.fieldsr   r{   Zpydantic.json_schemar   r   r   r   Zpydantic.v1r/   rl   Zpydantic.v1.fieldsr   Ztyping_extensionsr   r   Zpydantic_corer   ÚparseÚ__version__r"   r&   r#   ZPYDANTIC_MAJOR_VERSIONÚminorZPYDANTIC_MINOR_VERSIONZIS_PYDANTIC_V1ZIS_PYDANTIC_V2ZPydanticBaseModelr+   r�   r)   r1   r2   r6   r8   rU   rV   rs   r€   r‚   r„   rž   rˆ   r—   r˜   rœ   r    Údirrª   rŸ   r¡   r$   r$   r$   r%   Ú<module>   s–   ( 

		<ú"ú/úÿü?û þ)û