
    h-i:                       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Z d dlmc mZ d dlmc 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#	 d dl%m&Z& e&jN                  Z'dZ)dZ*ejV                  d        Z, G d dejZ                        Z- G d dej\                        Z.ej^                   G d d             Z0ddddddd	 	 	 	 	 	 	 	 	 	 	 d%dZ1 e0       Z2e2jc                          d&dZ3d'dZ4d(dZ5d)dZ6d*dZ7d+dZ8d,d Z9d-d!Z:d.d"Z;d/d#Z<d0d$Z=y# e($ r dZ'Y w xY w)1    )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               #     K   t         j                  j                  } dt        j                  v rd t         j                  _        	 d  | t         j                  _        y # | t         j                  _        w xY ww)NCOLAB_RELEASE_TAGc                      y)N)NN )argskwargss     YD:\DhanshreeandTeamAI\Github\ai_code\venv\Lib\site-packages\google/generativeai/client.py<lambda>z-patch_colab_gce_credentials.<locals>.<lambda>8   s    \    )r
   _default_get_gce_credentialsosenviron)get_gces    r   patch_colab_gce_credentialsr   4   sM     mm00Gbjj(-Q*5-4*W*s   AA:A  
A: A77A:c                  X     e Zd Z fdZdd	dZdddddd	 	 	 	 	 	 	 	 	 	 	 	 	 d
dZ xZS )FileServiceClientc                2    d | _         t        |   |i | y N)_discovery_apisuper__init__)selfr   r   	__class__s      r   r$   zFileServiceClient.__init__A   s    "$)&)r   r   c                   | j                   j                  }|t        d      t        j                  j                  t        j                         d t         d| t        |            }|j                         \  }}|j                  j                          |j                  d      }t        j                  j                  ||      | _        y )NzaInvalid operation: Uploading to the File API requires an API key. Please provide a valid API key.c                
    | |fS r!   r   )respcontents     r   r   z8FileServiceClient._setup_discovery_api.<locals>.<lambda>N   s    D'?r   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"   )r%   metadatar1   requestresponser*   discovery_docs          r   _setup_discovery_apiz&FileServiceClient._setup_discovery_apiE   s    &&..?s  "&&22:*++?yIN	 3 
 $OO-'w/-77KK L 
r   NT)	mime_typenamedisplay_name	resumabler>   c                  | j                   | j                  |       i }|||d<   |||d<   t        |t              r#t        j
                  j                  |||      }n"t        j
                  j                  |||      }| j                   j                         j                  d|i|      }	|D ]  \  }
}||	j                  |
<    |	j                         }| j                  d|d   d   i      S )NrD   displayName)fdmimetyperF   )filenamerJ   rF   file)body
media_body)r"   rB   
isinstancer   r3   r+   MediaIoBaseUploadMediaFileUploadmediauploadr.   r9   get_file)r%   pathrC   rD   rE   rF   r>   rL   rR   r?   keyvalueresults                r   create_filezFileServiceClient.create_fileZ   s    &%%h/DL#".DdF##((::)y ; E $((88	Y 9 E %%++-4464.UZ4["JC#(GOOC  #"}}ffVnV&<=>>r   )r   )r>   z dict | Sequence[tuple[str, str]])rU   z)str | pathlib.Path | os.PathLike | IOBaserC   
str | NonerD   rZ   rE   rZ   rF   boolr>   Sequence[tuple[str, str]]returnzprotos.File)__name__
__module____qualname__r$   rB   rY   __classcell__)r&   s   @r   r   r   @   sg    *
2 !%#'.0!?7!? 	!?
 !? !!? !? ,!? 
!?r   r   c                      e Zd Zd Zy)FileServiceAsyncClientc                    K   t        d      w)NzPThe `create_file` method is currently not supported for the asynchronous client.)NotImplementedError)r%   r   r   s      r   rY   z"FileServiceAsyncClient.create_file   s     !^
 	
s   N)r^   r_   r`   rY   r   r   r   rc   rc   ~   s    
r   rc   c                      e Zd ZU  ej                  e      Zded<   dZded<    ej                  e      Z	ded<   ddddddd		 	 	 	 	 	 	 	 	 	 	 	 	 dd
Z
d Zd ZddZy)_ClientManager)default_factoryzdict[str, Any]client_configr   r\   default_metadataclientsNr1   r   	transportr   client_inforj   c                  t        |t              rt        j                  |      }|t        j                         }t        t        j                  |      }t        |dd      }|r|@t        d      |t        j                  d      }|t        j                  d      }||_
        t         dt         }|r-|j                  r|xj                  d| z  c_        n(||_        n t        j                  j!                  |      }||||d	}	|	j#                         D 
ci c]  \  }
}|	|
| }	}
}|	| _        || _        i | _        yc c}}
w )
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.
        Nr1   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   rm   r   rn   )rO   r8   client_options_lib	from_dictClientOptionsr   getattrr2   r   getenvr1   
USER_AGENT__version__rt   r   rn   
ClientInfoitemsri   rj   rk   )r%   r1   r   rm   r   rn   rj   had_api_key_valuert   ri   rV   rW   s               r   	configurez_ClientManager.configure   sZ   D nd+/99.IN!/==?N0>>O#NItD" v   ))$45 ))$45%,N""|1[M2
%%&&Aj\*::&)3&"..99Z9PK '",&	
 7D6I6I6Ka6K
UuO`e6Ka* 0 bs   
E*Ec                    |dk(  rt         }nt|dk(  rt        }nh|j                  d      r6|j                  d      d   }t	        t
        |j                         dz         }n!t	        t
        |j                         dz         } j                  s
