o
    !gkA                     @  sD  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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< djd!d"Zddd#dkd+d,Zdld.d/ZG d0d1 d1Z e!ed2f Z"e
e#ee$e"e%d2 df Z&G d3d4 d4e Z'e
e ej e!eef e	d5ef f Z(dmd9d:Z)dnd=d>Z*G d?d@ d@Z+G dAdB dBeZ,e
e+ej+e,ee( e(f Z-dodEdFZ.G dGdH dHZ/e
ee- e-f Z0dpdLdMZ1e
e/e0f Z2dqdQdRZ3ej4j5Z6d	e6j7e6j7e6j7dSe6j7dTe6j7de6j8e6j8e6j8dUe6j8dVe6j8de6j9e6j9e6j9dWe6j9dXe6j9iZ:e
e6eef Z;drd[d\Z<G d]d^ d^eZ=e
e;e=ej4f Z>dsdbdcZ?G ddde deZ@e
e@ejAf ZBdtdhdiZCdS )u    )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_TYPExTypeOptionsreturnTypec                 C     t | tr	|  } t|  S N)
isinstancestrlowerr   r     r*   _/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/google/generativeai/responder.pyto_type?      
r,   )descriptionsrequiredfCallable[..., Any]r.   Mapping[str, str] | Noner/   Sequence[str] | Nonedict[str, Any]c          	        s   du ri |du rg }t t| j fdd  D }tj| jfi | }|	dd |
di  D ]$\}}|	dd  | j}t|tju r]tdt|v r]d|d< q9|re||d< n fd	d
 D |d< t | j| j|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.
    Nc              	     sZ   i | ])\}}|j tjjtjjtjjfv r||jtjjkr|jntt	j
 |d dfqS )N)description)kindinspect	ParameterPOSITIONAL_OR_KEYWORDKEYWORD_ONLYPOSITIONAL_ONLY
annotationemptyr   pydanticFieldget).0nameparamr.   r*   r+   
<dictcomp>_   s    
z$_generate_schema.<locals>.<dictcomp>title
propertiesTnullabler/   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*   )defaultr7   r8   r=   r6   r9   r:   r;   )rA   k)defaultsr*   r+   
<listcomp>   s    z$_generate_schema.<locals>.<listcomp>rB   r5   
parameters)dictr7   	signaturerN   itemsr>   create_model__name__model_json_schemapopr@   r<   typing
get_originr   typeget_args__doc__)	r0   r.   r/   fields_dictrN   rB   function_argr<   schemar*   )rK   r.   r+   _generate_schemaE   s2   




r^   r]   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 )	NrX   type_formatformat_rQ   rG   c                 S  s   i | ]	\}}|t |qS r*   )_rename_schema_fields)rA   rJ   vr*   r*   r+   rE      s    z)_rename_schema_fields.<locals>.<dictcomp>)copyrU   r@   r,   rb   rQ   )r]   r_   ra   rQ   rG   r*   r*   r+   rb      s&   rb   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rN   rB   r'   r5   rN   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`.rM   N)r
   re   rb   _proto)selfrB   r5   rN   r*   r*   r+   __init__   s   
zFunctionDeclaration.__init__r"   c                 C     | j jS r%   )rh   rB   ri   r*   r*   r+   rB         zFunctionDeclaration.namec                 C  rk   r%   )rh   r5   rl   r*   r*   r+   r5      rm   zFunctionDeclaration.descriptionprotos.Schemac                 C  rk   r%   )rh   rN   rl   r*   r*   r+   rN      rm   zFunctionDeclaration.parametersc                 C  s   | ddi d}||_ |S )N rM   rh   )clsprotori   r*   r*   r+   
from_proto   s   zFunctionDeclaration.from_protoprotos.FunctionDeclarationc                 C     | j S r%   rp   rl   r*   r*   r+   to_proto      zFunctionDeclaration.to_protofunctionr1   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.
        NrD   rx   r*   )r^   CallableFunctionDeclaration)rx   r.   r]   r*   r*   r+   from_function   s   z!FunctionDeclaration.from_function)rB   r'   r5   r'   rN   rg   )r"   r'   )r"   rn   )r"   re   )r"   rt   r%   )rx   r1   r.   ry   )rS   
