o
    ß­j”5  ã                   @  sþ  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZ d dlZd dlm  mZ d dlm  mZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dl Z!d dl"Z!zd dl#m$Z$ e$j%Z%W n e&y€   dZ%Y nw dZ'dZ(ej)dd„ ƒZ*G dd„ dej+ƒZ+G dd„ dej,ƒZ,ej-G dd„ dƒƒZ.dddddddœdNd$d%„Z/e.ƒ Z0e0 /¡  dOd(d)„Z1dPd+d,„Z2dQd.d/„Z3dRd1d2„Z4dSd4d5„Z5dTd7d8„Z6dUd:d;„Z7dVd=d>„Z8dWd@dA„Z9dXdCdD„Z:dYdFdG„Z;dZdIdJ„Z<d[dLdM„Z=dS )\é    )ÚannotationsN)ÚAnyÚcast)ÚSequence)Úcredentials)Ú
exceptions)Úauth)Úclient_options)Úgapic_v1)Úoperations_v1)Úversionz0.0.0zgenai-pyz9https://generativelanguage.googleapis.com/$discovery/restc                  c  s@   � t jj} dtjv rdd„ t j_z
d V  W | t j_d S | t j_w )NÚCOLAB_RELEASE_TAGc                  _  s   dS )N)NN© )ÚargsÚkwargsr   r   úW/var/www/html/CropPilot/venv/lib/python3.10/site-packages/google/generativeai/client.pyÚ<lambda>(   s    z-patch_colab_gce_credentials.<locals>.<lambda>)r   Ú_defaultÚ_get_gce_credentialsÚosÚenviron)Úget_gcer   r   r   Úpatch_colab_gce_credentials$   s   €
r   c                      s:   e Zd Z‡ fdd„Zdd„ Zdddddœddd„Z‡  ZS )ÚFileServiceClientc                   s   d | _ tƒ j|i |¤Ž d S ©N)Ú_discovery_apiÚsuperÚ__init__©Úselfr   r   ©Ú	__class__r   r   r   1   s   zFileServiceClient.__init__c                 C  sh   | j j}|d u rtdƒ‚tjjt ¡ dd„ t› d|› �d�}| 	¡ \}}| 
d¡}tjj||d�| _d S )NzaInvalid operation: Uploading to the File API requires an API key. Please provide a valid API key.c                 S  s   | |fS r   r   )ÚrespÚcontentr   r   r   r   >   s    z8FileServiceClient._setup_discovery_api.<locals>.<lambda>z?version=v1beta&key=)ÚhttpÚpostprocÚurizutf-8)ÚdeveloperKey)Ú_client_optionsÚapi_keyÚ
ValueErrorÚgoogleapiclientr$   ÚHttpRequestÚhttplib2ÚHttpÚGENAI_API_DISCOVERY_URLÚexecuteÚdecodeÚ	discoveryÚbuild_from_documentr   )r   r)   ÚrequestÚresponser#   Údiscovery_docr   r   r   Ú_setup_discovery_api5   s   ÿý
ÿz&FileServiceClient._setup_discovery_apiNT)Ú	mime_typeÚnameÚdisplay_nameÚ	resumableÚpathú str | pathlib.Path | os.PathLiker8   ú
str | Noner9   r:   r;   ÚboolÚreturnúprotos.Filec          
      C  s~   | j d u r	|  ¡  i }|d ur||d< |d ur||d< tjj|||d�}| j  ¡ jd|i|d�}| ¡ }	|  d|	d d i¡S )Nr9   ÚdisplayName)ÚfilenameÚmimetyper;   Úfile)ÚbodyÚ
media_body)	r   r7   r+   r$   ÚMediaFileUploadÚmediaÚuploadr0   Úget_file)
r   r<   r8   r9   r:   r;   rE   rI   r4   Úresultr   r   r   Úcreate_fileH   s   
	ÿzFileServiceClient.create_file)r<   r=   r8   r>   r9   r>   r:   r>   r;   r?   r@   rA   )Ú__name__Ú
__module__Ú__qualname__r   r7   rM   Ú__classcell__r   r   r    r   r   0   s    ùr   c                   @  s   e Zd Zdd„ ZdS )ÚFileServiceAsyncClientc                 Ï  s
   �t dƒ‚)NzPThe `create_file` method is currently not supported for the asynchronous client.)ÚNotImplementedErrorr   r   r   r   rM   d   s   €ÿz"FileServiceAsyncClient.create_fileN)rN   rO   rP   rM   r   r   r   r   rR   c   s    rR   c                   @  sŽ   e Zd ZU ejed�Zded< dZded< dZ	ded	< dZ
