
    hq                        d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlZddlZddlmZ ddl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 ddlm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z&  e       Z' e       Z( ed      Z)e$	 	 	 	 d'de
d   de
d   de*de
d   dee	e*e*f      dee	e*e*f      dee	e*e*f      de"fd       Z+e$	 	 	 	 d'de
d   de
d   de*de
d   dee	e*e*f      dee	e*e*f      dee	e*e*f      dee"eeee"f   f   fd       Z,e$	 	 	 	 d'de*de
d   dee	e*e*f      dee	e*e*f      dee	e*e*f      de"fd       Z-e$	 	 	 	 d'de*de
d   dee	e*e*f      dee	e*e*f      dee	e*e*f      dee"eeee"f   f   fd       Z.e$	 	 	 	 	 	 d(d ee*   d!ee/   de
d"   dee	e*e*f      dee	e*e*f      dee	e*e*f      fd#       Z0e$	 	 	 	 	 d)d ee*   d!ee/   de
d"   dee	e*e*f      dee	e*e*f      f
d$       Z1	 	 	 	 d'de*de
d"   dee	e*e*f      dee	e*e*f      dee	e*e*f      defd%Z2	 	 	 	 d'de*de
d"   dee	e*e*f      dee	e*e*f      dee	e*e*f      deeeeeef   f   fd&Z3y)*z
Main File for Batches API implementation

https://platform.openai.com/docs/api-reference/batch

- create_batch()
- retrieve_batch()
- cancel_batch()
- list_batch()

    N)partial)Any	CoroutineDictLiteralOptionalUnion)Logging)AzureBatchesAPI)OpenAIBatchesAPI)VertexAIBatchPrediction)get_secret_str)BatchCancelBatchRequestCreateBatchRequestRetrieveBatchRequest)GenericLiteLLMParams)LiteLLMBatch)clientget_litellm_paramssupports_httpx_timeout )gcs_bucket_namecompletion_window24hendpoint)z/v1/chat/completionsz/v1/embeddingsz/v1/completionsinput_file_idcustom_llm_provider)openaiazure	vertex_aimetadataextra_headers
extra_bodyreturnc           
      l  K   	 t        j                         }d|d<   t        t        | ||||||fi |}	t	        j
                         }
t        |
j                  |	      }|j                  d|       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r}|d}~ww xY ww)z
    Async: Creates and executes a batch from an uploaded file of request

    LiteLLM Equivalent of POST: https://api.openai.com/v1/batches
    Tacreate_batchN)
asyncioget_event_loopr   create_batchcontextvarscopy_contextrunrun_in_executoriscoroutine	Exception)r   r   r   r   r"   r#   r$   kwargsloopfuncctxfunc_with_contextinit_responseresponsees                  P/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/batches/main.pyr'   r'   ,   s      %%'"& 

 

 &&(#CGGT2"2249JKK}-**H  %H L +
  sY   B4A/B! 3B4B! BB! B4B! B4B! B! !	B1*B,,B11B4c                    	 t        d/i |}|j                  dd      }	|j                  dd      }
|j                  dd      }|j                  dd      du }t        d/i |}|j                  dd      }|j                  xs |j                  d	d
      xs d
}|j                  dd|j                         |	|
||di d|j                  d      |       |:t        |t        j                        r t        |      du r|j                  xs d
}|}n,|&t        |t        j                        st        |      }n|d}t        | |||||      }d}|dk(  r|j                  xsB t        j                  xs0 t!        j"                  d      xs t!        j"                  d      xs d}|j$                  xs, t        j$                  xs t!        j"                  dd      xs d}|j&                  xs9 t        j&                  xs' t        j(                  xs t!        j"                  d      }t*        j-                  ||||||j.                  |      }|S |dk(  r|j                  xs t        j                  xs t1        d      }|j2                  xs t        j2                  xs t1        d      }|j&                  xs< t        j&                  xs* t        j4                  xs t1        d      xs t1        d      }|j                  di       }||j                  dd       nt1        d       t6        j-                  ||||||j.                  ||      }|S |d k(  r|j                  xs d!}|j8                  xs t        j8                  xs t1        d"      }|j:                  xs t        j:                  xs t1        d#      }|j<                  xs t1        d$      }t>        j-                  |||||||j.                  |%      }|S t        j@                  jC                  d&jE                  |      d'|t        jF                  d(d)t        jH                  d*d+,      -      .      # tJ        $ r}|d}~ww xY w)0z
    Creates and executes a batch from an uploaded file of request

    LiteLLM Equivalent of POST: https://api.openai.com/v1/batches
    litellm_call_idNproxy_server_request
