o
    j6h:                     @  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 )NCOLAB_RELEASE_TAGc                  _  s   dS )N)NN )argskwargsr   r   i/var/www/html/chefvision.cloud.itp360.com/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)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=)httppostprocuriheaderszutf-8)developerKey)_client_optionsapi_key
ValueErrorgoogleapiclientr*   HttpRequesthttplib2HttpGENAI_API_DISCOVERY_URLdictexecuteclosedecode	discoverybuild_from_documentr   discovery_api)r#   r&   r0   requestresponser)   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 | IOBaserB   
str | NonerC   rD   rE   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 )NrC   displayName)fdmimetyperE   )filenamerO   rE   file)body
media_body)r   rA   
isinstancer   r2   r*   MediaIoBaseUploadMediaFileUploadr   r=   mediauploadr-   r8   get_file)r#   rF   rB   rC   rD   rE   r&   rQ   rW   r>   keyvalueresultr   r   r   create_file\   s&   



zFileServiceClient.create_file)r   )r&   r'   )rF   rG   rB   rH   rC   rH   rD   rH   rE   rI   r&   rJ   rK   rL   )__name__
__module____qualname__r!   rA   r]   __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   r]      s   z"FileServiceAsyncClient.create_fileN)r^   r_   r`   r]   r   r   r   r   rb      s    rb   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   rJ   default_metadataclientsNr0   r   	transportr
   client_inforg   r0   rH   r   (ga_credentials.Credentials | dict | Nonerj   r
   8client_options_lib.ClientOptions | dict[str, Any] | Nonerk   &gapic_v1.client_info.ClientInfo | NonerK   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.
        Nr0   z`Invalid configuration: Please set either `api_key` or `client_options['api_key']`, but not both.GEMINI_API_KEYGOOGLE_API_KEY/ )
user_agent)r   rj   r
   rk   c                 S  s   i | ]\}}|d ur||qS r   r   ).0rZ   r[   r   r   r   
<dictcomp>   s    z,_ClientManager.configure.<locals>.<dictcomp>)rT   r7   client_options_lib	from_dictClientOptionsr   getattrr1   r   getenvr0   
USER_AGENT__version__rt   r   rk   
ClientInfoitemsrf   rg   rh   )
r#   r0   r   rj   r
   rk   rg   had_api_key_valuert   rf   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 )NrQ   
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  s6   |  drdS t|sdS dt|j vrdS dS )Nr   Fr&   T)
startswithcallableinspect	signature
parameterskeys)rC   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&   )listrg   )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   rb   endswithsplitrz   glmtitlerf   r   r   ga_exceptionsDefaultCredentialsErrorr   rg   r   
getmemberssetattr)	r#   rC   clsclienter   r   r[   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_clientrh   getr   )r#   rC   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)rh   r   r   
_transportoperations_client)r#   r   model_clientr   r   r   r   %  s   

z,_ClientManager.get_default_operations_client)r0   rH   r   rl   rj   rH   r
   rm   rk   rn   rg   rJ   rK   ro   rK   r   )r^   r_   r`   dataclassesfieldr7   rf   __annotations__rg   rh   r   r   r   r   r   r   r   r   rd      s   
 S:rd   r   ri   r0   rH   r   rl   rj   r
   .client_options_lib.ClientOptions | dict | Nonerk   rn   rg   rJ   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.
    ri   )_client_managerr   ri   r   r   r   r   .  s   r   rK   glm.CacheServiceClientc                   C  
   t dS )Ncacher   r   r   r   r   r   get_default_cache_client[     
r   glm.FilesServiceClientc                   C  r   )NrQ   r   r   r   r   r   get_default_file_client_  r   r   glm.FilesServiceAsyncClientc                   C  r   )Nr   r   r   r   r   r   get_default_file_async_clientc  r   r   glm.GenerativeServiceClientc                   C  r   )N
generativer   r   r   r   r   get_default_generative_clientg  r   r    glm.GenerativeServiceAsyncClientc                   C  r   )N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   )N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   )Npermission_asyncr   r   r   r   r   #get_default_permission_async_client  r   r   )r0   rH   r   rl   rj   rH   r
   r   rk   rn   rg   rJ   )rK   r   )rK   r   )rK   r   )rK   r   )rK   r   r   )rK   r   )rK   r   )rK   r   )rK   r   )rK   r   )?
__future__r   r   
contextlibr   r   pathlibr   typingr   r   collections.abcr   r4   ior   google.ai.generativelanguageaigenerativelanguager   google.generativeai.protosgenerativeaiprotosgoogle.authr   ga_credentialsr   r   googler	   google.api_corer
   rw   r   r   googleapiclient.httpr2   googleapiclient.discoverygoogle.generativeair   r}   ImportErrorr|   r6   contextmanagerr   r   rb   	dataclassrd   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sp    

? ))









