
    g;;                        d dl Z d dlmZmZ d dlmZmZmZmZm	Z	m
Z
 d dlZd dlmZ d dlmZmZmZ d dl 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  e       Zej=                  d ee      gdg      ej=                  d ee      gdg      ej=                  d ee      gdg      d ee      fde	de
dee   de fd                     Z!ejE                  d ee      gdg      ejE                  d ee      gdg      ejE                  d ee      gdg       ee      d edd      fde	de
de dee   def
d                     Z#ejE                  d ee      gdg      ejE                  d ee      gdg      ejE                  d ee      gdg      ddd ee      fde	de
dee   dee$   dee   de fd                      Z%ej=                  d! ee      gdg      ej=                  d" ee      gdg      ej=                  d# ee      gdg      d ee      fde	dede
dee   de f
d$                     Z&y)%    N)DictOptional)	APIRouterDependsHTTPExceptionPathRequestResponse)verbose_proxy_logger)CancelBatchRequestCreateBatchRequestRetrieveBatchRequest)*)user_api_key_auth)_read_request_body))get_custom_llm_provider_from_request_body)is_known_model)handle_exception_on_proxyz/{provider}/v1/batchesbatch)dependenciestagsz/v1/batchesz/batchesrequestfastapi_responseprovideruser_api_key_dictc                   K   ddl m}m}m}m}m}m}	m}
 i }	 t        |        d{   }t        j                  dj                  t        j                  |d                    ||| |||
|       d{   }d}d	}t        j                  d
u r|j!                  dd      }t#        ||      }|xs |j%                  dd      xs d}t'        di |}t        j                  d
u r0|r.|,|t)        dddi       |j*                  di | d{   }nt        j*                  dd|i| d{   }t-        j.                  |	j1                  |j!                  dd      d             t3        |di       xs i }|j!                  dd      xs d}|j!                  dd      xs d}|j!                  dd      xs d}|j4                  j7                   ||||||
t3        |dd      |             |S 7 7 7 7 # t8        $ rZ}|	j;                  |||       d{  7   t        j<                  dj                  t?        |                   tA        |      d}~ww xY ww) a   
    Create large batches of API requests for asynchronous processing.
    This is the equivalent of POST https://api.openai.com/v1/batch
    Supports Identical Params as: https://platform.openai.com/docs/api-reference/batch

    Example Curl
    ```
    curl http://localhost:4000/v1/batches         -H "Authorization: Bearer sk-1234"         -H "Content-Type: application/json"         -d '{
            "input_file_id": "file-abc123",
            "endpoint": "/v1/chat/completions",
            "completion_window": "24h"
    }'
    ```
    r   )add_litellm_data_to_requestgeneral_settingsget_custom_headers
llm_routerproxy_configproxy_logging_objversionr   NRequest received by LiteLLM:
{}   indentdatar   r   r   r#   r!   FTmodel)r+   r    custom_llm_provideropenai  error9LLM Router not initialized. Ensure models added to proxy.status_codedetaillitellm_call_id successr4   status_hidden_paramsmodel_id	cache_keyapi_baseallowed_model_regionr   r:   r;   r<   r#   model_regionrequest_datar   original_exceptionr@   Alitellm.proxy.proxy_server.create_batch(): Exception occured - {} )!litellm.proxy.proxy_serverr   r   r   r    r!   r"   r#   r   r   debugformatjsondumpslitellm'enable_loadbalancing_on_batch_endpointsgetr   popr   r   acreate_batchasynciocreate_taskupdate_request_statusgetattrheadersupdate	Exceptionpost_call_failure_hook	exceptionstrr   )r   r   r   r   r   r   r   r    r!   r"   r#   r*   router_modelis_router_modelr,   _create_batch_dataresponsehidden_paramsr:   r;   r<   es                         ^/var/www/openai/venv/lib/python3.12/site-packages/litellm/proxy/batches_endpoints/endpoints.pycreate_batchr`      s    L   DQ+'88"".55djjaj6PQ	

 1-/%
 
 '+::dB88GT2L,<JWO I!6=I 	 07$7;;tC(!# #!\  6Z55K8JKKH$22 $7;M H
 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS!
	
 M 9