model_infor'   FTlitellm_logging_objrequest_timeoutX  )r;   r<   r=   r"   preset_cache_keystream_response)exclude_unsetmodeluseroptional_paramslitellm_paramsr        @)r   r   r   r"   r#   r$   r   OPENAI_BASE_URLOPENAI_API_BASEhttps://api.openai.com/v1OPENAI_ORGANIZATIONOPENAI_API_KEY)api_baseapi_keyorganizationcreate_batch_datatimeoutmax_retries	_is_asyncr    AZURE_API_BASEAZURE_API_VERSIONAZURE_OPENAI_API_KEYAZURE_API_KEYr$   azure_ad_tokenAZURE_AD_TOKEN)rU   rO   rP   api_versionrS   rT   rR   rH   r!   r   VERTEXAI_PROJECTVERTEXAI_LOCATIONVERTEXAI_CREDENTIALS)rU   rO   vertex_projectvertex_locationvertex_credentialsrS   rT   rR   zALiteLLM doesn't support custom_llm_provider={} for 'create_batch'n/a  Unsupported providerr*   "https://github.com/BerriAI/litellmmethodurlstatus_codecontentrequestmessagerE   llm_providerr7    )&r   getpopr   rS   update_environment_variables
model_dump
isinstancehttpxTimeoutr   readfloatr   rO   litellmosgetenvrQ   rP   
openai_keyopenai_batches_instancer*   rT   r   r\   	azure_keyazure_batches_instancer`   ra   rb   vertex_ai_batches_instance
exceptionsBadRequestErrorformatResponseRequestr0   )r   r   r   r   r"   r#   r$   r1   rG   r;   r<   r=   rU   rH   r>   rS   read_timeout_create_batch_requestrO   rQ   rP   r7   r\   vertex_ai_projectvertex_ai_locationrb   r8   s                              r9   r*   r*   \   s    \.88 **%6=%zz*@$GZZd3
JJ6$>	+5f517<QSW1X!))VVZZ8I3-OVSV88+668#2(<($$(#% ",,4,@ !4 	9 	
" 7EMM2&':;uD"<<.3L"G GU]])KGnG_G 2/''!!
 #'(*  (( /##/99.// 99.// /   ,, ''992D9 	   '' /??/%%/ 99-.	  /;;!)"7+77# < Hh W !G+(( 4##4!"23   ++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/0-::#!'+77"7- ; 	Hb O !K/&//52H.. 6))6!"45   // 7**7!"56 
 "1!C!C "~&H 2>>#!0 2#5+77"7 ? 	H.  $$44[bb' 0 #2!MMEij 5    s-   IQ C?Q B,Q ;AQ 	Q(!Q##Q(batch_idc                 h  K   	 t        j                         }d|d<   t        t        | ||||fi |}t	        j
                         }t        |j                  |      }	|j                  d|	       d{   }
t        j                  |
      r|
 d{   }|S |
}|S 7 )7 # t        $ r}|d}~ww xY ww)zo
    Async: Retrieves a batch.

    LiteLLM Equivalent of GET https://api.openai.com/v1/batches/{batch_id}
    Taretrieve_batchN)
r(   r)   r   retrieve_batchr+   r,   r-   r.   r/   r0   r   r   r"   r#   r$   r1   r2   r3   r4   r5   r6   r7   r8   s                r9   r   r     s     %%'$( ! 
 
 &&(#CGGT2"2249JKK}-**H  %H L*
  Y   B2A-B 1B2B BB B2B B2B B 	B/(B**B//B2c                 6   	 t        d+i |}|j                  dd      }|j                  xs |j                  dd      xs d}t        d+d|i|}	|j	                  dd|j                         |	|       |:t        |t        j                        r t        |      du r|j                  xs d}
|
}n,|&t        |t        j                        st        |      }n|d}t        | ||	      }|j                  d
d      du }d}|dk(  r|j                  xsB t        j                  xs0 t!        j"                  d      xs t!        j"                  d      xs d}|j$                  xs, t        j$                  xs t!        j"                  dd      xs d}|j&                  xs9 t        j&                  xs' t        j(                  xs t!        j"                  d      }t*        j-                  |||||||j.                        }|S |dk(  r|j                  xs t        j                  xs t1        d      }|j2                  xs t        j2                  xs t1        d      }|j&                  xs< t        j&                  xs* t        j4                  xs t1        d      xs t1        d      }|j                  di       }||j                  dd       nt1        d       t6        j-                  ||||||j.                  ||	      }|S |dk(  r|j                  xs d}|j8                  xs t        j8                  xs t1        d      }|j:                  xs t        j:                  xs t1        d      }|j<                  xs t1        d       }t>        j-                  || ||||||j.                  !      }|S t        j@                  jC                  d"jE                  |      d#|t        jF                  d$d%t        jH                  d&d'(      )      *      # tJ        $ r}|d}~ww xY w),zh
    Retrieves a batch.

    LiteLLM Equivalent of GET https://api.openai.com/v1/batches/{batch_id}
    r>   Nr?   r@   r   rD   FrI   r   r#   r$   r   Tr   rJ   rK   rL   rM   rN   )rU   retrieve_batch_datarO   rP   rQ   rS   rT   r    rV   rW   rX   rY   r$   rZ   r[   )rU   rO   rP   r\   rS   rT   r   rH   r!   r   r]   r^   r_   )rU   r   rO   r`   ra   rb   rS   rT   zJLiteLLM doesn't support {} for 'create_batch'. Only 'openai' is supported.rc   rd   re   create_threadrf   rg   rj   rn   rq   )&r   rr   rS   r   rt   ru   rv   rw   rx   r   ry   rz   r   rs   rO   r{   r|   r}   rQ   rP   r~   r   r   rT   r   r\   r   r   r`   ra   rb   r   r   r   r   r   r   r0   )r   r   r"   r#   r$   r1   rG   r>   rS   rH   r   _retrieve_batch_requestrU   rO   rQ   rP   r7   r\   r   r   rb   r8   s                         r9   r   r   5  s}   R.8817<QSW1X!))VVZZ8I3-OVSV+ 
 3

 	88+668) 3 	9 	
 7EMM2&':;uD"<<.3L"G GU]])KGnG_G"6'!#
 JJ0%8D@	"&(*  (( /##/99.// 99.// /   ,, ''992D9 	   '' /??/%%/ 99-.	  /==#$;!)+77 > Hh W !G+(( 4##4!"23   ++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/0-<<#!'+77$;- = 	Hb O !K/&//52H.. 6))6!"45   // 7**7!"56 
 "1!C!C "~&H 2@@#!!0 2#5+77 A 	H.  $$44dkk' 0 #2!MMFjk 5    s-   G;P >C?P >B,P +AP 	PPPafterlimit)r   r    c                 h  K   	 t        j                         }d|d<   t        t        | ||||fi |}t	        j
                         }	t        |	j                  |      }
