
    g                         d dl Z d dlZd dlmZmZmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZmZ  G d de      Zy)	    N)AnyDictListOptionalTupleUnion)+_convert_vertex_datetime_to_openai_datetime)_transform_request_body)VertexGeminiConfig)CreateFileRequest
FileObject	FileTypesPathLikec                       e Zd ZdZ	 ddee   deeef   fdZde	e
eef      fdZde	e
eef      defdZd	e
eef   de
eef   fd
ZdedefdZdede
eef   defdZy)VertexAIFilesTransformationzQ
    Transforms OpenAI /v1/files/* requests to VertexAI /v1/files/* requests
    Nopenai_file_contentreturnc                 H   |t        d      | j                  |      }|j                         D cg c](  }|j                         st	        j
                  |      * }}| j                  |      }dj                  d |D              }| j                  |      }||fS c c}w )zU
        Transforms OpenAI FileContentRequest to VertexAI FileContentRequest
        zcontents of file are None
c              3   F   K   | ]  }t        j                  |        y wN)jsondumps).0items     `/var/www/openai/venv/lib/python3.12/site-packages/litellm/llms/vertex_ai/files/transformation.py	<genexpr>zfVertexAIFilesTransformation.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_contentzSVertexAIFilesTransformation.transform_openai_file_content_to_vertex_ai_file_content   s     &89999:MN *6)@)@)B 
)BdjjlDJJt)B 	  
 KK$ 	
 #ii (
)=(
 
 //!5 0 
 #K// 
s   BBr   c           	          g }|D ]j  }|j                  d      xs i }t        |j                  dg       |j                  dd      | j                  |      di d      }|j                  d|i       l |S )	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)r0   r1   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$   zVVertexAIFilesTransformation._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                     |d   j                  di       j                  dd      }d|vrd| }d| dt        j                          }|S )	z
        Gets a unique GCS object name for the VertexAI batch prediction job

        named as: litellm-vertex-{model}-{uuid}
        r   r/   r1   r2   zpublishers/google/modelszpublishers/google/models/zlitellm-vertex-files//)r9   uuiduuid4)r'   r   _modelr,   s       r   r&   z0VertexAIFilesTransformation._get_gcs_object_nameK   sZ     &a(,,VR8<<WbI%V309F-fXQtzz|nEr?   r=   c                 T    |j                  dd      }| j                  ||i d      }|S )zF
        wrapper to call VertexGeminiConfig.map_openai_params
        r1   r2   F)r1   non_default_paramsr4   drop_params)r9   map_openai_params)r'   r=   rD   vertex_paramss       r   r:   z8VertexAIFilesTransformation._map_openai_to_vertex_paramsZ   s?     %(("5..2	 / 
 r?   c                    d}t        |t              r|d   }n|}t        |t              r|}nnt        |t              r|}n[t        |t              r/t        t        |      d      5 }|j                         }ddd       nt        |d      r|j                         }t        |t              r|j                  d      }|S # 1 sw Y   ,xY w)a  
        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
        r?      rbNreadzutf-8)	
isinstancetuplestrbytesr   openrM   hasattrdecode)r'   r   contentr(   fs        r   r    z9VertexAIFilesTransformation._get_content_from_openai_filej   s     &))51.q1L.L lC("Ge,"Gh/c,'.!&&( /.\6*"'')G gu%nnW-G /.s   &B??Ccreate_file_datagcs_upload_responsec                 2   |j                  dd      }|r#dj                  |j                  d      dd       nd}t        |j                  dd      d| |j                  d	d      t	        |j                  d
d            d|j                  dd      d      S )zQ
        Transforms GCS Bucket upload file response to OpenAI FileObject
        idr2   rA   Npurposebatchzgs://nametimeCreated)vertex_datetimeuploadedsizer   file)r\   rZ   filename
created_atstatusrQ   object)r9   r%   splitr   r	   )r'   rW   rX   gcs_ids       r   3transform_gcs_bucket_response_to_openai_file_objectzOVertexAIFilesTransformation.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

 
	
r?   r   )__name__
__module____qualname____doc__r   r   r   rP   r-   r   r   r   r$   r&   r:   r    r   r   rj    r?   r   r   r      s    
 :>0#+I#60	sCx0:$$(c3h$8$4"4S>2 
!#s(^ 
c3h # #s #J
 1
HLSRUX
	
r?   r   )r   rB   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   ro   r?   r   <module>ru      s9      : : Q Y XT
"4 T
r?   