o
    : h:                     @  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   b/var/www/html/figdemos/bartoux_crm/venv/lib/python3.10/site-packages/google/generativeai/client.py<lambda>9   s    z-patch_colab_gce_credentials.<locals>.<lambda>)r	   _default_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)_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 | IOBaser9   
str | Noner:   r;   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 )Nr:   ZdisplayName)fdmimetyper<   )filenamerE   r<   file)bodyZ
media_body)r   r8   
isinstancer   r.   r(   ZMediaIoBaseUploadZMediaFileUploadr   r5   mediaZuploadr*   r2   get_file)r!   r=   r9   r:   r;   r<   r$   rG   rJ   r6   keyvalueresultr   r   r   create_file\   s&   



zFileServiceClient.create_file)r   )r$   r%   )r=   r>   r9   r?   r:   r?   r;   r?   r<   r@   r$   rA   rB   rC   )__name__
__module____qualname__r   r8   rO   __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   rO      s   z"FileServiceAsyncClient.create_fileN)rP   rQ   rR   rO   r   r   r   r   rT      s    rT   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   rA   default_metadataclientsNr,   r   	transportr
   client_inforY   r,   r?   r   (ga_credentials.Credentials | dict | Noner\   r
   8client_options_lib.ClientOptions | dict[str, Any] | Noner]   &gapic_v1.client_info.ClientInfo | NonerB   Nonec          
      C  s   t |tr
t|}|du rt }ttj|}t|dd}|r)|dur(tdn|s0t	d}|s7t	d}||_
t dt }|rU|jrQ| 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.GEMINI_API_KEYZGOOGLE_API_KEY/ )
user_agent)r   r\   r
   r]   c                 S  s   i | ]\}}|d ur||qS r   r   ).0rL   rM   r   r   r   
<dictcomp>   s    z,_ClientManager.configure.<locals>.<dictcomp>)rI   r1   client_options_lib	from_dictClientOptionsr   getattrr-   r   getenvr,   
USER_AGENT__version__re   r   r]   
ClientInfoitemsrX   rY   rZ   )
r!   r,   r   r\   r
   r]   rY   Zhad_api_key_valuere   rX   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 )NrG   
file_async_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 )Nrt   Fr$   T)
startswithcallableinspect	signature
parameterskeys)r:   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$   )listrY   )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   rT   endswithsplitrk   glmtitlerX   rq   r   ga_exceptionsDefaultCredentialsErrorr   rY   rw   
getmemberssetattr)	r!   r:   clsclienter|   r   rM   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_clientrZ   getr   )r!   r:   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)rZ   r   r   
_transportoperations_client)r!   r   Zmodel_clientr   r   r   r   %  s   

z,_ClientManager.get_default_operations_client)r,   r?   r   r^   r\   r?   r
   r_   r]   r`   rY   rA   rB   ra   rB   r   )rP   rQ   rR   dataclassesfieldr1   rX   __annotations__rY   rZ   rq   r   r   r   r   r   r   r   rV      s   
 S:rV   r   r[   r,   r?   r   r^   r\   r
   .client_options_lib.ClientOptions | dict | Noner]   r`   rY   rA   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_managerrq   r[   r   r   r   rq   .  s   rq   rB   glm.CacheServiceClientc                   C  
   t dS )Ncacher   r   r   r   r   r   get_default_cache_client[     
r   glm.FilesServiceClientc                   C  r   )NrG   r   r   r   r   r   get_default_file_client_  r   r   glm.FilesServiceAsyncClientc                   C  r   )Nrr   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   )N	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   )N
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^   r\   r?   r
   r   r]   r`   rY   rA   )rB   r   )rB   r   )rB   r   )rB   r   )rB   r   r   )rB   r   )rB   r   )rB   r   )rB   r   )rB   r   )?
__future__r   r   
contextlibrw   r   pathlibr   typingr   r   collections.abcr   r/   ior   Zgoogle.ai.generativelanguageaiZgenerativelanguager   Zgoogle.generativeai.protosgenerativeaiprotosgoogle.authr   ga_credentialsr   r   googler	   google.api_corer
   rh   r   r   Zgoogleapiclient.httpr.   Zgoogleapiclient.discoverygoogle.generativeair   rn   ImportErrorrm   r0   contextmanagerr   r   rT   	dataclassrV   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sp    

? ))