|j                  d|
       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r}|d}~ww xY ww)z2
    Async: List your organization's batches.
    Talist_batchesN)
r(   r)   r   list_batchesr+   r,   r-   r.   r/   r0   )r   r   r   r"   r#   r$   r1   r2   r3   r4   r5   r6   r7   r8   s                 r9   r   r     s     %%'"& 
 
 &&(#CGGT2"2249JKK}-**H  %H L*
  r   c                 P   	 t        d"i |}t        d"d|i|}|j                  xs9 t        j                  xs' t        j                  xs t        j                  d      }|j                  xs |j                  dd      xs d}	|	:t        |	t        j                        r t        |      du r|	j                  xs d}
|
}	n,|	&t        |	t        j                        st        |	      }	n|	d}	|j                  dd      d	u }|d
k(  r|j                   xsB t        j                   xs0 t        j                  d      xs t        j                  d      xs d}|j"                  xs, t        j"                  xs t        j                  dd      xs d}t$        j'                  || |||||	|j(                        }|S |dk(  r|j                   xs t        j                   xs t+        d      }|j,                  xs t        j,                  xs t+        d      }|j                  xs< t        j                  xs* t        j.                  xs t+        d      xs t+        d      }|j                  di       }||j                  dd       nt+        d       t0        j'                  |||||	|j(                  |      }|S t        j2                  j5                  dj7                  |      d|t        j8                  ddt        j:                  dd             !      # t<        $ r}|d}~ww xY w)#z>
    Lists batches

    List your organization's batches.
    r   rN   r?   r@   NFrI   r   Tr   rJ   rK   rL   rM   )rU   r   r   rO   rP   rQ   rS   rT   r    rV   rW   rX   rY   r$   rZ   r[   )rU   rO   rP   r\   rS   rT   rH   zHLiteLLM doesn't support {} for 'list_batch'. Only 'openai' is supported.rc   rd   re   r   rf   rg   rj   rn   rq   )r   r   rP   r{   r~   r|   r}   rS   rr   rv   rw   rx   r   ry   rz   rs   rO   rQ   r   r   rT   r   r\   r   r   r   r   r   r   r   r0   )r   r   r   r#   r$   r1   rG   rH   rP   rS   r   rU   rO   rQ   r7   r\   r8   s                    r9   r   r     sK   f.88+ 
 3


 ## ++!!+ yy)*	 	 "))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_GJJ6$>	(*  (( /##/99.// 99.// /   ,, ''992D9 	  /;;#!)+77 < 	Hl Y !G+&//g73C3Cg~VfGgH++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/0-::#!'+77- ; H,  $$44bii' 0 #2!MMFjk 5    s&   F6L 9C>L 8AL 	L%L  L%c                 h  K   	 t        j                         }d|d<   t        t        | ||||fi |}t	        j
                         }t        |j                  |      }	|j                  d|	       d{   }
