
    ; iN                       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 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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        iZded<   dfd"Zddd#dgd+Z d, Z!d- Z"d. Z#d/ Z$d0 Z%d1 Z&dhd3Z' G d4 d5          Z(e)ed6f         Z*e
e+ee,e*e-d6         df         Z. G d7 d8e(          Z/e
e(ej(        e)eef         e	d9ef         f         Z0did=Z1djd@Z2 G dA dB          Z3 G dC dDe          Z4e
e3ej3        e4ee0         e0f         Z5dkdGZ6 G dH dI          Z7e
ee5         e5f         Z8dldMZ9e
e7e8f         Z:dmdQZ;ej<        j=        Z>d
e>j?        e>j?        e>j?        dRe>j?        dSe>j?        de>j@        e>j@        e>j@        dTe>j@        dUe>j@        de>jA        e>jA        e>jA        dVe>jA        dWe>jA        iZBe
e>eef         ZCdndZZD G d[ d\e          ZEe
eCeEej<        f         ZFdod`ZG G da db          ZHe
eHejI        f         ZJdpdeZKdS )q    )annotations)IterableMappingSequenceN)AnyCallableUnion)	TypedDict)protos)content_types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_TYPExTypeOptionsreturnTypec                n    t          | t                    r|                                 } t          |          S N)
isinstancestrlowerr!   r"   s    u/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/google/generativeai/responder.pyto_typer-   @   s+    !S GGIIa=    )descriptionsrequiredfCallable[..., Any]r/   Mapping[str, str] | Noner0   Sequence[str] | Nonedict[str, Any]c               j  	 |i }t          t          j        |           j                  	i }	                                D ]\  }}|j        t          j        j        t          j        j        t          j        j	        fv r\t          j        |                    |d                    }|j        t          j        j        k    r|j        |f||<   t          |f||<   t!          | j        |          }|||d<   n	fd	D             |d<   t          | j        | j                  }|d         r||d<   |S )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)descriptionr0   c                    g | ]c}|         j         t          j        j        k    rA|         j        t          j        j        t          j        j        t          j        j        fv a|dS  )defaultinspect	ParameteremptykindPOSITIONAL_OR_KEYWORDKEYWORD_ONLYPOSITIONAL_ONLY).0kdefaultss     r,   
<listcomp>z$_generate_schema.<locals>.<listcomp>   su     "
 "
 "
#w'8'>>>QK$%;%2%5 	   r.   )namer7   
properties
parameters)dictr;   	signaturerH   itemsr>   r<   r?   r@   rA   pydanticFieldget
annotationr=   r   _build_schema__name____doc__)
r1   r/   r0   fields_dictrF   paramfieldrH   schemarD   s
            @r,   _generate_schemarW   F   sh   * G%a((344HK~~'' / /e:3*-
 
 
 N(,,T488  E 7#4#:::$)$4e$;D!!$'JD!qz;77J !)
:"
 "
 "
 "
"
 "
 "

: qzqy999F, *)|Mr.   c                f   t          j        | fi |                                }|                    di           }|                                D ]\  }}t          ||           t          ||           t          |           t          |           t          |           t          |           |S )Nz$defs)
rL   create_modelmodel_json_schemapoprK   unpack_defsconvert_to_nullableadd_object_typestrip_titlesstrip_additional_properties)fnamerS   rH   defsrF   values         r,   rP   rP      s    &u<<<<NNPPJ>>'2&&Dzz|| ! !eE4    
D!!! 
###J
 
+++r.   c                   |                      dd           }|d S |                                D ]9\  }}|                     dd           }|7||                    d          d                  }t          ||           |||<   U|                     dd           }|dt	          |          D ]S\  }}	|	                     dd           }|6||                    d          d                  }t          ||           |||<   T|                     dd           }
|
P|
                     dd           }|8||                    d          d                  }t          ||           ||d<   9;d S )NrG   z$refzdefs/anyOfrK   )rN   rK   splitr\   	enumerate)rV   rb   rG   rF   rc   ref_keyrefanyofiatyperK   s              r,   r\   r\      s   L$//J!''))  e))FD))w}}W--b12CT""""Jt		'4((%e,, # #5))FD11&w}}W55b9:CT***"E!H		'4((ii--G"7==11"56C&&&!$g3 r.   c                   |                      dd           }|                     dd           }|)|                                D ]\  }}t          |           |                     dd           }|t          |           d S d S )NtitlerG   rK   )r[   rN   rK   r_   )rV   ro   rG   rF   rc   rK   s         r,   r_   r_      s    JJw%%EL$//J%++-- 	  	 KD%JJw%%EU r.   c                   |                      dd            |                     dd           }|)|                                D ]\  }}t          |           |                     dd           }|t          |           d S d S )NadditionalPropertiesrG   rK   )r[   rN   rK   r`   rV   rG   rF   rc   rK   s        r,   r`   r`      s    
JJ%t,,,L$//J%++-- 	/ 	/KD%'....JJw%%E#E***** r.   c                   |                      dd           }|D|                     dd            d| d<   |                                D ]\  }}t          |           |                      dd           }|t          |           d S d S )NrG   r0   r    typerK   )rN   r[   rK   r^   rr   s        r,   r^   r^      s    L$//J

:t$$$!v%++-- 	# 	#KD%E""""JJw%%E r.   c                   |                      dd           }|wt          |          dk    rt          d          |\  }}|ddik    r|                     |           n-|ddik    r|                     |           nt          d          d| d<   |                     dd           }|)|                                D ]\  }}t          |           |                     d	d           }|t          |           d S d S )
Nrf   r   zInvalid input: Type Unions are not supported, except for `Optional` types. Please provide an `Optional` type or a non-Union type.rt   nullTnullablerG   rK   )r[   len
ValueErrorupdaterN   rK   r]   )rV   rk   abrG   rF   rc   rK   s           r,   r]   r]      s9   JJw%%Eu::??I   1   MM!66"""MM!I   "zL$//J%++-- 	' 	'KD%&&&&JJw%%EE""""" r.   rV   c                   | | S |                                  } |                     dd           }||| d<   |                     dd           }|t          |          | d<   |                     dd           }||| d<   |                     dd           }|t	          |          | d<   |                     dd           }|!d |                                D             | d<   | S )Nrt   type_formatformat_rK   rG   c                4    i | ]\  }}|t          |          S r9   )_rename_schema_fields)rB   rC   vs      r,   
<dictcomp>z)_rename_schema_fields.<locals>.<dictcomp>'  s'    [[[1#8#;#;[[[r.   )copyr[   rN   r-   r   rK   )rV   r~   r   rK   rG   s        r,   r   r     s    ~[[]]FJJvt$$EwJJw%%E!%..wjj4((G#yJJw%%E/66wL$//J[[
HXHXHZHZ[[[|Mr.   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dS )FunctionDeclarationNrH   rF   r)   r7   rH   dict[str, Any] | Nonec               X    t          j        ||t          |                    | _        dS )zmA  class wrapping a `protos.FunctionDeclaration`, describes a function for `genai.GenerativeModel`'s `tools`.rF   r7   rH   N)r   r   r   _proto)selfrF   r7   rH   s       r,   __init__zFunctionDeclaration.__init__-  s0    0;;PQ[;\;\
 
 
r.   r$   c                    | j         j        S r'   )r   rF   r   s    r,   rF   zFunctionDeclaration.name3  s    {r.   c                    | j         j        S r'   )r   r7   r   s    r,   r7   zFunctionDeclaration.description7  s    {&&r.   protos.Schemac                    | j         j        S r'   )r   rH   r   s    r,   rH   zFunctionDeclaration.parameters;  s    {%%r.   c                0     | ddi           }||_         |S )N r   r   )clsprotor   s      r,   
from_protozFunctionDeclaration.from_proto?  s$    sr:::r.   protos.FunctionDeclarationc                    | j         S r'   r   r   s    r,   to_protozFunctionDeclaration.to_protoE  
    {r.   functionr2   r/   dict[str, str] | Nonec                L    |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.
        N)r/   r   r9   )rW   CallableFunctionDeclaration)r   r/   rV   s      r,   from_functionz!FunctionDeclaration.from_functionH  s@     L!(FFF*GGVGGhGGGGr.   )rF   r)   r7   r)   rH   r   )r$   r)   )r$   r   )r$   r   )r$   r   r'   )r   r2   r/   r   )rQ   
