
    hG                     "   d dl Z d dlZ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mZ d dlmZ d dlmZ d dl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m Z m!Z! d dl"m#Z# d dl$m%Z%m&Z& ddl'm(Z( ddl)m*Z*  G d de*e      Z+ G d de      Z,y)    N)AnyDictListOptionalTupleUnion)HeadersResponse)extract_file_data)BaseLLMException)BaseFilesConfigLiteLLMLoggingObj)+_convert_vertex_datetime_to_openai_datetime)_transform_request_bodyVertexGeminiConfig)AllMessageValuesCreateFileRequest	FileTypes%OpenAICreateFileRequestOptionalParamsOpenAIFileObjectPathLike)GcsBucketResponse)ExtractedFileDataLlmProviders   VertexAIError)
VertexBasec                       e Zd ZdZ fdZedefd       Z	 	 d%dede	de
e   ded	ed
ee	   dee	   defdZdede	fdZde
ee	ef      de	fdZdede	de	fdZdee	   d
ee	   de	ded	edede	fdZde	de
e   fdZdedede	dedef
dZdee	ef   dee	ef   fdZde
ee	ef      de
ee	ef      fdZde	deded	edeee	ef   f
dZdee	   de d e!d	ede"f
d!Z#d"e	d#e$deee%f   de&fd$Z' xZ(S )&VertexAIFilesConfigz#
    Config for VertexAI Files
    c                 @    t               | _        t        |           y N) VertexAIJsonlFilesTransformationjsonl_transformationsuper__init__)self	__class__s    g/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/vertex_ai/files/transformation.pyr'   zVertexAIFilesConfig.__init__*   s    $D$F!    returnc                 "    t         j                  S r#   )r   	VERTEX_AI)r(   s    r*   custom_llm_providerz'VertexAIFilesConfig.custom_llm_provider.   s    %%%r+   headersmodelmessagesoptional_paramslitellm_paramsapi_keyapi_basec                     |sI| j                  |j                  d      |j                  d            \  }}|st        d      d| |d<   |S )Nvertex_credentialsvertex_project)credentials
project_idzapi_key is requiredzBearer Authorization)get_access_tokenget
ValueError)	r(   r0   r1   r2   r3   r4   r5   r6   _s	            r*   validate_environmentz(VertexAIFilesConfig.validate_environment2   se     ..*../CD)--.>? / JGQ  !677)0	':GO$r+   openai_file_contentc                    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   openrG   hasattrdecoder(   rB   contentfile_contentfs        r*   _get_content_from_openai_filez1VertexAIFilesConfig._get_content_from_openai_fileF        &))51.q1L.L lC("Ge,"Gh/c,'. #!&&(# #\6*"'')G gu%nnW-G# #   &B??Copenai_jsonl_contentc                     |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   bodyr1    zpublishers/google/modelszpublishers/google/models/zlitellm-vertex-files//r>   uuiduuid4r(   rW   _modelobject_names       r*   %_get_gcs_object_name_from_batch_jsonlz9VertexAIFilesConfig._get_gcs_object_name_from_batch_jsonlk   Z     &a(,,VR8<<WbI%V309F-fXQtzz|nEr+   extracted_file_datapurposec                    |j                  d      }|t        d      |dk(  rq| j                  |      }|j                         D cg c](  }|j	                         st        j                  |      * }}t        |      dkD  r| j                  |      S |j                  d      }|r|S t        t        t        j                                     S c c}w )z5
        Get the object name for the request
        rQ   zfile content is requiredbatchr   filename)r>   r?   rT   