B L<  	+66/ATX 7 
 	
 	
 	&&OVVA	

 (**	+s}   I$G> G4AG> 6G77BG> G:G> 1G<2CG> 3I$4G> 7G> :G> <G> >	I!IH!=II!!I$z&/{provider}/v1/batches/{batch_id:path}z/v1/batches/{batch_id:path}z/batches/{batch_id:path}zBatch ID to retrievezThe ID of the batch to retrieve)titledescriptionbatch_idc                   K   ddl m}m}m}m} i }		 t        |      }
t        j                  du r,|t        dddi	       |j                  di |
 d{   }n;|xs t        | 
       d{   xs d}t        j                  dd|i|
 d{   }t        j                  |j                  |	j                  dd      d             t        |di       xs i }|j                  dd      xs d}|j                  dd      xs d}|j                  dd      xs d}|j                   j#                   ||||||t        |dd      |	             |S 7 7 7 # t$        $ rZ}|j'                  |||	       d{  7   t)        j*                  dj-                  t/        |                   t1        |      d}~ww xY ww)at  
    Retrieves a batch.
    This is the equivalent of GET https://api.openai.com/v1/batches/{batch_id}
    Supports Identical Params as: https://platform.openai.com/docs/api-reference/batch/retrieve

    Example Curl
    ```
    curl http://localhost:4000/v1/batches/batch_abc123     -H "Authorization: Bearer sk-1234"     -H "Content-Type: application/json" 
    ```
    r   )r   r    r"   r#   )rc   TNr.   r/   r0   r1   r$   r-   r,   r4   r5   r6   r7   r9   r:   r;   r<   r=   r>   rA   Clitellm.proxy.proxy_server.retrieve_batch(): Exception occured - {}rD   )rE   r   r    r"   r#   r   rJ   rK   r   aretrieve_batchr   rO   rP   rQ   rL   rR   rS   rT   rU   rV   r   rW   rG   rX   r   )r   r   r   r   rc   r   r    r"   r#   r*   _retrieve_batch_requestr\   r,   r]   r:   r;   r<   r^   s                     r_   retrieve_batchrh      s    J  D=+"6#
 ::dB!# #!\  8Z77R:QRRH  B7SS  
 %44 $7;R H
 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS!
	
 I S T<  	+66/ATX 7 
 	
 	
 	&&QXXA	

 (**	+sl   GAE EE /E0"E ECE GE E E 	F>$F9;E><=F99F>>Glimitafterc                   K   ddl m}m}m} t	        j
                  dj                  ||             	 |xs t        |        d{   xs d}	t        j                  |	||       d{   }
t        |
di       xs i }|j                  d	d      xs d
}|j                  dd      xs d
}|j                  dd      xs d
}|j                  j                   ||||||t        |dd
                   |