t                	 t               5   |di  j                  }d d d         j                  sS d	 } fd
}t        j                  |      D ]0  \  }} |||      st	        |      } ||      }t!        |||       2 S # 1 sw Y   ixY w# t        j                  $ r}d|_        |d }~ww xY w)NrL   
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                    | j                  d      ryt        |      sydt        j                  |      j                  j                         vryy)Nr   Fr>   T)
startswithcallableinspect	signature
parameterskeys)rD   fs     r   keepz(_ClientManager.make_client.<locals>.keep   sC    s#A;!2!21!5!@!@!E!E!GGr   c                     dd fd
}|S )Nr   )r>   c                ^    t        |       t        j                        z   }  |i |d| iS )Nr>   )listrj   )r>   r   r   r   r%   s      r   callzN_ClientManager.make_client.<locals>.add_default_metadata_wrapper.<locals>.call	  s1    >D1F1F,GG$<&<8<<r   r   )r   r   r%   s   ` r   add_default_metadata_wrapperz@_ClientManager.make_client.<locals>.add_default_metadata_wrapper  s    %' = Kr   r   )r   rc   endswithsplitrx   glmtitleri   r   r   ga_exceptionsDefaultCredentialsErrorr   rj   r   
getmemberssetattr)	r%   rD   clsclienter   r   rW   r   s	   `        r   make_clientz_ClientManager.make_client   s6   6>#C\!(C]]8$::c?1%D#tzz|.BBCC#tzz|o=>C !!K
	,.2t112 / $$M
		 #--c2KD%e$%A,Q/AFD!$ 3 Q /.44 	AF G	s0   
D* #D6D* D'#D* *E=	EEc                    |j                         }|dk(  r| j                         S | j                  j                  |      }| | j	                  |      }|| j                  |<   |S N
operations)lowerget_default_operations_clientrk   getr   )r%   rD   r   s      r   get_default_clientz!_ClientManager.get_default_client  s`    zz|<5577!!$'>%%d+F!'DLLr   c                    | j                   j                  dd       }|6| j                  d      }|j                  j                  }|| j                   d<   |S )Nr   Model)rk   r   r   
_transportoperations_client)r%   r   model_clients      r   r   z,_ClientManager.get_default_operations_client#  sQ    !!,5>227;L!,,>>F)/DLL&r   )r1   rZ   r   (ga_credentials.Credentials | dict | Nonerm   rZ   r   z8client_options_lib.ClientOptions | dict[str, Any] | Nonern   &gapic_v1.client_info.ClientInfo | Nonerj   r\   r]   Noner]   zoperations_v1.OperationsClient)r^   r_   r`   dataclassesfieldr8   ri   __annotations__rj   rk   r   r   r   r   r   r   r   rg   rg      s    $5K$5$5d$KM>K24/4/k//EG^E
 #@D !%SW>B68Q Q >	Q Q QQ <Q 4Q 
Qf8t	r   rg   r   rl   c                8    t         j                  | |||||      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.
    rl   )_client_managerr   rl   s         r   r   r   ,  s.    > $$%) %  r   c                 ,    t         j                  d      S )Ncacher   r   r   r   r   get_default_cache_clientr   Y      --g66r   c                 ,    t         j                  d      S )NrL   r   r   r   r   get_default_file_clientr   ]  s    --f55r   c                 ,    t         j                  d      S )Nr   r   r   r   r   get_default_file_async_clientr   a      --l;;r   c                 ,    t         j                  d      S )N
generativer   r   r   r   get_default_generative_clientr   e  r   r   c                 ,    t         j                  d      S )Ngenerative_asyncr   r   r   r   #get_default_generative_async_clientr   i      --.@AAr   c                 ,    t         j                  d      S r   r   r   r   r   r   r   m  r   r   c                 ,    t         j                  d      S )Nmodelr   r   r   r   get_default_model_clientr   q  r   r   c                 ,    t         j                  d      S )N	retrieverr   r   r   r   get_default_retriever_clientr   u  s    --k::r   c                 ,    t         j                  d      S )Nretriever_asyncr   r   r   r   "get_default_retriever_async_clientr   y  s    --.?@@r   c                 ,    t         j                  d      S )N
permissionr   r   r   r   get_default_permission_clientr   }  r   r   c                 ,    t         j                  d      S )Npermission_asyncr   r   r   r   #get_default_permission_async_clientr     r   r   )r1   rZ   r   r   rm   rZ   r   z.client_options_lib.ClientOptions | dict | Nonern   r   rj   r\   )r]   zglm.CacheServiceClient)r]   zglm.FilesServiceClient)r]   zglm.FilesServiceAsyncClient)r]   zglm.GenerativeServiceClient)r]   z glm.GenerativeServiceAsyncClientr   )r]   zglm.ModelServiceAsyncClient)r]   zglm.RetrieverClient)r]   zglm.RetrieverAsyncClient)r]   zglm.PermissionServiceClient)r]   z glm.PermissionServiceAsyncClient)>
__future__r   r   
contextlibr   r   pathlibtypingr   r   collections.abcr   r5   ior   google.ai.generativelanguageaigenerativelanguager   google.generativeai.protosgenerativeaiprotosgoogle.authr   ga_credentialsr	   r   googler
   google.api_corer   ru   r   r   googleapiclient.httpr3   googleapiclient.discoverygoogle.generativeair   r{   ImportErrorrz   r7   contextmanagerr   r   rc   	dataclassrg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s   " 	      $   * * + + 5 3  @ $ )   +%%K 
  V  5 5;?-- ;?|
S77 
 c c cP <@ !EI:>24&& :& & C& 8& 0&R !"    76<<B<7;A<BG  Ks   2D8 8EE