o
    Zh:                     @  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Zd dlm	Z	m
Z
 d dlmZ d dlZd dlm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j0G dd dZ1ddddddddId%d&Z2e1 Z3e32  dJd)d*Z4dKd,d-Z5dLd/d0Z6dMd2d3Z7dNd5d6Z8dOd8d9Z9dPd;d<Z:dQd>d?Z;dRdAdBZ<dSdDdEZ=dTdGdHZ>dS )U    )annotationsN)Anycast)Sequence)IOBase)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 )NZCOLAB_RELEASE_TAGc                  _  s   dS )N)NN )argskwargsr   r   Q/var/www/html/lang_env/lib/python3.10/site-packages/google/generativeai/client.py<lambda>9   s    z-patch_colab_gce_credentials.<locals>.<lambda>)r	   _defaultZ_get_gce_credentialsosenviron)Zget_gcer   r   r   patch_colab_gce_credentials5   s   
r   c                      s@   e Zd Z fddZddddZdddd	dd
dddZ  ZS )FileServiceClientc                   s&   d | _ t | _t j|i | d S N)_discovery_api	threadinglocal_localsuper__init__selfr   r   	__class__r   r   r   B   s   
zFileServiceClient.__init__r   metadata dict | Sequence[tuple[str, str]]c                 C  sz   | j j}|d u rtdtjjt dd t d| t	|d}|
 \}}|j  |d}tjj||d| j_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   P   s    z8FileServiceClient._setup_discovery_api.<locals>.<lambda>z?version=v1beta&key=)httpZpostprocuriheaderszutf-8)ZdeveloperKey)Z_client_optionsapi_key
ValueErrorgoogleapiclientr'   ZHttpRequesthttplib2ZHttpGENAI_API_DISCOVERY_URLdictexecuteclosedecodeZ	discoveryZbuild_from_documentr   discovery_api)r    r#   r*   requestresponser&   Zdiscovery_docr   r   r   _setup_discovery_apiG   s"   

z&FileServiceClient._setup_discovery_apiNT)	mime_typenamedisplay_name	resumabler#   path)str | pathlib.Path | os.PathLike | IOBaser7   
str | Noner8   r9   r:   boolSequence[tuple[str, str]]returnprotos.Filec                C  s   | j d u r
| | i }|d ur||d< |d ur||d< t|tr+tjj|||d}n	tjj|||d}| jj	
 jd|i|d}	|D ]	\}
}||	j|
< qC|	 }| d|d d iS )Nr8   ZdisplayName)fdmimetyper:   )filenamerC   r:   file)bodyZ
media_body)r   r6   
isinstancer   r,   r'   ZMediaIoBaseUploadZMediaFileUploadr   r3   mediaZuploadr)   r0   Zget_file)r    r;   r7   r8   r9   r:   r#   rE   rH   r4   keyvalueresultr   r   r   create_file\   s&   



zFileServiceClient.create_file)r   )r#   r$   )r;   r<   r7   r=   r8   r=   r9   r=   r:   r>   r#   r?   r@   rA   )__name__
__module____qualname__r   r6   rL   __classcell__r   r   r!   r   r   A   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   rL      s   z"FileServiceAsyncClient.create_fileN)rM   rN   rO   rL   r   r   r   r   rQ      s    rQ   c                   @  sv   e Zd ZU eje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   r?   default_metadataclientsNr*   r   	transportr
   client_inforV   r*   r=   r   (ga_credentials.Credentials | dict | NonerY   r
   8client_options_lib.ClientOptions | dict[str, Any] | NonerZ   &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}|du r;t	d}||_
t dt }|rY|jrU| 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
        `GEMINI_API_KEY` environment variable is set, it will be used as the API key. If not,
        if the `GOOGLE_API_KEY` environement 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 `GEMINI_API_KEY` or the `GOOGLE_API_KEY` environment variable
                are set, they will be used in this order of priority.
            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.ZGEMINI_API_KEYZGOOGLE_API_KEY/ )
user_agent)r   rY   r
   rZ   c                 S  s   i | ]\}}|d ur||qS r   r   ).0rI   rJ   r   r   r   
