
    h-ikA                       U d dl mZ d dlmZmZmZ d dlZd dlZd dlmZm	Z	m
Z
 d dlmZ d dlZd dlmZ ej                   Ze
eeef   Zi ej(                  ej(                  d ej(                  dej(                  dej(                  ej*                  ej*                  d	ej*                  d
ej*                  dej*                  ej,                  ej,                  dej,                  dej,                  dej,                  ej.                  ej.                  dej.                  dej.                  dej.                  ej0                  ej0                  dej.                  dej0                  dej0                  ej2                  ej2                  dej2                  dej2                  dej2                  ej4                  ej4                  dej4                  dej4                  dej4                  iZded<   d?dZddd	 	 	 	 	 	 	 d@dZdAd Z G d! d"      Z e!ed#f   Z"e
e#ee$e"e%d#   df   Z& G d$ d%e       Z'e
e ej@                  e!eef   e	d&ef   f   Z(	 	 	 	 dBd'Z)dCd(Z* G d) d*      Z+ G d+ d,e      Z,e
e+ejV                  e,ee(   e(f   Z-dDd-Z. G d. d/      Z/e
ee-   e-f   Z0dEd0Z1e
e/e0f   Z2dFd1Z3ejh                  jj                  Z6d	e6jn                  e6jn                  e6jn                  d2e6jn                  d3e6jn                  de6jp                  e6jp                  e6jp                  d4e6jp                  d5e6jp                  de6jr                  e6jr                  e6jr                  d6e6jr                  d7e6jr                  iZ:e
e6eef   Z;dGd8Z< G d9 d:e      Z=e
e;e=ejh                  f   Z>dHd;Z? G d< d=      Z@e
e@ej                  f   ZBdId>ZCy)J    )annotations)IterableMappingSequenceN)AnyCallableUnion)	TypedDict)protostype_unspecifiedunspecified   type_stringstring   type_numbernumber   type_integerinteger   type_booleanboolean   
type_arrayarray   type_objectobjectzdict[TypeOptions, Type]
_TYPE_TYPEc                T    t        | t              r| j                         } t        |    S N)
isinstancestrlowerr    xs    \D:\DhanshreeandTeamAI\Github\ai_code\venv\Lib\site-packages\google/generativeai/responder.pyto_typer)   ?   s!    !SGGIa=    )descriptionsrequiredc               ^   |i }|g }t        t        j                  |       j                        }|j	                         D ci c]  \  }}|j
                  t        j                  j                  t        j                  j                  t        j                  j                  fv r`||j                  t        j                  j                  k7  r|j                  nt        t        j                  |j                  |d            f }}}t        j                   | j"                  fi |j%                         }|j'                  dd       |j                  di       j	                         D ]r  \  }}|j'                  dd       ||   j                  }	t)        j*                  |	      t(        j,                  u sMt/        d      t)        j0                  |	      v snd|d<   t |r||d<   n|D 
cg c]  }
||
   j2                  t        j                  j                  k(  r^||
   j
                  t        j                  j                  t        j                  j                  t        j                  j                  fv r|
 c}
|d<   t        | j"                  | j4                  |      }|S c c}}w c c}
w )	a  Generates the OpenAPI Schema for a python function.

    Args:
        f: The function to generate an OpenAPI Schema for.
        descriptions: Optional. A `{name: description}` mapping for annotating input
            arguments of the function with user-provided descriptions. It
            defaults to an empty dictionary (i.e. there will not be any
            description for any of the inputs).
        required: Optional. For the user to specify the set of required arguments in
            function calls to `f`. If unspecified, it will be automatically
            inferred from `f`.

    Returns:
        dict[str, Any]: The OpenAPI Schema for the function `f` in JSON format.
    N)descriptiontitle
propertiesTnullabler,   namer.   
parameters)dictinspect	signaturer4   itemskind	ParameterPOSITIONAL_OR_KEYWORDKEYWORD_ONLYPOSITIONAL_ONLY
annotationemptyr   pydanticFieldgetcreate_model__name__model_json_schemapoptyping
get_originr	   typeget_argsdefault__doc__)fr+   r,   defaultsr3   paramfields_dictr4   function_argr>   kschemas               r(   _generate_schemarT   E   sg   * G%%a(334H  $>>+ ,KD%::33**--

! 	 "'!1!1W5F5F5L5L!LURUNN ),,T48	
 	
 ,  0 &&qzzA[ASSUJ
 NN7D!(nn\2>DDFl$'d^..

 Z(FLL8T$Z6??L
 >
 (,L$ G !)
:
 "
##w'8'8'>'>>QK$$%%;;%%22%%55	 "

: qzzqyyZPFM{^"
s   CJ$+BJ*c                   | | S | j                         } | j                  dd       }||| d<   | j                  dd       }|t        |      | d<   | j                  dd       }||| d<   | j                  dd       }|t	        |      | d<   | j                  dd       }|0|j                         D ci c]  \  }}|t	        |       c}}| d<   | S c c}}w )NrI   type_formatformat_r8   r0   )copyrF   rB   r)   _rename_schema_fieldsr8   )rS   rV   rX   r8   r0   rR   vs          r(   rZ   rZ      s    ~[[]FJJvt$EwJJw%E!%.wjj4(G#yJJw%E/6wL$/JHRHXHXHZ[HZ1#8#; ;HZ[|M  \s   2Cc                  ~    e Zd Zddd
dZedd       Zedd       Zedd       Zedd       Z	ddZ
eddd	       Zy)FunctionDeclarationNr4   c               P    t        j                  ||t        |            | _        y)zmA  class wrapping a `protos.FunctionDeclaration`, describes a function for `genai.GenerativeModel`'s `tools`.r2   N)r   r]   rZ   _proto)selfr3   r.   r4   s       r(   __init__zFunctionDeclaration.__init__   s"    00;;PQ[;\
r*   c                .    | j                   j                  S r"   )r`   r3   ra   s    r(   r3   zFunctionDeclaration.name   s    {{r*   c                .    | j                   j                  S r"   )r`   r.   rd   s    r(   r.   zFunctionDeclaration.description   s    {{&&&r*   c                .    | j                   j                  S r"   )r`   r4   rd   s    r(   r4   zFunctionDeclaration.parameters   s    {{%%%r*   c                *     | ddi       }||_         |S )N r2   r`   )clsprotora   s      r(   
from_protozFunctionDeclaration.from_proto   s    r:r*   c                    | j                   S r"   ri   rd   s    r(   to_protozFunctionDeclaration.to_proto       {{r*   c                B    |i }t        | |      }t        di |d| iS )a  Builds a `CallableFunctionDeclaration` from a python function.

        The function should have type annotations.

        This method is able to generate the schema for arguments annotated with types:

        `AllowedTypes = float | int | str | list[AllowedTypes] | dict`

        This method does not yet build a schema for `TypedDict`, that would allow you to specify the dictionary
        contents. But you can build these manually.
        )r+   function )rT   CallableFunctionDeclaration)rq   r+   rS   s      r(   from_functionz!FunctionDeclaration.from_function   s.     L!(F*GVGhGGr*   )r3   r$   r.   r$   r4   dict[str, Any] | None)returnr$   )rv   zprotos.Schema)rv   r]   )rv   protos.FunctionDeclarationr"   )rq   Callable[..., Any]r+   zdict[str, str] | None)rD   
