o
    i&                  
   @  sV  d dl mZ d dlZd dlmZ d dlmZmZ d dl	m
Z
 d dlmZmZ d dlZd dl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 zd dlZd dlm Z! d dl"m#Z# d dl$m Z% W n e&y{ Z' ze&de'dZ'['ww dZ(edG dd deej) Z*G dd dej+Z,d$ddZ-d%ddZ.d&d!d"Z/ed# Z0dS )'    )annotationsN)Lock)AsyncGeneratorMapping)Path)Literaloverload)
deprecated)ModelProfile)	UserError)cached_async_http_clientgoogle_model_profile)Provider)Credentials)Requestu   Please install the `google-auth` package to use the Google Vertex AI provider, you can use the `vertexai` optional group — `pip install "pydantic-ai-slim[vertexai]"`)GoogleVertexProviderzV`GoogleVertexProvider` is deprecated, use `GoogleProvider` with `GoogleModel` instead.c                   @  s   e Zd ZdZed%ddZed%ddZed&d	d
Zd'ddZe	ddddddd(ddZ
e	ddddddd)d"dZ
ddddddd#d*d$dZ
dS )+r   zProvider for Vertex AI API.returnstrc                 C  s   dS )Ngoogle-vertex selfr   r   i/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/providers/google_vertex.pyname&   s   zGoogleVertexProvider.namec              	   C  s&   d| j  d| j d| j  d| j d	S )Nzhttps://z'-aiplatform.googleapis.com/v1/projects/z/locations/z/publishers/z/models/)region
project_idmodel_publisherr   r   r   r   base_url*   s   
zGoogleVertexProvider.base_urlhttpx.AsyncClientc                 C  s   | j S N)_clientr   r   r   r   client3   s   zGoogleVertexProvider.client
model_nameModelProfile | Nonec                 C  s   t |S r    r   )r   r#   r   r   r   model_profile7   s   z"GoogleVertexProvider.model_profileNus-central1google)service_account_filer   r   r   http_clientr(   Path | str | Noner   
str | Noner   VertexAiRegionr   r)   httpx.AsyncClient | NoneNonec                C     d S r    r   )r   r(   r   r   r   r)   r   r   r   __init__:      	zGoogleVertexProvider.__init__)service_account_infor   r   r   r)   r2   Mapping[str, str] | Nonec                C  r/   r    r   )r   r2   r   r   r   r)   r   r   r   r0   E   r1   )r(   r2   r   r   r   r)   c                C  s^   |r|rt d|ptdd| _|| _|| _|| _|| _|| _t||||| j_	| j
| j_
dS )a!  Create a new Vertex AI provider.

        Args:
            service_account_file: Path to a service account file.
                If not provided, the service_account_info or default environment credentials will be used.
            service_account_info: The loaded service_account_file contents.
                If not provided, the service_account_file or default environment credentials will be used.
            project_id: The project ID to use, if not provided it will be taken from the credentials.
            region: The region to make requests to.
            model_publisher: The model publisher to use, I couldn't find a good list of available publishers,
                and from trial and error it seems non-google models don't work with the `generateContent` and
                `streamGenerateContent` functions, hence only `google` is currently supported.
                Please create an issue or PR if you know how to use other publishers.
            http_client: An existing `httpx.AsyncClient` to use for making HTTP requests.
        zMOnly one of `service_account_file` or `service_account_info` can be provided.r   )providerN)
ValueErrorr   r!   r(   r2   r   r   r   _VertexAIAuthauthr   )r   r(   r2   r   r   r   r)   r   r   r   r0   P   s   r   r   )r   r   )r#   r   r   r$   )r(   r*   r   r+   r   r,   r   r   r)   r-   r   r.   )r2   r3   r   r+   r   r,   r   r   r)   r-   r   r.   )r(   r*   r2   r3   r   r+   r   r,   r   r   r)   r-   r   r.   )__name__
__module____qualname____doc__propertyr   r   r"   r%   r   r0   r   r   r   r   r   "   s<    

