
    gk$                    p   d dl mZ d dlmZmZ d dlmZ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mZmZ ddlmZmZ 	 d dlZd dlmZ d dl m!Z! d dl"mZ# dZ&	  ed       G d de             Z'ddZ(ddZ) ed      Z*e G d d             Z+ed   Z,y# e$$ rZ% e$d      e%dZ%[%ww xY w)     )annotations)	dataclassfield)datetime	timedelta)Path)Literal)AsyncClient   )run_in_executor)	UserError)ToolDefinition   )Modelcached_async_http_client)GeminiAgentModelGeminiModelNameN)Credentials)Requestu   Please install `google-auth` to use the VertexAI model, you can use the `vertexai` optional group — `pip install 'pydantic-ai-slim[vertexai]'`zhttps://{region}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{region}/publishers/{model_publisher}/models/{model}:F)initc                      e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   ded<   ddddded	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZddZddZ	y)VertexAIModelzLA model that uses Gemini via the `*-aiplatform.googleapis.com` VertexAI API.r   
model_namePath | str | Noneservice_account_file
str | None
project_idVertexAiRegionregionLiteral['google']model_publisherAsyncHTTPClienthttp_clientstrurl_templatezBearerTokenAuth | NoneauthurlNus-central1google)r   r   r   r!   r#   r%   c                   || _         || _        || _        || _        || _        |xs
 t               | _        || _        d| _        d| _	        y)a  Initialize a Vertex AI Gemini model.

        Args:
            model_name: The name of the model to use. I couldn't find a list of supported Google models, in VertexAI
                so for now this uses the same models as the [Gemini model][pydantic_ai.models.gemini.GeminiModel].
            service_account_file: Path to a service account file.
                If not provided, the 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.
            url_template: URL template for Vertex AI, see
                [`VERTEX_AI_URL_TEMPLATE` docs][pydantic_ai.models.vertexai.VERTEX_AI_URL_TEMPLATE]
                for more information.
        N)
r   r   r   r   r!   r   r#   r%   r&   r'   )selfr   r   r   r   r!   r#   r%   s           P/var/www/openai/venv/lib/python3.12/site-packages/pydantic_ai/models/vertexai.py__init__zVertexAIModel.__init__F   sP    : %$8!$.&D*B*D(	    c          	        K   | j                          d {   \  }}t        | j                  | j                  |||||      S 7 -w)N)r#   r   r&   r'   function_toolsallow_text_resultresult_tools)ainitr   r#   r   )r+   r0   r1   r2   r'   r&   s         r,   agent_modelzVertexAIModel.agent_modeln   sK      **,&	T(()/%
 	
 's   AA.Ac                  K   | j                   $| j                  | j                   | j                  fS | j                  Lt        | j                        }|j                  t        |j                  t              sJ |j                  }d}nt                d{   \  }}d}| j                  |t        d|       |}n;|-| j                  |k7  rt        d| d| j                  d|      | j                  }| j                  j                  | j                  || j                  | j                        x| _         }t        |      x| _        }||fS 7 w)	zsInitialize the model, setting the URL and auth.

        This will raise an error if authentication fails.
        Nzservice account filez`google.auth.default()`z)No project_id provided and none found in z8The project_id you provided does not match the one from z: z != )r   r   r!   model)r'   r&   r   _creds_from_filer   
isinstancer$   _async_google_authr   r%   formatr   r!   r   BearerTokenAuth)r+   credscreds_project_idcreds_sourcer   r'   r&   s          r,   r3   zVertexAIModel.ainit   sh    
 88DII$988TYY&&$$0AQRVRkRkAlE##+z%:J:JC/PPP+0+;+;1L,>,@&@#E#4L??"'"KL> Z[[)J+CS0SN|n\^).>-AC  J**11;;! 00//	 2 
 	
