
    gn                     B   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlZddlZ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 dd
lmZmZmZ ddl ddlmZm Z   e       Z! e       Z" e       Z#ei dddddddfde$de$dee%   dee$   dee$   dee&e$      dee'   de
d   dee	e$e$f      dee	e$e$f      defd       Z(ei dddddddfde$de$dee%   dee$   dee$   dee&e$      dee'   de
d   dee	e$e$f      dee	e$e$f      deeeeeef   f   fd       Z)	 	 	 d%de$de
d   dee	e$e$f      dee	e$e$f      def
dZ*	 	 	 d%de$de
d   dee	e$e$f      dee	e$e$f      deeeeeef   f   f
dZ+	 	 	 	 	 d&dee$   d ee'   de
d   dee	e$e$f      dee	e$e$f      f
d!Z,	 	 	 	 	 d&dee$   d ee'   de
d   dee	e$e$f      dee	e$e$f      f
d"Z-	 	 	 d%de$de
d   dee	e$e$f      dee	e$e$f      def
d#Z.	 	 	 d%de$de
d   dee	e$e$f      dee	e$e$f      deeeeeef   f   f
d$Z/y)'z
Main File for Fine Tuning API implementation

https://platform.openai.com/docs/api-reference/fine-tuning

- fine_tuning.jobs.create()
- fine_tuning.jobs.list()
- client.fine_tuning.jobs.list_events()
    N)partial)Any	CoroutineDictLiteralOptionalUnion)verbose_logger)AzureOpenAIFineTuningAPI)OpenAIFineTuningAPI)VertexFineTuningAPI)get_secret_str)FineTuningJobFineTuningJobCreateHyperparameters)*)clientsupports_httpx_timeoutopenaimodeltraining_filehyperparameterssuffixvalidation_fileintegrationsseedcustom_llm_provider)r   azure	vertex_aiextra_headers
extra_bodyreturnc
                   K   t        j                  d| |
       	 t        j                         }d|
d<   t	        t
        | |||||||||	fi |
}t        j                         }t	        |j                  |      }|j                  d|       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r}|d}~ww xY ww)zO
    Async: Creates and executes a batch from an uploaded file of request

    z5inside acreate_fine_tuning_job model=%s and kwargs=%sTacreate_fine_tuning_jobN)r
   debugasyncioget_event_loopr   create_fine_tuning_jobcontextvarscopy_contextrunrun_in_executoriscoroutine	Exception)r   r   r   r   r   r   r   r   r    r!   kwargsloopfuncctxfunc_with_contextinit_responseresponsees                     M/var/www/openai/venv/lib/python3.12/site-packages/litellm/fine_tuning/main.pyr$   r$   (   s     $ ?%%',0() "
 
  &&(#CGGT2"2249JKK}-**H  %H L*  sY   CA2B; B7B; ,B9-B; 2C3B; 6C7B; 9B; ;	CCCCc
                    	 |
j                  dd      du }t        d)i |
}|xs i }t        d)i |}|j                  xs |
j	                  dd      xs d}|:t        |t        j                        r t        |      du r|j                  xs d}|}n,|&t        |t        j                        st        |      }n|d}|dk(  r|j                  xs+ t        j                  xs t        j                  d	      xs d
}|j                  xs, t        j                  xs t        j                  dd      xs d}|j                   xs9 t        j                   xs' t        j"                  xs t        j                  d      }t%        | ||||||      }|j'                  d      }t(        j+                  |||j,                  ||||j.                  |      }|S |dk(  r)|j                  xs t        j                  xs t1        d      }|j,                  xs t        j,                  xs t1        d      }|j                   xs< t        j                   xs* t        j2                  xs t1        d      xs t1        d      }|j	                  di       }	|	|	j                  dd       nt1        d       t%        | ||||||      }|j'                  d      }t4        j+                  ||||||j.                  ||j                        }|S |dk(  r|j                  xs d}|j6                  xs t        j6                  xs t1        d      }|j8                  xs t        j8                  xs t1        d      }|j:                  xs t1        d      }t%        | ||||||      }t<        j+                  ||||||||
|	      }|S t        j>                  jA                  djC                  |      d |t        jD                  d!d"t        jF                  d#d$%      &      '      # tH        $ r&}tK        jL                  d(tO        |             |d}~ww xY w)*z
    Creates a fine-tuning job which begins the process of creating a new model from a given dataset.

    Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete

    r$   FTrequest_timeoutX  N     @r   OPENAI_API_BASEhttps://api.openai.com/v1OPENAI_ORGANIZATIONOPENAI_API_KEY)r   r   r   r   r   r   r   )exclude_none)api_baseapi_keyapi_versionorganizationcreate_fine_tuning_job_datatimeoutmax_retries	_is_asyncr   AZURE_API_BASEAZURE_API_VERSIONAZURE_OPENAI_API_KEYAZURE_API_KEYr!   azure_ad_tokenAZURE_AD_TOKEN)rA   rB   rC   rE   rF   rG   rH   rD   r    VERTEXAI_PROJECTVERTEXAI_LOCATIONVERTEXAI_CREDENTIALS)	rH   rE   vertex_credentialsvertex_projectvertex_locationrF   rA   r/   original_hyperparametersJLiteLLM doesn't support {} for 'create_batch'. Only 'openai' is supported.n/a  Unsupported providercreate_thread"https://github.com/BerriAI/litellmmethodurlstatus_codecontentrequestmessager   llm_providerr5   z*got exception in create_fine_tuning_job=%s )(popGenericLiteLLMParamsr   rF   get
