o
    j6hN                     @  s  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< dwd"d#Zddd$dxd,d-Z d.d/ Z!d0d1 Z"d2d3 Z#d4d5 Z$d6d7 Z%d8d9 Z&dyd;d<Z'G d=d> d>Z(e)ed?f Z*e
e+ee,e*e-d? df Z.G d@dA dAe(Z/e
e(ej(e)eef e	dBef f Z0dzdFdGZ1d{dJdKZ2G dLdM dMZ3G dNdO dOeZ4e
e3ej3e4ee0 e0f Z5d|dRdSZ6G dTdU dUZ7e
ee5 e5f Z8d}dYdZZ9e
e7e8f Z:d~d^d_Z;ej<j=Z>d
e>j?e>j?e>j?d`e>j?dae>j?de>j@e>j@e>j@dbe>j@dce>j@de>jAe>jAe>jAdde>jAdee>jAiZBe
e>eef ZCddhdiZDG djdk dkeZEe
eCeEej<f ZFddodpZGG dqdr drZHe
eHejIf ZJddudvZKdS )    )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                 C     t | tr	|  } t|  S N)
isinstancestrlowerr    r!    r+   l/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/generativeai/responder.pyto_type@      
r-   )descriptionsrequiredfCallable[..., Any]r/   Mapping[str, str] | Noner0   Sequence[str] | Nonedict[str, Any]c          	        s   |du ri }t t| j i }  D ]1\}}|jtjjtjjtjj	fv rEt
j||dd}|jtjjkr?|j|f||< qt|f||< qt| j|}|durU||d< n fdd D |d< t | j| jd}|d rp||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                   sB   g | ]} | j tjjkr | jtjjtjjtjjfv r|qS r+   )defaultinspect	ParameteremptykindPOSITIONAL_OR_KEYWORDKEYWORD_ONLYPOSITIONAL_ONLY).0kdefaultsr+   r,   
<listcomp>   s    z$_generate_schema.<locals>.<listcomp>)namer6   
properties
parameters)dictr8   	signaturerF   itemsr;   r9   r<   r=   r>   pydanticFieldget
annotationr:   r   _build_schema__name____doc__)	r1   r/   r0   fields_dictrD   paramfieldrF   schemar+   rA   r,   _generate_schemaF   s6   




rU   c                 C  sl   t j| fi | }|di }| D ]	\}}t|| qt|| t| t| t| t	| |S )Nz$defs)
rJ   create_modelmodel_json_schemapoprI   unpack_defsconvert_to_nullableadd_object_typestrip_titlesstrip_additional_properties)fnamerQ   rF   defsrD   valuer+   r+   r,   rN      s   
rN   c                 C  s  |  dd }|d u rd S | D ]x\}}| dd }|d ur1||dd  }t|| |||< q| dd }|d urat|D ] \}}	|	 dd }|d ur_||dd  }t|| |||< q?q| dd }
|
d ur|
 dd }|d ur||dd  }t|| ||d< qqd S )NrE   z$refzdefs/anyOfrI   )rL   rI   splitrY   	enumerate)rT   r_   rE   rD   r`   ref_keyrefanyofiatyperI   r+   r+   r,   rY      s<   


rY   c                 C  s^   |  dd }| dd }|d ur| D ]\}}t| q| dd }|d ur-t| d S d S )NtitlerE   rI   )rX   rL   rI   r\   )rT   rj   rE   rD   r`   rI   r+   r+   r,   r\         
r\   c                 C  s^   |  dd  | dd }|d ur| D ]\}}t| q| dd }|d ur-t| d S d S )NadditionalPropertiesrE   rI   )rX   rL   rI   r]   rT   rE   rD   r`   rI   r+   r+   r,   r]      rk   r]   c                 C  sf   |  dd }|d ur!| dd  d| d< | D ]\}}t| q|  dd }|d ur1t| d S d S )NrE   r0   r   typerI   )rL   rX   rI   r[   rm   r+   r+   r,   r[      s   
r[   c                 C  s   |  dd }|d ur8t|dkrtd|\}}|ddikr$| | n|ddikr0| | ntdd| d< | dd }|d urO| D ]\}}t| qF| d	d }|d ur_t| d S d S )
Nrb   r   zInvalid input: Type Unions are not supported, except for `Optional` types. Please provide an `Optional` type or a non-Union type.rn   nullTnullablerE   rI   )rX   len
ValueErrorupdaterL   rI   rZ   )rT   rg   abrE   rD   r`   rI   r+   r+   r,   rZ      s.   
rZ   rT   c                 C  s   | d u r| S |   } | dd }|d ur|| d< | dd }|d ur(t|| d< | dd }|d ur6|| d< | dd }|d urFt|| d< | dd }|d ur[dd | D | d< | S )	Nrn   type_formatformat_rI   rE   c                 S  s   i | ]	\}}|t |qS r+   )_rename_schema_fields)r?   r@   vr+   r+   r,   
<dictcomp>'  s    z)_rename_schema_fields.<locals>.<dictcomp>)copyrX   rL   r-   ry   rI   )rT   rv   rx   rI   rE   r+   r+   r,   ry     s&   ry   c                   @  sn   e Zd Zddddd	ZedddZedddZedddZed ddZ	d!ddZ
ed"d#ddZdS )$FunctionDeclarationNrF   rD   r(   r6   rF   dict[str, Any] | Nonec                C  s   t j||t|d| _dS )zmA  class wrapping a `protos.FunctionDeclaration`, describes a function for `genai.GenerativeModel`'s `tools`.rD   r6   rF   N)r
   r}   ry   _proto)selfrD   r6   rF   r+   r+   r,   __init__-  s   
zFunctionDeclaration.__init__r#   c                 C     | j jS r&   )r   rD   r   r+   r+   r,   rD   3     zFunctionDeclaration.namec                 C  r   r&   )r   r6   r   r+   r+   r,   r6   7  r   zFunctionDeclaration.descriptionprotos.Schemac                 C  r   r&   )r   rF   r   r+   r+   r,   rF   ;  r   zFunctionDeclaration.parametersc                 C  s   | ddi d}||_ |S )N r   r   )clsprotor   r+   r+   r,   
from_proto?  s   zFunctionDeclaration.from_protoprotos.FunctionDeclarationc                 C     | j S r&   r   r   r+   r+   r,   to_protoE     zFunctionDeclaration.to_protofunctionr2   r/   dict[str, str] | Nonec                 C  s.   |du ri }t | |d}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   r+   )rU   CallableFunctionDeclaration)r   r/   rT   r+   r+   r,   from_functionH  s   z!FunctionDeclaration.from_function)rD   r(   r6   r(   rF   r   )r#   r(   )r#   r   )r#   r}   )r#   r   r&   )r   r2   r/   r   )rO   
__module____qualname__r   propertyrD   r6   rF   classmethodr   r   staticmethodr   r+   r+   r+   r,   r}   ,  s    
r}   	ValueTypec                      s2   e Zd ZdZddd fddZdddZ  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~   rD   r(   r6   rF   r   r   r2   c                  s   t  j|||d || _d S )Nr   )superr   r   )r   rD   r6   rF   r   	__class__r+   r,   r   h  s   
z$CallableFunctionDeclaration.__init__fcprotos.FunctionCallr#   protos.FunctionResponsec                 C  s4   | j di |j}t|tsd|i}tj|j|dS )Nresult)rD   responser+   )r   argsr'   rG   r
   FunctionResponserD   )r   r   r   r+   r+   r,   __call__s  s   
z$CallableFunctionDeclaration.__call__)rD   r(   r6   r(   rF   r   r   r2   )r   r   r#   r   )rO   r   r   rP   r   r   __classcell__r+   r+   r   r,   r   b  s
    
r   .funFunctionDeclarationType0FunctionDeclaration | protos.FunctionDeclarationc                 C  sl   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: .r+   )
r'   r}   r
   rG   r   callabler   	TypeErrorrn   rO   )r   r+   r+   r,   _make_function_declaration  s   

r   fdr   c                 C  s   t | tjr| S |  S r&   )r'   r
   r}   r   )r   r+   r+   r,   
_encode_fd  s   r   c                   @  sD   e Zd ZdZdddZeddd	ZdddZdddZdd Z	dS )Toolz\A wrapper for `protos.Tool`, Contains a collection of related `FunctionDeclaration` objects.function_declarations!Iterable[FunctionDeclarationType]c                 C  sd   dd |D | _ i | _| j D ]}|j}|| jv rtd|| j|j< qtjdd | j D d| _d S )Nc                 S     g | ]}t |qS r+   )r   )r?   r1   r+   r+   r,   rC         z!Tool.__init__.<locals>.<listcomp>r   c                 S  r   r+   )r   )r?   r   r+   r+   r,   rC     r   r   )_function_declarations_indexrD   rr   r
   r   r   )r   r   r   rD   r+   r+   r,   r     s   

zTool.__init__r#   6list[FunctionDeclaration | protos.FunctionDeclaration]c                 C  r   r&   )r   r   r+   r+   r,   r     s   zTool.function_declarationsrD   str | protos.FunctionCallr   c                 C     t |ts|j}| j| S r&   r'   r(   rD   r   r   rD   r+   r+   r,   __getitem__     

zTool.__getitem__r   r   protos.FunctionResponse | Nonec                 C  s   | | }t |s
d S ||S r&   )r   )r   r   declarationr+   r+   r,   r     s   zTool.__call__c                 C  r   r&   r   r   r+   r+   r,   r     r   zTool.to_protoN)r   r   )r#   r   rD   r   r#   r   )r   r   r#   r   )
rO   r   r   rP   r   r   r   r   r   r   r+   r+   r+   r,   r     s    


r   c                   @     e Zd ZU ded< dS )ToolDictzlist[FunctionDeclarationType]r   NrO   r   r   __annotations__r+   r+   r+   r,   r        
 r   toolToolTypec              
   C  s   t | tr| S t | tjrt| jdS t | tr1d| v r#tdi | S | }ttjdi |gdS t | tr;t| dS zt| gdW S  ty[ } ztdt	| j
 d| |d }~ww )Nr   r   zPInvalid argument type: Expected an instance of `genai.ToolType`. Received type: r   r+   )r'   r   r
   r   rG   r}   r   	Exceptionr   rn   rO   )r   r   er+   r+   r,   
_make_tool  s,   



r   c                   @  s6   e Zd ZdZdddZdd
dZdddZdd ZdS )FunctionLibraryzYA container for a set of `Tool` objects, manages lookup and execution of their functions.toolsIterable[ToolType]c                 C  s`   t |}t|| _i | _| jD ]}|jD ]}|j}|| jv r&td| d|| j|j< qqd S )Nz2Invalid operation: A `FunctionDeclaration` named 'zI' is already defined. Each `FunctionDeclaration` must have a unique name.)_make_toolslist_toolsr   r   rD   rr   )r   r   r   r   rD   r+   r+   r,   r     s   




