
    huI                        d dl Z d dlmZ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 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mZ d dlmZm Z  d dl!m"Z"  e       Z#e#jI                  d ee      gdg      e#jI                  d ee      gdg      e#jI                  d ee      gdg      d ee      fde
dedee%   de&fd                     Z'e#jQ                  d ee      gdg      e#jQ                  d ee      gdg      e#jQ                  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#jQ                  d ee      gdg      e#jQ                  d ee      gdg      e#jQ                  d ee      gdg      ddd ee      dfde
dedee%   d ee*   d!ee%   de&d"ee%   fd#                     Z+e#jI                  d$ ee      gdg      e#jI                  d% ee      gdg      e#jI                  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cast)	APIRouterDependsHTTPExceptionPathRequestResponse)verbose_proxy_logger)CancelBatchRequestRetrieveBatchRequest)*)user_api_key_auth)ProxyBaseLLMRequestProcessing)_read_request_body))get_custom_llm_provider_from_request_body)"_is_base64_encoded_unified_file_idget_models_from_unified_file_id)handle_exception_on_proxyis_known_model)LiteLLMBatchCreateRequestz/{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} i }		 t        |        d{   }	t        j                  dj                  t        j                  |	d                   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 |	}|j!                  dd      }d}|rt)        |      }t        j                  du r0|r.|,|t+        dddi       |j,                  d'i | d{   }n|r|rt/        |      }t1        |      dk7  r't+        dddj                  t1        |            i      |d   }||d<   |t+        dddi       |j,                  d'i | d{   }||_        ||j4                  d<   nt        j,                  d'd|i| d{   }|j7                  |	||       d{   }t9        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@                  jC                  t        jD                  |||||t?        |d#d      |	$             |S 7 7 ^7 7 +7 7 # tF        $ rZ}|jI                  |||	%       d{  7   t        jJ                  d&j                  tM        |                   tO        |      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   general_settings
llm_routerproxy_configproxy_logging_objversionr   NRequest received by LiteLLM:
{}   indentdataacreate_batchr   r"   r   r&   r%   r$   
route_typeFTmodel)r1   r#   custom_llm_provideropenaiinput_file_id  error9LLM Router not initialized. Ensure models added to proxy.status_codedetail   i  zExpected 1 model, got {}unified_file_idr-   r   responselitellm_call_id successr?   status_hidden_paramsmodel_id	cache_keyapi_baseallowed_model_regionr   rE   rF   rG   r&   model_regionrequest_datar   original_exceptionrK   Alitellm.proxy.proxy_server.create_batch(): Exception occured - {} )(litellm.proxy.proxy_serverr"   r#   r$   r%   r&   r   r   debugformatjsondumpsr    common_processing_pre_call_logiclitellm'enable_loadbalancing_on_batch_endpointsgetr   popr   r   r   r.   r   lenr4   rD   post_call_success_hookasynciocreate_taskupdate_request_statusgetattrheadersupdateget_custom_headers	Exceptionpost_call_failure_hook	exceptionstrr   )r   r   r   r   r"   r#   r$   r%   r&   r-   base_llm_response_processorlitellm_logging_objrouter_modelis_router_modelr2   _create_batch_datar4   r<   r>   target_model_namesr1   hidden_paramsrE   rF   rG   es                             e/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/batches_endpoints/endpoints.pycreate_batchrp       s    L  Dx+'88"".55djjaj6PQ	
 'D&N# .NN-//%& O 
 
	
 '+::dB88GT2L,<JWO I!6=I 	 7>>*..E6;@OO;;tC(!# #!\  6Z55K8JKKH!@!Q%&!+# #!;!B!B 23"  'q)E*/w'!# #!\  6Z55K8JKKH%2H"9HH##$56$22 $7;M H
 +AA):X B 
 

 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  '')<<"3!#!$%68NPRS!
	
 [ 9
L L2 L

<  	+66/ATX 7 
 	
 	
 	&&OVVA	

 (**	+s   MK3 K#A$K3 K&	B>K3 K)A9K3 K,5K3 7K/8K3 K1CK3 "M#K3 &K3 )K3 ,K3 /K3 1K3 3	M<ML=MMM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}m}	 i }
	 t        |      }t        t        |      }
t        |      }t        |
      }|j                  | |||	||d       d{   \  }
}t        j                  du s|r;|t        d	d
di       |j                  di |
 d{   }||j                   d<   n;|xs t#        |        d{   xs d}t        j                  dd|i|
 d{   }|j%                  |
