
    h[                        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 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 ZdefdZejA                  d ee      gdgd      ejA                  d ee      gdgd       ee      fde	de
dede!fd              Z"ejG                  d ee      gdgd      ejG                  d ee      gdgd      d ee      fde	de
dedee$d      de!f
d              Z%ejG                  d ee      gdgd      ejG                  d ee      gdgd      d e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%              Z'ejA                  d& ee      gdgd'      ejA                  d( ee      gdgd'       ee      fde	de
dede!fd)              Z(y)*    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                    | y t        | t              st        d      | D ]f  }t        |t              s|j	                         D ]@  \  }}t        |t
              s|j                  d      s)t        j                  |      ||<   B h | a	y )Nz9invalid fine_tuning config, expected a list is not a listzos.environ/)

isinstancelist
ValueErrordictitemsstr
startswithlitellm
get_secretfine_tuning_config)configelementkeyvalues       i/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/fine_tuning_endpoints/endpoints.pyset_fine_tuning_configr#      s}    ~ fd#TUUgt$%mmo
UeS)e.>.>}.M#*#5#5e#<GCL .       custom_llm_providerc                 l    t         t        d      t         D ]  }|j                  d      | k(  s|c S  y )Nz?fine_tuning_config is not set, set it on your config.yaml file.r%   )r   r   get)r%   settings     r"   get_fine_tuning_provider_configr)   1   sC     !M
 	
 &;;,-1DDN & 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                   K   ddl m}m}m}m}m}m}	 |j                  d      }
	 |dur&t        dt        j                  j                         t        j                  dj                  t        j!                  |
d                   t#        |
	      }|j%                  | |||	||d
       d{   \  }
}d}|j&                  }d}|rt)        |      }|rQ	 |t+        dddi      t-        t.         |j0                  d"i |
 d{         }||_        ||j2                  d<   nR|j4                  rFt7        |j4                        }||
j9                  |       t;        j0                  d"i |
 d{   }|t        d      |j=                  |
||       d{   }|t?        |t.              r|}tA        jB                  |jE                  |
jG                  dd      d             tI        |di       xs i }|jG                  dd      xs d}|jG                  dd      xs d}|jG                  dd      xs d}|jJ                  j9                  t#        jL                  |||||	tI        |dd                   |S 7 7 |7 7 # tN        $ rZ}|jQ                  |||
        d{  7   t        jR                  d!j                  tU        |                   tW        |      d}~ww xY 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)exclude_none+Only premium users can use this endpoint + zRequest received by LiteLLM:
{}   )indentdataacreate_fine_tuning_jobr-   r3   r0   r8   r7   r6   
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.r>   r0   responselitellm_call_id successrM   status_hidden_paramsmodel_id	cache_keyapi_baseallowed_model_regionr0   rS   rT   rU   r8   model_regionr0   original_exceptionrequest_datazLlitellm.proxy.proxy_server.create_fine_tuning_job(): Exception occurred - {} ),litellm.proxy.proxy_serverr3   r4   r5   r6   r7   r8   
model_dumpr   CommonProxyErrorsnot_premium_userr!   r   debugformatjsondumpsr    common_processing_pre_call_logictraining_filer   r   r   r   r?   rR   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/   r0   r3   r4   r5   r6   r7   r8   r>   base_llm_response_processorlitellm_logging_objrH   rf   rL   llm_provider_config	_responsehidden_paramsrS   rT   rU   es                          r"   create_fine_tuning_jobrx   ?   sM    L  ))t)<Dl+t#=>O>`>`>f>f=gh 
 	"".55djjaj6PQ	

 'D&N# .NN-//%0 O 
 
	
 7<+9937@OO!# #!\  $,NJ,N,N,VQU,V&VH &5H"9HH##$56 44"A$7$K$K# #./0$<<DtDDH^ 
 ,BB/ C 
 
	
  Z	;O%P H 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  '')<<"3!#!$%68NPRS		
 e