__module____qualname__r   propertyrF   r7   rH   classmethodr   r   staticmethodr   r9   r.   r,   r   r   ,  s        [_ 
 
 
 
 
 
       X  ' ' ' X' & & & X&    [
    H H H H \H H Hr.   r   	ValueTypec                  2     e Zd ZdZddd fdZddZ xZS )r   zAn extension of `FunctionDeclaration` that can be built from a Python function, and is callable.

    Note: The Python function must have type annotations.
    Nr   rF   r)   r7   rH   r   r   r2   c               ^    t                                          |||           || _        d S )Nr   )superr   r   )r   rF   r7   rH   r   	__class__s        r,   r   z$CallableFunctionDeclaration.__init__h  s/     	d
SSS r.   fcprotos.FunctionCallr$   protos.FunctionResponsec                     | j         di |j        }t          |t                    sd|i}t	          j        |j        |          S )Nresult)rF   responser9   )r   argsr(   rI   r   FunctionResponserF   )r   r   r   s      r,   __call__z$CallableFunctionDeclaration.__call__s  sO    ))))&$'' 	('F&BGfEEEEr.   )rF   r)   r7   r)   rH   r   r   r2   )r   r   r$   r   )rQ   r   r   rR   r   r   __classcell__)r   s   @r,   r   r   b  sr          -1	! 	! 	! 	! 	! 	! 	! 	!F F F F F F F Fr.   r   .funFunctionDeclarationType0FunctionDeclaration | protos.FunctionDeclarationc                H   t          | t          t          j        f          r| S t          | t                    rd| v rt	          di | S t          di | S t          |           rt                              |           S t          dt          |           j	         d|           )Nr   z_Invalid argument type: Expected an instance of `genai.FunctionDeclarationType`. Received type: .r9   )
r(   r   r   rI   r   callabler   	TypeErrorrt   rQ   )r   s    r,   _make_function_declarationr     s     #+V-GHII 

	C		 
.55555&-----	# 
*88=== Dnrsvnwnw  oA  D  D  D
 
 	
r.   fdr   c                b    t          | t          j                  r| S |                                 S r'   )r(   r   r   r   )r   s    r,   
_encode_fdr     s*    "f01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	dS )Toolz\A wrapper for `protos.Tool`, Contains a collection of related `FunctionDeclaration` objects.function_declarations!Iterable[FunctionDeclarationType]c                    d |D             | _         i | _        | j         D ]0}|j        }|| j        v rt          d          || j        |j        <   1t	          j        d | j         D                       | _        d S )Nc                ,    g | ]}t          |          S r9   )r   )rB   r1   s     r,   rE   z!Tool.__init__.<locals>.<listcomp>  s!    &d&d&d'A!'D'D&d&d&dr.   r   c                ,    g | ]}t          |          S r9   )r   )rB   r   s     r,   rE   z!Tool.__init__.<locals>.<listcomp>  s    "X"X"Xb:b>>"X"X"Xr.   r   )_function_declarations_indexrF   ry   r   r   r   )r   r   r   rF   s       r,   r   zTool.__init__  s    &d&dNc&d&d&d#- 	& 	&B7Dt{"" nn$#%DK  k"X"XD<W"X"X"X
 
 
r.   r$   6list[FunctionDeclaration | protos.FunctionDeclaration]c                    | j         S r'   )r   r   s    r,   r   zTool.function_declarations  s    **r.   rF   str | protos.FunctionCallr   c                T    t          |t                    s|j        }| j        |         S r'   r(   r)   rF   r   r   rF   s     r,   __getitem__zTool.__getitem__  *     $$$ 	9D{4  r.   r   r   protos.FunctionResponse | Nonec                J    | |         }t          |          sd S  ||          S r'   )r   )r   r   declarations      r,   r   zTool.__call__  s.    2h$$ 	4{2r.   c                    | j         S r'   r   r   s    r,   r   zTool.to_proto  r   r.   N)r   r   )r$   r   rF   r   r$   r   )r   r   r$   r   )
rQ   r   r   rR   r   r   r   r   r   r   r9   r.   r,   r   r     s        ff
 
 
 
 + + + X+! ! ! !       r.   r   c                      e Zd ZU ded<   dS )ToolDictzlist[FunctionDeclarationType]r   NrQ   r   r   __annotations__r9   r.   r,   r   r     s         888888r.   r   toolToolTypec                   t          | t                    r| S t          | t          j                  rt          | j                  S t          | t                    r2d| 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   r9   )r(   r   r   r   rI   r   r   	Exceptionr   rt   rQ   )r   r   es      r,   