||       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}|j0                  j3                  t        j4                  |||||	t/        |dd      |
             |S 7 y7 :7 7 7 # t6        $ rZ}|j9                  |||
       d{  7   t;        j<                  dj?                  tA        |                   tC        |      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!   )rs   r,   aretrieve_batchr/   NTr5   r6   r7   r8   unified_batch_idr'   r3   r2   r=   r?   r@   rA   rB   rD   rE   rF   rG   rH   rI   rL   Clitellm.proxy.proxy_server.retrieve_batch(): Exception occured - {}rO   )"rP   r"   r#   r$   r%   r&   r   r   dictr   r   rU   rV   rW   r   ru   rD   r   r[   r\   r]   r^   rX   r_   r`   ra   rb   rc   rd   r   re   rR   rf   r   )r   r   r   r   rs   r"   r#   r$   r%   r&   r-   _retrieve_batch_requestrv   rg   rh   r>   r2   rm   rE   rF   rG   rn   s                         ro   retrieve_batchrz      s    J  DT+"6#
 D12=hG&C&N# .NN-//%( O 
 
	
 ::dBFV!# #!\  8Z77?$??H:JH##$67  B7SS  
 %44 $7;? H
 +AA):X B 
 

 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  '')<<"3!#!$%68NPRS!
	
 {
& @
 T

<  	+66/ATX 7 
 	
 	
 	&&QXXA	

 (**	+s   IAG( "G#AG( $G%'G( G!"G( /G$0G( G&CG( IG( G( !G( $G( &G( (	I1IH	=IIIlimitafterrl   c                   K   ddl m}m}m}	 t	        j
                  dj                  ||             	 |'t        ddt        j                  j                  i      t        |        d{   }
|xs |
j                  d	d      }|r1|j                  d
      d   }|j                  |||       d{   }n<|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)        j*                  |||||	t#        |dd                   |S 7 7 7 7 # t,        $ r]}|j/                  ||||d       d{  7   t	        j0                  dj                  t3        |                   t5        |      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={}Nr5   r6   r8   r'   rl   ,)r1   r|   r{   r3   )r2   r|   r{   rD   rE   r@   rF   rG   rH   )r   rE   rF   rG   r&   rJ   )r|   r{   rL   rw   )rP   r#   r%   r&   r   rQ   rR   r   CommonProxyErrorsno_llm_routervaluer   rX   splitalist_batchesr   rV   r_   r`   ra   r   rb   rc   rd   r6   rf   r   )r   r   r   r{   r|   r   rl   r#   r%   r&   r-   r1   r>   r2   rm   rE   rF   rG   rn   s                      ro   list_batchesr   O  s*    J RQBII%QVWX:+!2!@!@!F!FG  (88/W488<PRV3W&,,S1!4E'55 6  H  B7SS  
 %22$7 H  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  '')<<"3!#!$%68NPRS		
 M 9 T0  +66/ #(59 7 
 	
 	

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

 (**+s~   0G;8F +F	,AF 3F4F F#F 0F1BF G;	F F F F 	G8G35F86=G33G88G;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}	 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/        j0                  |||||	t)        |dd      |
             |S 7 e7 7 # t2        $ rZ}|j5                  |||
       d{  7   t        j6                  dj                  t9        |                   t;        |      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   )add_litellm_data_to_requestr"   r$   r%   r&   r'   Nr(   r)   r*   )r-   r   r"   r   r&   r$   r2   r3   rs   r?   r@   rA   rB   rD   rE   rF   rG   rH   rI   rL   rN   rO   )rP   r   r"   r$   r%   r&   r   r   rQ   rR   rS   rT   rY   r   rV   acancel_batchr\   r]   r^   rX   r_   r`   ra   r   rb   rc   rd   re   rf   r   )r   rs   r   r   r   r   r"   r$   r%   r&   r-   r2   _cancel_batch_datar>   rm   rE   rF   rG   rn   s                      ro   cancel_batchr     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   G6F FAF 2F3AF 8F9CF G6F F F 	G3G.0F31=G..G33G6)-r\   typingr   r   r   fastapir   r   r   r	   r
   r   rV   litellm._loggingr   litellm.batches.mainr   r   litellm.proxy._types$litellm.proxy.auth.user_api_key_authr   'litellm.proxy.common_request_processingr   -litellm.proxy.common_utils.http_parsing_utilsr   0litellm.proxy.common_utils.openai_endpoint_utilsr   1litellm.proxy.openai_files_endpoints.common_utilsr   r   litellm.proxy.utilsr   r   litellm.types.llms.openair   routerpostrf   UserAPIKeyAuthrp   rX   rz   intr   r   rO       ro   <module>r      s    ' ' N N  1 I " B Q L J ?	 +,-
  
 +,-
  
 +,-
   #(/0A(B	X+X+X+ smX+ &	X+X+v ,+,-
  
 !+,-
  
 +,-
   )00A(B"$2Ss+s+s+ &s+ sm	s+
 s+s+l +,-
  
 +,-
  
 +,-
   #(/0A(B(,S+S+S+ smS+ C=	S+
 C=S+ &S+ !S+S+l 3+,-
  
 (+,-
  
 %+,-
   #(/0A(B[+[+[+ [+ sm	[+
 &[+[+r   