8 'W E
B  	+66/ATX 7 
 	
 	
 	&&ZaaA	

 (**	+s   #K2B
J 0J1AJ J
	A,J 5J6(J J
C!J  K2J J J 
J 	K/K*,J/-=K**K//K2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)openaiazurec                   K   ddl m}m}m}m}m}	m}
 d|i}	 |dur&t        dt        j                  j                         t        |      }|j                  | |||
|	|t        j                  j                         d{   \  }}	 | j                          d{   }|j#                  d	d      }d
}d}|rt%        |      }|rI|t'        dddi      t)        t*         |j                  d i | d{         }||j,                  d<   n>|r<t/        |      }||j1                  |       t3        j                  d i | d{   }|t'        dd      |	j5                  |||       d{   }|t7        |t*              r|}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@                  j1                  t        jB                  |||||
t?        |dd                   |S 7 7 # t         $ r i }Y w xY w7 x7 )7 # t         $ rZ}|	jE                  |||       d{  7   tG        jH                  djK                  tM        |                   tO        |      d}~ww xY 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   r2   ry   Tr:   r=   r@   Nr%   FrB   rC   rD   rE   unified_finetuning_job_idrI     rJ   rK   rM   rN   rO   rP   rR   rS   rT   rU   rV   rW   rY   zNlitellm.proxy.proxy_server.retrieve_fine_tuning_job(): Exception occurred - {}r\   )(r]   r3   r4   r5   r6   r7   r8   r   r_   r`   r!   r   re   	CallTypesaretrieve_fine_tuning_jobrc   ro   r'   r   r   r   r   rR   r)   rg   r   rh   r   ri   rj   rk   rl   rm   rn   rp   r   rq   rb   r   r   )r-   r.   ry   r%   r0   r3   r4   r5   r6   r7   r8   r>   rr   rs   request_bodyr}   rL   rt   ru   rv   rS   rT   rU   rw   s                           r"   retrieve_fine_tuning_jobr      se    6  '(:;Dr+t#=>O>`>`>f>f=gh  'D&N# .NN-//% ::@@ O 
 
	
	!(/L +../DdK AF!37(J")% %!# #!\  $:j::  H * ##+ !"A$7# #./0$>>  H e  ,BB/ C 
 
	
  Z	;O%P H 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  '')<<"3!#!$%68NPRS		
 {
 0 	L	, 
D  	+66/ATX 7 
 	
 	
 	&&\ccA	

 (**	+s   KA)I8 II8 
I II "AI8 7I/
8AI8 	I2
*I8 4I55C!I8 KI8 I I,(I8 +I,,I8 2I8 5I8 8	KKJ=KKK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                   K   ddl m}m}m}	m}
m}m} i }	 |	dur&t        dt        j                  j                         t        |      }|j                  | |||||
t        j                  j                         d{   \  }}d}|rtt        |t               rd|j#                  d      }t%        |      d	k7  rt'        d
d      |t'        dd      |d   |d<    |j                  di |||d d{   }|S |rAt)        |      }||j+                  |       t-        j                  di |||d d{   }|t'        d
d      t/        |di       xs i }|j1                  dd      xs d}|j1                  dd      xs d}|j1                  dd      xs d}|j2                  j+                  t        j4                  |||||t/        |dd                   |S 7 f7 7 # t6        $ rZ}|j9                  |||       d{  7   t;        j<                  dj?                  t!        |                   tA        |      d}~ww xY 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   r2   Tr:   r=   r@   N,   r~   zatarget_model_names on list fine-tuning jobs must be a list of one model name. Example: ['gpt-4o']rE   rB   rD   model)r   r   rI   rJ   rR   rS   rN   rT   rU   rV   rW   rY   zKlitellm.proxy.proxy_server.list_fine_tuning_jobs(): Exception occurred - {}r\   )!r]   r3   r4   r5   r6   r7   r8   r   r_   r`   r!   r   re   r   alist_fine_tuning_jobsr   r   splitlenr   r)   rg   r   rl   r'   rm   rn   ro   rp   r   rq   rb   r   )r-   r.   r%   r   r   r   r0   r3   r4   r5   r6   r7   r8   r>   rr   rs   rL   target_model_names_listrt   rv   rS   rT   rU   rw   s                           r"   list_fine_tuning_jobsr   x  s    B  DY+t#=>O>`>`>f>f=gh  'D&N# .NN-//% 77== O 
 
	
 #'*-?"E&8&>&>s&C#*+q0# #~ 
 !# #V  4A6DM>Z>>  H
 O "A$7# #./0$;;  H
 e   *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  '')<<"3!#!$%68NPRS		
 I