zFunctionLibrary.__init__rD   r   r#   r   c                 C  r   r&   r   r   r+   r+   r,   r     r   zFunctionLibrary.__getitem__r   r   protos.Part | Nonec                 C  s(   | | }t |s
d S ||}tj|dS )N)function_response)r   r
   Part)r   r   r   r   r+   r+   r,   r     s
   zFunctionLibrary.__call__c                 C  s   dd | j D S )Nc                 S  s   g | ]}|  qS r+   )r   )r?   r   r+   r+   r,   rC     r   z,FunctionLibrary.to_proto.<locals>.<listcomp>)r   r   r+   r+   r,   r     s   zFunctionLibrary.to_protoN)r   r   r   )r   r   r#   r   )rO   r   r   rP   r   r   r   r   r+   r+   r+   r,   r     s    


r   r   	ToolsType
list[Tool]c                 C  sf   t | tr,t | ts,dd | D } t| dkr*tdd | D r*tdd | D g} | S | }t|gS )Nc                 S  r   r+   )r   r?   tr+   r+   r,   rC     r   z_make_tools.<locals>.<listcomp>r   c                 s  s    | ]
}t |jd kV  qdS )r   N)rq   r   r   r+   r+   r,   	<genexpr>  s    z_make_tools.<locals>.<genexpr>c                 S  s   g | ]}|j d  qS )r   r   r   r+   r+   r,   rC     s    )r'   r   r   rq   allr   )r   r   r+   r+   r,   r     s   
r   libFunctionLibraryType | NoneFunctionLibrary | Nonec                 C  s$   | d u r| S t | tr| S t| dS )N)r   )r'   r   )r   r+   r+   r,   to_function_library  s
   

