
    ^h                         S SK r S SKrS SKJrJrJrJrJrJr  S SK	J
r
  S SKJr  S SKJr  S SKJrJrJrJr   " S S\5      rg)	    N)AnyDictListOptionalTupleUnion)+_convert_vertex_datetime_to_openai_datetime)_transform_request_body)VertexGeminiConfig)CreateFileRequest
FileObject	FileTypesPathLikec                       \ rS rSrSr SS\\   S\\\4   4S jjr	S\
\\\4      4S jrS\
\\\4      S\4S	 jrS
\\\4   S\\\4   4S jrS\S\4S jrS\S\\\4   S\4S jrSrg)VertexAIFilesTransformation   zI
Transforms OpenAI /v1/files/* requests to VertexAI /v1/files/* requests
Nopenai_file_contentreturnc                 T   Uc  [        S5      eU R                  U5      nUR                  5        Vs/ s H/  o3R                  5       (       d  M  [        R
                  " U5      PM1     nnU R                  U5      nSR                  S U 5       5      nU R                  US9nXg4$ s  snf )zE
Transforms OpenAI FileContentRequest to VertexAI FileContentRequest
zcontents of file are None
c              3   N   #    U  H  n[         R                  " U5      v   M     g 7fN)jsondumps).0items     e/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/vertex_ai/files/transformation.py	<genexpr>fVertexAIFilesTransformation.transform_openai_file_content_to_vertex_ai_file_content.<locals>.<genexpr>)   s       (
)=DJJt)=s   #%)openai_jsonl_content)	
ValueError_get_content_from_openai_file
splitlinesstripr   loads:_transform_openai_jsonl_content_to_vertex_ai_jsonl_contentjoin_get_gcs_object_name)selfr   file_contentliner    vertex_jsonl_contentvertex_jsonl_stringobject_names           r   7transform_openai_file_content_to_vertex_ai_file_contentSVertexAIFilesTransformation.transform_openai_file_content_to_vertex_ai_file_content   s     &89999:MN *6)@)@)B 
)BjjlDJJt)B 	  
 KK$ 	
 #ii (
)=(
 
 //!5 0 
 #// 
s   B%B%r    c           
          / nU Hm  nUR                  S5      =(       d    0 n[        UR                  S/ 5      UR                  SS5      U R                  U5      S0 SS9nUR                  SU05        Mo     U$ )	a  
Transforms OpenAI JSONL content to VertexAI JSONL content

jsonl body for vertex is {"request": <request_body>}
Example Vertex jsonl
{"request":{"contents": [{"role": "user", "parts": [{"text": "What is the relation between the following video and image samples?"}, {"fileData": {"fileUri": "gs://cloud-samples-data/generative-ai/video/animals.mp4", "mimeType": "video/mp4"}}, {"fileData": {"fileUri": "gs://cloud-samples-data/generative-ai/image/cricket.jpeg", "mimeType": "image/jpeg"}}]}]}}
{"request":{"contents": [{"role": "user", "parts": [{"text": "Describe what is happening in this video."}, {"fileData": {"fileUri": "gs://cloud-samples-data/generative-ai/video/another_video.mov", "mimeType": "video/mov"}}]}]}}
bodymessagesmodel 	vertex_aiN)r3   r4   optional_paramscustom_llm_providerlitellm_paramscached_contentrequest)getr
   _map_openai_to_vertex_paramsappend)r)   r    r,   _openai_jsonl_contentopenai_request_bodyvertex_request_bodys         r   r&   VVertexAIFilesTransformation._transform_openai_jsonl_content_to_vertex_ai_jsonl_content1   s      "%9!"7";";F"C"Ir"9,00R@)--gr: $ A ABU V$/!## !''4G(HI &: $#    c                     US   R                  S0 5      R                  SS5      nSU;  a  SU 3nSU S[        R                  " 5        3nU$ )	zn
Gets a unique GCS object name for the VertexAI batch prediction job

named as: litellm-vertex-{model}-{uuid}
r   r2   r4   r5   zpublishers/google/modelszpublishers/google/models/zlitellm-vertex-files//)r<   uuiduuid4)r)   r    _modelr.   s       r   r(   0VertexAIFilesTransformation._get_gcs_object_nameK   sZ     &a(,,VR8<<WbI%V309F-fXQtzz|nErC   r@   c                 N    UR                  SS5      nU R                  UU0 SS9nU$ )z6
wrapper to call VertexGeminiConfig.map_openai_params
r4   r5   F)r4   non_default_paramsr7   drop_params)r<   map_openai_params)r)   r@   rH   vertex_paramss       r   r=   8VertexAIFilesTransformation._map_openai_to_vertex_paramsZ   s?     %(("5..2	 / 
 rC   c                    Sn[        U[        5      (       a  US   nOUn[        U[        5      (       a  UnO}[        U[        5      (       a  UnOe[        U[        5      (       a/  [        [        U5      S5       nUR                  5       nSSS5        O![        US5      (       a  UR                  5       n[        U[        5      (       a  UR                  S5      nU$ ! , (       d  f       N6= f)z
Helper to extract content from various OpenAI file types and return as string.

Handles:
- Direct content (str, bytes, IO[bytes])
- Tuple formats: (filename, content, [content_type], [headers])
- PathLike objects
rC      rbNreadzutf-8)	
isinstancetuplestrbytesr   openrS   hasattrdecode)r)   r   contentr*   fs        r   r"   9VertexAIFilesTransformation._get_content_from_openai_filej   s     &))511.q1L.L lC(("Ge,,"Gh//c,'.!&&( /.\6**"'')G gu%%nnW-G /.s   :C
C+create_file_datagcs_upload_responsec                 0   UR                  SS5      nU(       a#  SR                  UR                  S5      SS 5      OSn[        UR                  SS5      SU 3UR                  S	S5      [	        UR                  S
S5      S9SUR                  SS5      SS9$ )zA
Transforms GCS Bucket upload file response to OpenAI FileObject
idr5   rE   Npurposebatchzgs://nametimeCreated)vertex_datetimeuploadedsizer   file)rc   ra   filename
created_atstatusrW   object)r<   r'   splitr   r	   )r)   r^   r_   gcs_ids       r   3transform_gcs_bucket_response_to_openai_file_objectOVertexAIFilesTransformation.transform_gcs_bucket_response_to_openai_file_object   s     %((r25;&,,s+CR01$((G<vh(,,VR8B 3 7 7r J %))&!4

 
	
rC    r   )__name__
__module____qualname____firstlineno____doc__r   r   r   rV   r/   r   r   r   r&   r(   r=   r"   r   r   rq   __static_attributes__rs   rC   r   r   r      s    
 :>0#+I#60	sCx0:$$(c3h$8$4"4S>2 
!#s(^ 
c3h # #s #J
 1
HLSRUX
	
rC   r   )r   rF   typingr   r   r   r   r   r   #litellm.llms.vertex_ai.common_utilsr	   ,litellm.llms.vertex_ai.gemini.transformationr
   @litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_geminir   litellm.types.llms.openair   r   r   r   r   rs   rC   r   <module>r      s9      : : Q Y XT
"4 T
rC   