__module____qualname__rb   propertyr3   r.   r4   classmethodrl   rn   staticmethodrt   rr   r*   r(   r]   r]      su    [_ 
     ' ' & &  
 H Hr*   r]   	ValueTypec                  @     e Zd ZdZdd	 	 	 	 	 	 	 d fdZddZ xZS )rs   zAn extension of `FunctionDeclaration` that can be built from a Python function, and is callable.

    Note: The Python function must have type annotations.
    Nr^   c               8    t         |   |||       || _        y )Nr2   )superrb   rq   )ra   r3   r.   r4   rq   	__class__s        r(   rb   z$CallableFunctionDeclaration.__init__   s      	d
S r*   c                     | j                   di |j                  }t        |t              sd|i}t	        j
                  |j                  |      S )Nresult)r3   responserr   )rq   argsr#   r5   r   FunctionResponser3   )ra   fcr   s      r(   __call__z$CallableFunctionDeclaration.__call__  sD    ))&$''F&&BGGfEEr*   )r3   r$   r.   r$   r4   ru   rq   rx   )r   protos.FunctionCallrv   zprotos.FunctionResponse)rD   ry   rz   rL   rb   r   __classcell__)r   s   @r(   rs   rs      s@     -1	! 	! 		!
 *	! %	!Fr*   rs   .c                    t        | t        t        j                  f      r| S t        | t              rd| v rt	        di | S t        di | S t        |       rt        j                  |       S t        dt        |       j                   d|       )Nrq   z_Invalid argument type: Expected an instance of `genai.FunctionDeclarationType`. Received type: .rr   )
