
    g6=                     f   d dl Z d dlZd dlmZ d dl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  e       Zd dlmZ dad	 Zd
efdZej1                  d ee      gdgd      ej1                  d ee      gdgd       ee      fdedededefd              Zej7                  d ee      gdgd      ej7                  d ee      gdgd       ee      fdededed
ed   def
d              Zej7                  d ee      gdgd      ej7                  d ee      gdgd      dd ee      fdeded
ed   dee   dee   defd              Zej1                  d  ee      gdgd!      ej1                  d" ee      gdgd!       ee      fdedededefd#              Z y)$    N)Optional)	APIRouterDependsRequestResponse)verbose_proxy_logger)*)user_api_key_auth)handle_exception_on_proxy)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       b/var/www/openai/venv/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#   -   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                 p  K   ddl m}m}m}m}m}m}	m}
 |j                  d      }	 |dur&t        dt        j                  j                         t        j                  dj                  t         j#                  |d                    ||| |||
|	       d
{   }t%        |j&                        }||j)                  |       t+        j,                  di | d
{   }t/        j0                  |	j3                  |j5                  dd      d             t7        |di       xs i }|j5                  dd
      xs d}|j5                  dd
      xs d}|j5                  dd
      xs d}|j8                  j)                   ||||||
t7        |dd                   |S 7 7 # t:        $ r}|	j=                  |||       d
{  7   t        j>                  dj                  tA        |                   t        j                  tC        jD                                tG        |      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   add_litellm_data_to_requestgeneral_settingsget_custom_headerspremium_userproxy_configproxy_logging_objversionT)exclude_none+Only premium users can use this endpoint + zRequest received by LiteLLM:
{}   )indentdatar'   r.   r*   r3   r1   Nr   litellm_call_id success)r;   status_hidden_paramsmodel_id	cache_keyapi_baseallowed_model_regionr*   r@   rA   rB   r3   model_regionr*   original_exceptionrequest_datazLlitellm.proxy.proxy_server.create_fine_tuning_job(): Exception occurred - {} )$litellm.proxy.proxy_serverr-   r.   r/   r0   r1   r2   r3   
model_dumpr   CommonProxyErrorsnot_premium_userr   r   debugformatjsondumpsr#   r   updater   acreate_fine_tuning_jobasynciocreate_taskupdate_request_statusr!   getattrheaders	Exceptionpost_call_failure_hookerrorr   	traceback
format_excr   )r'   r(   r)   r*   r-   r.   r/   r0   r1   r2   r3   r9   llm_provider_configresponsehidden_paramsr@   rA   rB   es                      r   create_fine_tuning_jobrb   ;   sI    L   ))t)<DC+t#=>O>`>`>f>f=gh 
 	"".55djjaj6PQ	

 1-/%
 
 > 3 G G

 *KK+, 88@4@@ 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 Y
$ A6  
+66/ATX 7 
 	
 	
 	""ZaaA	

 	""9#7#7#9:'**
+s\   %H6A4F) F$AF) "F'#C F) #H6$F) 'F) )	H32H.	G
A$H..H33H6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                 d  K   ddl m}m}m}m}m}	m}
m} i }	 |dur&t        dt        j                  j                          ||| ||||	       d{   }t        |      }||j                  |       t        j                  di |d|i 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{  7   t+        j,                  dj/                  t1        |                   t+        j2                  t5        j6                                t9        |      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   r,   Tr5   r8   Nr:   rc   r?   r@   r<   rA   rB   rC   rD   rF   Klitellm.proxy.proxy_server.list_fine_tuning_jobs(): Exception occurred - {}rI   )rJ   r-   r.   r/   r0   r1   r2   r3   r   rL   rM   r   r#   rR   r   aretrieve_fine_tuning_jobrW   r!   rX   rY   rZ   r   r[   rO   r   rN   r\   r]   r   )r'   r(   rc   r   r*   r-   r.   r/   r0   r1   r2   r3   r9   r^   r_   r`   r@   rA   rB   ra   s                       r   retrieve_fine_tuning_jobri      s    6   D9+t#=>O>`>`>f>f=gh  1-/%
 
 > 3
 *KK+, :: 

