o
    ß­j`A  ã                   @  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*   úZ/var/www/html/CropPilot/venv/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__ÚschemaÚpopr@   r<   ÚtypingÚ
get_originr   ÚtypeÚget_argsÚ__doc__)	r0   r.   r/   Úfields_dictrN   rB   Úfunction_argr<   rT   r*   )rK   r.   r+   Ú_generate_schemaE   s2   
ñ
ÿ€


þr]   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 )	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,   ra   rQ   )rT   r^   r`   rQ   rG   r*   r*   r+   ra   Ÿ   s&   ra   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
   rd   ra   Ú_proto)ÚselfrB   r5   rN   r*   r*   r+   Ú__init__¼   s   
ÿzFunctionDeclaration.__init__r"   c                 C  ó   | j jS r%   )rg   rB   ©rh   r*   r*   r+   rB   Â   ó   zFunctionDeclaration.namec                 C  rj   r%   )rg   r5   rk   r*   r*   r+   r5   Æ   rl   zFunctionDeclaration.descriptionúprotos.Schemac                 C  rj   r%   )rg   rN   rk   r*   r*   r+   rN   Ê   rl   zFunctionDeclaration.parametersc                 C  s   | ddi d�}||_ |S )NÚ rM   ©rg   )ÚclsÚprotorh   r*   r*   r+   Ú
from_protoÎ   s   zFunctionDeclaration.from_protoúprotos.FunctionDeclarationc                 C  ó   | j S r%   ro   rk   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   rw   r*   )r]   ÚCallableFunctionDeclaration)rw   r.   rT   r*   r*   r+   Úfrom_function×   s   z!FunctionDeclaration.from_function)rB   r'   r5   r'   rN   rf   )r"   r'   )r"   rm   )r"   rd   )r"   rs   r%   )rw   r1   r.   rx   )rS   Ú
__module__Ú__qualname__ri   ÚpropertyrB   r5   rN   Úclassmethodrr   ru   Ústaticmethodrz   r*   r*   r*   r+   rd   »   s    
rd   Ú	ValueTypec                      s2   e Zd ZdZddœd‡ fdd„Zddd„Z‡  ZS )ry   z An extension of `FunctionDeclaration` that can be built from a Python function, and is callable.

    Note: The Python function must have type annotations.
    Nre   rB   r'   r5   rN   rf   rw   r1   c                  s   t ƒ j|||d� || _d S )NrM   )Úsuperri   rw   )rh   rB   r5   rN   rw   ©Ú	__class__r*   r+   ri   ÷   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*   )rw   Úargsr&   rO   r
   ÚFunctionResponserB   )rh   r„   r‡   r*   r*   r+   Ú__call__  s   
z$CallableFunctionDeclaration.__call__)rB   r'   r5   r'   rN   rf   rw   r1   )r„   r…   r"   r†   )rS   r{   r|   rZ   ri   r‹   Ú__classcell__r*   r*   r‚   r+   ry   ñ   s
    
ûry   .ÚfunÚFunctionDeclarationTypeú0FunctionDeclaration | 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�| ƒ‚)Nrw   z_Invalid argument type: Expected an instance of `genai.FunctionDeclarationType`. Received type: Ú.r*   )
r&   rd   r
   rO   ry   Úcallablerz   Ú	TypeErrorrX   rS   )r�   r*   r*   r+   Ú_make_function_declaration  s   

þr“   Úfdrs   c                 C  s   t | tjƒr| S |  ¡ S r%   )r&   r
   rd   ru   )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>rn   c                 S  r™   r*   )r•   )rA   r”   r*   r*   r+   rL   9  rš   ©r—   )Ú_function_declarationsÚ_indexrB   Ú
ValueErrorr
   r–   rg   )rh   r—   r”   rB   r*   r*   r+   ri   .  s   

ÿzTool.__init__r"   ú6list[FunctionDeclaration | protos.FunctionDeclaration]c                 C  rt   r%   )rœ   rk   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�   ©rh   rB   r*   r*   r+   Ú__getitem__@  ó   

zTool.__getitem__r„   r…   úprotos.FunctionResponse | Nonec                 C  s   | | }t |ƒs
d S ||ƒS r%   )r‘   )rh   r„   Údeclarationr*   r*   r+   r‹   H  s   zTool.__call__c                 C  rt   r%   ro   rk   r*   r*   r+   ru   O  rv   zTool.to_protoN)r—   r˜   )r"   rŸ   ©rB   r    r"   r�   )r„   r…   r"   r¦   )
rS   r{   r|   rZ   ri   r}   r—   r¤   r‹   ru   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   rd   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ž   )rh   r´   r®   r§   rB   r*   r*   r+   ri   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)rh   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*   )ru   )rA   r®   r*   r*   r+   rL   ”  rš   z,FunctionLibrary.to_proto.<locals>.<listcomp>)r¸   rk   r*   r*   r+   ru   “  s   zFunctionLibrary.to_protoN)r´   rµ   r¨   )r„   r…   r"   r¹   )rS   r{   r|   rZ   ri   r¤   r‹   ru   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   rc   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   )rT   r4   )r�   rŽ   r"   r�   )r”   r�   r"   rs   )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]   ra   rd   rO   Ú
StructTypeÚfloatÚboolr·   r€   ry   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

ýÿ

(ÿ
$

	ò

ÿ