isinstancehttpxTimeoutr   readfloatrA   litellmosgetenvrD   rB   
openai_keyr   
model_dump openai_fine_tuning_apis_instancer(   rC   rG   r   	azure_keyazure_fine_tuning_apis_instancerT   rU   rS    vertex_fine_tuning_apis_instance
exceptionsBadRequestErrorformatResponseRequestr.   r
   errorstr)r   r   r   r   r   r   r   r   r    r!   r/   rH   optional_params_oai_hyperparametersrF   read_timeoutrA   rD   rB   rE    create_fine_tuning_job_data_dictr5   rC   vertex_ai_projectvertex_ai_locationrS   r6   s                              r7   r(   r(   ^   s   (kJJ8%@DH	.88 */R0? 1
1
 "))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_G (*  (( /##/99.// /	   ,, ''992D9 	   '' /??/%%/ 99-.	  +>+ 4 /)+' 0K/U/U! 0V 0, 8NN!+77),L+77# O 	HR } !G+&//g73C3Cg~VfGgH  ++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/0*=+ 4 /)+' 0K/U/U! 0V 0, 7MM!',L+77#,99 N 	Ht a !K/&//52H.. 6))6!"45   // 7**7!"56 
 "1!C!C "~&H +>+ 4 /)+' 8NN#,G#50 2!)8 O 
H0  $$44dkk' 0 #2!MMFjk 5    I3q6Rs-   GP D.P B5P :AP 	Q !QQfine_tuning_job_idc                 f  K   	 t        j                         }d|d<   t        t        | |||fi |}t	        j
                         }t        |j                  |      }|j                  d|       d{   }	t        j                  |	      r|	 d{   }
|
S |	}
|
S 7 )7 # t        $ r}|d}~ww xY ww)z4
    Async: Immediately cancel a fine-tune job.
    Tacancel_fine_tuning_jobN)