t        j                  |
      r|
 d{   }|S |
}|S 7 )7 # t        $ r}|d}~ww xY ww)zu
    Async: Cancels a batch.

    LiteLLM Equivalent of POST https://api.openai.com/v1/batches/{batch_id}/cancel
    Tacancel_batchN)
r(   r)   r   cancel_batchr+   r,   r-   r.   r/   r0   r   s                r9   r   r   z  s     %%'"& 
 
 &&(#CGGT2"2249JKK}-**H  %H L*
  r   c                 p   	 t        d#i |}t        d#d|i|}|j                  xs |j                  dd      xs d}|:t	        |t
        j                        r t        |      du r|j                  xs d}	|	}n,|&t	        |t
        j                        st        |      }n|d}t        | ||      }
|j                  dd      d	u }d}|d
k(  r|j                  xsB t        j                  xs0 t        j                  d      xs t        j                  d      xs d}|j                   xs, t        j                   xs t        j                  dd      xs d}|j"                  xs9 t        j"                  xs' t        j$                  xs t        j                  d      }t&        j)                  ||
|||||j*                        }|S |dk(  r|j                  xs t        j                  xs t-        d      }|j.                  xs t        j.                  xs t-        d      }|j"                  xs< t        j"                  xs* t        j0                  xs t-        d      xs t-        d      }|j                  di       }||j                  dd       nt-        d       t2        j)                  ||||||j*                  |
|      }|S t        j4                  j7                  dj9                  |      d|t        j:                  ddt        j<                  dd       !      "      # t>        $ r}|d}~ww xY w)$zn
    Cancels a batch.

    LiteLLM Equivalent of POST https://api.openai.com/v1/batches/{batch_id}/cancel
    r   r?   r@   NFrI   r   r   Tr   rJ   rK   rL   rM   rN   )rU   cancel_batch_datarO   rP   rQ   rS   rT   r    rV   rW   rX   rY   r$   rZ   r[   )rU   rO   rP   r\   rS   rT   r   rH   zWLiteLLM doesn't support {} for 'cancel_batch'. Only 'openai' and 'azure' are supported.rc   rd   re   r   rf   rg   rj   rn   rq   ) r   r   rS   rr   rv   rw   rx   r   ry   rz   r   rs   rO   r{   r|   r}   rQ   rP   r~   r   r   rT   r   r\   r   r   r   r   r   r   r   r0   )r   r   r"   r#   r$   r1   rG   rH   rS   r   _cancel_batch_requestrU   rO   rQ   rP   r7   r\   r8   s                     r9   r   r     so   o.88+ 
 3


 "))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_G 2'!!
 JJ6$>	"&(*(( /##/99.// 99.// /   ,, ''992D9 	   '' /??/%%/ 99-.	  /;;#"7!)+77 < Ht c !G+(( 4##4!"23   ++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/0-::#!'+77"7- ; 	H.  $$44qxx' 0 #2!MMEij 5    s&   GL% C?L% AL% %	L5.L00L5)r   NNN)NNr   NNN)NNr   NN)4__doc__r(   r+   r|   	functoolsr   typingr   r   r   r   r   r	   rw   r{   *litellm.litellm_core_utils.litellm_loggingr
   LiteLLMLoggingObj"litellm.llms.azure.batches.handlerr   litellm.llms.openai.openair   &litellm.llms.vertex_ai.batches.handlerr   litellm.secret_managers.mainr   litellm.types.llms.openair   r   r   r   litellm.types.routerr   litellm.types.utilsr   litellm.utilsr   r   r   r   r   r   strr'   r*   r   r   intr   r   r   r   rq       r9   <module>r      s.  
   	  A A   S > 7 J 7  6 , L L +, (* 4RH  
 DL)-.2+/,u~,QR, , !!?@	,
 tCH~&, DcN+, c3h(, , ,^ 
 DL)-.2+/ku~kQRk k !!?@	k
 tCH~&k DcN+k c3h(k <3\#9::;k k\  DL)-.2+/&& !?@& tCH~&& DcN+	&
 c3h(& & &R  DL)-.2+/__ !?@_ tCH~&_ DcN+	_
 c3h(_ <3\#9::;_ _D 6>)-.2+/'C='C=' !!23' tCH~&	'
 DcN+' c3h(' 'T 6>.2+/sC=sC=s !!23s DcN+	s
 c3h(s sp 7?)-.2+/&& !23& tCH~&& DcN+	&
 c3h(& &V 7?)-.2+/|| !23| tCH~&| DcN+	|
 c3h(| 5)CeO,,-|r   