o
    ZhkA                     @  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)protosZtype_unspecifiedunspecified   Ztype_stringstring   Ztype_numbernumber   Ztype_integerinteger   Ztype_booleanboolean   Z
type_arrayarray   Ztype_objectobjectzdict[TypeOptions, Type]
_TYPE_TYPExTypeOptionsreturnTypec                 C     t | tr	|  } t|  S N)
isinstancestrlowerr   r    r#   T/var/www/html/lang_env/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
propertiesTZ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#   )defaultr0   r1   r6   r/   r2   r3   r4   )r:   k)defaultsr#   r$   
<listcomp>   s    z$_generate_schema.<locals>.<listcomp>r;   r.   
parameters)dictr0   	signaturerF   itemsr7   Zcreate_model__name__Zmodel_json_schemapopr9   r5   typing
get_originr   typeget_args__doc__)	r)   r'   r(   fields_dictrF   r;   Zfunction_argr5   schemar#   )rC   r'   r$   _generate_schemaE   s2   




rS   rR   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   r@   c                 S  s   i | ]	\}}|t |qS r#   )_rename_schema_fields)r:   rB   vr#   r#   r$   r>      s    z)_rename_schema_fields.<locals>.<dictcomp>)copyrK   r9   r%   rW   rI   )rR   rT   rV   rI   r@   r#   r#   r$   rW      s&   rW   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   r;   r    r.   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`.rE   N)r
   rZ   rW   _proto)selfr;   r.   rF   r#   r#   r$   __init__   s   
zFunctionDeclaration.__init__r   c                 C     | j jS r   )r]   r;   r^   r#   r#   r$   r;         zFunctionDeclaration.namec                 C  r`   r   )r]   r.   ra   r#   r#   r$   r.      rb   zFunctionDeclaration.descriptionprotos.Schemac                 C  r`   r   )r]   rF   ra   r#   r#   r$   rF      rb   zFunctionDeclaration.parametersc                 C  s   | ddi d}||_ |S )N rE   r]   )clsprotor^   r#   r#   r$   
from_proto   s   zFunctionDeclaration.from_protoprotos.FunctionDeclarationc                 C     | j S r   re   ra   r#   r#   r$   to_proto      zFunctionDeclaration.to_protofunctionr*   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.
        Nr=   rm   r#   )rS   CallableFunctionDeclaration)rm   r'   rR   r#   r#   r$   from_function   s   z!FunctionDeclaration.from_function)r;   r    r.   r    rF   r\   )r   r    )r   rc   )r   rZ   )r   ri   r   )rm   r*   r'   rn   )rJ   
__module____qualname__r_   propertyr;   r.   rF   classmethodrh   rk   staticmethodrp   r#   r#   r#   r$   rZ      s    
rZ   	ValueTypec                      s2   e Zd ZdZddd fddZdddZ  ZS )ro   zAn extension of `FunctionDeclaration` that can be built from a Python function, and is callable.

    Note: The Python function must have type annotations.
    Nr[   r;   r    r.   rF   r\   rm   r*   c                  s   t  j|||d || _d S )NrE   )superr_   rm   )r^   r;   r.   rF   rm   	__class__r#   r$   r_      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)r;   responser#   )rm   argsr   rG   r
   ZFunctionResponser;   )r^   rz   r}   r#   r#   r$   __call__  s   
z$CallableFunctionDeclaration.__call__)r;   r    r.   r    rF   r\   rm   r*   )rz   r{   r   r|   )rJ   rq   rr   rP   r_   r   __classcell__r#   r#   rx   r$   ro      s
    
ro   .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| )Nrm   z_Invalid argument type: Expected an instance of `genai.FunctionDeclarationType`. Received type: .r#   )
r   rZ   r
   rG   ro   callablerp   	TypeErrorrN   rJ   )r   r#   r#   r$   _make_function_declaration  s   

r   fdri   c                 C  s   t | tjr| S |  S r   )r   r
   rZ   rk   )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:   r)   r#   r#   r$   rD   0      z!Tool.__init__.<locals>.<listcomp>rd   c                 S  r   r#   )r   )r:   r   r#   r#   r$   rD   9  r   r   )_function_declarations_indexr;   
ValueErrorr
   r   r]   )r^   r   r   r;   r#   r#   r$   r_   .  s   

zTool.__init__r   6list[FunctionDeclaration | protos.FunctionDeclaration]c                 C  rj   r   )r   ra   r#   r#   r$   r   <  s   zTool.function_declarationsr;   str | protos.FunctionCallr   c                 C     t |ts|j}| j| S r   r   r    r;   r   r^   r;   r#   r#   r$   __getitem__@     

zTool.__getitem__rz   r{   protos.FunctionResponse | Nonec                 C  s   | | }t |s
d S ||S r   )r   )r^   rz   declarationr#   r#   r$   r   H  s   zTool.__call__c                 C  rj   r   re   ra   r#   r#   r$   rk   O  rl   zTool.to_protoN)r   r   )r   r   r;   r   r   r   )rz   r{   r   r   )
rJ   rq   rr   rP   r_   rs   r   r   r   rk   r#   r#   r#   r$   r   +  s    


r   c                   @     e Zd ZU ded< dS )ToolDictzlist[FunctionDeclarationType]r   NrJ   rq   rr   __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   rG   rZ   r   	Exceptionr   rN   rJ   )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   r;   r   )r^   r   r   r   r;   r#   r#   r$   r_   v  s   




zFunctionLibrary.__init__r;   r   r   r   c                 C  r   r   r   r   r#   r#   r$   r     r   zFunctionLibrary.__getitem__rz   r{   protos.Part | Nonec                 C  s(   | | }t |s
d S ||}tj|dS )N)Zfunction_response)r   r
   ZPart)r^   rz   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#   )rk   )r:   r   r#   r#   r$   rD     r   z,FunctionLibrary.to_proto.<locals>.<listcomp>)r   ra   r#   r#   r$   rk     s   zFunctionLibrary.to_protoN)r   r   r   )rz   r{   r   r   )rJ   rq   rr   rP   r_   r   r   rk   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   r:   tr#   r#   r$   rD     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$   rD     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   Z	mode_autoautoZmode_anyanyZ	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]Z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   rG   rY   rK   r   rN   rJ   )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
   
ToolConfigrG   rK   r   r   rN   rJ   )r   Zfccr#   r#   r$   to_tool_config  s   

r   )r   r   r   r   )r)   r*   r'   r+   r(   r,   r   r-   )rR   r-   )r   r   r   r   )r   r   r   ri   )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   r0   rL   r   r   r   Ztyping_extensionsr	   r7   Zgoogle.generativeair
   r   r   r    r   ZTYPE_UNSPECIFIEDSTRINGNUMBERZINTEGERZBOOLEANZARRAYZOBJECTr   r   r%   rS   rW   rZ   rG   Z
StructTypefloatboolr   rv   ro   r   r   r   r   r   r   r   r   r   r   ZFunctionLibraryTyper   r   ZModer   ZAUTOANYNONEr   r   r   r   r   r   r   r   r   r   r#   r#   r#   r$   <module>   s   	

 	
Z2



(
$

	


