o
    ưi[                     @   sV  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T 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d dlmZ dadd ZdefddZej deegdgddej deegdgddeefde	de
dede!fddZ"ej#deegdgddej#deegdgdddeefde	de
dedee$d   de!f
d!d"Z%ej#deegdgd#dej#deegdgd#ddedd$d%ddeefde	de
dee$d   d&ee d'ee d(ee& de!fd)d*Z'ej d+eegdgd,dej d-eegdgd,deefde	de
dede!fd.d/Z(dS )0    N)Optionalcast)	APIRouterDependsHTTPExceptionQueryRequestResponse)verbose_proxy_logger)*)user_api_key_auth)ProxyBaseLLMRequestProcessing)"_is_base64_encoded_unified_file_id)handle_exception_on_proxy)LiteLLMFineTuningJob)LiteLLMFineTuningJobCreatec                 C   sn   | d u rd S t | tstd| D ]!}t |tr2| D ]\}}t |tr1|dr1t|||< qq| a	d S )Nz9invalid fine_tuning config, expected a list is not a listzos.environ/)

isinstancelist
ValueErrordictitemsstr
startswithlitellmZ
get_secretfine_tuning_config)configelementkeyvalue r   d/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/fine_tuning_endpoints/endpoints.pyset_fine_tuning_config   s   

r!   custom_llm_providerc                 C   s4   t d u rtdt D ]}|d| kr|  S q
d S )Nz?fine_tuning_config is not set, set it on your config.yaml file.r"   )r   r   get)r"   Zsettingr   r   r    get_fine_tuning_provider_config1   s   r$   z/v1/fine_tuning/jobszfine-tuningu'   ✨ (Enterprise) Create Fine-Tuning Job)dependenciestagssummaryz/fine_tuning/jobsrequestfastapi_responsefine_tuning_requestuser_api_key_dictc                    s@  ddl m}m}m}m}m}m}	 |jdd}
z|dur%tdt	j
j tdtj|
dd t|
d	}|j| |||	||d
dI dH \}
}d}|j}d}|rUt|}|r{	 |du rdtdddidtt|jd"i |
I dH }||_||jd< n|jrt|jd}|dur|
| tjd"i |
I dH }|du rtd|j|
||dI dH }|durt|tr|}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*| t+|d}~ww )#ad  
    Creates a fine-tuning job which begins the process of creating a new model from a given dataset.
    This is the equivalent of POST https://api.openai.com/v1/fine_tuning/jobs

    Supports Identical Params as: https://platform.openai.com/docs/api-reference/fine-tuning/create

    Example Curl:
    ```
    curl http://localhost:4000/v1/fine_tuning/jobs       -H "Content-Type: application/json"       -H "Authorization: Bearer sk-1234"       -d '{
        "model": "gpt-3.5-turbo",
        "training_file": "file-abc123",
        "hyperparameters": {
          "n_epochs": 4
        }
      }'
    ```
    r   general_settings
llm_routerpremium_userproxy_configproxy_logging_objversionT)Zexclude_none+Only premium users can use this endpoint + zRequest received by LiteLLM:
{}   )indentdataacreate_fine_tuning_jobr(   r-   r+   r2   r1   r0   Z
route_typeNF  error9LLM Router not initialized. Ensure models added to proxy.status_codedetailunified_file_idr"   LInvalid request, No litellm managed file id or custom_llm_provider provided.r7   r+   responselitellm_call_id successrE   status_hidden_paramsmodel_id	cache_keyapi_baseallowed_model_regionr+   rK   rL   rM   r2   Zmodel_regionr+   Zoriginal_exceptionZrequest_datazLlitellm.proxy.proxy_server.create_fine_tuning_job(): Exception occurred - {}r   ),litellm.proxy.proxy_serverr-   r.   r/   r0   r1   r2   Z
model_dumpr   CommonProxyErrorsnot_premium_userr   r
   debugformatjsondumpsr    common_processing_pre_call_logictraining_filer   r   r   r   r8   rJ   r"   r$   updater   post_call_success_hookr   asynciocreate_taskupdate_request_statusr#   getattrheadersget_custom_headers	Exceptionpost_call_failure_hook	exceptionr   r   )r(   r)   r*   r+   r-   r.   r/   r0   r1   r2   r7   base_llm_response_processorlitellm_logging_objr@   rY   rD   llm_provider_config	_responsehidden_paramsrK   rL   rM   er   r   r    create_fine_tuning_job?   s    &	



rk   z./v1/fine_tuning/jobs/{fine_tuning_job_id:path}u)   ✨ (Enterprise) Retrieve Fine-Tuning Jobz+/fine_tuning/jobs/{fine_tuning_job_id:path}fine_tuning_job_id)ZopenaiZazurec                    sR  ddl m}m}m}m}m}	m}
 d|i}z|dur#tdtj	j
 t|d}|j| |||