_make_toolr     s'   $ 	D&+	&	& $*DEEEE	D$		 "d**<<$<<Bv/I/O/OB/O/O.PQQQQ	D(	#	# 	$////	tf5555 	 	 	ycghlcmcmcvyyy  	s   4C 
C;'C66C;c                  0    e Zd ZdZddZdd	ZddZd ZdS )FunctionLibraryzYA container for a set of `Tool` objects, manages lookup and execution of their functions.toolsIterable[ToolType]c                    t          |          }t          |          | _        i | _        | j        D ]>}|j        D ]4}|j        }|| j        v rt          d| d          || j        |j        <   5?d S )Nz2Invalid operation: A `FunctionDeclaration` named 'zI' is already defined. Each `FunctionDeclaration` must have a unique name.)_make_toolslist_toolsr   r   rF   ry   )r   r   r   r   rF   s        r,   r   zFunctionLibrary.__init__  s    E""5kkK 	< 	<D#9 < <"'4;&&$ ]T  ]  ]  ]   1<K,--<	< 	<r.   rF   r   r$   r   c                T    t          |t                    s|j        }| j        |         S r'   r   r   s     r,   r   zFunctionLibrary.__getitem__  r   r.   r   r   protos.Part | Nonec                t    | |         }t          |          sd S  ||          }t          j        |          S )N)function_response)r   r   Part)r   r   r   r   s       r,   r   zFunctionLibrary.__call__  s@    2h$$ 	4;r??{X6666r.   c                $    d | j         D             S )Nc                6    g | ]}|                                 S r9   )r   )rB   r   s     r,   rE   z,FunctionLibrary.to_proto.<locals>.<listcomp>  s     888D888r.   )r   r   s    r,   r   zFunctionLibrary.to_proto  s    88DK8888r.   N)r   r   r   )r   r   r$   r   )rQ   r   r   rR   r   r   r   r   r9   r.   r,   r   r     se        cc< < < <! ! ! !7 7 7 79 9 9 9 9r.   r   r   	ToolsType
list[Tool]c                "   t          | t                    rit          | t                    sTd | D             } t          |           dk    r3t	          d | D                       rt          d | D                       g} | S | }t          |          gS )Nc                ,    g | ]}t          |          S r9   )r   rB   ts     r,   rE   z_make_tools.<locals>.<listcomp>  s    ...1A...r.   r   c              3  F   K   | ]}t          |j                  d k    V  dS )r   N)rx   r   r   s     r,   	<genexpr>z_make_tools.<locals>.<genexpr>  s2      !S!S#a&=">">!"C!S!S!S!S!S!Sr.   c                (    g | ]}|j         d          S )r   r   r   s     r,   rE   z_make_tools.<locals>.<listcomp>  s      K K K!8!; K K Kr.   )r(   r   r   rx   allr   )r   r   s     r,   r   r     s    %"" ":eW+E+E ".....u::>>c!S!SU!S!S!SSS> K KU K K KLLME4  !!r.   libFunctionLibraryType | NoneFunctionLibrary | Nonec                X    | | S t          | t                    r| S t          |           S )N)r   )r(   r   )r   s    r,   to_function_libraryr     s4    
{
	C	)	) *
