
    ^h/                         S SK r S SKJr  S SKJrJrJr  S SKrS SKJrJ	r	  S SK
r
S SKJr  S SKJrJr  S SKJr  S SKJr  S S	KJrJrJr   " S
 S\5      rg)    N)datetime)LiteralOptionalUnion)FineTuningJobHyperparameters)verbose_logger)HTTPHandlerget_async_httpx_client)	VertexLLM)FineTuningJobCreate)FineTuneJobCreateFineTunesupervisedTuningSpecResponseTuningJobc                      ^  \ rS rSrSrSU 4S jjrS\4S jrS\S\4S jr	S\
S\4S	 jrS
\S\S\4S jrS\S\
S\\   S\\   S\\   S\\   S\\\R*                  4   4S jrS\S\S\S\S\4
S jrSrU =r$ )VertexFineTuningAPI   z'
Vertex methods to support for batches
returnc                 t   > [         TU ]  5         [        [        R                  R
                  SS0S9U l        g )Ntimeout     @)llm_providerparams)super__init__r   litellmLlmProviders	VERTEX_AIasync_handler)self	__class__s    d/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/vertex_ai/fine_tuning/handler.pyr   VertexFineTuningAPI.__init__   s2    3 --77u%
    responsec                      UR                  SS5      =(       d    Sn[        R                  " UR                  SS5      5      n[	        UR                  5       5      nU$ ! [         a     gf = f)N
createTime Zz+00:00r   )getr   fromisoformatreplaceint	timestamp	Exception)r    r%   create_time_strcreate_time_datetime
created_ats        r"   convert_response_created_at/VertexFineTuningAPI.convert_response_created_at    sk    	&ll<<BO#+#9#9''X6$  1;;=>J 		s   AA 
A+*A+c           	         SnUS   S:X  a  SnUS   S:X  a  SnUS   S:X  a  SnUS   S:X  a  S	nUS   S
:X  a  SnU R                  U5      nSnSU;   a  US   (       a  US   S   =(       d    Sn[        S%0 SUS   =(       d    S_SU_SUS   _SS _S[        SS9_SUS   =(       d    S_SS_SS_S/ _SS_SU_S S _S!U_S"S _S#S _S$/ _6$ )&NqueuedstateJOB_STATE_PENDINGJOB_STATE_SUCCEEDED	succeededJOB_STATE_FAILEDfailedJOB_STATE_CANCELLED	cancelledJOB_STATE_RUNNINGrunningr(   supervisedTuningSpectrainingDatasetUriidnamer2   fine_tuned_modeltunedModelDisplayNamefinished_athyperparametersr   )n_epochsmodel	baseModelobjectzfine_tuning.joborganization_idresult_filesseedstatustrained_tokenstraining_filevalidation_fileestimated_finishintegrations )r3   r   r   )r    r%   rP   r2   training_uris        r"   +convert_vertex_response_to_open_ai_response?VertexFineTuningAPI.convert_vertex_response_to_open_ai_response.   sv   
  	 G 33FG 55 FG 22FG 55 FG 33F55h?
!X-(;Q2R#$:;<PQWUWL 
%2
!
 &&=>
 	

 ,
 ;'-2
 %
 
 
 
 
  
 '
  !!
" "#
$ %
 	
r$   create_fine_tuning_job_datac                 r   UR                   n[        UR                  UR                  S9nU(       aR  UR                  (       a  [        UR                  5      US'   UR                  (       a  [        UR                  5      US'   UR                  S5      US'   [        UR                  UUR                  S9nU$ )z
convert request from OpenAI format to Vertex format
https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/tuning
supervised_tuning_spec = FineTunesupervisedTuningSpec(
)training_dataset_urivalidation_datasetepoch_countlearning_rate_multiplieradapter_size)rK   rA   rF   )rH   r   rR   rS   rI   r-   r_   floatr*   r   rJ   suffix)r    rZ   kwargsrH   supervised_tuning_specfine_tune_jobs         r"    convert_openai_request_to_vertex4VertexFineTuningAPI.convert_openai_request_to_vertexZ   s     6EE!=!<!J!J:JJ"

 ''8;O<T<T8U&}577EJ#<<F&'AB 28N1K~.)177!7"="D"D
 r$   fine_tuning_urlheadersrequest_datac                   #     [         R                  " SUU5        U R                  c  [        S5      eU R                  R	                  UUUS9I S h  vN nUR
                  S:w  a%  [        SUR
                   SUR                   35      e[         R                  " SUR                  5       5        [        S
0 UR                  5       D6n[         R                  " SU5        U R                  U5      nU$  N! [         aI  n[         R                  " S	U5        [        R                  " 5       n[         R                  " U5        UeS nAff = f7f)N5about to create fine tuning job: %s, request_data: %s7VertexAI Fine Tuning - async_handler is not initializedri   urljson   -Error creating fine tuning job. Status code: . Response: .got response from creating fine tuning job: %svertex_response %sz&asyncerror creating fine tuning job %srV   )r	   debugr   
ValueErrorpoststatus_coder/   textrp   r   rX   error	traceback
format_exc)	r    rh   ri   rj   r%   vertex_responseopen_ai_responseetrace_back_strs	            r"   acreate_fine_tuning_job+VertexFineTuningAPI.acreate_fine_tuning_jobz   sS    '	  G
 !!) M  "//44#! 5  H ##s*CHDXDXCYYefnfsfsetu    @(--/ 0 --/O   !5G#OO  $#/2  	  !I1M&113N  0G		s=   EAC6 C4B C6 3E4C6 6
E	 AEE		E	_is_asyncvertex_projectvertex_locationvertex_credentialsapi_baser   c                    [         R                  " SU5        U R                  UUSS9u  pU R                  SU	S UUUSSUS9	u  pSU
 3SS	.nU R                  " SS
U0UD6nSU SU SU S3nUSL a  U R                  UUUS9$ [        [        R                  " SSS9S9n[         R                  " SUU5        UR                  UUUS9nUR                  S:w  a%  [        SUR                   SUR                   35      e[         R                  " SUR                  5       5        [        S0 UR                  5       D6n[         R                  " SU5        U R                  U5      nU$ )Nz"creating fine tuning job, args= %svertex_ai_betacredentials
project_idcustom_llm_providerr(   F	rJ   auth_headergemini_api_keyr   r   r   streamr   r   Bearer application/jsonAuthorizationzContent-TyperZ   https://'-aiplatform.googleapis.com/v1/projects//locations//tuningJobsT)rh   ri   rj   r   g      @)r   connect)r   rl   rn   rq   rr   rs   rt   ru   rV   )r	   rv   _ensure_access_token_get_token_and_urlrf   r   r
   httpxTimeoutrx   ry   r/   rz   rp   r   rX   )r    r   rZ   r   r   r   r   r   rc   _auth_headerr   _ri   re   rh   sync_handlerr%   r~   r   s                      r"   create_fine_tuning_job*VertexFineTuningAPI.create_fine_tuning_job   s    	02M	
 (,'@'@*% 0 (A (
$ 00$1)+ 0 1 

  '{m4.

 == 
(C
GM
 %_$55\]k\llw  yH  xI  IT  U// /* 0  
 #5==PS+TUC	

  $$ % 
 3&?@T@T?UUabjboboapq  	<hmmo	
 , 
mmo
 	1?CKK
  r$   request_routec                 $  #    U R                  UUSS9I S h  vN u  pbU R                  SUS UUUSSSS9	u  pxSU 3SS.n	S n
US	:X  a  S
U SU SU S	3n
OSU;   a  SU;   a  S
U SU SU S	U 3n
OSU;   a  S
U SU SU U 3n
OSU;   a  S
U SU SU U 3n
OrSU;   a  S
U SU SU U 3n
O^SU;   a  S
U SU SU U 3n
OJSU;   a6  UR                  S5      nUb  SU;  a  SU SU SU 3US'   S
U SU SU U 3n
O[        SU 35      eU R                  c  [        S5      eU R                  R                  U	U
US9I S h  vN nUR                  S:w  a%  [        SUR                   SUR                   35      eUR                  5       nU$  GNt NN7f)Nr   r   r(   Fr   r   r   r   r   r   r   r   z/tuningJobs/cancelgenerateContentpredictz/batchPredictionJobscountTokenscachedContentsrJ   z/publishers/google/models/z	projects/z,-aiplatform.googleapis.com/v1beta1/projects/z%Unsupported Vertex AI request route: rm   rn   rq   rr   rs   )
_ensure_access_token_asyncr   r*   rw   r   rx   ry   r/   rz   rp   )r    rj   r   r   r   r   r   r   r   ri   ro   _modelr%   response_jsons                 r"   #pass_through_vertex_ai_POST_request7VertexFineTuningAPI.pass_through_vertex_ai_POST_request   s#     .2-L-L*% 0 .M .
 (
$
 00$1)+ 0 1 

  '{m4.

 M)_--TUcTddop  pA  AL  MC},]1J_--TUcTddop  pA  AL  MZ  L[  \C-/_--TUcTddop  pA  BO  AP  QC-'_--TUcTddop  pA  BO  AP  QC#}4_--TUcTddop  pA  BO  AP  QCm+_--TUcTddop  pA  BO  AP  QC.!%%g.F!&B&&P/{?:KKeflemn W% _--YZhYiit  vE  uF  GT  FU  VCD]OTUU%VWW++00 1 
 
 3&?@T@T?UUabjboboapq  !y(
`
s#   FFD'F?F AFF)r   )r   N)__name__
__module____qualname____firstlineno____doc__r   r   r3   r   rX   r   r   rf   strdictr   boolr   r   ra   r   r   r   r   __static_attributes____classcell__)r!   s   @r"   r   r      s   
4E *
)*
	*
X+>	@.. . (	.`N N  &9N  !	N 
 "#N  %SMN  3-N  uemm+,N `DD D 	D
  D D Dr$   r   )r|   r   typingr   r   r   r   (openai.types.fine_tuning.fine_tuning_jobr   r   r   litellm._loggingr	   &litellm.llms.custom_httpx.http_handlerr
   r   @litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_geminir   litellm.types.llms.openair   litellm.types.llms.vertex_air   r   r   r   rV   r$   r"   <module>r      s>      + +  S  + V V 9 j) jr$   