
    g                     V    d dl Z d dlmZ d dlmZ d dlmZmZmZ d dl	  G d d      Z
y)    N)Dict)+_convert_vertex_datetime_to_openai_datetime)BatchBatchJobStatusCreateBatchRequest)*c                       e Zd ZdZededefd       Zedede	fd       Z
ededefd       Zededefd       Zededefd	       Zededefd
       Zededefd       Zededefd       Zy)VertexAIBatchTransformationz
    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                 &   dt        j                          }|j                  d      }|t        d      t	        t        |      d      }| j                  |      }t        dt        | j                  |                  }t        ||||	      S )
zN
        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          b/var/www/openai/venv/lib/python3.12/site-packages/litellm/llms/vertex_ai/batches/transformation.py9transform_openai_batch_request_to_vertex_ai_batch_requestzUVertexAIBatchTransformation.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                     t        | j                  |      dt        |j                  dd            d| j	                  |      d| j                  |      d | j                  |      	      S )N24hrs
createTime )vertex_datetimebatch)	idcompletion_window
created_atendpointr   objectstatuserror_file_idoutput_file_id)r   +_get_batch_id_from_vertex_ai_batch_responser   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_responsezWVertexAIBatchTransformation.transform_vertex_ai_batch_response_to_openai_batch_response/   su     >>xH%B (\2 > NN JJ8TPP
 	
r+   c                 `    |j                  dd      }|sy|j                  d      }|r|d   S |S )z
        Gets the batch id from the Vertex AI Batch response safely

        vertex response: `projects/510528649030/locations/us-central1/batchPredictionJobs/3814889423749775360`
        returns: `3814889423749775360`
        namer0   /)r   split)r%   r,   _namepartss       r)   r;   zGVertexAIBatchTransformation._get_batch_id_from_vertex_ai_batch_responseE   s;     VR( C !uRy,u,r+   c                     d}|j                  d      }||S |j                  d      }||S |j                  dd      }t        |      dk(  r|S |d   S )zJ
        Gets the input file id from the Vertex AI Batch response
        r0   r   r   r   r   )r   len)r%   r,   r   r'   
gcs_sourcer   s         r)   r<   zLVertexAIBatchTransformation._get_input_file_id_from_vertex_ai_batch_responseW   si      ||M2  !%%k2
  ~~fb)t9>  Awr+   c                     d}|j                  d      }||S |j                  d      }||S |j                  dd      }|S )zK
        Gets the output file id from the Vertex AI Batch response
        r0   r   r   r   r   )r%   r,   r:   r(   gcs_destinationoutput_uri_prefixs         r)   r>   zMVertexAIBatchTransformation._get_output_file_id_from_vertex_ai_batch_responsem   sZ     ! ^4 !!'++,<="!!+//0A2F  r+   c                 N    ddddddddddddd}|j                  d	d
      }||   S )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staterV   rK   )r%   r,   state_mappingvertex_states       r)   r=   zOVertexAIBatchTransformation._get_batch_job_status_from_vertex_ai_batch_response   sM     &. ,!-!.#. ($0#. -!*"/-84
  ||G-DE\**r+   r   c                 0    |j                  dd      }|d   S )aK  
        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"
        rB      r   )rsplit)r%   r   
path_partss      r)   r#   z9VertexAIBatchTransformation._get_gcs_uri_prefix_from_file   s      #))#q1
!}r+   gcs_file_uric                     ddl m}  ||      }|j                  d      d   }|j                  d      }ddj                  |dd        }|S )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   rB   N   )urllib.parserk   rD   join)r%   ri   rk   decoded_uri
model_pathrF   r   s          r)   r    z4VertexAIBatchTransformation._get_model_from_gcs_file   sW     	)l+ &&}5a8
  %chhuRay123r+   N)__name__
__module____qualname____doc__classmethodr   r$   r*   VertexBatchPredictionResponser   r?   strr;   r<   r>   r   r=   r#   r     r+   r)   r
   r
      s%    
#
 
$
 
8 
4
	
 
* -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
   rx   r+   r)   <module>r}      s(      P O *u ur+   