
    ^hz                     V    S SK r S SKJr  S SKJr  S SKJrJrJr  S SK	7   " S S5      r
g)    N)Dict)+_convert_vertex_datetime_to_openai_datetime)BatchBatchJobStatusCreateBatchRequest)*c                       \ rS rSrSr\S\S\4S j5       r\S\	S\
4S j5       r\S\	S\4S j5       r\S\	S\4S	 j5       r\S\	S\4S
 j5       r\S\S\4S j5       r\S\S\4S j5       rSrg)VertexAIBatchTransformation   z
Transforms OpenAI Batch requests to Vertex AI Batch requests

API Ref: https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/batch-prediction-gemini
requestreturnc                    S[         R                  " 5        3nUR                  S5      nUc  [        S5      e[	        [        US9SS9nU R                  U5      n[        S[        U R                  U5      S9S9n[        UUUUS	9$ )
z>
Transforms OpenAI Batch requests to Vertex AI Batch requests
zlitellm-vertex-batch-input_file_idz+input_file_id is required, but not provided)urisjsonl)	gcsSourceinstancesFormat)outputUriPrefix)predictionsFormatgcsDestination)inputConfigoutputConfigmodeldisplayName)uuiduuid4get
ValueErrorInputConfig	GcsSource_get_model_from_gcs_fileOutputConfigGcsDestination_get_gcs_uri_prefix_from_fileVertexAIBatchPredictionJob)clsr   request_display_namer   input_configr   output_configs          g/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/vertex_ai/batches/transformation.py9transform_openai_batch_request_to_vertex_ai_batch_requestUVertexAIBatchTransformation.transform_openai_batch_request_to_vertex_ai_batch_request   s     "7tzz|nEO4 JKK$/]3W%
 11-@&2%) # A A- P'
 *$&,	
 	
    responsec                     [        UR                  SS5      S[        UR                  SS5      S9SU R                  U5      SU R	                  U5      S U R                  U5      S9	$ )Nname 24hrs
createTime)vertex_datetimebatch)	idcompletion_window
created_atendpointr   objectstatuserror_file_idoutput_file_id)r   r   r   0_get_input_file_id_from_vertex_ai_batch_response3_get_batch_job_status_from_vertex_ai_batch_response1_get_output_file_id_from_vertex_ai_batch_response)r&   r.   s     r*   ;transform_vertex_ai_batch_response_to_openai_batch_responseWVertexAIBatchTransformation.transform_vertex_ai_batch_response_to_openai_batch_response/   su     ||FB'%B (\2 > NN JJ8TPP
 	
r-   c                     SnUR                  S5      nUc  U$ UR                  S5      nUc  U$ UR                  SS5      n[        U5      S:X  a  U$ US   $ )z:
Gets the input file id from the Vertex AI Batch response
r1   r   r   r   r   )r   len)r&   r.   r   r(   
gcs_sourcer   s         r*   r>   LVertexAIBatchTransformation._get_input_file_id_from_vertex_ai_batch_responseE   si      ||M2  !%%k2
  ~~fb)t9>  Awr-   c                     SnUR                  S5      nUc  U$ UR                  S5      nUc  U$ UR                  SS5      nU$ )z;
Gets the output file id from the Vertex AI Batch response
r1   r   r   r   r   )r&   r.   r=   r)   gcs_destinationoutput_uri_prefixs         r*   r@   MVertexAIBatchTransformation._get_output_file_id_from_vertex_ai_batch_response[   sZ     ! ^4 !!'++,<="!!+//0A2F  r-   c                 L    SSSSSSSSSSSSS.nUR                  S	S
5      nX#   $ )z
Gets the batch job status from the Vertex AI Batch response

ref: https://cloud.google.com/vertex-ai/docs/reference/rest/v1/JobState
failed
validatingin_progress	completed
cancelling	cancelledexpired)JOB_STATE_UNSPECIFIEDJOB_STATE_QUEUEDJOB_STATE_PENDINGJOB_STATE_RUNNINGJOB_STATE_SUCCEEDEDJOB_STATE_FAILEDJOB_STATE_CANCELLINGJOB_STATE_CANCELLEDJOB_STATE_PAUSEDJOB_STATE_EXPIREDJOB_STATE_UPDATINGJOB_STATE_PARTIALLY_SUCCEEDEDstaterT   rH   )r&   r.   state_mappingvertex_states       r*   r?   OVertexAIBatchTransformation._get_batch_job_status_from_vertex_ai_batch_responsen   sK     &. ,!-!.#. ($0#. -!*"/-84
  ||G-DE**r-   r   c                 0    UR                  SS5      nUS   $ )a  
Gets the gcs uri prefix from the input file id

Example:
input_file_id: "gs://litellm-testing-bucket/vtx_batch.jsonl"
returns: "gs://litellm-testing-bucket"

input_file_id: "gs://litellm-testing-bucket/batches/vtx_batch.jsonl"
returns: "gs://litellm-testing-bucket/batches"
/   r   )rsplit)r&   r   
path_partss      r*   r$   9VertexAIBatchTransformation._get_gcs_uri_prefix_from_file   s      #))#q1
!}r-   gcs_file_uric                     SSK Jn  U" U5      nUR                  S5      S   nUR                  S5      nSSR                  USS 5       3nU$ )a  
Extracts the model from the gcs file uri

When files are uploaded using LiteLLM (/v1/files), the model is stored in the gcs file uri

Why?
- Because Vertex Requires the `model` param in create batch jobs request, but OpenAI does not require this


gcs_file_uri format: gs://litellm-testing-bucket/litellm-vertex-files/publishers/google/models/gemini-1.5-flash-001/e9412502-2c91-42a6-8e61-f5c294cc0fc8
returns: "publishers/google/models/gemini-1.5-flash-001"
r   )unquotezpublishers/rf   re   N   )urllib.parserl   splitjoin)r&   rj   rl   decoded_uri
model_pathpartsr   s          r*   r!   4VertexAIBatchTransformation._get_model_from_gcs_file   sW     	)l+ &&}5a8
  %chhuRay123r-    N)__name__
__module____qualname____firstlineno____doc__classmethodr   r%   r+   VertexBatchPredictionResponser   rA   strr>   r@   r   r?   r$   r!   __static_attributes__ru   r-   r*   r
   r
      s    
#
 
$
 
8 
4
	
 
* 4	 * !4!	! !$ +4+	+ +4 # #   C C  r-   r
   )r   typingr   #litellm.llms.vertex_ai.common_utilsr   litellm.types.llms.openair   r   r   litellm.types.llms.vertex_air
   ru   r-   r*   <module>r      s(      P O *c cr-   