r   	mode_autoautomode_anyany	mode_nonenoneFunctionCallingModeTypeFunctionCallingModec                 C  r%   r&   )r'   r(   r)   _FUNCTION_CALLING_MODEr*   r+   r+   r,   to_function_calling_mode;  r.   r   c                   @  s   e Zd ZU ded< ded< dS )FunctionCallingConfigDictr   modez	list[str]allowed_function_namesNr   r+   r+   r+   r,   r   A  s   
 r   objFunctionCallingConfigTypeprotos.FunctionCallingConfigc                 C  sz   t | tjr| S t | tttfrdt| i} n!t | tr,|  } | 	d}t|| d< nt
dt| j d| t| S )Nr   zaInvalid argument type: Could not convert input to `protos.FunctionCallingConfig`. Received type: r   )r'   r
   FunctionCallingConfigr   r(   intr   rG   r|   rX   r   rn   rO   )r   r   r+   r+   r,   to_function_calling_configK  s   


r   c                   @  r   )ToolConfigDictr   function_calling_configNr   r+   r+   r+   r,   r   ^  r   r   ToolConfigTypeprotos.ToolConfigc                 C  sZ   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   r+   )	r'   r
   
ToolConfigrG   rX   r   r   rn   rO   )r   fccr+   r+   r,   to_tool_confige  s   

r   )r!   r"   r#   r$   )r1   r2   r/   r3   r0   r4   r#   r5   )rT   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   r8   typingr   r   r   typing_extensionsr	   rJ   google.generativeair
   google.generativeai.typesr   r$   r   r(   r"   TYPE_UNSPECIFIEDSTRINGNUMBERINTEGERBOOLEANARRAYOBJECTr    r   r-   rU   rN   rY   r\   r]   r[   rZ   ry   r}   rG   
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+   r,   <module>   s   	

 	M!
2



(
$

	