|	|tjj
dI dH \}}z	|  I dH }W n tyO   i }Y nw |d	dpW|}d
}d}|rbt|}|r|du rptdddidtt|jd i |I dH }||jd< n|rt|d}|dur|| tjd i |I dH }|du rtddd|	j|||dI dH }|durt|tr|}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&| t'|d}~ww )!a&  
    Retrieves a fine-tuning job.
    This is the equivalent of GET https://api.openai.com/v1/fine_tuning/jobs/{fine_tuning_job_id}

    Supported Query Params:
    - `custom_llm_provider`: Name of the LiteLLM provider
    - `fine_tuning_job_id`: The ID of the fine-tuning job to retrieve.
    r   r,   rl   Tr3   r6   r9   Nr"   Fr:   r;   r<   r=   unified_finetuning_job_idrA     rB   rC   rE   rF   rG   rH   rJ   rK   rL   rM   rN   rO   rP   zNlitellm.proxy.proxy_server.retrieve_fine_tuning_job(): Exception occurred - {}r   )(rQ   r-   r.   r/   r0   r1   r2   r   rR   rS   r   r   rX   	CallTypesZaretrieve_fine_tuning_jobrV   rb   r#   r   r   r   r   rJ   r$   rZ   r   r[   r   r\   r]   r^   r_   r`   ra   rc   r
   rd   rU   r   r   )r(   r)   rl   r"   r+   r-   r.   r/   r0   r1   r2   r7   re   rf   request_bodyrm   rD   rg   rh   ri   rK   rL   rM   rj   r   r   r    retrieve_fine_tuning_job   s    	




rq   u&   ✨ (Enterprise) List Fine-Tuning JobszOComma separated list of model names to filter by. Example: 'gpt-4o,gpt-4o-mini')defaultdescriptiontarget_model_namesafterlimitc                    s  ddl m}m}m}	m}
m}m} i }z|	dur!tdtj	j
 t|d}|j| |||||
tjj
dI dH \}}d}|rvt|trv|d}t|d	krStd
dd|du r]tddd|d |d< |jdi |||dI dH }|W S |rt|d}|dur|| tjdi |||dI dH }|du rt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| t |d}~ww )ar  
    Lists fine-tuning jobs for the organization.
    This is the equivalent of GET https://api.openai.com/v1/fine_tuning/jobs

    Supported Query Params:
    - `custom_llm_provider`: Name of the LiteLLM provider
    - `after`: Identifier for the last job from the previous pagination request.
    - `limit`: Number of fine-tuning jobs to retrieve (default is 20).
    r   r,   Tr3   r6   r9   N,   rn   zatarget_model_names on list fine-tuning jobs must be a list of one model name. Example: ['gpt-4o']r=   r:   r<   model)ru   rv   rA   rB   rJ   rK   rF   rL   rM   rN   rO   rP   zKlitellm.proxy.proxy_server.list_fine_tuning_jobs(): Exception occurred - {}r   )!rQ   r-   r.   r/   r0   r1   r2   r   rR   rS   r   r   rX   ro   Zalist_fine_tuning_jobsr   r   splitlenr   r$   rZ   r   r_   r#   r`   ra   rb   rc   r
   rd   rU   r   )r(   r)   r"   rt   ru   rv   r+   r-   r.   r/   r0   r1   r2   r7   re   rf   rD   Ztarget_model_names_listrg   ri   rK   rL   rM   rj   r   r   r    list_fine_tuning_jobsx  s    !	




r|   z5/v1/fine_tuning/jobs/{fine_tuning_job_id:path}/cancelu(   ✨ (Enterprise) Cancel Fine-Tuning Jobsz2/fine_tuning/jobs/{fine_tuning_job_id:path}/cancelc                    sJ  ddl m}m}m}m}m}m}	 d|i}
z|dur#tdtj	j
 t|
d}|j| |||	||tjj
dI dH \}
}z	|  I dH }W n tyO   i }Y nw |d	d}d
}d}|r`t|}|r|du rntdddidtt|jd i |
I dH }||jd< nt|d}|dur|
| tjd i |
I dH }|du rtddd|j|
||dI dH }|durt|tr|}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&| t'|d}~ww )!a*  
    Cancel a fine-tuning job.

    This is the equivalent of POST https://api.openai.com/v1/fine_tuning/jobs/{fine_tuning_job_id}/cancel

    Supported Query Params:
    - `custom_llm_provider`: Name of the LiteLLM provider
    - `fine_tuning_job_id`: The ID of the fine-tuning job to cancel.
    r   r,   rl   Tr3   r6   r9   Nr"   Fr:   r;   r<   r=   rm   rA   rn   rB   rC   rE   rF   rG   rH   rJ   rK   rL   rM   rN   rO   rP   zLlitellm.proxy.proxy_server.cancel_fine_tuning_job(): Exception occurred - {}r   )(rQ   r-   r.   r/   r0   r1   r2   r   rR   rS   r   r   rX   ro   Zacancel_fine_tuning_jobrV   rb   r#   r   r   r   r   rJ   r$   rZ   r   r[   r   r\   r]   r^   r_   r`   ra   rc   r
   rd   rU   r   r   )r(   r)   rl   r+   r-   r.   r/   r0   r1   r2   r7   re   rf   rp   r"   rm   rD   rg   rh   ri   rK   rL   rM   rj   r   r   r    cancel_fine_tuning_job  s    	




r}   ))r\   typingr   r   Zfastapir   r   r   r   r   r	   r   Zlitellm._loggingr
   Zlitellm.proxy._typesZ$litellm.proxy.auth.user_api_key_authr   Z'litellm.proxy.common_request_processingr   Z1litellm.proxy.openai_files_endpoints.common_utilsr   Zlitellm.proxy.utilsr   Zlitellm.types.utilsr   ZrouterZlitellm.types.llms.openair   r   r!   r   r$   postZUserAPIKeyAuthrk   r#   Literalrq   intr|   r}   r   r   r   r    <module>   s    

 

 	
	
{
