o
    ưi                     @   s  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T 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mZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( d dl)m*Z*m+Z+ d dl,m-Z- e Z.e.j/deegdgde.j/deegdgde.j/deegdgddeefde
dedee0 de1fddZ2e.j3deegdgde.j3deegdgde.j3deegdgdeede	dddfde
dede1dee0 de0f
d d!Z4e.j3deegdgde.j3deegdgde.j3deegdgddddeedfde
dedee0 d"ee5 d#ee0 de1d$ee0 fd%d&Z6e.j/d'eegdgde.j/d(eegdgde.j/d)eegdgddeefde
de0dedee0 de1f
d*d+Z7dS ),    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_headers*get_custom_llm_provider_from_request_query)"_is_base64_encoded_unified_file_iddecode_model_from_file_idencode_batch_response_idsencode_file_id_with_modelget_batch_from_databaseget_credentials_for_model"get_model_id_from_unified_batch_idget_models_from_unified_file_idget_original_file_idprepare_data_with_credentials resolve_input_file_id_to_unifiedupdate_batch_in_database)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           "         s  ddl m}m}m}m}m} i }	z3t| dI dH }	td	t
j|	dd t|	d}
|
j| |||||d	d
I dH \}	}d}d}tjdu rS|	dd}t||d}|pa|	ddpat| dpad}td?i |	}|jpmi }|d}|durd|vsd|vrtdddid|d dkrtddd|d  diddt|d d|d< |dd}d}d}|rt|}t|}|dur6|r6t||dd}t|}||d< t||d  tjd?d|d i|I dH }|r2t|d!r2|jr2|j}t ||d"d#}||_t|d$r|j!rt |j!|d%|_!t|d&r$|j"r$t |j"|d%|_"td'| d(| d)|  ||_#ntjdu r]|r]|dur]|du rQtddd*id|jd?i |I dH }n|r|rt$|}t%|d+kr{td,dd-	t%|id|d }||d< |du rtddd*id|jd?i |I dH }||_#||j&d.< nN|	dp| j'dp| j(d/}|rt||d0d}t||d  tjd?d|d i|I dH }t)||d1 td'|  ntjd?d|i|I dH }|j*|	||d2I dH }t+,|j-|	d3d4d5d6 t.|d7i pi }|d8dpd4}|d9dp%d4}|d:dp.d4} |j(/tj0|||| |t.|d;d4|	d< |W S  t1yk }! z|j2||!|	d=I dH  t3d>	t4|! t5|!d}!~!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&   NzRequest received by LiteLLM:
{}   )indentdataacreate_batchr&   r+   r)   r/   r.   r-   Z
route_typeFTmodel)r7   r,   custom_llm_provideropenaiZ#enforced_batch_output_expires_afteranchorseconds  errorzServer configuration error: team metadata field 'enforced_batch_output_expires_after' is malformed - must contain 'anchor' and 'seconds' keys. Contact your team or proxy admin to fix this setting.status_codedetailZ
created_atzjServer configuration error: team metadata field 'enforced_batch_output_expires_after' has invalid anchor 'zO' - must be 'created_at'. Contact your team or proxy admin to fix this setting.)r:   r;   Zoutput_expires_afterinput_file_idz(batch creation (file created with model)r,   model_idZoperation_context)r4   credentialsidr#   )file_idr7   Zid_typeoutput_file_id)rF   r7   error_file_idzCreated batch using model: z, original_batch_id: z, encoded: 9LLM Router not initialized. Ensure models added to proxy.   i  zExpected 1 model, got {}unified_file_idx-litellm-modelzbatch creationr7   r4   r)   responselitellm_call_id successrP   status_hidden_paramsrC   	cache_keyapi_baseallowed_model_regionr)   rC   rV   rW   r/   model_regionrequest_datar)   Zoriginal_exceptionr[   Alitellm.proxy.proxy_server.create_batch(): Exception occured - {} )6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"   team_metadatar   intr   r   r   r   r   r5   hasattrrE   r   rG   rH   rA   r   lenrU   query_paramsheadersr   post_call_success_hookasynciocreate_taskupdate_request_statusgetattrupdateget_custom_headers	Exceptionpost_call_failure_hook	exceptionstrr    )"r&   r'   r(   r)   r+   r,   r-   r.   r/   r4   base_llm_response_processorlitellm_logging_objZrouter_modelZis_router_modelr8   Z_create_batch_datari   Zenforced_batch_expiryrA   rK   Zmodel_from_file_idrD   Zoriginal_file_idrO   original_batch_idZencoded_batch_idtarget_model_namesr7   model_paramhidden_paramsrC   rV   rW   er^   r^   `/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/batches_endpoints/endpoints.pycreate_batch+   s  &












