a
    ”˜ëji  ã                   @  s¬   d Z ddlmZ ddl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mZ ddlmZmZ dd	lmZmZmZmZ er˜dd
lmZ G dd„ deƒZdS )ú2Tool that takes in function or coroutine directly.é    )Úannotations)Ú	Awaitable)Ú	signature)ÚTYPE_CHECKINGÚAnyÚCallableÚOptionalÚUnion)Úoverride)ÚAsyncCallbackManagerForToolRunÚCallbackManagerForToolRun)ÚRunnableConfigÚrun_in_executor)Ú
ArgsSchemaÚBaseToolÚToolExceptionÚ_get_runnable_config_param)ÚToolCallc                      sø   e Zd ZU dZdZded< ded< dZded	< ed/d
ddddœ‡ fdd„ƒZe	ddœ‡ fdd„ƒZ
ddddœ‡ fdd„Zddœddddddœdd„Zddœddd dddœ‡ fd!d"„Zdd#ddd$d%œ‡ fd&d'„Zed0d#ddd)d*d+dd d,œd-d.„ƒZ‡  ZS )1ÚToolr   Ú ÚstrÚdescriptionzOptional[Callable[..., str]]ÚfuncNz'Optional[Callable[..., Awaitable[str]]]Ú	coroutinezUnion[str, dict, ToolCall]zOptional[RunnableConfig]r   )ÚinputÚconfigÚkwargsÚreturnc                 ‹  s@   | j s$t|| j||fi |¤ŽI d H S tƒ j||fi |¤ŽI d H S )N)r   r   ZinvokeÚsuperÚainvoke)Úselfr   r   r   ©Ú	__class__© úZ/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/tools/simple.pyr    ,   s    zTool.ainvokeÚdict)r   c                   s   | j durtƒ jS dddiiS )zdThe tool's input arguments.

        Returns:
            The input arguments for the tool.
        NÚ
tool_inputÚtypeÚstring)Úargs_schemar   Úargs)r!   r"   r$   r%   r+   ;   s    
z	Tool.argszUnion[str, dict]zOptional[str]ztuple[tuple, dict])r'   Útool_call_idr   c                   sX   t ƒ  ||¡\}}t|ƒt| ¡ ƒ }t|ƒdkrLd| j› d|› �}t|ƒ‚t|ƒi fS )a,  Convert tool input to pydantic model.

        Args:
            tool_input: The input to the tool.
            tool_call_id: The ID of the tool call.

        Raises:
            ToolException: If the tool input is invalid.

        Returns:
            the pydantic model args and kwargs.

        é   z(Too many arguments to single-input tool z?.
                Consider using StructuredTool instead. Args: )r   Ú_to_args_and_kwargsÚlistÚvaluesÚlenÚnamer   Útuple)r!   r'   r,   r+   r   Úall_argsÚmsgr"   r$   r%   r.   H   s    
þÿzTool._to_args_and_kwargs)Úrun_managerr   z#Optional[CallbackManagerForToolRun])r+   r   r6   r   r   c                O  s^   | j rN|r(t| j ƒj d¡r(| ¡ |d< t| j ƒ }r>|||< | j |i |¤ŽS d}t|ƒ‚dS )aP  Use the tool.

        Args:
            *args: Positional arguments to pass to the tool
            config: Configuration for the run
            run_manager: Optional callback manager to use for the run
            **kwargs: Keyword arguments to pass to the tool

        Returns:
            The result of the tool execution
        Ú	callbacksz&Tool does not support sync invocation.N)r   r   Ú
parametersÚgetÚ	get_childr   ÚNotImplementedError)r!   r   r6   r+   r   Úconfig_paramr5   r$   r$   r%   Ú_rund   s    z	Tool._runz(Optional[AsyncCallbackManagerForToolRun]c                �  sr   | j rT|r(t| j ƒj d¡r(| ¡ |d< t| j ƒ }r>|||< | j |i |¤ŽI dH S tƒ j|||dœ|¤ŽI dH S )a_  Use the tool asynchronously.

        Args:
            *args: Positional arguments to pass to the tool
            config: Configuration for the run
            run_manager: Optional callback manager to use for the run
            **kwargs: Keyword arguments to pass to the tool

        Returns:
            The result of the tool execution
        r7   N)r   r6   )r   r   r8   r9   r:   r   r   Ú_arun)r!   r   r6   r+   r   r<   r"   r$   r%   r>      s    ÿÿz
Tool._arunzOptional[Callable]ÚNone)r2   r   r   r   r   c                   s   t ƒ jf |||dœ|¤Ž dS )zInitialize tool.)r2   r   r   N)r   Ú__init__)r!   r2   r   r   r   r"   r$   r%   r@   Ÿ   s    zTool.__init__FÚboolzOptional[ArgsSchema]z'Optional[Callable[..., Awaitable[Any]]])r   r2   r   Úreturn_directr*   r   r   r   c           	   	   K  s8   |du r|du rd}t |ƒ‚| f ||||||dœ|¤ŽS )a{  Initialize tool from a function.

        Args:
            func: The function to create the tool from.
            name: The name of the tool.
            description: The description of the tool.
            return_direct: Whether to return the output directly. Defaults to False.
            args_schema: The schema of the tool's input arguments. Defaults to None.
            coroutine: The asynchronous version of the function. Defaults to None.
            kwargs: Additional arguments to pass to the tool.

        Returns:
            The tool.

        Raises:
            ValueError: If the function is not provided.
        Nz*Function and/or coroutine must be provided)r2   r   r   r   rB   r*   )Ú
ValueError)	Úclsr   r2   r   rB   r*   r   r   r5   r$   r$   r%   Úfrom_function¥   s    úùzTool.from_function)N)FNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r    Úpropertyr+   r.   r=   r>   r@   ÚclassmethodrE   Ú__classcell__r$   r$   r"   r%   r   !   s(   
 ý üü    ÷r   N)rI   Ú
__future__r   Úcollections.abcr   Úinspectr   Útypingr   r   r   r	   r
   Ztyping_extensionsr   Zlangchain_core.callbacksr   r   Zlangchain_core.runnablesr   r   Zlangchain_core.tools.baser   r   r   r   Zlangchain_core.messagesr   r   r$   r$   r$   r%   Ú<module>   s   