r&   r'   r   cancel_fine_tuning_jobr)   r*   r+   r,   r-   r.   r   r   r    r!   r/   r0   r1   r2   r3   r4   r5   r6   s               r7   r   r      s     %%',0() "
 
 &&(#CGGT2"2249JKK}-**H  %H L*  Y   B1A,B 0B1B BB B1B B1B B 	B.'B))B..B1c                 4   	 t        d i |}|j                  xs |j                  dd      xs d}|:t        |t        j
                        r t        |      du r|j                  xs d}|}n,|&t        |t        j
                        st        |      }n|d}|j                  dd      du }|dk(  r|j                  xs+ t        j                  xs t        j                  d	      xs d
}	|j                  xs, t        j                  xs t        j                  dd      xs d}
|j                  xs9 t        j                  xs' t        j                   xs t        j                  d      }t"        j%                  |	||j&                  |
| ||j(                  |      }|S |dk(  r|j                  xs t        j                  xs t+        d      }	|j&                  xs t        j&                  xs t-        d      }|j                  xs< t        j                  xs* t        j.                  xs t-        d      xs t-        d      }|j                  di       }||j                  dd       nt-        d       t0        j%                  |	||| ||j(                  ||j                        }|S t        j2                  j5                  dj7                  |      d|t	        j8                  ddt	        j:                  dd                  # t<        $ r}|d}~ww xY w)!z
    Immediately cancel a fine-tune job.

    Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete

    r9   r:   NFr;   r   Tr   r<   r=   r>   r?   rA   rB   rC   rD   r   rF   rG   rH   r   rI   rJ   rK   rL   r!   rM   rN   rA   rB   rC   r   rF   rG   rH   rD   rW   rX   rY   rZ   r[   r\   r]   r`   rd   rg   )ri   rF   rj   rk   rl   rm   r   rn   ro   rh   rA   rp   rq   rr   rD   rB   rs   ru   r   rC   rG   
get_secretr   rv   rw   ry   rz   r{   r|   r}   r.   r   r   r    r!   r/   r   rF   r   rH   rA   rD   rB   r5   rC   r6   s                  r7   r   r   E  s1   g.88!))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_GJJ8%@DH	 (*  (( /##/99.// /	   ,, ''992D9 	   '' /??/%%/ 99-.	  8NN!+77)#5+77# O 	Hr ] !G+&//c73C3CczRbGcH  ++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/06MM!'#5+77#,99 N 	H.  $$44dkk' 0 #2!MMFjk 5    &   FL D
L *AL 	LLLafterlimitc                 h  K   	 t        j                         }d|d<   t        t        | ||||fi |}t	        j
                         }t        |j                  |      }	|j                  d|	       d{   }
t        j                  |
      r|
 d{   }|S |
}|S 7 )7 # t        $ r}|d}~ww xY ww)z:
    Async: List your organization's fine-tuning jobs
    Talist_fine_tuning_jobsN)
r&   r'   r   list_fine_tuning_jobsr)   r*   r+   r,   r-   r.   )r   r   r   r    r!   r/   r0   r1   r2   r3   r4   r5   r6   s                r7   r   r     s     %%'+/'( !
 
 &&(#CGGT2"2249JKK}-**H  %H L*  sY   B2A-B 1B2B BB B2B B2B B 	B/(B**B//B2c                 8   	 t        d i |}|j                  xs |j                  dd      xs d}|:t        |t        j
                        r t        |      du r|j                  xs d}|}n,|&t        |t        j
                        st        |      }n|d}|j                  dd      du }	|dk(  r|j                  xs+ t        j                  xs t        j                  d	      xs d
}
|j                  xs, t        j                  xs t        j                  dd      xs d}|j                  xs9 t        j                  xs' t        j                   xs t        j                  d      }t"        j%                  |
||j&                  || |||j(                  |		      }|S |dk(  r|j                  xs t        j                  xs t+        d      }
|j&                  xs t        j&                  xs t+        d      }|j                  xs< t        j                  xs* t        j,                  xs t+        d      xs t+        d      }|j                  di       }||j                  dd       nt/        d       t0        j%                  |
||| |||j(                  |	|j                  	      }|S t        j2                  j5                  dj7                  |      d|t	        j8                  ddt	        j:                  dd                  # t<        $ r}|d}~ww xY w)!a   
    List your organization's fine-tuning jobs

    Params:

    - after: Optional[str] = None, Identifier for the last job from the previous pagination request.
    - limit: Optional[int] = None, Number of fine-tuning jobs to retrieve. Defaults to 20
    r9   r:   NFr;   r   Tr   r<   r=   r>   r?   )	rA   rB   rC   rD   r   r   rF   rG   rH   r   rI   rJ   rK   rL   r!   rM   rN   )	rA   rB   rC   r   r   rF   rG   rH   rD   rW   rX   rY   rZ   r[   r\   r]   r`   rd   rg   )ri   rF   rj   rk   rl   rm   r   rn   ro   rh   rA   rp   rq   rr   rD   rB   rs   ru   r   rC   rG   r   rv   r   rw   ry   rz   r{   r|   r}   r.   )r   r   r   r    r!   r/   r   rF   r   rH   rA   rD   rB   r5   rC   r6   s                   r7   r   r     s7    i.88!))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_GJJ7?4G	 (*  (( /##/99.// /	   ,, ''992D9 	   '' /??/%%/ 99-.	  8MM!+77)+77# N 
Hv _ !G+&//g73C3Cg~VfGgH  ++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6+,6LL!'+77#,99 M 
H0  $$44dkk' 0 #2!MMFjk 5    s&   FL	  DL	 ,AL	 		LLLc                 f  K   	 t        j                         }d|d<   t        t        | |||fi |}t	        j
                         }t        |j                  |      }|j                  d|       d{   }	t        j                  |	      r|	 d{   }
|
S |	}
|
S 7 )7 # t        $ r}|d}~ww xY ww)z2
    Async: Get info about a fine-tuning job.
    Taretrieve_fine_tuning_jobN)
