a
    ”˜ëj_<  ã                
   @   s  d Z ddlZddlZddlZddlZddlZddl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 ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ eedf edœdd„Zeddœdd„Z ej!ejee" dœdd„ƒZ#dddœeee ee edœdd„Z$d>eee ee ee ee ddœdd„Z%ee&e dœdd„Z'e(eef e&e e(eef dœd d!„Z)e(eef e(eef e&e e(eef d"œd#d$„Z*eeef ed%œd&d'„Z+G d(d)„ d)ƒZ,e,ƒ Z-eeeg ef d*œd+d,„ƒZ.eeeeg ef d-œd.d,„ƒZ.ee	e eeg ef d-œd/d,„ƒZ.eeeeg ef d0œd1d,„ƒZ.eeee	e f eee eg ef d2œd3d,„ƒZ.eedee eg ee f d2œd4d,„ƒZ.eeee	e f deg ee f d-œd5d,„ƒZ.e-dd6œeee	e f eee,df ee eeg ef eg ee f f d2œd7d,„Z.eeee	e f eg ef d*œd8d9„ƒZ/eeeeg ef d-œd:d9„ƒZ/eeee	e f deg ee f d-œd;d9„ƒZ/eeeeg ef d0œd<d9„ƒZ/e-dd6œeee	e f eee,df ee eeg ee f eg ef f d2œd=d9„Z/dS )?zGeneric utility functions.é    N)ÚIteratorÚSequence)Úversion)ÚAnyÚCallableÚOptionalÚUnionÚoverload)Úparse)Ú	SecretStr)Ú	HTTPErrorÚResponse)Úoverride)Úis_pydantic_v1_subclass.)Ú