__module____qualname__rj   propertyrB   r5   rN   classmethodrs   rv   staticmethodr{   r*   r*   r*   r+   re      s    
re   	ValueTypec                      s2   e Zd ZdZddd fddZdddZ  ZS )rz   zAn extension of `FunctionDeclaration` that can be built from a Python function, and is callable.

    Note: The Python function must have type annotations.
    Nrf   rB   r'   r5   rN   rg   rx   r1   c                  s   t  j|||d || _d S )NrM   )superrj   rx   )ri   rB   r5   rN   rx   	__class__r*   r+   rj      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)rB   responser*   )rx   argsr&   rO   r
   FunctionResponserB   )ri   r   r   r*   r*   r+   __call__  s   
z$CallableFunctionDeclaration.__call__)rB   r'   r5   r'   rN   rg   rx   r1   )r   r   r"   r   )rS   r|   r}   rZ   rj   r   __classcell__r*   r*   r   r+   rz      s
    
rz   .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| )Nrx   z_Invalid argument type: Expected an instance of `genai.FunctionDeclarationType`. Received type: .r*   )
r&   re   r
   rO   rz   callabler{   	TypeErrorrX   rS   )r   r*   r*   r+   _make_function_declaration  s   

r   fdrt   c                 C  s   t | tjr| S |  S r%   )r&   r
   re   rv   )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   )rA   r0   r*   r*   r+   rL   0      z!Tool.__init__.<locals>.<listcomp>ro   c                 S  r   r*   )r   )rA   r   r*   r*   r+   rL   9  r   r   )_function_declarations_indexrB   
ValueErrorr
   r   rh   )ri   r   r   rB   r*   r*   r+   rj   .  s   

zTool.__init__r"   6list[FunctionDeclaration | protos.FunctionDeclaration]c                 C  ru   r%   )r   rl   r*   r*   r+   r   <  s   zTool.function_declarationsrB   str | protos.FunctionCallr   c                 C     t |ts|j}| j| S r%   r&   r'   rB   r   ri   rB   r*   r*   r+   __getitem__@     

zTool.__getitem__r   r   protos.FunctionResponse | Nonec                 C  s   | | }t |s
d S ||S r%   )r   )ri   r   declarationr*   r*   r+   r   H  s   zTool.__call__c                 C  ru   r%   rp   rl   r*   r*   r+   rv   O  rw   zTool.to_protoN)r   r   )r"   r   rB   r   r"   r   )r   r   r"   r   )
rS   r|   r}   rZ   rj   r~   r   r   r   rv   r*   r*   r*   r+   r   +  s    


r   c                   @     e Zd ZU ded< dS )ToolDictzlist[FunctionDeclarationType]r   NrS   r|   r}   __annotations__r*   r*   r*   r+   r   S     
 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   rO   re   r   	Exceptionr   rX   rS   )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   rB   r   )ri   r   r   r   rB   r*   r*   r+   rj   v  s   




zFunctionLibrary.__init__rB   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)ri   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*   )rv   )rA   r   r*   r*   r+   rL     r   z,FunctionLibrary.to_proto.<locals>.<listcomp>)r   rl   r*   r*   r+   rv     s   zFunctionLibrary.to_protoN)r   r   r   )r   r   r"   r   )rS   r|   r}   rZ   rj   r   r   rv   r*   r*   r*   r+   r   s  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   rA   tr*   r*   r+   rL     r   z_make_tools.<locals>.<listcomp>r   c                 s  s    | ]
}t |jd kV  qdS )r   N)lenr   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+   rL     s    )r&   r   r   r   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     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   rO   rd   rU   r   rX   rS   )r   r   r*   r*   r+   to_function_calling_config  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
   
ToolConfigrO   rU   r   r   rX   rS   )r   fccr*   r*   r+   to_tool_config  s   

r   )r    r!   r"   r#   )r0   r1   r.   r2   r/   r3   r"   r4   )r]   r4   )r   r   r"   r   )r   r   r"   rt   )r   r   r"   r   )r   r   r"   r   )r   r   r"   r   )r    r   r"   r   )r   r   r"   r   )r   r   r"   r   )D
__future__r   collections.abcr   r   r   r7   rV   r   r   r   typing_extensionsr	   r>   google.generativeair
   r#   r   r'   r!   TYPE_UNSPECIFIEDSTRINGNUMBERINTEGERBOOLEANARRAYOBJECTr   r   r,   r^   rb   re   rO   
StructTypefloatboolr   r   rz   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   	

 	
Z2



(
$

	