<dictcomp>   s    z,_ClientManager.configure.<locals>.<dictcomp>)rG   r/   client_options_lib	from_dictZClientOptionsr   getattrr+   r   getenvr*   
USER_AGENT__version__ra   r   rZ   Z
ClientInfoitemsrU   rV   rW   )
r    r*   r   rY   r
   rZ   rV   Zhad_api_key_valuera   rU   r   r   r   	configure   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}t|D ]\}}|||sqwt||}||}t||| qw|S )NrE   
file_asyncZ_async_r   ZServiceAsyncClientZ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  s6   |  drdS t|sdS dt|j vrdS dS )Nrm   Fr#   T)
startswithcallableinspect	signature
parameterskeys)r8   fr   r   r   keep   s   
z(_ClientManager.make_client.<locals>.keepc                   s   dd fdd
}|S )Nr   )r#   c                   s(   t | t j }  |i |d| iS )Nr#   )listrV   )r#   r   r   )rt   r    r   r   call  s   zN_ClientManager.make_client.<locals>.add_default_metadata_wrapper.<locals>.callr   )rt   rw   r    )rt   r   add_default_metadata_wrapper
  s   z@_ClientManager.make_client.<locals>.add_default_metadata_wrapperr   )r   rQ   endswithsplitrf   glmtitlerU   rk   r   ga_exceptionsZDefaultCredentialsErrorr   rV   rp   
getmemberssetattr)	r    r8   clsclienteru   ry   rJ   rt   r   rx   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_clientrW   getr   )r    r8   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   ZModel)rW   r   r   
_transportZoperations_client)r    r   Zmodel_clientr   r   r   r   %  s   

z,_ClientManager.get_default_operations_client)r*   r=   r   r[   rY   r=   r
   r\   rZ   r]   rV   r?   r@   r^   r@   r   )rM   rN   rO   dataclassesfieldr/   rU   __annotations__rV   rW   rk   r   r   r   r   r   r   r   rS      s   
 S:rS   r   rX   r*   r=   r   r[   rY   r
   .client_options_lib.ClientOptions | dict | NonerZ   r]   rV   r?   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.
    rX   )_client_managerrk   rX   r   r   r   rk   .  s   rk   r@   glm.CacheServiceClientc                   C  
   t dS )Ncacher   r   r   r   r   r   get_default_cache_client[     
r   glm.FilesServiceClientc                   C  r   )NrE   r   r   r   r   r   get_default_file_client_  r   r   glm.FilesServiceAsyncClientc                   C  r   )Nrl   r   r   r   r   r   get_default_file_async_clientc  r   r   glm.GenerativeServiceClientc                   C  r   )NZ
generativer   r   r   r   r   get_default_generative_clientg  r   r    glm.GenerativeServiceAsyncClientc                   C  r   )NZgenerative_asyncr   r   r   r   r   #get_default_generative_async_clientk  r   r   r   c                   C  r   r   r   r   r   r   r   r   o  r   r   glm.ModelServiceAsyncClientc                   C  r   )Nmodelr   r   r   r   r   get_default_model_clients  r   r   glm.RetrieverClientc                   C  r   )NZ	retrieverr   r   r   r   r   get_default_retriever_clientw  r   r   glm.RetrieverAsyncClientc                   C  r   )NZretriever_asyncr   r   r   r   r   "get_default_retriever_async_client{  r   r   glm.PermissionServiceClientc                   C  r   )NZ
permissionr   r   r   r   r   get_default_permission_client  r   r    glm.PermissionServiceAsyncClientc                   C  r   )NZpermission_asyncr   r   r   r   r   #get_default_permission_async_client  r   r   )r*   r=   r   r[   rY   r=   r
   r   rZ   r]   rV   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   
contextlibrp   r   pathlibr   typingr   r   collections.abcr   r-   ior   Zgoogle.ai.generativelanguageZaiZgenerativelanguager|   Zgoogle.generativeai.protosZgenerativeaiprotosZgoogle.authr   Zga_credentialsr   r~   Zgoogler	   Zgoogle.api_corer
   rd   r   r   Zgoogleapiclient.httpr,   Zgoogleapiclient.discoveryZgoogle.generativeair   ri   ImportErrorrh   r.   contextmanagerr   r   rQ   	dataclassrS   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sp    

? ))