S 7 7 # t        $ r]}|j                  ||||d       d{  7   t	        j                   dj                  t#        |                   t%        |      d}~ww xY ww)aU  
    Lists 
    This is the equivalent of GET https://api.openai.com/v1/batches/
    Supports Identical Params as: https://platform.openai.com/docs/api-reference/batch/list

    Example Curl
    ```
    curl http://localhost:4000/v1/batches?limit=2     -H "Authorization: Bearer sk-1234"     -H "Content-Type: application/json" 
    ```
    r   )r   r"   r#   z!GET /v1/batches after={} limit={}r$   Nr-   )r,   rj   ri   r9   r:   r5   r;   r<   r=   )r   r:   r;   r<   r#   r?   )rj   ri   rA   re   )rE   r   r"   r#   r   rF   rG   r   rJ   alist_batchesrR   rL   rS   rT   rU   rV   r/   rX   r   )r   r   r   ri   rj   r   r   r"   r#   r,   r\   r]   r:   r;   r<   r^   s                   r_   list_batchesrm     s    H  BII%QVWX)+ >wOO 	
 !.. 3
 
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 5 P
0  +66/ #(59 7 
 	
 	

 	""QXXA	

 (**+sY   0E#C: C6#C: *C8+B
C: 5E#6C: 8C: :	E ED =EE  E#z-/{provider}/v1/batches/{batch_id:path}/cancelz"/v1/batches/{batch_id:path}/cancelz/batches/{batch_id:path}/cancelc                   K   ddl m}m}m}m}m}	m}
 i }	 t        |        d{   }t        j                  dj                  t        j                  |d                    ||| |||
|       d{   }|xs |j                  d	d      xs d
}t        dd|i|}t        j                   dd	|i| d{   }t#        j$                  |	j'                  |j)                  dd      d             t+        |di       xs i }|j)                  dd      xs d}|j)                  dd      xs d}|j)                  dd      xs d}|j,                  j/                   ||||||
t+        |dd      |             |S 7 X7 7 # t0        $ rZ}|	j3                  |||       d{  7   t        j4                  dj                  t7        |                   t9        |      d}~ww xY ww)a  
    Cancel a batch.
    This is the equivalent of POST https://api.openai.com/v1/batches/{batch_id}/cancel

    Supports Identical Params as: https://platform.openai.com/docs/api-reference/batch/cancel

    Example Curl
    ```
    curl http://localhost:4000/v1/batches/batch_abc123/cancel         -H "Authorization: Bearer sk-1234"         -H "Content-Type: application/json"         -X POST

    ```
    r   )r   r   r   r!   r"   r#   r$   Nr%   r&   r'   r)   r,   r-   rc   r4   r5   r6   r7   r9   r:   r;   r<   r=   r>   rA   rC   rD   )rE   r   r   r   r!   r"   r#   r   r   rF   rG   rH   rI   rM   r   rJ   acancel_batchrO   rP   rQ   rL   rR   rS   rT   rU   rV   rW   rX   r   )r   rc   r   r   r   r   r   r   r!   r"   r#   r*   r,   _cancel_batch_datar\   r]   r:   r;   r<   r^   s                       r_   cancel_batchrq   h  s    J  D<+'88"".55djjaj6PQ	

 1-/%
 
 I!6=I 	 0JJTJ .. 
 3
 
 
 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS!
	
 c 9

>  	+66/ATX 7 
 	
 	
 	&&OVVA	

 (**	+sk   G+F E=AF 4F 5AF :F;CF <G+=F  F F 	G(G#%F(&=G##G((G+)'rO   typingr   r   fastapir   r   r   r   r	   r
   rJ   litellm._loggingr   litellm.batches.mainr   r   r   litellm.proxy._types$litellm.proxy.auth.user_api_key_authr   -litellm.proxy.common_utils.http_parsing_utilsr   0litellm.proxy.common_utils.openai_endpoint_utilsr   4litellm.proxy.openai_files_endpoints.files_endpointsr   litellm.proxy.utilsr   routerpostrX   UserAPIKeyAuthr`   rL   rh   intrm   rq   rD       r_   <module>r      s  
  " N N  1 
 # B L P 9	 +,-
  
 +,-
  
 +,-
   #(/0A(B	s+s+s+ sms+ &	s+s+l ,+,-
  
 !+,-
  
 +,-
   )00A(B"$2S[+[+[+ &[+ sm	[+
 [+[+| +,-
  
 +,-
  
 +,-
   #(/0A(BE+E+E+ smE+ C=	E+
 C=E+ &E+E+P 3+,-
  
 (+,-
  
 %+,-
   #(/0A(B\+\+\+ \+ sm	\+
 &\+\+r   