r#   r]   r   r5   rs   callablert   	TypeErrorrI   rD   )funs    r(   _make_function_declarationr     s     #+V-G-GHI
	C	.555&---	#*88==mnrsvnw  oA  oA  nB  BC  D
 	
r*   c                Z    t        | t        j                        r| S | j                         S r"   )r#   r   r]   rn   )fds    r(   
_encode_fdr   $  s#    "f001	;;=r*   c                  H    e Zd ZdZddZed	d       Z	 	 	 	 d
dZddZd Z	y)Toolz\A wrapper for `protos.Tool`, Contains a collection of related `FunctionDeclaration` objects.c                x   |D cg c]  }t        |       c}| _        i | _        | j                  D ]@  }|j                  }|| j                  v rt	        d      || j                  |j                  <   B t        j                  | j                  D cg c]  }t        |       c}      | _        y c c}w c c}w )Nrh   function_declarations)	r   _function_declarations_indexr3   
ValueErrorr   r   r   r`   )ra   r   rM   r   r3   s        r(   rb   zTool.__init__.  s    Nc&dNc'A!'DNc&d#--B77Dt{{" n$#%DKK 	 . kk<@<W<W"X<Wb:b><W"X
 'e #Ys   B2B7c                    | j                   S r"   )r   rd   s    r(   r   zTool.function_declarations<  s    ***r*   c                X    t        |t              s|j                  }| j                  |   S r"   r#   r$   r3   r   ra   r3   s     r(   __getitem__zTool.__getitem__@  &     $$99D{{4  r*   c                4    | |   }t        |      sy  ||      S r"   )r   )ra   r   declarations      r(   r   zTool.__call__H  s     2h$2r*   c                    | j                   S r"   ri   rd   s    r(   rn   zTool.to_protoO  ro   r*   N)r   z!Iterable[FunctionDeclarationType])rv   z6list[FunctionDeclaration | protos.FunctionDeclaration]r3   zstr | protos.FunctionCallrv   0FunctionDeclaration | protos.FunctionDeclaration)r   r   rv   zprotos.FunctionResponse | None)
rD   ry   rz   rL   rb   r{   r   r   r   rn   rr   r*   r(   r   r   +  s=    f
 + +!-!	9!r*   r   c                      e Zd ZU ded<   y)ToolDictzlist[FunctionDeclarationType]r   NrD   ry   rz   __annotations__rr   r*   r(   r   r   S  s    88r*   r   c                   t        | t              r| S t        | t        j                        rt        | j                        S t        | t              r1d| v rt        di | S | }t        t        j
                  di |g      S t        | t              rt        |       S 	 t        | g      S # t        $ r)}t        dt        |       j                   d|       |d }~ww xY w)Nr   r   zPInvalid argument type: Expected an instance of `genai.ToolType`. Received type: r   rr   )r#   r   r   r   r5   r]   r   	Exceptionr   rI   rD   )toolr   es      r(   
_make_toolr   \  s    $	D&++	&$*D*DEE	D$	"d*<$<Bv/I/I/OB/O.PQQ	D(	#$//	tf55 	bcghlcmcvcvbwwxy 	s   !B. .	C 7$CC c                  6    e Zd ZdZddZ	 	 	 	 ddZd	dZd Zy)
FunctionLibraryzYA container for a set of `Tool` objects, manages lookup and execution of their functions.c                   t        |      }t        |      | _        i | _        | j                  D ]U  }|j                  D ]D  }|j
                  }|| j                  v rt        d| d      || j                  |j
                  <   F W y )Nz2Invalid operation: A `FunctionDeclaration` named 'zI' is already defined. Each `FunctionDeclaration` must have a unique name.)_make_toolslist_toolsr   r   r3   r   )ra   toolsr   r   r3   s        r(   rb   zFunctionLibrary.__init__v  s    E"5kKKD#99"''4;;&$LTF  S\  ]  1<K,,-  :  r*   c                X    t        |t              s|j                  }| j                  |   S r"   r   r   s     r(   r   zFunctionLibrary.__getitem__  r   r*   c                `    | |   }t        |      sy  ||      }t        j                  |      S )N)function_response)r   r   Part)ra   r   r   r   s       r(   r   zFunctionLibrary.__call__  s.    2h$r?{{X66r*   c                \    | j                   D cg c]  }|j                          c}S c c}w r"   )r   rn   )ra   r   s     r(   rn   zFunctionLibrary.to_proto  s$    ,0KK8KDK888s   )N)r   zIterable[ToolType]r   )r   r   rv   zprotos.Part | None)rD   ry   rz   rL   rb   r   r   rn   rr   r*   r(   r   r   s  s)    c<!-!	9!79r*   r   c                4   t        | t              rqt        | t              sa| D cg c]  }t        |       } }t	        |       dkD  r9t        d | D              r't        | D cg c]  }|j                  d    c}      g} | S | }t        |      gS c c}w c c}w )Nr   c              3  L   K   | ]  }t        |j                        d k(    yw)r   N)lenr   ).0ts     r(   	<genexpr>z_make_tools.<locals>.<genexpr>  s!     !SU#a&=&=">!"CUs   "$r   )r#   r   r   r   r   allr   )r   r   r   s      r(   r   r     s    %":eW+E(-.1A.u:>c!SU!SSU KU!8!8!;U KLME4 !! / !Ls   B"Bc                F    | | S t        | t              r| S t        |       S )N)r   )r#   r   )libs    r(   to_function_libraryr     s&    
{
	C	)