r   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                    s  ddl m}m}m}m}m}	 i }
zt|}t|d}tt	|}
t
|}t|
d}|j| |||	||ddI dH \}
}|d}dd	l m} t||||td
I dH \}}|dur|jdv r|j|
||dI dH }|rrt||I dH  t|j|
dddd t|di pi }|ddpd}|ddpd}|ddpd}|jtj|||||	t|dd|
d |W S |durtd| d|j d |durt||dd}t|}t|
||d |
 d||
d< t!j"d/d|d i|
I dH }t#||d  td!| d"|  nTt!j$d#u s|r>|du rt%d$d%d&id'|j"d/i |
I dH }||j&d(< |r=t'|}|r=||j&d< n|pNt(| d)pNt)| d)pNd*}t!j"d/d|i|
I dH }t*|||||t|d+d,I dH  |j|
||dI dH }|rt||I dH  t|j|
dddd t|di pi }|ddpd}|ddpd}|ddpd}|jtj|||||	t|dd|
d |W S  t+y } z|j,|||
d-I dH  t-d..t/| t0|d}~ww )0at  
    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   r3   aretrieve_batchr6   Nmanaged_filesprisma_client)r   unified_batch_idmanaged_files_objr   r   )	completedfailed	cancelledZexpiredrN   rP   rQ   rR   rS   rU   rC   rV   rW   rX   rY   zBatch z is in non-terminal state z, syncing with providerz*batch retrieval (batch created with model)rB   r4   rD   rF   rF   r   r8   rM   zRetrieved batch using model: , original_id: Tr<   r=   rI   r>   r   r0   r9   retrieve)r   r   rO   r   r   r   db_batch_object	operationr\   Clitellm.proxy.proxy_server.retrieve_batch(): Exception occured - {}r^   )1r_   r+   r,   r-   r.   r/   r   r   r   dictr   r   rd   get_proxy_hookr   r   r   rT   ro   r   rp   rq   rr   rg   rs   rn   rt   ru   r`   r   r   r   rh   re   r   r   rf   r   rU   r   r   r   r   rv   rw   rx   ra   ry   r    )r&   r'   r)   r(   r   r+   r,   r-   r.   r/   r4   model_from_idZ_retrieve_batch_requestr   rz   r{   r   r   r   rO   r   rC   rV   rW   rD   r|   Zmodel_id_from_batchr8   r   r^   r^   r   retrieve_batchF  s>  %



	





r   limitafterr}   c                    s  ddl m}m}m}	m}
m} td|| zD|du r(t	ddt
jjidt| dI dH }t|d	}|j| ||||
|	d
dI dH \}}|
d}|durht|drhtd |j||||||dI dH }n|dpx| jdpx| jd }rt||dd}|| tjd)|d ||d|I dH }|rt|dr|jr|jD ]}t||d qtd|  nR|s|ddr|p|dd}|du rtd|dd }|dd |jd)|||d|I dH }n|pt| dpt | dpd}tjd)|||d|I dH }|
j!|||dI dH }|dur$t"|t"|u r$|}t#|di p,i }|d dp5d!}|d"dp>d!}|d#dpGd!}|jtj$|||||t#|d$d!d% |W S  t%y } z|
j&||||d&d'I dH  t'd(t(| t)|d}~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*   z!GET /v1/batches after={} limit={}Nr<   r=   r>   r0   r3   alist_batchesr6   r   list_user_batchesz-Using managed objects table for batch listing)r)   r   r   r(   r}   r,   r7   rL   zbatch listingrB   r8   )r8   r   r   r4   rM   zListed batches using model: r}   z8target_model_names is required for this routing scenario,)r7   r   r   r9   rN   rU   rC   rQ   rV   rW   rX   )r)   rC   rV   rW   r/   rZ   )r   r   r\   r   r^   )*r_   r+   r,   r-   r.   r/   r   r`   ra   r   ZCommonProxyErrorsZno_llm_routervaluer   r   rd   r   rk   r   rg   rm   rn   r   rt   re   r   r4   r   
ValueErrorsplitrh   r   r   ro   typers   ru   rv   rw   r=   ry   r    )r&   r'   r(   r   r   r)   r}   r+   r,   r-   r.   r/   r4   rz   r{   r   rO   r~   rD   r#   r7   r8   	_responser   rC   rV   rW   r   r^   r^   r   list_batches8  s   %




	


	


