
    g                         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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)BatchCreateBatchRequest)VertexAIBatchPredictionJob   )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       [/var/www/openai/venv/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r5   r<   data   Error:  response)r   _ensure_access_tokencreate_vertex_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_baser2   _r<   r;   rC   _json_responsevertex_batch_responses                       r   create_batchz$VertexAIBatchPrediction.create_batch   s    )*#'#<#<*% + $= $
 j  11+<})7Z 2 

 %%c*+a/'--c226HH.. +  / 
8 >&|n5
 (aa) 	 ++%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?   r@   rA   rB   )r	   litellmLlmProviders	VERTEX_AIrK   rL   rM   rN   rO   rP   r   rQ   )r   r;   r   r<   clientrC   rV   rW   s           r   rJ   z+VertexAIBatchPrediction._async_create_batch`   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   rE   z)VertexAIBatchPrediction.create_vertex_urlw   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.   r/   r0   r6   r7   r8   T)r   r<   r5   r<   r?   r@   rA   rB   )r   rD   rE   rF   rG   rH   _async_retrieve_batchgetrN   rO   rP   rL   r   rQ   )r   r   rb   r   r   r    r!   r"   r#   rR   rS   r)   rT   r2   rU   r<   rC   rV   rW   s                      r   retrieve_batchz&VertexAIBatchPrediction.retrieve_batch   s    )*#'#<#<*% + $= $
 j  11+<})7Z 2 
 //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)NrZ   re   r?   r@   rA   rB   )r	   r\   r]   r^   rg   rN   rO   rP   rL   r   rQ   )r   r   r<   r_   rC   rV   rW   s          r   rf   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   floathttpxTimeoutintr   r   r   rX   r   r   rJ   rE   rh   rf   __classcell__)r   s   @r   r   r      s   / /G%G% .G% 3-	G%
 %SMG% !G% "#G% uemm+,G% c]G% 
uiS%00	1G%R%8% % c3h	%
 
%.SS S 
	SA%A% A% 3-	A%
 %SMA% !A% "#A% uemm+,A% c]A% 
uiS%00	1A%F%% c3h% 
	%r   r   )rL   typingr   r   r   r   r   rp   r\   &litellm.llms.custom_httpx.http_handlerr   r	   @litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_geminir
   litellm.types.llms.openair   r   litellm.types.llms.vertex_air   transformationr   r   ra   r   r   <module>rz      s6     8 8   W ? C 7D%i D%r   