S))))r.   	mode_autoautomode_anyany	mode_nonenoneFunctionCallingModeTypeFunctionCallingModec                n    t          | t                    r|                                 } t          |          S r'   )r(   r)   r*   _FUNCTION_CALLING_MODEr+   s    r,   to_function_calling_moder  ;  s,    !S GGII!!$$r.   c                  $    e Zd ZU ded<   ded<   dS )FunctionCallingConfigDictr  modez	list[str]allowed_function_namesNr   r9   r.   r,   r  r  A  s*         !!!!%%%%%%r.   r  objFunctionCallingConfigTypeprotos.FunctionCallingConfigc                   t          | t          j                  r| S t          | t          t          t
          f          rdt          |           i} nwt          | t                    r<|                                 } | 	                    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   FunctionCallingConfigr  r)   intr  rI   r   r[   r   rt   rQ   )r  r  s     r,   to_function_calling_configr  K  s    #v344 

	C-sC8	9	9 
/445	C		 	
hhjjwwv.t44F5#Cyy15 5 5
 
 	
 ',,,r.   c                      e Zd ZU ded<   dS )ToolConfigDictr  function_calling_configNr   r9   r.   r,   r  r  ^  s         666666r.   r  ToolConfigTypeprotos.ToolConfigc                "   t          | t          j                  r| S t          | t                    r:|                     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   r9   )	r(   r   