r   z-/{provider}/v1/batches/{batch_id:path}/cancelz"/v1/batches/{batch_id:path}/cancelz/batches/{batch_id:path}/cancelc                    s  ddl m}m}m}m}m}	m}
 i }z8t|}t|d}t	t
|}t|}t|d}|j| |||
|	|ddI dH \}}||| |||
|dI dH }|durt||d	d
}t|}t|||d |d||d< tjd*d|d i|I dH }t||d td| d|  nW|r|du rtdddid|jd*i |I dH }||jd< |jds|dr|d |jd< n&|p|ddpd}|dd td*d|i|}tjd*d|i|I dH }|	d}ddl m} t|||||tddI dH  |	j|||dI dH }t|	j |dd d!d" t!|d#i pi }|ddp#d }|d$dp,d }|d%dp5d }|j"#tj$|||||
t!|d&d |d' |W S  t%yr } z|	j&|||d(I dH  t'd)(t)| t*|d}~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/   r   r3   acancel_batchr6   N)r4   r&   r+   r)   r/   r-   z-batch cancellation (batch created with model)rB   r   rF   r   r8   rM   zCancelled batch using model: r   r<   r=   rI   r>   r   rC   r7   r9   r   r   cancel)r   r   rO   r   r   r   r   rN   rP   rQ   rR   rS   rU   rV   rW   rX   rY   r\   r]   r^   )+r_   r   r+   r,   r-   r.   r/   r   r   r   r   r   r   rd   r   r   r   rh   re   r   r   r   r`   r   rU   rg   r   r   r   ro   rp   rq   rr   rs   rn   rt   ru   rv   rw   rx   ra   ry   r    )r&   r   r'   r(   r)   r   r+   r,   r-   r.   r/   r4   r   Z_cancel_batch_requestr   rz   r{   rD   r|   rO   r8   Z_cancel_batch_datar   r   r   rC   rV   rW   r   r^   r^   r   cancel_batch  s    %	






r   )8rp   typingr   r   r   Zfastapir   r   r   r   r	   r
   re   Zlitellm._loggingr   Zlitellm.batches.mainr   r   Zlitellm.proxy._typesZ$litellm.proxy.auth.user_api_key_authr   Z'litellm.proxy.common_request_processingr   Z-litellm.proxy.common_utils.http_parsing_utilsr   Z0litellm.proxy.common_utils.openai_endpoint_utilsr   r   Z1litellm.proxy.openai_files_endpoints.common_utilsr   r   r   r   r   r   r   r   r   r   r   r   Zlitellm.proxy.utilsr    r!   Zlitellm.types.llms.openair"   Zrouterpostry   ZUserAPIKeyAuthr   rg   r   rj   r   r   r^   r^   r^   r   <module>   s   8   d &	