d
ed< ejed�Zded< dddddddœd"dd„Zdd„ Zdd„ Zd#d d!„ZdS )$Ú_ClientManager)Údefault_factoryzdict[str, Any]Úclient_configr   úSequence[tuple[str, str]]Údefault_metadataNzglm.DiscussServiceClient | NoneÚdiscuss_clientz$glm.DiscussServiceAsyncClient | NoneÚdiscuss_async_clientÚclients©r)   r   Ú	transportr	   Úclient_inforX   r)   r>   r   ú(ga_credentials.Credentials | dict | Noner]   r	   ú8client_options_lib.ClientOptions | dict[str, Any] | Noner^   ú&gapic_v1.client_info.ClientInfo | Noner@   ÚNonec          
      C  sä   t |tƒr
t |¡}|du rt ¡ }ttj|ƒ}t|ddƒ}|r)|dur(tdƒ‚n|du r2t 	d¡}||_
t› dt› �}|rP|jrL| jd|› �7  _n||_ntjj|d�}||||dœ}	d	d
„ |	 ¡ D ƒ}	|	| _|| _i | _dS )aù  Initializes default client configurations using specified parameters or environment variables.

        If no API key has been provided (either directly, or on `client_options`) and the
        `GOOGLE_API_KEY` environment variable is set, it will be used as the API key.

        Note: Not all arguments are detailed below. Refer to the `*ServiceClient` classes in
        `google.ai.generativelanguage` for details on the other arguments.

        Args:
            transport: A string, one of: [`rest`, `grpc`, `grpc_asyncio`].
            api_key: The API-Key to use when creating the default clients (each service uses
                a separate client). This is a shortcut for `client_options={"api_key": api_key}`.
                If omitted, and the `GOOGLE_API_KEY` environment variable is set, it will be
                used.
            default_metadata: Default (key, value) metadata pairs to send with every request.
                when using `transport="rest"` these are sent as HTTP headers.
        Nr)   z`Invalid configuration: Please set either `api_key` or `client_options['api_key']`, but not both.ÚGOOGLE_API_KEYú/ú )Ú
user_agent)r   r]   r	   r^   c                 S  s   i | ]\}}|d ur||“qS r   r   )Ú.0ÚkeyÚvaluer   r   r   Ú
<dictcomp>¹   s    z,_ClientManager.configure.<locals>.<dictcomp>)Ú
isinstanceÚdictÚclient_options_libÚ	from_dictÚClientOptionsr   Úgetattrr*   r   Úgetenvr)   Ú
USER_AGENTÚ__version__rf   r
   r^   Ú
ClientInfoÚitemsrV   rX   r[   )
r   r)   r   r]   r	   r^   rX   Úhad_api_key_valuerf   rV   r   r   r   Ú	configures   s<   
!
ÿÿ
ü
z_ClientManager.configurec           	   
     s&  |dkrt }n&|dkrt}n| d¡r$| d¡d }tt| ¡ d ƒ}n	tt| ¡ d ƒ}ˆ js3tƒ  zt	ƒ � |di ˆ j¤Ž}W d   ƒ n1 sJw   Y  W n t
jyb } zd|_|‚d }~ww ˆ jsh|S d	d
„ }‡ fdd„}|j ¡ D ]\}}|||ƒs�qwt||ƒ}||ƒ}t|||ƒ qw|S )NrE   Ú
file_asyncÚ_asyncÚ_r   ÚServiceAsyncClientÚServiceClient)a  
  No API_KEY or ADC found. Please either:
    - Set the `GOOGLE_API_KEY` environment variable.
    - Manually pass the key with `genai.configure(api_key=my_api_key)`.
    - Or set up Application Default Credentials, see https://ai.google.dev/gemini-api/docs/oauth for more information.c                 S  sJ   |   d¡rdS | dkrdS t|tjƒsdS t|tƒrdS t|tƒr#dS dS )Nrz   FrM   T)Ú
startswithrk   ÚtypesÚFunctionTypeÚclassmethodÚstaticmethod)r9   Úfr   r   r   ÚkeepÞ   s   


z(_ClientManager.make_client.<locals>.keepc                   s   ddœ‡ ‡fdd„
}|S )Nr   )Úmetadatac                   s(   t | ƒt ˆjƒ } ˆ |i |¤d| i¤ŽS )Nr„   )ÚlistrX   )r„   r   r   )r‚   r   r   r   Úcallí   s   zN_ClientManager.make_client.<locals>.add_default_metadata_wrapper.<locals>.callr   )r‚   r†   ©r   )r‚   r   Úadd_default_metadata_wrapperì   s   z@_ClientManager.make_client.<locals>.add_default_metadata_wrapperr   )r   rR   ÚendswithÚsplitrp   ÚglmÚtitlerV   rw   r   Úga_exceptionsÚDefaultCredentialsErrorr   rX   Ú__dict__ru   Úsetattr)	r   r9   ÚclsÚclientÚerƒ   rˆ   ri   r‚   r   r‡   r   Úmake_clientÀ   s>   
ÿ€€ù	

z_ClientManager.make_clientc                 C  sD   |  ¡ }|dkr|  ¡ S | j |¡}|d u r |  |¡}|| j|< |S ©NÚ
operations)ÚlowerÚget_default_operations_clientr[   Úgetr”   )r   r9   r’   r   r   r   Úget_default_clientü   s   

z!_ClientManager.get_default_clientúoperations_v1.OperationsClientc                 C  s6   | j  dd ¡}|d u r|  d¡}|jj}|| j d< |S )Nr–   ÚModel)r[   r™   rš   Ú
_transportÚoperations_client)r   r’   Úmodel_clientr   r   r   r˜     s   

z,_ClientManager.get_default_operations_client)r)   r>   r   r_   r]   r>   r	   r`   r^   ra   rX   rW   r@   rb   ©r@   r›   )rN   rO   rP   ÚdataclassesÚfieldrl   rV   Ú__annotations__rX   rY   rZ   r[   rw   r”   rš   r˜   r   r   r   r   rT   j   s    
 òM<rT   r   r\   r)   r>   r   r_   r]   r	   ú.client_options_lib.ClientOptions | dict | Noner^   ra   rX   rW   c                 C  s   t j| |||||d�S )a�  Captures default client configuration.

    If no API key has been provided (either directly, or on `client_options`) and the
    `GOOGLE_API_KEY` environment variable is set, it will be used as the API key.

    Note: Not all arguments are detailed below. Refer to the `*ServiceClient` classes in
    `google.ai.generativelanguage` for details on the other arguments.

    Args:
        transport: A string, one of: [`rest`, `grpc`, `grpc_asyncio`].
        api_key: The API-Key to use when creating the default clients (each service uses
            a separate client). This is a shortcut for `client_options={"api_key": api_key}`.
            If omitted, and the `GOOGLE_API_KEY` environment variable is set, it will be
            used.
        default_metadata: Default (key, value) metadata pairs to send with every request.
            when using `transport="rest"` these are sent as HTTP headers.
    r\   )Ú_client_managerrw   r\   r   r   r   rw     s    úrw   r@   úglm.DiscussServiceClientc                   C  ó
   t  d¡S )NÚdiscuss©r¥   rš   r   r   r   r   Úget_default_discuss_client>  ó   
