
    rh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j0                  ej0                  dej0                  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j2                  dej4                  dej4                  ej6                  ej6                  dej6                  dej6                  dej6                  ej8                  ej8                  dej8                  dej8                  dej8                  iZded<   dFdZddd	 	 	 	 	 	 	 dGd Z d! Z!d" Z"d# Z#d$ Z$d% Z%d& Z&dHd'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jP                  e)eef   e	d-ef   f   Z0	 	 	 	 dId.Z1dJd/Z2 G d0 d1      Z3 G d2 d3e      Z4e
e3ejf                  e4ee0   e0f   Z5dKd4Z6 G d5 d6      Z7e
ee5   e5f   Z8dLd7Z9e
e7e8f   Z:dMd8Z;ejx                  jz                  Z>d
e>j~                  e>j~                  e>j~                  d9e>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                  iZBe
e>eef   ZCdNd?ZD G d@ dAe      ZEe
eCeEejx                  f   ZFdOdBZG G dC dD      ZHe
eHej                  f   ZJdPdEZKy)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_TYPEc                T    t        | t              r| j                         } t        |    S N)
isinstancestrlowerr!   xs    p/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/google/generativeai/responder.pyto_typer*   @   s!    !SGGIa=    )descriptionsrequiredc                  |i }t        t        j                  |       j                        }i }|j	                         D ]  \  }}|j
                  t        j                  j                  t        j                  j                  t        j                  j                  fv s`t        j                  |j                  |d            }|j                  t        j                  j                  k7  r|j                  |f||<   t        |f||<    t!        | j"                  |      }|||d<   n|D 	cg c]  }	||	   j$                  t        j                  j                  k(  r^||	   j
                  t        j                  j                  t        j                  j                  t        j                  j                  fv r|	 c}	|d<   t        | j"                  | j&                        }
|d   r||
d<   |
S 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)descriptionr-   )namer/   
properties
parameters)dictinspect	signaturer2   itemskind	ParameterPOSITIONAL_OR_KEYWORDKEYWORD_ONLYPOSITIONAL_ONLYpydanticFieldget
annotationemptyr   _build_schema__name__default__doc__)fr,   r-   defaultsfields_dictr0   paramfieldr2   kschemas              r)   _generate_schemarL   F   s   * G%%a(334HK~~' /e::33**--
 
 NN(,,T48E 7#4#4#:#::$)$4$4e$;D!$'JD!+/. qzz;7J !)
:
 "
##w'8'8'>'>>QK$$%%;;%%22%%55	 "

: qzzqyy9F,)|M#"
s   -BG/c                *   t        j                  | fi |j                         }|j                  di       }|j	                         D ]  \  }}t        ||        t        ||       t        |       t        |       t        |       t        |       |S )Nz$defs)
r<   create_modelmodel_json_schemapopr6   unpack_defsconvert_to_nullableadd_object_typestrip_titlesstrip_additional_properties)fnamerG   r2   defsr0   values         r)   rA   rA      s    &&u<<NNPJ>>'2&Dzz| !eE4 !
D! 
#J
 
+r+   c                F   | j                  dd       }|y |j                         D ]  \  }}|j                  dd       }|)||j                  d      d      }t        ||       |||<   C|j                  dd       }|Qt	        |      D ]B  \  }}	|	j                  dd       }|||j                  d      d      }t        ||       |||<   D |j                  dd       }
|
|
j                  dd       }|||j                  d      d      }t        ||       ||d<    y )Nr1   z$refzdefs/anyOfr6   )r>   r6   splitrQ   	enumerate)rK   rW   r1   r0   rX   ref_keyrefanyofiatyper6   s              r)   rQ   rQ      sJ   L$/J!'') e))FD)w}}W-b12CT""Jt		'4(%e, #5))FD1&w}}W5b9:CT*"E!H# 		'4(ii-G"7==1"56C&!$g3r+   c                    | j                  dd       }| j                  dd       }|#|j                         D ]  \  }}t        |        | j                  dd       }|t        |       y y )Ntitler1   r6   )rP   r>   r6   rT   )rK   rd   r1   r0   rX   r6   s         r)   rT   rT      sq    JJw%EL$/J%++- 	 KD%	  JJw%EU r+   c                    | j                  dd        | j                  dd       }|#|j                         D ]  \  }}t        |        | j                  dd       }|t        |       y y )NadditionalPropertiesr1   r6   )rP   r>   r6   rU   rK   r1   r0   rX   r6   s        r)   rU   rU      sp    
JJ%t,L$/J%++- 	/KD%'.	/ JJw%E#E* r+   c                    | j                  dd       }|:| j                  dd        d| d<   |j                         D ]  \  }}t        |        | j                  dd       }|t        |       y y )Nr1   r-   r    typer6   )r>   rP   r6   rS   rg   s        r)   rS   rS      sx    L$/J

:t$!v%++- 	#KD%E"	# JJw%E r+   c                   | j                  dd       }|`t        |      dk7  rt        d      |\  }}|ddik(  r| j                  |       n$|ddik(  r| j                  |       nt        d      d| d<   | j	                  dd       }|#|j                         D ]  \  }}t        |        | j	                  d	d       }|t        |       y y )
Nr[   r   zInvalid input: Type Unions are not supported, except for `Optional` types. Please provide an `Optional` type or a non-Union type.ri   nullTnullabler1   r6   )rP   len
ValueErrorupdater>   r6   rR   )rK   r`   abr1   r0   rX   r6   s           r)   rR   rR      s    JJw%Eu:?I  1  MM!66""MM!I  "zL$/J%++- 	'KD%&	' JJw%EE" r+   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_r6   r1   )copyrP   r>   r*   _rename_schema_fieldsr6   )rK   rs   ru   r6   r1   rJ   vs          r)   rw   rw     s    ~[[]FJJvt$EwJJw%E!%.wjj4(G#yJJw%E/6wL$/JHRHXHXHZ[1#8#; ;[|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r2   c               P    t        j                  ||t        |            | _        y)zmA  class wrapping a `protos.FunctionDeclaration`, describes a function for `genai.GenerativeModel`'s `tools`.r0   r/   r2   N)r   rz   rw   _proto)selfr0   r/   r2   s       r)   __init__zFunctionDeclaration.__init__-  s"    00;;PQ[;\
r+   c                .    | j                   j                  S r#   )r~   r0   r   s    r)   r0   zFunctionDeclaration.name3  s    {{r+   c                .    | j                   j                  S r#   )r~   r/   r   s    r)   r/   zFunctionDeclaration.description7  s    {{&&&r+   c                .    | j                   j                  S r#   )r~   r2   r   s    r)   r2   zFunctionDeclaration.parameters;  s    {{%%%r+   c                *     | ddi       }||_         |S )N r}   r~   )clsprotor   s      r)   
from_protozFunctionDeclaration.from_proto?  s    r:r+   c                    | j                   S r#   r   r   s    r)   to_protozFunctionDeclaration.to_protoE      {{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 )rL   CallableFunctionDeclaration)r   r,   rK   s      r)   from_functionz!FunctionDeclaration.from_functionH  s.     L!(F*GVGhGGr+   )r0   r%   r/   r%   r2   dict[str, Any] | None)returnr%   )r   zprotos.Schema)r   rz   )r   protos.FunctionDeclarationr#   )r   Callable[..., Any]r,   zdict[str, str] | None)rB   
__module____qualname__r   propertyr0   r/   r2   classmethodr   r   staticmethodr   r   r+   r)   rz   rz   ,  su    [_ 
     ' ' & &  
 H Hr+   rz   	ValueTypec                  @     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{   c               8    t         |   |||       || _        y )Nr}   )superr   r   )r   r0   r/   r2   r   	__class__s        r)   r   z$CallableFunctionDeclaration.__init__h  s      	d
S r+   c                     | j                   di |j                  }t        |t              sd|i}t	        j
                  |j                  |      S )Nresult)r0   responser   )r   argsr$   r3   r   FunctionResponser0   )r   fcr   s      r)   __call__z$CallableFunctionDeclaration.__call__s  sD    ))&$''F&&BGGfEEr+   )r0   r%   r/   r%   r2   r   r   r   )r   protos.FunctionCallr   zprotos.FunctionResponse)rB   r   r   rD   r   r   __classcell__)r   s   @r)   r   r   b  s@     -1	! 	! 		!
 *	! %	!Fr+   r   .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|       )Nr   z_Invalid argument type: Expected an instance of `genai.FunctionDeclarationType`. Received type: .r   )
r$   rz   r   r3   r   callabler   	TypeErrorri   rB   )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   rz   r   )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 )Nr   function_declarations)	r   _function_declarations_indexr0   rn   r   r   r   r~   )r   r   rE   r   r0   s        r)   r   zTool.__init__  s    Nc&d'A!'D&d#-- 	&B77Dt{{" n$#%DKK 		& kk<@<W<W"Xb:b>"X
 'e #Ys   B2B7c                    | j                   S r#   )r   r   s    r)   r   zTool.function_declarations  s    ***r+   c                X    t        |t              s|j                  }| j                  |   S r#   r$   r%   r0   r   r   r0   s     r)   __getitem__zTool.__getitem__  &     $$99D{{4  r+   c                4    | |   }t        |      sy  ||      S r#   )r   )r   r   declarations      r)   r   zTool.__call__  s     2h$2r+   c                    | j                   S r#   r   r   s    r)   r   zTool.to_proto  r   r+   N)r   z!Iterable[FunctionDeclarationType])r   z6list[FunctionDeclaration | protos.FunctionDeclaration]r0   zstr | protos.FunctionCallr   0FunctionDeclaration | protos.FunctionDeclaration)r   r   r   zprotos.FunctionResponse | None)
rB   r   r   rD   r   r   r   r   r   r   r   r+   r)   r   r     s=    f
 + +!-!	9!r+   r   c                      e Zd ZU ded<   y)ToolDictzlist[FunctionDeclarationType]r   NrB   r   r   __annotations__r   r+   r)   r   r     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   r   )r$   r   r   r   r3   rz   r   	Exceptionr   ri   rB   )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   r0   rn   )r   toolsr   r   r0   s        r)   r   zFunctionLibrary.__init__  s    E"5kKK 	<D#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)r   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   r   )r   r   s     r)   r   zFunctionLibrary.to_proto  s     ,0KK8D888s   )N)r   zIterable[ToolType]r   )r   r   r   zprotos.Part | None)rB   r   r   rD   r   r   r   r   r   r+   r)   r   r     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)rm   r   ).0ts     r)   	<genexpr>z_make_tools.<locals>.<genexpr>  s      !S#a&=&=">!"C!Ss   "$r   )r$   r   r   r   rm   allr   )r   r   r   s      r)   r   r     s    %":eW+E(-.1A..u:>c!SU!SSU K!8!8!; 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   r   r+   r)   r   r   A  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   r3   rv   rP   r   ri   rB   )objr   s     r)   to_function_calling_configr   K  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   r   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   r   )	r$   r   
ToolConfigr3   rP   r   r   ri   rB   )r   fccs     r)   to_tool_configr   e  s    #v(()
	C	gg/0(-),%&  '3''"3i0014
 	
r+   )r(   TypeOptionsr   Type)rE   r   r,   zMapping[str, str] | Noner-   zSequence[str] | Noner   dict[str, Any])rK   r  )r   FunctionDeclarationTyper   r   )r   r   r   r   )r   ToolTyper   r   )r   	ToolsTyper   z
list[Tool])r   zFunctionLibraryType | Noner   zFunctionLibrary | None)r(   r   r   r   )r   r   r   zprotos.FunctionCallingConfig)r   ToolConfigTyper   zprotos.ToolConfig)L
__future__r   collections.abcr   r   r   r4   typingr   r   r	   typing_extensionsr
   r<   google.generativeair   google.generativeai.typesr   r   r   r%   r   TYPE_UNSPECIFIEDSTRINGNUMBERINTEGERBOOLEANARRAYOBJECTr!   r   r*   rL   rA   rQ   rT   rU   rS   rR   rw   rz   r3   
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   r   r+   r)   <module>r     s   # 7 7   ' ' '  & 3{{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%)	JJ +J #	J
 JZ.B

+
#<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+   