r&   r'   r   retrieve_fine_tuning_jobr)   r*   r+   r,   r-   r.   r   s               r7   r   r   _  s     %%'.2*+ $
 
 &&(#CGGT2"2249JKK}-**H  %H L*  r   c                 4   	 t        d i |}|j                  xs |j                  dd      xs d}|:t        |t        j
                        r t        |      du r|j                  xs d}|}n,|&t        |t        j
                        st        |      }n|d}|j                  dd      du }|dk(  r|j                  xs+ t        j                  xs t        j                  d	      xs d
}	|j                  xs, t        j                  xs t        j                  dd      xs d}
|j                  xs9 t        j                  xs' t        j                   xs t        j                  d      }t"        j%                  |	||j&                  |
| ||j(                  |      }|S |dk(  r|j                  xs t        j                  xs t+        d      }	|j&                  xs t        j&                  xs t+        d      }|j                  xs< t        j                  xs* t        j,                  xs t+        d      xs t+        d      }|j                  di       }||j                  dd       nt+        d       t.        j%                  |	||| ||j(                  ||j                        }|S t        j0                  j3                  dj5                  |      d|t	        j6                  ddt	        j8                  dd                  # t:        $ r}|d}~ww xY w)!z+
    Get info about a fine-tuning job.
    r9   r:   NFr;   r   Tr   r<   r=   r>   r?   r   r   rI   rJ   rK   rL   r!   rM   rN   r   zcLiteLLM doesn't support {} for 'retrieve_fine_tuning_job'. Only 'openai' and 'azure' are supported.rX   rY   rZ   r   r\   r]   r`   rd   rg   )ri   rF   rj   rk   rl   rm   r   rn   ro   rh   rA   rp   rq   rr   rD   rB   rs   ru   r   rC   rG   r   rv   rw   ry   rz   r{   r|   r}   r.   r   s                  r7   r   r     s6   d.88!))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_GJJ:EBdJ	 (*(( /##/99.// /	   ,, ''992D9 	   '' /??/%%/ 99-.	  8PP!+77)#5+77# Q 	Hr ] !G+&//g73C3Cg~VfGgH  ++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/06OO!'#5+77#,99 P 	H.  $$44}  E  E' 0 #2!MM1KQuv 5    r   )r   NN)NNr   NN)0__doc__r&   r)   rq   	functoolsr   typingr   r   r   r   r   r	   rl   rp   litellm._loggingr
   &litellm.llms.azure.fine_tuning.handlerr   'litellm.llms.openai.fine_tuning.handlerr   *litellm.llms.vertex_ai.fine_tuning.handlerr   litellm.secret_managers.mainr   litellm.types.llms.openair   r   r   litellm.types.routerlitellm.utilsr   r   ru   rw   rx   r   dictListintr$   r(   r   r   r   r   r   r   rg       r7   <module>r      s     	  A A   + K G J 7 
 # 8 $7#8  ":"< #6#8    ') %)(,CK.2+/222 d^2 SM	2
 c]2 49%2 3-2 !!?@2 DcN+2 c3h(2 2 2j  ') %)(,CK.2+/~~~ d^~ SM	~
 c]~ 49%~ 3-~ !!?@~ DcN+~ c3h(~ =)Cm$;<<=~ ~F DL.2+/	"" !?@" DcN+" c3h(	" "N DL.2+/	tt !?@t DcN+t c3h(	t =)Cm$;<<=tp  CK.2+/$C=$C=$ !!?@$ DcN+	$
 c3h($P  CK.2+/yC=yC=y !!?@y DcN+	y
 c3h(y| DL.2+/	"" !?@" DcN+" c3h(	" "N DL.2+/	nn !?@n DcN+n c3h(	n =)Cm$;<<=nr   