splitlinesstripjsonloadslenrc   rK   inttime)r(   re   rf   extracted_file_data_contentrR   linerW   ri   s           r*   get_object_namez#VertexAIFilesConfig.get_object_namez   s     ':&=&=i&H#&.788g==+L .:-D-D-F$%)$**,

4 $  $ '(1,AABVWW '**:6O3tyy{#$$$s   CCdatac                 b   |j                  d      xs t        j                  d      }|st        d      |j                  d      }|j                  d      }	|t        d      |	t        d      t	        |      }
| j                  |
|	      }d| d	| }|xs d
}|st        d      | d| S )z6
        Get the complete url for the request
        bucket_nameGCS_BUCKET_NAMEzGCS bucket_name is requiredfilerf   file is requiredzpurpose is requiredzupload/storage/v1/b/z/o?uploadType=media&name=zhttps://storage.googleapis.comzapi_base is requiredr\   )r>   osgetenvr?   r   rs   )r(   r6   r5   r1   r3   r4   rt   rv   	file_datarf   re   rb   endpoints                r*   get_complete_file_urlz)VertexAIFilesConfig.get_complete_file_url   s     %((7W299EV;W:;;HHV$	((9%/00?233/	:**+>H";-/HV 	 ??3441XJ''r+   c                     g S r#    )r(   r1   s     r*   get_supported_openai_paramsz/VertexAIFilesConfig.get_supported_openai_params   s	     	r+   non_default_paramsdrop_paramsc                     |S r#   r   )r(   r   r3   r1   r   s        r*   map_openai_paramsz%VertexAIFilesConfig.map_openai_params   s
     r+   openai_request_bodyc                 n    ddl m}  |       }|j                  dd      }|j                  ||i d      }|S )F
        wrapper to call VertexGeminiConfig.map_openai_params
        r   r   r1   r[   Fr1   r   r3   r   )@litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_geminir   r>   r   )r(   r   r   configra   vertex_paramss         r*   _map_openai_to_vertex_paramsz0VertexAIFilesConfig._map_openai_to_vertex_params   sL    	
 $%$(("5002	 1 
 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"}}]}]}}
        rZ   r2   r1   r[   	vertex_aiN)r2   r1   r3   r/   r4   cached_contentrequestr>   r   r   appendr(   rW   vertex_jsonl_content_openai_jsonl_contentr   vertex_request_bodys         r*   :_transform_openai_jsonl_content_to_vertex_ai_jsonl_contentzNVertexAIFilesConfig._transform_openai_jsonl_content_to_vertex_ai_jsonl_content         "%9!"7";";F"C"Ir"9,00R@)--gr: $ A ABU V$/!## !''4G(HI &: $#r+   create_file_datac                    |j                  d      }|t        d      t        |      }|j                  d      }|j                  d      dk(  r|j                  d      dk(  rz|x| j                  |      }|j	                         D 	cg c](  }	|	j                         st        j                  |	      * }
}	| j                  |
      }t        j                  |      S t        |t              r|S t        d      c c}	w )	zk
        2 Cases:
        1. Handle basic file upload
        2. Handle batch file upload (.jsonl)
        rx   ry   rQ   rf   rh   content_typezapplication/jsonlzUnsupported file content type)r>   r?   r   rT   rj   rk   rl   rm   r   dumpsrI   rL   )r(   r1   r   r3   r4   r|   re   rq   rR   rr   rW   r   s               r*   transform_create_file_requestz1VertexAIFilesConfig.transform_create_file_request   s    %((0	/00/	:&9&=&=i&H#  +w6#''7;NN+7  ==+L .:-D-D-F$%)$**,

4 $  $ OO( !
 ::2333U;..<==$s   C:C:raw_responselogging_objc                    |j                         }	 t        di |}|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t        |j                  dd            d      S # t        $ r*}t        |j                  d| |j
                        d}~ww xY w)zV
        Transform VertexAI File upload response into OpenAI-style FileObject
        z#Error reading GCS bucket response: status_codemessager0   Nidr[   r\   rf   rh   gs://nametimeCreatedvertex_datetimeuploadedsizer   rx   rf   r   ri   
