
    +#hh                         d 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	Z	ddl
mZmZ ddlmZ d Zd	 Z G d
 de      Zy# e$ r Y w xY w)z-Module for interacting with Google Vertex AI.    )AnyDictN)PydanticCustomError)LM)CodeGenerationModelTextGenerationModel)GenerativeModelc                 L    t        d| d   dd| d    d| d    d| d	           y
)z.Handler from https://pypi.org/project/backoff/zBacking off waitz0.1fz seconds after triesz tries calling function targetz with kwargs kwargsN)printdetailss    W/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dsp/modules/googlevertexai.pybackoff_hdlrr      sF    	L.ogg>N=O P%h/0X! "    c                 "    d| j                   v ryy)z6wrapper function that decides when to give up on retryzrate limitsFT)messager   s    r   giveup_hdlrr      s    'r   c                        e Zd ZdZ	 ddef fdZededdfd       Zde	de
fd	Zd
efdZ ej                  ej                  edee      d
efd       Z	 	 dd
ededefdZ xZS )GoogleVertexAIzcWrapper around GoogleVertexAI's API.

    Currently supported models include `gemini-pro-1.0`.
    modelc                    t         |   |       d|v | _        | j                  |       d|v rt        }h d| _        n?d|v rt        }h d| _        n+d|v rt        }h d| _        nt        ddt        |            | j                  r |||j                  d	      
      | _        n|j                  |      | _        d| _        i | j                  ddddd|| _        y)a  
        Parameters
        ----------
        model : str
            Which pre-trained model from Google to use?
            Choices are ['gemini-1.0-pro-001', 'gemini-1.0-pro',
            'claude-3-sonnet@20240229', 'claude-3-sonnet@20240229', 'claude-3-haiku@20240307',
            'text-bison@002', 'text-bison-32k@002', 'text-bison',]
            full list at https://console.cloud.google.com/vertex-ai/model-garden
        **kwargs: dict
            Additional arguments to pass to the API provider.
        geminicode>   suffixtemperaturestop_sequencescandidate_countmax_output_tokens>   top_ktop_pr   r    r!   r"   textr   z+model name is not valid, got "{model_name}")wrong_valuesafety_settings)
model_namer'   googlevertexaigffffff?i   g      ?   )r   r"   r$   r#   N)super__init__
_is_gemini_init_vertexair   available_argsr	   r   r   dictgetclientfrom_pretrainedproviderr   )selfr   r   	model_cls	__class__s       r   r,   zGoogleVertexAI.__init__$   s     	"e+F#U?+I#D 'I#D u_+I#D &=' 
 ??#ufjjQbFcdDK#33E:DK(
kk
!%
 
r   valuesreturnNc                     t        j                  |j                  d      |j                  d      |j                  d             y )Nprojectlocationcredentials)r;   r<   r=   )vertexaiinitr1   )clsr8   s     r   r.   zGoogleVertexAI._init_vertexaig   s7    JJy)ZZ
+

=1	

 	r   
parametersc                 B   |j                  d      }ddd}|j                         D ci c]  \  }}|j                  ||      | }}}i | j                  d|i|}t        |j	                               | j
                  z  D ci c]  }|||   
 c}S c c}}w c c}w )Nstopr!   r"   )n
max_tokensr    )r1   itemsr   setkeysr/   )r5   rA   r    params_mappingkvparamss          r   _prepare_paramszGoogleVertexAI._prepare_paramsp   s     $/0?RS:D:J:J:LM$!Q.$$Q*A-MMLDKKL!1>LVL&)&++-&84;N;N&NO6!9OO NOs   BBpromptc                    |}| j                  |      }| j                  r| j                  j                  |g|      }|||j                  D cg c]^  }dj                  d |j                  j                  D              |j                  D ci c]  }|j                  |j                   c}d` c}}d||d}nR | j                  j                  |fi |j                  }|||j                  D cg c]  }|d   |d   d c}d||d}| j                  j                  |       |d	   d
   D cg c]  }|d   	 c}S c c}w c c}}w c c}w c c}w )N)generation_config
c              3   4   K   | ]  }|j                     y wN)r%   ).0rK   s     r   	<genexpr>z/GoogleVertexAI.basic_request.<locals>.<genexpr>   s     )JQ!&&)Js   )r%   safetyAttributes)rN   choices)rN   responser   
raw_kwargscontentrV   rX   rW   r%   )rM   r-   r2   generate_content
candidatesjoinrZ   partssafety_ratingscategoryprobabilitypredictraw_prediction_responsepredictionshistoryappend)	r5   rN   r   rY   rX   crK   re   is	            r   basic_requestzGoogleVertexAI.basic_requestz   si   
%%j1??{{33"( 4 H
 !$
 "*!4!4	 6  !%		)J!))//)J JPQP`P`,a1QZZ-F,a!  6 !(G +t{{**6<V<TTH $)1)=)= ?$% *+91M_K` a  ?
 !(	G 	G$#*:#6y#ABa&	BB+ -b 6 ? Cs$   >E
E+E
4E4EE
i  )max_time
on_backoffgiveupc                 (     | j                   |fi |S )zGHandles retrieval of completions from Google whilst handling API errors)ri   )r5   rN   r   s      r   requestzGoogleVertexAI.request   s     "t!!&3F33r   only_completedreturn_sortedc                 (     | j                   |fi |S rS   )rn   )r5   rN   ro   rp   r   s        r   __call__zGoogleVertexAI.__call__   s     t||F-f--r   )ztext-bison@002)TF)__name__
__module____qualname____doc__strr,   classmethodr   r.   r   r0   rM   ri   backoffon_exceptionexpo	Exceptionr   r   rn   boolrr   __classcell__)r7   s   @r   r   r      s     ,A
A
F D T  PP 
P#CC #CJ W	4c 44  $#	.. . 	.r   r   )rv   typingr   r   ry   pydantic_corer   dsp.modules.lmr   r>   vertexai.language_modelsr   r   "vertexai.preview.generative_modelsr	   ImportErrorr   r   r    r   r   <module>r      sN    3   - 	QB
"S.R S.#  		s   A   AA