arg_groupsÚreturnc                     s   t t dœ‡ fdd„}|S )a  Validate specified keyword args are mutually exclusive.

    Args:
        *arg_groups (tuple[str, ...]): Groups of mutually exclusive keyword args.

    Returns:
        Callable: Decorator that validates the specified keyword args
            are mutually exclusive.
    )Úfuncr   c                    s&   t  ˆ ¡tttdœ‡‡ fdd„ƒ}|S )N)ÚargsÚkwargsr   c                     s`   ‡ fdd„ˆD ƒ}dd„ t |ƒD ƒ}|rR‡fdd„|D ƒ}dd |¡› �}t|ƒ‚ˆ| i ˆ ¤ŽS )z3Validate exactly one arg in each group is not None.c                    s"   g | ]}t ‡ fd d„|D ƒƒ‘qS )c                 3   s    | ]}ˆ   |¡d urdV  qd S )Né   )Úget)Ú.0Úarg©r   © úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/utils/utils.pyÚ	<genexpr>'   ó    zJxor_args.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>.<genexpr>)Úsum)r   Z	arg_groupr   r   r   Ú
<listcomp>&   s   ÿz@xor_args.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>c                 S   s   g | ]\}}|d kr|‘qS )r   r   )r   ÚiÚcountr   r   r   r   *   r   c                    s   g | ]}d   ˆ | ¡‘qS )ú, )Újoin)r   r    ©r   r   r   r   ,   r   zFExactly one argument in each of the following groups must be defined: r"   )Ú	enumerater#   Ú
ValueError)r   r   ÚcountsZinvalid_groupsZinvalid_group_namesÚmsg)r   r   r   r   Úwrapper#   s    
þþÿz,xor_args.<locals>.decorator.<locals>.wrapper)Ú	functoolsÚwrapsr   )r   r)   r$   )r   r   Ú	decorator"   s    zxor_args.<locals>.decorator)r   )r   r,   r   r$   r   Úxor_args   s    r-   )Úresponser   c              
   C   sB   z|   ¡  W n0 ty< } zt| jƒ|‚W Y d}~n
d}~0 0 dS )zÀRaise an error with the response text.

    Args:
        response (Response): The response to check for errors.

    Raises:
        ValueError: If the response has an error status code.
    N)Úraise_for_statusr   r&   Útext)r.   Úer   r   r   Úraise_for_status_with_text:   s    	r2   )Údt_valuer   c                 #   sD   G ‡ ‡fdd„dt j ƒ‰ t j }ˆ t _ zt j V  W |t _ n|t _ 0 dS )ah  Context manager for mocking out datetime.now() in unit tests.

    Args:
        dt_value: The datetime value to use for datetime.now().

    Yields:
        datetime.datetime: The mocked datetime class.

    Example:
    with mock_now(datetime.datetime(2011, 2, 3, 10, 11)):
        assert datetime.datetime.now() == datetime.datetime(2011, 2, 3, 10, 11)
    c                       s:   e Zd ZdZeedeejdf ddœ‡ ‡fdd„ƒƒZ	dS )zmock_now.<locals>.MockDateTimez3Mock datetime.datetime.now() with a fixed datetime.NÚMockDateTime)Útzr   c              	      s&   ˆ ˆj ˆjˆjˆjˆjˆjˆjˆjƒS ©N)ÚyearÚmonthÚdayÚhourÚminuteÚsecondÚmicrosecondÚtzinfo)Úclsr5   ©r4   r3   r   r   Únow[   s    øz"mock_now.<locals>.MockDateTime.now)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   r   Údatetimer>   rA   r   r@   r   r   r4   X   s   r4   N)rG   )r3   Zreal_datetimer   r@   r   Úmock_nowI   s    
rH   )Úpip_nameÚpackage)Úmodule_namerI   rJ   r   c             
   C   sx   zt  | |¡}W nb ttfyr } zF|p@| jddd�d  dd¡}d| › d|› d	�}t|ƒ|‚W Y d
}~n
d
}~0 0 |S )aå  Dynamically import a module.

    Raise an exception if the module is not installed.

    Args:
        module_name (str): The name of the module to import.
        pip_name (str, optional): The name of the module to install with pip.
            Defaults to None.
        package (str, optional): The package to import the module from.
            Defaults to None.

    Returns:
        Any: The imported module.

    Raises:
        ImportError: If the module is not installed.
    Ú.r   )Úmaxsplitr   Ú_ú-zCould not import z5 python package. Please install it with `pip install z`.N)Ú	importlibÚimport_moduleÚImportErrorÚModuleNotFoundErrorÚsplitÚreplace)rK   rI   rJ   Úmoduler1   r(   r   r   r   Úguard_importr   s    ÿÿ rW   )rJ   Ú
lt_versionÚlte_versionÚ
gt_versionÚgte_versionr   c                 C   sà   t t| ƒƒ}|dur@|t |ƒkr@d| › d|› d|› d�}t|ƒ‚|durt|t |ƒkrtd| › d|› d|› d�}t|ƒ‚|dur¨|t |ƒkr¨d| › d|› d|› d�}t|ƒ‚|durÜ|t |ƒk rÜd| › d|› d|› d�}t|ƒ‚dS )	ao  Check the version of a package.

    Args:
        package (str): The name of the package.
        lt_version (str, optional): The version must be less than this.
            Defaults to None.
        lte_version (str, optional): The version must be less than or equal to this.
            Defaults to None.
        gt_version (str, optional): The version must be greater than this.
            Defaults to None.
        gte_version (str, optional): The version must be greater than or equal to this.
            Defaults to None.

    Raises:
        ValueError: If the package version does not meet the requirements.
    Nz	Expected z version to be < z. Received rL   z version to be <= z version to be > z version to be >= )r
   r   r&   )rJ   rX   rY   rZ   r[   Zimported_versionr(   r   r   r   Úcheck_package_version’   s2    ÿÿÿÿÿÿÿÿr\   )Úpydantic_clsr   c                 C   sr   t ƒ }t| ƒr>| j ¡ D ]"}| |j¡ |jr| |j¡ qn0| j 	¡ D ]$\}}| |¡ |jrH| |j¡ qH|S )zœGet field names, including aliases, for a pydantic class.

    Args:
        pydantic_cls: Pydantic class.

    Returns:
        set[str]: Field names.
    )
Úsetr   Z
__fields__ÚvaluesÚaddÚnameZ	has_aliasÚaliasZmodel_fieldsÚitems)r]   Úall_required_field_namesÚfieldra   r   r   r   Úget_pydantic_field_namesÄ   s    	
rf   )r_   rd   r   c              	   C   sº   |   di ¡}t| ƒD ]X}||v r4d|› d�}t|ƒ‚||vrtjd|› d|› d|› d�dd	� |  |¡||< q| | ¡ ¡}|r®tjd
|› d�dd	� |D ]}| |¡| |< qš|| d< | S )a›  Build "model_kwargs" param from Pydantic constructor values.

    Args:
        values: All init args passed in by user.
        all_required_field_names: All required field names for the pydantic class.

    Returns:
        dict[str, Any]: Extra kwargs.

    Raises:
        ValueError: If a field is specified in both values and extra_kwargs.
        ValueError: If a field is specified in model_kwargs.
    Zmodel_kwargsúFound ú supplied twice.ú	WARNING! ú+ is not default parameter.
                úF was transferred to model_kwargs.
                Please confirm that ú is what you intended.é   ©Ú
stacklevelúParameters úa should be specified explicitly. Instead they were passed in as part of `model_kwargs` parameter.)r   Úlistr&   ÚwarningsÚwarnÚpopÚintersectionÚkeys)r_   rd   Úextra_kwargsÚ
field_namer(   Úinvalid_model_kwargsÚkr   r   r   Ú_build_model_kwargsÛ   s*    ü
ýr|   )rx   r_   rd   r   c              	   C   sŒ   t |ƒD ]X}|| v r(d|› d�}t|ƒ‚||vrtjd|› d|› d|› d�dd� | |¡| |< q| |  ¡ ¡}|rˆd	|› d
�}t|ƒ‚| S )a¾  Build extra kwargs from values and extra_kwargs.

    Args:
        extra_kwargs: Extra kwargs passed in by user.
        values: Values passed in by user.
        all_required_field_names: All required field names for the pydantic class.

    Returns:
        dict[str, Any]: Extra kwargs.

    Raises:
        ValueError: If a field is specified in both values and extra_kwargs.
        ValueError: If a field is specified in model_kwargs.
    rg   rh   ri   rj   rk   rl   rm   rn   rp   rq   )rr   r&   rs   rt   ru   rv   rw   )rx   r_   rd   ry   r(   rz   r   r   r   Úbuild_extra_kwargs	  s     ü
ÿr}   )Úvaluer   c                 C   s   t | tƒr| S t| ƒS )z­Convert a string to a SecretStr if needed.

    Args:
        value (Union[SecretStr, str]): The value to convert.

    Returns:
        SecretStr: The SecretStr value.
    )Ú
isinstancer   )r~   r   r   r   Úconvert_to_secret_str4  s    	
r€   c                   @   s   e Zd ZdZdS )Ú_NoDefaultTypez.Type to indicate no default value is provided.N)rB   rC   rD   rE   r   r   r   r   r�   B  s   r�   )Úkeyr   c                C   s   d S r6   r   ©r‚   r   r   r   Úfrom_envI  s    r„   )r‚   Údefaultr   c               C   s   d S r6   r   ©r‚   r…   r   r   r   r„   M  s    c               C   s   d S r6   r   r†   r   r   r   r„   Q  s    )r‚   Úerror_messager   c               C   s   d S r6   r   ©r‚   r‡   r   r   r   r„   U  s    )r‚   r…   r‡   r   c               C   s   d S r6   r   ©r‚   r…   r‡   r   r   r   r„   Y  s    c               C   s   d S r6   r   r‰   r   r   r   r„   _  s    c               C   s   d S r6   r   r†   r   r   r   r„   e  s    )r…   r‡   c                  s   t t dœ‡ ‡‡fdd„}|S )aÀ  Create a factory method that gets a value from an environment variable.

    Args:
        key: The environment variable to look up. If a list of keys is provided,
            the first key found in the environment will be used.
            If no key is found, the default value will be used if set,
            otherwise an error will be raised.
        default: The default value to return if the environment variable is not set.
        error_message: the error message which will be raised if the key is not found
            and no default value is provided.
            This will be raised as a ValueError.

    Returns:
        factory method that will look up the value from the environment.
    ©r   c                     s”   t ˆttfƒr0ˆD ]} | tjv rtj|    S qt ˆtƒrNˆtjv rNtjˆ S t ˆ ttdƒfƒrdˆ S ˆrptˆƒ‚dˆ› dˆ› dˆ› d�}t|ƒ‚dS )zìGet a value from an environment variable.

        Raises:
            ValueError: If the environment variable is not set and no default is
                provided.

        Returns:
            The value from the environment.
        NúDid not find ú&, please add an environment variable `ú` which contains it, or pass `ú` as a named parameter.)r   rr   ÚtupleÚosÚenvironÚstrÚtyper&   ©r{   r(   ©r…   r‡   r‚   r   r   Úget_from_env_fn‚  s"    


ÿþÿz!from_env.<locals>.get_from_env_fn)r   r’   )r‚   r…   r‡   r–   r   r•   r   r„   k  s    c                C   s   d S r6   r   rƒ   r   r   r   Úsecret_from_env¡  s    r—   c               C   s   d S r6   r   r†   r   r   r   r—   ¥  s    c               C   s   d S r6   r   r†   r   r   r   r—   ©  s    c               C   s   d S r6   r   rˆ   r   r   r   r—   ¯  s    c                  s   t t dœ‡ ‡‡fdd„}|S )a´  Secret from env.

    Args:
        key: The environment variable to look up.
        default: The default value to return if the environment variable is not set.
        error_message: the error message which will be raised if the key is not found
            and no default value is provided.
            This will be raised as a ValueError.

    Returns:
        factory method that will look up the secret from the environment.
    rŠ   c                     s¤   t ˆttfƒr4ˆD ] } | tjv rttj|  ƒ  S qt ˆtƒrVˆtjv rVttjˆ ƒS t ˆ tƒrhtˆ ƒS ˆ du rtdS ˆr€tˆƒ‚dˆ› dˆ› dˆ› d�}t|ƒ‚dS )zíGet a value from an environment variable.

        Raises:
            ValueError: If the environment variable is not set and no default is
                provided.

        Returns:
            The secret from the environment.
        Nr‹   rŒ   r�   rŽ   )r   rr   r�   r�   r‘   r   r’   r&   r”   r•   r   r   Úget_secret_from_envÇ  s&    


ÿþÿz,secret_from_env.<locals>.get_secret_from_env)r   r   )r‚   r…   r‡   r˜   r   r•   r   r—   ³  s    )NNNN)0rE   Ú
contextlibrG   r*   rP   r�   rs   Úcollections.abcr   r   Úimportlib.metadatar   Útypingr   r   r   r   r	   Zpackaging.versionr
   Zpydanticr   Úrequestsr   r   Ztyping_extensionsr   Zlangchain_core.utils.pydanticr   r�   r’   r-   r2   Úcontextmanagerr“   rH   rW   r\   r^   rf   Údictr|   r}   r€   r�   Z
_NoDefaultr„   r—   r   r   r   r   Ú<module>   s¼   #)ÿþ"    ûú2

ý/


ü+ 
þ
þþ	ûú6&þûú