rª   úglm.DiscussServiceAsyncClientc                   C  r§   )NÚdiscuss_asyncr©   r   r   r   r   Ú get_default_discuss_async_clientB  r«   r®   úglm.FilesServiceClientc                   C  r§   )NrE   r©   r   r   r   r   Úget_default_file_clientF  r«   r°   úglm.FilesServiceAsyncClientc                   C  r§   )Nrx   r©   r   r   r   r   Úget_default_file_async_clientJ  r«   r²   úglm.GenerativeServiceClientc                   C  r§   )NÚ
generativer©   r   r   r   r   Úget_default_generative_clientN  r«   rµ   ú glm.GenerativeServiceAsyncClientc                   C  r§   )NÚgenerative_asyncr©   r   r   r   r   Ú#get_default_generative_async_clientR  r«   r¸   úglm.TextServiceClientc                   C  r§   )NÚtextr©   r   r   r   r   Úget_default_text_clientV  r«   r»   r›   c                   C  r§   r•   r©   r   r   r   r   r˜   Z  r«   r˜   úglm.ModelServiceAsyncClientc                   C  r§   )NÚmodelr©   r   r   r   r   Úget_default_model_client^  r«   r¾   úglm.RetrieverClientc                   C  r§   )NÚ	retrieverr©   r   r   r   r   Úget_default_retriever_clientb  r«   rÁ   úglm.RetrieverAsyncClientc                   C  r§   )NÚretriever_asyncr©   r   r   r   r   Ú"get_default_retriever_async_clientf  r«   rÄ   úglm.PermissionServiceClientc                   C  r§   )NÚ
permissionr©   r   r   r   r   Úget_default_permission_clientj  r«   rÇ   ú glm.PermissionServiceAsyncClientc                   C  r§   )NÚpermission_asyncr©   r   r   r   r   Ú#get_default_permission_async_clientn  r«   rÊ   )r)   r>   r   r_   r]   r>   r	   r¤   r^   ra   rX   rW   )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È   )>Ú
__future__r   r   Ú
contextlibr¡   Úpathlibr~   Útypingr   r   Úcollections.abcr   r-   Úgoogle.ai.generativelanguageÚaiÚgenerativelanguager‹   Úgoogle.generativeai.protosÚgenerativeaiÚprotosÚgoogle.authr   Úga_credentialsr   r�   Úgoogler   Úgoogle.api_corer	   rm   r
   r   Úgoogleapiclient.httpr+   Úgoogleapiclient.discoveryÚgoogle.generativeair   rs   ÚImportErrorrr   r/   Úcontextmanagerr   r   rR   Ú	dataclassrT   rw   r¥   rª   r®   r°   r²   rµ   r¸   r»   r˜   r¾   rÁ   rÄ   rÇ   rÊ   r   r   r   r   Ú<module>   sp    
ÿ
3 (ó*











