
    iq                      f    d dl mZmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
 d dlmZ  G d d      Zy	)
    )AnyDict)uuid)+_convert_vertex_datetime_to_openai_datetime)BatchJobStatusCreateBatchRequest)*)LiteLLMBatchc                      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eef   deeef   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        |g      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)r   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          ~/Users/manta/Documents/Projects/TheRoad-I1/.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$/m_5w%
 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_response0   su     >>xH%B (\2 > NN JJ8TPP
 	
r,   c                 6   |j                  dg       xs g }|D cg c]  }| j                  |       }}t        |      dkD  r|d   j                  nd}t        |      dkD  r|d   j                  nd}|j                  d      }d|||t	        |      |dS c c}w )z`
        Transforms Vertex AI batch list response into OpenAI-compatible list response.
        batchPredictionJobsr   NnextPageTokenlist)r8   datafirst_idlast_idhas_morenext_page_token)r   r@   lenr4   bool)r&   r-   
batch_jobsjobrF   rG   rH   rJ   s           r*   ?transform_vertex_ai_batch_list_response_to_openai_list_responsez[VertexAIBatchTransformation.transform_vertex_ai_batch_list_response_to_openai_list_responseF   s     \\"7<B
 "
! KKCP! 	 

 "%TQ47::D!$TQ$r(++D",,7  _-.
 	

s   B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`
        namer1   /rC   )r   split)r&   r-   _namepartss       r*   r<   zGVertexAIBatchTransformation._get_batch_id_from_vertex_ai_batch_responsea   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
        r1   r   r   r   r   )r   rK   )r&   r-   r   r(   
gcs_sourcer   s         r*   r=   zLVertexAIBatchTransformation._get_input_file_id_from_vertex_ai_batch_responses   si      ||M2  !%%k2
  ~~fb)t9>  Awr,   c                 Z   |j                  dt                     j                  dd      }|r|j                  d      dz   }|r|dk7  r|S |j                  d      }||S |j                  d      }||S |j                  dd      }|j                  d      r|S |j                  d      dz   S )	zK
        Gets the output file id from the Vertex AI Batch response
        
outputInfogcsOutputDirectoryr1   rR   z/predictions.jsonlr   r   r   )r   
OutputInforstripendswith)r&   r-   r;   r)   gcs_destinationoutput_uri_prefixs         r*   r?   zMVertexAIBatchTransformation._get_output_file_id_from_vertex_ai_batch_response   s     'll<FJJ "
 +2237:NNNn0DD!! ^4 !!'++,<="!!+//0A2F%%&:;$$ '',/CCCr,   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staterh   )r   )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"
        rR      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/rx   rR   N   )urllib.parser}   rS   join)r&   r{   r}   decoded_uri
model_pathrU   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@   r   strr   rO   r<   r=   r?   r   r>   r$   r!    r,   r*   r   r      s]    
#
 
$
 
8 
4
	
 
* 
CH~
	c3h
 
4 -4-	- -" 4	 * D4D	D D8 +4+	+ +4 # #   C C  r,   r   N)typingr   r   litellm._uuidr   #litellm.llms.vertex_ai.common_utilsr   litellm.types.llms.openair   r   litellm.types.llms.vertex_ailitellm.types.utilsr
   r   r   r,   r*   <module>r      s(      I * ,Z Zr,   