
    g3fi%                        d Z ddlm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 ddl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mZm Z m!Z! ddl"m#Z# erddl$m%Z%  G d de      Z&ddZ'y)zStructured tool.    )annotationsN)	AwaitableCallable)	signature)TYPE_CHECKING	AnnotatedAnyLiteral)FieldSkipValidation)override)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)RunnableConfigrun_in_executor)
_EMPTY_SETFILTERED_ARGS
ArgsSchemaBaseTool_get_runnable_config_param_is_injected_arg_typecreate_schema_from_function)is_basemodel_subclass)ToolCallc                  T    e Zd ZU dZdZded<    edd      Zded	<   	 d
Zded<   	 d
Z	ded<   	 e
	 d	 	 	 	 	 	 	 d fd       Zd
d	 	 	 	 	 	 	 	 	 ddZd
d	 	 	 	 	 	 	 	 	 d fdZe	 	 	 	 	 	 	 ddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zej"                  dd       Z xZS )StructuredToolz.Tool that can operate on any number of inputs. strdescription.zThe tool schema.)r   z'Annotated[ArgsSchema, SkipValidation()]args_schemaNzCallable[..., Any] | Nonefunc$Callable[..., Awaitable[Any]] | None	coroutinec                   K   | j                   s!t        || j                  ||fi | d {   S t        |   ||fi | d {   S 7 7 wN)r#   r   invokesuperainvoke)selfinputconfigkwargs	__class__s       ]/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_core/tools/structured.pyr(   zStructuredTool.ainvoke8   sP      ~~(eVVvVVVW_UF=f=== W=s!   )AA
AAAA)run_managerc                  | j                   rq|rAt        | j                         j                  j                  d      r|j	                         |d<   t        | j                         x}r|||<    | j                   |i |S d}t        |      )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
        	callbacksz0StructuredTool does not support sync invocation.)r!   r   
parametersget	get_childr   NotImplementedError)r)   r+   r/   argsr,   config_parammsgs          r.   _runzStructuredTool._runG   s    $ 99y3>>BB;O&1&;&;&={#9$))DD|D'-|$499d-f--@!#&&    c               R  K   | j                   ry|rAt        | j                         j                  j                  d      r|j	                         |d<   t        | j                         x}r|||<    | j                   |i | d{   S t        |   |||d| d{   S 7 7 w)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
        r1   N)r+   r/   )r#   r   r2   r3   r4   r   r'   _arun)r)   r+   r/   r6   r,   r7   r-   s         r.   r<   zStructuredTool._arunb   s     $ >>y8CCGGT&1&;&;&={#9$..II|I'-|$'8888 W]&k
=C
 
 	
	 9
s$   BB'B#B'B%B'%B'Fcontent)response_formatparse_docstringerror_on_invalid_docstringc               6   ||}n||}nd}t        |      |xs |j                  }||rt        |||	|
t        |            }|}||	s|j                  xs d}|i|rgt        |t              r%t        |      r|j                  }|rd|v rd}n7|s5d}n2t        |t              r|j                  d      }nd| }t        |      |d}t        |      |#t        j                  |      j                         }|j                          } | d
|||||||d	|S )a  Create tool from a given function.

        A classmethod that helps to create a tool from a function.

        Args:
            func: The function from which to create a tool.
            coroutine: The async function from which to create a tool.
            name: The name of the tool. Defaults to the function name.
            description: The description of the tool.
                Defaults to the function docstring.
            return_direct: Whether to return the result directly or as a callback.
            args_schema: The schema of the tool's input arguments.
            infer_schema: Whether to infer the schema from the function's signature.
            response_format: The tool response format.

                If `"content"` then the output of the tool is interpreted as the
                contents of a `ToolMessage`. If `"content_and_artifact"` then the output
                is expected to be a two-tuple corresponding to the `(content, artifact)`
                of a `ToolMessage`.
            parse_docstring: If `infer_schema` and `parse_docstring`, will attempt
                to parse parameter descriptions from Google Style function docstrings.
            error_on_invalid_docstring: if `parse_docstring` is provided, configure
                whether to raise `ValueError` on invalid Google Style docstrings.
            **kwargs: Additional arguments to pass to the tool

        Returns:
            The tool.

        Raises:
            ValueError: If the function is not provided.
            ValueError: If the function does not have a docstring and description
                is not provided.
            TypeError: If the `args_schema` is not a `BaseModel` or dict.

        Examples:
            ```python
            def add(a: int, b: int) -> int:
                """Add two numbers"""
                return a + b
            tool = StructuredTool.from_function(add)
            tool.run(1, 2) # 3

            ```
        Nz*Function and/or coroutine must be provided)r?   r@   filter_argsz)A base class for creating Pydantic modelsr   r   z5Invalid args_schema: expected BaseModel or dict, got z;Function must have a docstring if description not provided.)namer!   r#   r    r   return_directr>    )