r   c                   @  sZ   e Zd ZU dZe Zded< ded< 				dd ddZd!ddZd"ddZ	d#ddZ
dS )$r6   zAuth class for Vertex AI API.r   _refresh_lockz2BaseCredentials | ServiceAccountCredentials | NonecredentialsNr&   r(   r*   r2   r3   r   r+   r   r,   r   r.   c                 C  s"   || _ || _|| _|| _d | _d S r    )r(   r2   r   r   r?   )r   r(   r2   r   r   r   r   r   r0   ~   s
   
z_VertexAIAuth.__init__requesthttpx.Request-AsyncGenerator[httpx.Request, httpx.Response]c                 C s   | j d u r|  I d H | _ | j jd u r|  I d H  d| j j |jd< tt|j	dd| j
 |_|V }|jdkrT|  I d H  d| j j |jd< |V  d S d S )NzBearer Authorizationzprojects/Nonez	projects/i  )r?   _get_credentialstoken_refresh_tokenheadershttpxURLr   urlreplacer   status_code)r   r@   responser   r   r   async_auth_flow   s   
"

z_VertexAIAuth.async_auth_flow+BaseCredentials | ServiceAccountCredentialsc                   s   | j d ur!t| j I d H }|jd u st|jtsJ |j}d}n*| jd urAt| jI d H }|jd u s;t|jts;J |j}d}n
t I d H \}}d}| jd u r^|d u r[td| || _|S )Nzservice account filezservice account infoz`google.auth.default()`z)No project_id provided and none found in )	r(   _creds_from_filer   
isinstancer   r2   _creds_from_info_async_google_authr   )r   credscreds_project_idcreds_sourcer   r   r   rD      s$   


z_VertexAIAuth._get_credentialsr   c              	     s   | j 4 I d H 3 | jd usJ tj| jjt I d H  t| jjt	s-J d| jj | jjW  d   I d H  S 1 I d H sAw   Y  d S )Nz#Expected token to be a string, got )
r>   r?   anyio	to_threadrun_syncrefreshr   rQ   rE   r   r   r   r   r   rF      s   0z_VertexAIAuth._refresh_token)NNNr&   )
r(   r*   r2   r3   r   r+   r   r,   r   r.   )r@   rA   r   rB   )r   rO   r8   )r9   r:   r;   r<   r   r>   __annotations__r0   rN   rD   rF   r   r   r   r   r6   w   s   
 

r6   r   "tuple[BaseCredentials, str | None]c                     s   t jtjjdgI d H S )N.https://www.googleapis.com/auth/cloud-platform)rW   rX   rY   r'   r7   defaultr   r   r   r   rS      s   rS   r(   
str | PathServiceAccountCredentialsc                   s,   t jtjdgd}tj|t| I d H S Nr]   )scopes)	functoolspartialr`   from_service_account_filerW   rX   rY   r   )r(   %service_account_credentials_from_filer   r   r   rP      s   rP   r2   Mapping[str, str]c                   s(   t jtjdgd}tj|| I d H S ra   )rc   rd   r`   from_service_account_inforW   rX   rY   )r2   'service_account_credentials_from_stringr   r   r   rR      s   rR   )z
asia-east1z
asia-east2zasia-northeast1zasia-northeast3zasia-south1zasia-southeast1zaustralia-southeast1zeurope-central2zeurope-north1zeurope-southwest1zeurope-west1zeurope-west2zeurope-west3zeurope-west4zeurope-west6zeurope-west8zeurope-west9zme-central1zme-central2zme-west1znorthamerica-northeast1zsouthamerica-east1r&   zus-east1zus-east4zus-east5z	us-south1zus-west1zus-west4)r   r\   )r(   r_   r   r`   )r2   rg   r   r`   )1
__future__r   _annotationsrc   asyncior   collections.abcr   r   pathlibr   typingr   r   anyio.to_threadrW   rH   typing_extensionsr	   pydantic_air
   pydantic_ai.exceptionsr   pydantic_ai.modelsr   pydantic_ai.profiles.googler   pydantic_ai.providersr   google.authr'   google.auth.credentialsr   BaseCredentialsgoogle.auth.transport.requestsr   google.oauth2.service_accountr`   ImportError_import_error__all__AsyncClientr   Authr6   rS   rP   rR   r,   r   r   r   r   <module>   sL    T
C