1
 
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 O
"
0  
+66/ATX 7 
 	
 	
 	""Y``A	

 	""9#7#7#9:'**
+sZ   F0;D# D?D# D!B
D# F0D# !D# #	F-,F(EA$F((F--F0u&   ✨ (Enterprise) List Fine-Tuning Jobsafterlimitc                 f  K   ddl m}m}m}m}	m}
m}m} i }	 |	dur&t        dt        j                  j                          ||| ||||
       d{   }t        |      }||j                  |       t        j                  di |||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 # t&        $ r}|j)                  |||       d{  7   t+        j,                  dj/                  t1        |                   t+        j2                  t5        j6                                t9        |      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   r,   Tr5   r8   Nr:   )rj   rk   r?   r@   r<   rA   rB   rC   rD   rF   rg   rI   )rJ   r-   r.   r/   r0   r1   r2   r3   r   rL   rM   r   r#   rR   r   alist_fine_tuning_jobsrW   r!   rX   rY   rZ   r   r[   rO   r   rN   r\   r]   r   )r'   r(   r   rj   rk   r*   r-   r.   r/   r0   r1   r2   r3   r9   r^   r_   r`   r@   rA   rB   ra   s                        r   list_fine_tuning_jobsrn     s    :   D:+t#=>O>`>`>f>f=gh  1-/%
 
 > 3
 *KK+, 77 


 
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 Q
"
2  
+66/ATX 7 
 	
 	
 	""Y``A	

 	""9#7#7#9:'**
+s[   F1;D$ D A D$ D"B
D$ F1 D$ "D$ $	F.-F)EA$F))F..F1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}	m}
 i }	 |dur&t        dt        j                  j                          ||| |||
|       d{   }| j                          d{   }|j                  dd      }t        |      }||j                  |       t!        j"                  di |d	|i 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(        $ r}|	j+                  |||       d{  7   t-        j.                  dj1                  t3        |                   t-        j4                  t7        j8                                t;        |      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   r,   Tr5   r8   Nr   r:   rc   r?   r@   r<   rA   rB   rC   rD   rF   rg   rI   )rJ   r-   r.   r/   r0   r1   r2   r3   r   rL   rM   r   rP   r!   r#   rR   r   acancel_fine_tuning_jobrW   rX   rY   rZ   r   r[   rO   r   rN   r\   r]   r   )r'   r(   rc   r*   r-   r.   r/   r0   r1   r2   r3   r9   request_bodyr   r^   r_   r`   r@   rA   rB   ra   s                        r   cancel_fine_tuning_jobrr   y  s    6   D=+t#=>O>`>`>f>f=gh  1-/%
 
 %\\^+*../DdK > 3
 *KK+, 88 

1
 
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 W
 ,
0  
+66/ATX 7 
 	
 	
 	""Y``A	

 	""9#7#7#9:'**
+sm   G;E E	E +E,AE =E>B
E G	E E E 	GG/E20A$GGG)!rT   r\   typingr   fastapir   r   r   r   r   litellm._loggingr   litellm.proxy._types$litellm.proxy.auth.user_api_key_authr
   litellm.proxy.utilsr   routerlitellm.types.llms.openair   r   r   r   r#   postUserAPIKeyAuthrb   r!   Literalri   intrn   rr   rI   r   r   <module>r      s      9 9  1 " B 9	 @  $ +,-
5	   +,-
5	   )00A(B	h+h+h+ 4h+ &	h+h+V 4+,-
7	   1+,-
7	   )00A(BS+S+S+ S+ !!23	S+
 &S+S+l +,-
4	   +,-
4	    (/0A(BV+V+V+ !!23V+ C=	V+
 C=V+ &V+V+r ;+,-
6	   8+,-
6	   )00A(B	W+W+W+ W+ &	W+W+r   