created_atstatusrL   objectr   )rl   r   	Exceptionr   r   r0   r>   joinsplitr   r   ro   )	r(   r1   r   r   r4   response_jsonresponse_objectegcs_ids	            r*   transform_create_file_responsez2VertexAIFilesConfig.transform_create_file_response   s    %))+	/@-@O !$$T2.5;&,,s+CR01#''	7;vh$((4B / 3 3M2 F o))&!45

 
	
  	(44=aSA$,, 	s   B> >	C1%C,,C1error_messager   c                     t        |||      S )Nr   r   )r(   r   r   r0   s       r*   get_error_classz#VertexAIFilesConfig.get_error_classE  s     #]G
 	
r+   )NN))__name__
__module____qualname____doc__r'   propertyr   r/   dictrK   r   r   r   rA   r   rT   r   r   rc   r   rs   r   r~   r   r   boolr   r   r   r   rL   r   r
   r   r   r   ro   r	   r   r   __classcell__)r)   s   @r*   r!   r!   %   s    &\ & & "&"&  '(	
   # 3- 
(# #s #J"4S>2 
%#4%?B%	%>(3-( #( 	(
 ( (  ( 
(@	3	4
   	
  
!#s(^ 
c3h*$$(c3h$8$	d38n	$4(>(> ,(> 	(>
 (> 
uc4	 (>T#
}#
 #
 '	#

 #
 
#
J
 
/2
=B4==Q
	
r+   r!   c                       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)r$   zQ
    Transforms OpenAI /v1/files/* requests to VertexAI /v1/files/* requests
    NrB   r,   c                 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r#   )rl   r   ).0items     r*   	<genexpr>zkVertexAIJsonlFilesTransformation.transform_openai_file_content_to_vertex_ai_file_content.<locals>.<genexpr>g  s      (
!%DJJt(
s   !)rW   )	r?   rT   rj   rk   rl   rm   r   r   _get_gcs_object_name)r(   rB   rR   rr   rW   r   vertex_jsonl_stringrb   s           r*   7transform_openai_file_content_to_vertex_ai_file_contentzXVertexAIJsonlFilesTransformation.transform_openai_file_content_to_vertex_ai_file_contentR  s     &89999:MN *6)@)@)B 
!%djjlDJJt 
  
 KK$ 	
 #ii (
)=(
 
 //!5 0 
 #K// 
s   BBrW   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 r   r   r   s         r*   r   z[VertexAIJsonlFilesTransformation._transform_openai_jsonl_content_to_vertex_ai_jsonl_contento  r   r+   c                     |d   j                  di       j                  dd      }d|vrd| }d| dt        j                          }|S rY   r]   r`   s       r*   r   z5VertexAIJsonlFilesTransformation._get_gcs_object_name  rd   r+   r   c                 T    |j                  dd      }| j                  ||i d      }|S )r   r1   r[   Fr   )r>   r   )r(   r   ra   r   s       r*   r   z=VertexAIJsonlFilesTransformation._map_openai_to_vertex_params  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rD   rH   rP   s        r*   rT   z>VertexAIJsonlFilesTransformation._get_content_from_openai_file  rU   rV   r   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
        r   r[   r\   Nr   rf   rh   r   r   r   r   r   r   r   rx   r   )r>   r   r   r   r   )r(   r   r   r   s       r*   3transform_gcs_bucket_response_to_openai_file_objectzTVertexAIJsonlFilesTransformation.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#   )r   r   r   r   r   r   r   rK   r   r   r   r   r   r   r   rT   r   r   r   r   r+   r*   r$   r$   M  s    
 :>0#+I#60	sCx0:$$(c3h$8$4"4S>2 
!#s(^ 
c3h # #s #J
 1
HLSRUX
	
r+   r$   )-rl   rz   rp   r^   typingr   r   r   r   r   r   httpxr	   r
   8litellm.litellm_core_utils.prompt_templates.common_utilsr   )litellm.llms.base_llm.chat.transformationr   *litellm.llms.base_llm.files.transformationr   r   #litellm.llms.vertex_ai.common_utilsr   ,litellm.llms.vertex_ai.gemini.transformationr   r   r   litellm.types.llms.openair   r   r   r   r   r   litellm.types.llms.vertex_air   litellm.types.utilsr   r   common_utilsr   vertex_llm_baser   r!   r$   r   r+   r*   <module>r      sm     	   : : # V F Q  ; ? ( (e
*o e
P	T
'9 T
r+   