ValueError__name__r   _filter_schema_args__doc__
isinstancetyper   dictr3   	TypeErrortextwrapdedentstrip)clsr!   r#   rC   r   rD   r    infer_schemar>   r?   r@   r,   source_functionr8   description_s                  r.   from_functionzStructuredTool.from_function   sj   x "O"'O>CS/!///<5 /+E/@K #*22:dLK+t,1F{1S*22 C|S#%L%#'LK.*}=&-)   n$OCS/!#??<8>>@L ',,./ 	
#$'+	
 	
 		
r:   c                    | j                   xs | j                  }|t        S t        d t	        |      j
                  j                         D              S )Nc              3  P   K   | ]  \  }}t        |j                        r|   y wr%   )r   
annotation).0kvs      r.   	<genexpr>z5StructuredTool._injected_args_keys.<locals>.<genexpr>   s(      
1$Q\\2 
s   $&)r!   r#   r   	frozensetr   r2   items)r)   fns     r.   _injected_args_keysz"StructuredTool._injected_args_keys   sL    YY($..: 
!"00668
 
 	
r:   r%   )r*   zstr | dict | ToolCallr+   zRunnableConfig | Noner,   r	   returnr	   )
r6   r	   r+   r   r/   z CallbackManagerForToolRun | Noner,   r	   ra   r	   )
r6   r	   r+   r   r/   z%AsyncCallbackManagerForToolRun | Noner,   r	   ra   r	   )NNNNFNT)r!   zCallable | Noner#   r"   rC   
str | Noner   rb   rD   boolr    zArgsSchema | NonerR   rc   r>   z*Literal['content', 'content_and_artifact']r?   rc   r@   rc   r,   r	   ra   r   )ra   zfrozenset[str])rG   
__module____qualname__rI   r   __annotations__r   r    r!   r#   r   r(   r9   r<   classmethodrU   	functoolscached_propertyr`   __classcell__)r-   s   @r.   r   r   (   s   8K;@+<K8  '&*D
#*66:I3:3
  )-
>$
> &
> 	
>
 

> 
>$ 9=	'' ' 6	'
 ' 
'> >B	

 
 ;	

 
 

>  !%:>"&#)-!t
 GP %+0t
t
 8t
 	t

  t
 t
 't
 t
 Dt
 t
 %)t
 t
 
t
 t
l 
 
r:   r   c                `    t        t              }t        |       x}r|j                  |       |S r%   )listr   r   append)r!   rB   r7   s      r.   rH   rH     s0    }%K1$77|7<(r:   )r!   r   ra   z	list[str])(rI   
__future__r   rh   rN   collections.abcr   r   inspectr   typingr   r   r	   r
   pydanticr   r   typing_extensionsr   langchain_core.callbacksr   r   langchain_core.runnablesr   r   langchain_core.tools.baser   r   r   r   r   r   r   langchain_core.utils.pydanticr   langchain_core.messagesr   r   rH   rE   r:   r.   <module>ry      sc     "   /   + & E   @0Y
X Y
xr:   