3 +511	DDy- 'As   BEEB>Ec                     d| j                    S )Nzgoogle-vertex:)r   r+   s    r,   namezVertexAIModel.name   s    011r.   )r   r   r   r   r   r   r   r   r!   r    r#   zAsyncHTTPClient | Noner%   r$   )r0   list[ToolDefinition]r1   boolr2   rB   returnr   )rD   ztuple[str, BearerTokenAuth]rD   r$   )
__name__
__module____qualname____doc____annotations__VERTEX_AI_URL_TEMPLATEr-   r4   r3   rA    r.   r,   r   r   6   s    V++&&  
  	O 37!%!.-5.22&#& 0	&
 & & +& ,& &P
 -
  	

 +
 

$$L2r.   r   c                D    t        j                  t        |       dg      S Nz.https://www.googleapis.com/auth/cloud-platform)scopes)ServiceAccountCredentialsfrom_service_account_filer$   )r   s    r,   r7   r7      s$    $>> !+[*\ r.   c                 j   K   t        t        j                  j                  dg       d {   S 7 wrN   )r   r)   r&   defaultrL   r.   r,   r9   r9      s'      !4!4>n=opppps   *313i  )secondsc                  R    e Zd ZU dZded<    edd      Zded<   dd	Zdd
ZddZ	y)r;   z=Authentication using a bearer token generated by google-auth.z+BaseCredentials | ServiceAccountCredentialscredentialsNF)rS   r   zdatetime | Nonetoken_createdc                   K   | j                   j                  | j                         r6t        | j                         d {    t        j                         | _        dd| j                   j                   iS 7 8w)NAuthorizationzBearer )rV   token_token_expiredr   _refresh_tokenr   nowrW   r@   s    r,   headerszBearerTokenAuth.headers   si     !!)T-@-@-B!$"5"5666!)D74+;+;+A+A*B!CDD 7s   ?A<A:9A<c                l    | j                   yt        j                         | j                   z
  t        kD  S )NT)rW   r   r]   MAX_TOKEN_AGEr@   s    r,   r[   zBearerTokenAuth._token_expired   s-    %LLNT%7%77=HHr.   c                    | j                   j                  t                      t        | j                   j                  t
              sJ d| j                   j                          | j                   j                  S )Nz#Expected token to be a string, got )rV   refreshr   r8   rZ   r$   r@   s    r,   r\   zBearerTokenAuth._refresh_token   sa      +$**00#6v:]^b^n^n^t^t]u8vv6%%%r.   )rD   zdict[str, str])rD   rC   rE   )
rF   rG   rH   rI   rJ   r   rW   r^   r[   r\   rL   r.   r,   r;   r;      s-    G<<%*4e%DM?DEI&r.   r;   )&r(   zus-east1zus-east4z	us-south1zus-west1zus-west2zus-west3zus-west4zus-east5zeurope-central2zeurope-north1zeurope-southwest1zeurope-west1zeurope-west2zeurope-west3zeurope-west4zeurope-west6zeurope-west8zeurope-west9zeurope-west12zafrica-south1z
asia-east1z
asia-east2zasia-northeast1zasia-northeast2zasia-northeast3zasia-south1zasia-southeast1zasia-southeast2zaustralia-southeast1zaustralia-southeast2zme-central1zme-central2zme-west1znorthamerica-northeast1znorthamerica-northeast2zsouthamerica-east1zsouthamerica-west1)r   z
str | PathrD   rP   )rD   z"tuple[BaseCredentials, str | None])-
__future__r   _annotationsdataclassesr   r   r   r   pathlibr   typingr	   httpxr
   r"   _utilsr   
exceptionsr   toolsr    r   r   geminir   r   google.authr)   google.auth.credentialsr   BaseCredentialsgoogle.auth.transport.requestsr   google.oauth2.service_accountrP   ImportError_import_errorrK   r   r7   r9   r`   r;   r   rL   r.   r,   <module>ru      s    2 ( (   0 $ " " - 5	F6V & p2E p2 p2hq
 $' & & &0 %'PQ  
	c s   B" "B5'	B00B5