ToolConfigrI   r[   r  r   rt   rQ   )r  fccs     r,   to_tool_configr  e  s    #v()) 

	C		 	
gg/00(--),%& ''3'''4"3ii04 4 4
 
 	
r.   )r"   r#   r$   r%   )r1   r2   r/   r3   r0   r4   r$   r5   )rV   r5   )r   r   r$   r   )r   r   r$   r   )r   r   r$   r   )r   r   r$   r   )r   r   r$   r   )r"   r  r$   r  )r  r  r$   r  )r  r  r$   r  )L
__future__r   collections.abcr   r   r   r;   typingr   r   r	   typing_extensionsr
   rL   google.generativeair   google.generativeai.typesr   r%   r  r)   r#   TYPE_UNSPECIFIEDSTRINGNUMBERINTEGERBOOLEANARRAYOBJECTr!   r   r-   rW   rP   r\   r_   r`   r^   r]   r   r   rI   
StructTypefloatboolr   r   r   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  r9   r.   r,   <module>r5     s9   # " " " " " " 7 7 7 7 7 7 7 7 7 7   ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '  & & & & & & 3 3 3 3 3 3{CdN#'40't' -' 4(	'
 	K' t{' 4;' dk' 	K' t{' 4;' dk' 	L$,' t|' DL'  t|!'" 	L$,#'$ t|DLt|J
tz$*TZKt{4;dk9' '
    @    .2%)	J J J J J JZ  .  B
 
 

+ 
+ 
+
 
 
# # #<   8/H /H /H /H /H /H /H /Hd #{"#
%dJ[0A4GH	F F F F F"5 F F F0  
cNS#X 
 
 
 
&   % % % % % % % %P9 9 9 9 9y 9 9 9 &+x*A!BD[[
   .!9 !9 !9 !9 !9 !9 !9 !9H (8$h./		" 	" 	" 	" OY67 * * * * 27  16$)
$04#'	"16$)
$ $   3S# => % % % %& & & & &	 & & &
 "68TT 
- - - -&7 7 7 7 7 7 7 7 ~v'889
 
 
 
 
 
r.   