
    hR                         d dl Z d dlmZmZmZmZmZ d dlZd dl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  G d
 de      Zy)    N)Any	CoroutineDictOptionalUnion)_get_httpx_clientget_async_httpx_client)	VertexLLM)CreateBatchRequest)VERTEX_CREDENTIALS_TYPESVertexAIBatchPredictionJob)LiteLLMBatch   )VertexAIBatchTransformationc                   t    e Zd Zdef fdZdededee   dee   dee   dee   d	e	e
ej                  f   d
ee   de	eeeeef   f   fdZdededeeef   defdZdededefdZdededee   dee   dee   dee   d	e	e
ej                  f   d
ee   de	eeeeef   f   fdZdedeeef   defdZ xZS )VertexAIBatchPredictiongcs_bucket_namec                 2    t        |   |i | || _        y )N)super__init__r   )selfr   argskwargs	__class__s       b/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/vertex_ai/batches/handler.pyr   z VertexAIBatchPrediction.__init__   s    $)&).    	_is_asynccreate_batch_dataapi_basevertex_credentialsvertex_projectvertex_locationtimeoutmax_retriesreturnc	           	         t               }	| j                  ||d      \  }
}| j                  |xs d|xs |      }t        |j	                  d            dkD  r|j	                  d      d   }nd}| j                  |dd |d d |	      \  }}d
d|
 d}t        j                  |      }|du r| j                  |||      S |	j                  ||t        j                  |            }|j                  dk7  r%t        d|j                   d|j                         |j                         }t        j                  |      }|S )N	vertex_aicredentials
project_idcustom_llm_providerus-central1r"   r!   :r    r   r+   gemini_api_keyendpointstreamauth_headerurlapplication/json; charset=utf-8Bearer zContent-TypeAuthorization)requestT)vertex_batch_requestr   headersr6   r=   data   Error:  response)r   _ensure_access_tokencreate_vertex_batch_urllensplit_check_custom_proxyr   9transform_openai_batch_request_to_vertex_ai_batch_request_async_create_batchpostjsondumpsstatus_code	Exceptiontext;transform_vertex_ai_batch_response_to_openai_batch_response)r   r   r   r   r    r!   r"   r#   r$   sync_handleraccess_tokenr*   default_api_baser3   _r=   r<   rD   _json_responsevertex_batch_responses                       r   create_batchz$VertexAIBatchPrediction.create_batch   s    )*#'#<#<*% + $= $
 j  77+<})7Z 8 

 %%c*+a/'--c226HH.. +  / 
8 >&|n5

 <W  <Q  <Q%<
 ++%9! ,    $$01 % 
 3&gh&:&:%;1X]]OLMM! ; w w#!
 %$r   r<   r=   c                 l  K   t        t        j                  j                        }|j	                  ||t        j                  |             d {   }|j                  dk7  r%t        d|j                   d|j                         |j                         }t        j                  |      }|S 7 `w)Nllm_providerr>   r@   rA   rB   rC   )r	   litellmLlmProviders	VERTEX_AIrL   rM   rN   rO   rP   rQ   r   rR   )r   r<   r   r=   clientrD   rW   rX   s           r   rK   z+VertexAIBatchPrediction._async_create_batcha   s      ( --77
  01 % 
 

 3&gh&:&:%;1X]]OLMM! ; w w#!
 %$
s   AB4B2A!B4c                     d| d| d| dS )z0Return the base url for the vertex garden modelszhttps://z'-aiplatform.googleapis.com/v1/projects/z/locations/z/batchPredictionJobs )r   r"   r!   s      r   rF   z/VertexAIBatchPrediction.create_vertex_batch_urlx   s1     /**QR`Qaalm|l}  ~R  S  	Sr   batch_idc	           	      B   t               }	| j                  ||d      \  }
}| j                  |xs d|xs |      }| d| }t        |j	                  d            dkD  r|j	                  d      d   }nd	}| j                  |dd |d d |
      \  }}dd|
 d}|du r| j                  ||      S |	j                  ||      }|j                  dk7  r%t        d|j                   d|j                         |j                         }t        j                  |      }|S )Nr'   r(   r,   r-   /r.   r   r/   r0   r1   r7   r8   r9   T)r   r=   r6   r=   r@   rA   rB   rC   )r   rE   rF   rG   rH   rI   _async_retrieve_batchgetrO   rP   rQ   rM   r   rR   )r   r   rc   r   r    r!   r"   r#   r$   rS   rT   r*   rU   r3   rV   r=   rD   rW   rX   s                      r   retrieve_batchz&VertexAIBatchPrediction.retrieve_batch   s    )*#'#<#<*% + $= $
 j  77+<})7Z 8 
 //q
;%%c*+a/'--c226HH.. +  / 
8 >&|n5

 --! .  
  ## $ 

 3&gh&:&:%;1X]]OLMM! ; w w#!
 %$r   c                 D  K   t        t        j                  j                        }|j	                  ||       d {   }|j
                  dk7  r%t        d|j
                   d|j                         |j                         }t        j                  |      }|S 7 `w)Nr[   rf   r@   rA   rB   rC   )r	   r]   r^   r_   rh   rO   rP   rQ   rM   r   rR   )r   r   r=   r`   rD   rW   rX   s          r   rg   z-VertexAIBatchPrediction._async_retrieve_batch   s     
 ( --77
   $ 
 
 3&gh&:&:%;1X]]OLMM! ; w w#!
 %$
s   ;B BA!B )__name__
__module____qualname__strr   boolr   r   r   r   floathttpxTimeoutintr   r   r   rY   r   r   rK   rF   ri   rg   __classcell__)r   s   @r   r   r      s   / /D%D% .D% 3-	D%
 %%=>D% !D% "#D% uemm+,D% c]D% 
|YsC'=>>	?D%L%8% % c3h	%
 
%.SS S 
	SA%A% A% 3-	A%
 %%=>A% !A% "#A% uemm+,A% c]A% 
|YsC'=>>	?A%F%% c3h% 
	%r   r   )rM   typingr   r   r   r   r   rq   r]   &litellm.llms.custom_httpx.http_handlerr   r	   @litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_geminir
   litellm.types.llms.openair   litellm.types.llms.vertex_air   r   litellm.types.utilsr   transformationr   r   rb   r   r   <module>r|      s=     8 8   W 8 - 7A%i A%r   