2<  	+66/ATX 7 
 	
 	
 	&&Y``A	

 (**	+sy   IA)G, ?G% A8G, 8G(9G, >I?>G, =G*>B&G, $I%G, (G, *G, ,	I5I
H=I

II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                   K   ddl m}m}m}m}m}m}	 d|i}
	 |dur&t        dt        j                  j                         t        |
      }|j                  | |||	||t        j                  j                         d{   \  }
}	 | j                          d{   }|j#                  d	d      }d
}d}|rt%        |      }|rI|t'        dddi      t)        t*         |j                  d i |
 d{         }||j,                  d<   n<t/        |      }||
j1                  |       t3        j                  d i |
 d{   }|t'        dd      |j5                  |
||       d{   }|t7        |t*              r|}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@                  j1                  t        jB                  |||||	t?        |dd                   |S 7 7 # t         $ r i }Y w xY w7 v7 )7 # t         $ rZ}|jE                  |||
       d{  7   tG        jH                  djK                  tM        |                   tO        |      d}~ww xY 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   r2   ry   Tr:   r=   r@   Nr%   FrB   rC   rD   rE   r}   rI   r~   rJ   rK   rM   rN   rO   rP   rR   rS   rT   rU   rV   rW   rY   zLlitellm.proxy.proxy_server.cancel_fine_tuning_job(): Exception occurred - {}r\   )(r]   r3   r4   r5   r6   r7   r8   r   r_   r`   r!   r   re   r   acancel_fine_tuning_jobrc   ro   r'   r   r   r   r   rR   r)   rg   r   rh   r   ri   rj   rk   rl   rm   rn   rp   r   rq   rb   r   r   )r-   r.   ry   r0   r3   r4   r5   r6   r7   r8   r>   rr   rs   r   r%   r}   rL   rt   ru   rv   rS   rT   rU   rw   s                           r"   cancel_fine_tuning_jobr     sc    6  '(:;Dr+t#=>O>`>`>f>f=gh  'D&N# .NN-//% 88>> O 
 
	
	!(/L +../DdK AF!37(J")% %!# #!\  $8j88  H * ##+
 #B$7# #./0$<<  H e  ,BB/ C 
 
	
  Z	;O%P H 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  '')<<"3!#!$%68NPRS		
 {
 0 	L	, 
D  	+66/ATX 7 
 	
 	
 	&&ZaaA	

 (**	+s   KA)I6 II6 
I II "AI6 7I-
8AI6 I0*I6 2I33C!I6 KI6 I I*&I6 )I**I6 0I6 3I6 6	K?KJ=KKK))ri   typingr   r   fastapir   r   r   r   r	   r
   r   litellm._loggingr   litellm.proxy._types$litellm.proxy.auth.user_api_key_authr   'litellm.proxy.common_request_processingr   1litellm.proxy.openai_files_endpoints.common_utilsr   litellm.proxy.utilsr   litellm.types.utilsr   routerlitellm.types.llms.openair   r   r#   r   r)   postUserAPIKeyAuthrx   r'   Literalr   intr   r   r\   r$   r"   <module>r      s6    ! O O  1 " B Q : 4	 @  $ +,-
5	   +,-
5	   )00A(B	P+P+P+ 4P+ &	P+P+f 4+,-
7	   1+,-
7	   AE(/0A(BK+K+K+ K+ "'*;"<=	K+
 &K+K+\ +,-
4	   +,-
4	   AE(-e)  (/0A(Bx+x+x+ "'*;"<=x+ !	x+ C=x+ C=x+ &x+x+v ;+,-
6	   8+,-
6	   )00A(B	K+K+K+ K+ &	K+K+r$   