S))r*   	mode_autoautomode_anyany	mode_nonenonec                T    t        | t              r| j                         } t        |    S r"   )r#   r$   r%   _FUNCTION_CALLING_MODEr&   s    r(   to_function_calling_moder     s"    !SGGI!!$$r*   c                  "    e Zd ZU ded<   ded<   y)FunctionCallingConfigDictFunctionCallingModeTypemodez	list[str]allowed_function_namesNr   rr   r*   r(   r   r     s    
!!%%r*   r   c                |   t        | t        j                        r| S t        | t        t        t
        f      rdt        |       i} nct        | t              r0| j                         } | j                  d      }t        |      | d<   n#t        dt        |       j                   d|       t        j                  |       S )Nr   zaInvalid argument type: Could not convert input to `protos.FunctionCallingConfig`. Received type: r   )r#   r   FunctionCallingConfigFunctionCallingModer$   intr   r5   rY   rF   r   rI   rD   )objr   s     r(   to_function_calling_configr     s    #v334
	C-sC8	9/45	C	hhjwwv.t4F#Cy112!5
 	
 '',,r*   c                      e Zd ZU ded<   y)ToolConfigDictFunctionCallingConfigTypefunction_calling_configNr   rr   r*   r(   r   r     s    66r*   r   c                
   t        | t        j                        r| S t        | t              r6| j	                  d      }t        |      }|| d<   t        j                  di | S t        dt        |       j                   d      )Nr   zVInvalid argument type: Could not convert input to `protos.ToolConfig`. Received type: r   rr   )	r#   r   
ToolConfigr5   rF   r   r   rI   rD   )r   fccs     r(   to_tool_configr     s    #v(()
	C	gg/0(-),%&  '3''"3i0014
 	
r*   )r'   TypeOptionsrv   Type)rM   rx   r+   zMapping[str, str] | Noner,   zSequence[str] | Nonerv   dict[str, Any])rS   r   )r   FunctionDeclarationTyperv   r   )r   r   rv   rw   )r   ToolTyperv   r   )r   	ToolsTyperv   z
list[Tool])r   zFunctionLibraryType | Nonerv   zFunctionLibrary | None)r'   r   rv   r   )r   r   rv   zprotos.FunctionCallingConfig)r   ToolConfigTyperv   zprotos.ToolConfig)D
__future__r   collections.abcr   r   r   r6   rG   r   r   r	   typing_extensionsr
   r@   google.generativeair   r   r   r$   r   TYPE_UNSPECIFIEDSTRINGNUMBERINTEGERBOOLEANARRAYOBJECTr    r   r)   rT   rZ   r]   r5   
StructTypefloatboolr   r~   rs   r   r   r   r   r   r   r   r   r   r   FunctionLibraryTyper   r   Moder   AUTOANYNONEr   r   r   r   r   r   r   r   r   r   rr   r*   r(   <module>r      sr   # 7 7   ' ' '  &{{CdN#'400't' --' 4((	'
 	KK' t{{' 4;;' dkk' 	KK' t{{' 4;;' dkk' 	LL$,,' t||' DLL'  t||!'" 	LL$,,#'$ t||DLLt||JJ

tzz$**TZZKKt{{4;;dkk9'
# @ .2%)	WW +W #	W
 Wt8/H /Hd #{"#
%dJ[0A4GH	F"5 F0  
cNS#X 
	 
5
&% %P9y 9 &++x*A!BD[[
.!9 !9H (8$h./		" OY67 * 2277  166$))
$$044#''	""166$))
$$ $   3S# => %&	 &
 "68T8TT 
-&7 7 ~v'8'889
r*   