a
    f	g                     @   sH  d dl Z d dlZd dlZd dlZd dlZd dlm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 G dd dZdd Zd	d
 Zdd ZG dd dZG dd dZG dd dZG dd dZG dd dZG dd dZG dd dZG dd dZG dd dZG dd  d ZG d!d" d"Zd#d$ Z d%d& Z!d'd( Z"dS ))    N)Optional)BufferReader)apply_necessary_remediationapply_validatorsget_validatorsread_any_formatwrite_out_filec                   @   s,   e Zd ZdZdZdZdZdZdZdZ	dZ
d	S )
bcolorsz[95mz[94mz[92mz[93mz[91mz[0mz[1mz[4mN)__name__
__module____qualname__HEADERZOKBLUEZOKGREENWARNINGFAILENDCZBOLDZ	UNDERLINE r   r   M/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/openai/cli.pyr	      s   r	   c                 C   s&   t | dd }|d urd|S dS d S )Norganizationz[organization={}]  )getattrformat)objr   r   r   r   organization_info    s    
r   c                 C   s&   t jt|  t j  t|  d S N)sysstderrwriter   flushprint)r   r   r   r   display(   s    
r   c              	   C   s@   | j d urd| j nd}tjdt| tjtj| | d S )Nz (HTTP status code: {})r   z{}{}Error:{} {}{}
)	Zhttp_statusr   r   r   r   r   r	   r   r   )eextrar   r   r   display_error.   s    r"   c                   @   s<   e Zd Zedd Zedd Zedd Zedd Zd	S )
Enginec                 C   s   t jj|jd}t| d S Nid)openair#   retriever&   r   clsargsenginer   r   r   get<   s    z
Engine.getc                 C   s    t jj|j|jd}t| d S )N)replicas)r'   r#   modifyr&   r.   r   r)   r   r   r   updateA   s    zEngine.updatec                 C   s  t dt |jr*|jdkr*|jr*tdi }|jd urB|j|d< tj|j	dj
f |j|j|j|j|j|j|j|jd|}|js|g}|D ]t}t|d }t|d D ]V\}}|dkrtjd| tjd	|d
  |dkrtjd tj  qqd S )Nz4Engine.generate is deprecated, use Completion.create   z1Can't stream multiple completions with openai CLImodelr%   )completionscontextlengthstreamtemperaturetop_plogprobsstopdata===== Completion {} =====
r   text
)warningswarnDeprecationWarningr3   r6   
ValueErrorr2   r'   r#   r&   generater4   r5   r7   r8   r9   r:   len	enumerater   stdoutr   r   joinr   )r*   r+   kwargsresppartr3   c_idxcr   r   r   rC   F   s>    

	zEngine.generatec                 C   s   t j }t| d S r   )r'   r#   listr   )r*   r+   Zenginesr   r   r   rM   i   s    
zEngine.listN)r
   r   r   classmethodr-   r0   rC   rM   r   r   r   r   r#   ;   s   


"r#   c                   @   s   e Zd Zedd ZdS )ChatCompletionc           	      C   s  |j d ur"|j dkr"|jr"tddd |jD }tjj|j|j||j |j	|j
|j|j|jd	}|jsl|g}|D ]}|d }tt|dd d	D ]\}}t|dkrtjd
| |jr|d }d|v rtj|d  n.tj|d d  t|dkrtjd tj  qqpd S )Nr1   z;Can't stream chat completions with n>1 with the current CLIc                 S   s   g | ]\}}||d qS ))rolecontentr   ).0rP   rQ   r   r   r   
<listcomp>w   s   z)ChatCompletion.create.<locals>.<listcomp>)	r2   r,   messagesn
max_tokensr7   r8   r:   r6   choicesc                 S   s   | d S Nindexr   sr   r   r   <lambda>       z'ChatCompletion.create.<locals>.<lambda>keyz===== Chat Completion {} =====
deltarQ   messager>   )rU   r6   rB   ra   r'   rO   creater2   r,   rV   r7   r8   r:   rE   sortedrD   r   rF   r   r   r   )	r*   r+   rT   rI   rJ   rW   rK   rL   r`   r   r   r   rb   p   sB    zChatCompletion.createNr
   r   r   rN   rb   r   r   r   r   rO   o   s   rO   c                   @   s   e Zd Zedd ZdS )
Completionc                 C   s   |j d ur"|j dkr"|jr"td|jr8|jr8td tjj	|j|j|j |j
|j|j|j|j|j|jdd}|jsz|g}|D ]z}|d }tt|dd d	D ]X\}}t|dkrtjd
| tj|d  t|dkrtjd tj  qq~d S )Nr1   z6Can't stream completions with n>1 with the current CLIzBIn most cases, you should not be specifying both engine and model.T)r,   r2   rU   rV   r9   promptr6   r7   r8   r:   ZechorW   c                 S   s   | d S rX   r   rZ   r   r   r   r\      r]   z#Completion.create.<locals>.<lambda>r^   r<   r=   r>   )rU   r6   rB   r,   r2   r?   r@   r'   re   rb   rV   r9   rf   r7   r8   r:   rE   rc   rD   r   rF   r   r   r   )r*   r+   rI   rJ   rW   rK   rL   r   r   r   rb      s<    zCompletion.createNrd   r   r   r   r   re      s   re   c                   @   s<   e Zd Zedd Zedd Zedd Zedd Zd	S )

Deploymentc                 C   s   t jj|jd}t| d S r$   )r'   rg   r(   r&   r   r*   r+   rI   r   r   r   r-      s    zDeployment.getc                 C   s   t j|j}t| d S r   )r'   rg   deleter&   r   r*   r+   r2   r   r   r   ri      s    zDeployment.deletec                 C   s   t j }t| d S r   )r'   rg   rM   r   r*   r+   modelsr   r   r   rM      s    
zDeployment.listc                 C   s$   t jj|jd|jid}t| d S )N
scale_type)r2   Zscale_settings)r'   rg   rb   r2   rm   r   rk   r   r   r   rb      s    zDeployment.createN)r
   r   r   rN   r-   ri   rM   rb   r   r   r   r   rg      s   


rg   c                   @   s0   e Zd Zedd Zedd Zedd ZdS )Modelc                 C   s   t jj|jd}t| d S r$   )r'   rn   r(   r&   r   rh   r   r   r   r-      s    z	Model.getc                 C   s   t j|j}t| d S r   )r'   rn   ri   r&   r   rj   r   r   r   ri      s    zModel.deletec                 C   s   t j }t| d S r   )r'   rn   rM   r   rk   r   r   r   rM      s    
z
Model.listN)r
   r   r   rN   r-   ri   rM   r   r   r   r   rn      s   

rn   c                   @   s<   e Zd Zedd Zedd Zedd Zedd Zd	S )
Filec                 C   s^   t |jd }t| dd}W d    n1 s20    Y  tjj||j|jd}t| d S )NrbUpload progressdescfilepurposeZuser_provided_filename)	openru   r   readr'   ro   rb   rv   r   r*   r+   file_readerbuffer_readerrI   r   r   r   rb      s    .zFile.createc                 C   s   t jj|jd}t| d S r$   )r'   ro   r(   r&   r   rh   r   r   r   r-      s    zFile.getc                 C   s   t j|j}t| d S r   )r'   ro   ri   r&   r   r*   r+   ru   r   r   r   ri      s    zFile.deletec                 C   s   t j }t| d S r   )r'   ro   rM   r   r|   r   r   r   rM      s    
z	File.listN)r
   r   r   rN   rb   r-   ri   rM   r   r   r   r   ro      s   



ro   c                   @   s0   e Zd Zedd Zedd Zedd ZdS )Imagec                 C   s(   t jj|j|j|j|jd}t| d S )N)rf   sizerU   response_format)r'   r}   rb   rf   r~   
num_imagesr   r   rh   r   r   r   rb     s    zImage.createc                 C   sb   t |jd }t| dd}W d    n1 s20    Y  tjj||j|j|j	d}t
| d S )Nrp   rq   rr   )imager~   rU   r   )rw   r   r   rx   r'   r}   create_variationr~   r   r   r   ry   r   r   r   r     s    .zImage.create_variationc                 C   s   t |jd }t| dd}W d    n1 s20    Y  d }|jd urt |jd }t| dd}W d    n1 s|0    Y  tjj|||j|j	|j
|jd}t| d S )Nrp   rq   rr   )r   maskrf   r~   rU   r   )rw   r   r   rx   r   r'   r}   create_editrf   r~   r   r   r   )r*   r+   rz   Zimage_readerZmask_readerrI   r   r   r   r     s    .
.zImage.create_editN)r
   r   r   rN   rb   r   r   r   r   r   r   r}     s   
	
r}   c                   @   s$   e Zd Zedd Zedd ZdS )Audioc              	   C   sn   t |jd }t| dd}W d    n1 s20    Y  tjj|j||j|j|j	|j
|jd}t| d S Nrp   rq   rr   )r2   ru   filenamer   languager7   rf   )rw   ru   r   rx   r'   r   Ztranscribe_rawr2   r   r   r7   rf   r   r*   r+   rrz   rI   r   r   r   
transcribe1  s    .zAudio.transcribec              	   C   sn   t |jd }t| dd}W d    n1 s20    Y  tjj|j||j|j|j	|j
|jd}t| d S r   )rw   ru   r   rx   r'   r   Ztranslate_rawr2   r   r   r7   rf   r   r   r   r   r   	translateC  s    .zAudio.translateN)r
   r   r   rN   r   r   r   r   r   r   r   0  s   
r   c                   @   s   e Zd Zedd ZeedddZeeee dddZ	ed"ee ee ee e
dddZed#ddZedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zd	S )$FineTunec                 C   s   t j }t| d S r   )r'   r   rM   r   rh   r   r   r   rM   V  s    
zFineTune.listru   c                 C   s   |  dS Nhttplower
startswithr*   ru   r   r   r   _is_url[  s    zFineTune._is_urlurlreturnc                 C   s"   t |}|jdkr|jS d S d S N   requestsr-   status_coderQ   r*   r   rI   r   r   r   _download_file_from_public_url_  s    

z'FineTune._download_file_from_public_urlNTru   rQ   user_provided_filecheck_if_file_existsc                 C   s  |d u |d u krt d|d u r^|d us,J t|d}| }W d    n1 sT0    Y  |rZt|}tjj|pz|j|dd}t|dkrZdd |D }tj	
djtj|d d	 d
|d v r|d d
 n
|d d d tj	
d| tj	
d tj  }	|	|v r4tj	
dj|	d |	S |	dkrDqZqtj	
dj|	d qt|dd}
tjj|
d|px|d}tj	
dj|p||d d |d S )N3Exactly one of `file` or `content` must be providedrp   	fine-tunenamebytesrv   r   c                 S   s   g | ]}|d  qS r%   r   rR   fr   r   r   rS   ~  r]   z/FineTune._maybe_upload_file.<locals>.<listcomp>zaFound potentially duplicated files with name '{name}', purpose 'fine-tune' and size {size} bytes
r   r   r~   r   r~   r>   a
Enter file ID to reuse an already uploaded file, or an empty string to upload this file anyway: $Reusing already uploaded file: {id}
r%   r   HFile id '{id}' is not among the IDs of the potentially duplicated files
rq   rr   rt    Uploaded file from {file}: {id}
r&   ru   r&   )rB   rw   rx   rD   r'   ro   find_matching_filesr   r   rF   r   r   ospathbasenamerG   stdinreadlinestripr   rb   )r*   ru   rQ   r   r   r   r   matching_filesfile_idsinpr{   rI   r   r   r   _maybe_upload_fileg  sd    	&




zFineTune._maybe_upload_filec                 C   st   zt j| |W S  t jjy(   Y n0 tj|rD| j||dS | 	|rp| 
|}|d urp| j|||dS |S N)ru   r   )rQ   r   r   r'   ro   r(   errorInvalidRequestErrorr   r   isfiler   r   r   r*   ru   r   rQ   r   r   r   _get_or_upload  s$    

zFineTune._get_or_uploadc                 C   s   d|  |j|ji}|jr.|  |j|j|d< dD ]}t||}|d ur2|||< q2tjjf i |}|jrvt	| d S t
jdj|d d | |d  d S )Ntraining_filevalidation_file)
r2   suffixn_epochsZ
batch_sizeZlearning_rate_multiplierZprompt_loss_weightcompute_classification_metricsZclassification_n_classesZclassification_positive_classZclassification_betaszCreated fine-tune: {job_id}
Streaming events until fine-tuning is complete...

(Ctrl-C will interrupt the stream, but not cancel the fine-tune)
r&   job_id)r   r   check_if_files_existr   r   r'   r   rb   Z	no_followr   r   rF   r   r   _stream_events)r*   r+   create_argsZhparamattrrI   r   r   r   rb     s,    

zFineTune.createc                 C   s   t jj|jd}t| d S r$   )r'   r   r(   r&   r   rh   r   r   r   r-     s    zFineTune.getc                 C   sz   t jj|jd}d|vs(t|d dkr>t jd|j dt jj|jdd d }t jj|d d}t	|
d d S Nr%   Zresult_filesr   z(No results file available for fine-tune r&   zutf-8)r'   r   r(   r&   rD   r   r   ro   downloadr   decoder*   r+   Z	fine_tuneZresult_filerI   r   r   r   results  s    zFineTune.resultsc                 C   s:   |j rtjjdj|jddtjj|jd}t| d S )NzmThe --stream parameter is deprecated, use fine_tunes.follow instead:

  openai api fine_tunes.follow -i {id}
r%   ra   )	r6   r'   r   OpenAIErrorr   r&   r   list_eventsr   rh   r   r   r   events  s    zFineTune.eventsc                 C   s   |  |j d S r   )r   r&   r*   r+   r   r   r   follow	  s    zFineTune.followc                    s    fdd}t  t j| tj }zH|D ]>}tjdtj	|d |d f  tjd tj
  q,W n( ty   tjdj d Y d S 0 tjj d	}|d
 }|dkrtjd tjdj|d d n|dkrtjd tjd d S )Nc                    s2   t j j}tjdj| d td d S )Nz
Stream interrupted. Job is still {status}.
To resume the stream, run:

  openai api fine_tunes.follow -i {job_id}

To cancel your job, run:

  openai api fine_tunes.cancel -i {job_id}

)statusr   r   )	r'   r   r(   r   r   rF   r   r   exit)sigframer   r   r   r   signal_handler  s    	z/FineTune._stream_events.<locals>.signal_handlerz[%s] %sZ
created_atra   r>   zs
Stream interrupted (client disconnected).
To resume the stream, run:

  openai api fine_tunes.follow -i {job_id}

r   r%   r   Z	succeededu%   
Job complete! Status: succeeded 🎉zZ
Try out your fine-tuned model:

openai api completions.create -m {model} -p <YOUR_PROMPT>Zfine_tuned_model)r2   failedza
Job failed. Please contact us through our help center at help.openai.com if you need assistance.)signalSIGINTr'   r   Zstream_eventsr   rF   r   datetimefromtimestampr   	Exceptionr   r(   )r*   r   r   r   eventrI   r   r   r   r   r     sF    zFineTune._stream_eventsc                 C   s   t jj|jd}t| d S r$   )r'   r   cancelr&   r   rh   r   r   r   r   C  s    zFineTune.cancelc                 C   s   t jj|jd}t| d S )N)Zsid)r'   r   ri   r&   r   rh   r   r   r   ri   H  s    zFineTune.deletec                 C   sL   t jd |j}|j}t|\}}td | t }t|||||t	d d S )NzAnalyzing...
)Zwrite_out_file_func)
r   rF   r   ru   quietr   r   r   r   r   )r*   r+   fnameZauto_acceptZdfZremediationZ
validatorsr   r   r   prepare_dataM  s    
zFineTune.prepare_data)NNNT)T)r
   r   r   rN   rM   strr   r   r   r   boolr   r   rb   r-   r   r   r   r   r   ri   r   r   r   r   r   r   U  sJ   
    @
*





5

r   c                   @   s   e Zd Zedd ZeedddZeeee dddZ	edee ee ee e
dddZedddZedd Zedd Zedd Zedd Zedd Zedd Zd	S )FineTuningJobc                 C   s0   d}t j D ]}d}t| q|s,td d S )NFTzNo fine-tuning jobs found.)r'   r   Zauto_paging_iterr   )r*   r+   Zhas_ft_jobsZfine_tune_jobr   r   r   rM   b  s    
zFineTuningJob.listr   c                 C   s   |  dS r   r   r   r   r   r   r   k  s    zFineTuningJob._is_urlr   c                 C   s"   t |}|jdkr|jS d S d S r   r   r   r   r   r   r   o  s    

z,FineTuningJob._download_file_from_public_urlNTr   c                 C   s  |d u |d u krt d|d u r^|d us,J t|d}| }W d    n1 sT0    Y  |rZt|}tjj|pz|j|dd}t|dkrZdd |D }tj	
djtj|d d	 d
|d v r|d d
 n
|d d d tj	
d| tj	
d tj  }	|	|v r4tj	
dj|	d |	S |	dkrDqZqtj	
dj|	d qt|dd}
tjj|
d|px|d}tj	
dj|p||d d tj	
d tj	  tj|d }|dkrtjdj|d |dtj	
dj|d d tj	  |d S )Nr   rp   r   r   r   c                 S   s   g | ]}|d  qS r%   r   r   r   r   r   rS     r]   z4FineTuningJob._maybe_upload_file.<locals>.<listcomp>zbFound potentially duplicated files with name '{name}', purpose 'fine-tune', and size {size} bytes
r   r   r~   r   r>   r   r   r%   r   r   rq   rr   rt   r   r&   r   z:Waiting for file to finish processing before proceeding..
	processedz-File {id} failed to process, status={status}.)r&   r   zAFile {id} finished processing and is ready for use in fine-tuning)rB   rw   rx   rD   r'   ro   r   r   r   rF   r   r   r   r   r   rG   r   r   r   r   rb   r   Zwait_for_processingr   r   )r*   ru   rQ   r   r   r   r   r   r   r   r{   rI   r   r   r   r   r   w  s    	&







z FineTuningJob._maybe_upload_filec                 C   st   zt j| |W S  t jjy(   Y n0 tj|rD| j||dS | 	|rp| 
|}|d urp| j|||dS |S r   r   r   r   r   r   r     s$    

zFineTuningJob._get_or_uploadc                 C   s   d|  |j|ji}|jr.|  |j|j|d< dD ]}t||}|d ur2|||< q2t|drjd|ji|d< tjjf i |}t	| d S )Nr   r   )r2   r   r   Zhyperparameters)
r   r   r   r   r   r   r'   r   rb   r   )r*   r+   r   paramr   rI   r   r   r   rb     s$    


zFineTuningJob.createc                 C   s   t jj|jd}t| d S r$   )r'   r   r(   r&   r   rh   r   r   r   r-     s    zFineTuningJob.getc                 C   sv   t jj|jd}d|vs(t|d dkr>t jd|j dt jj|jdd d }t jj|d}t	|
d d S r   )r'   r   r(   r&   rD   r   r   ro   r   r   r   r   r   r   r   r     s    zFineTuningJob.resultsc                 C   s   d\}}}|r~t jj|j|d}|d D ].}t| |d7 }|jd ur(||jkr( d S q(|dd}|d r
|d d d }q
d S )	N)r   TN)r&   afterr;   r1   has_moreFr&   )r'   r   r   r&   r   limitr-   )r*   r+   seenr   r   rI   r   r   r   r   r     s    
zFineTuningJob.eventsc                 C   s   t jjddd S )NzAEvent streaming is not yet supported for `fine_tuning.job` eventsr   )r'   r   r   r   r   r   r   r     s    zFineTuningJob.followc                 C   s   t jj|jd}t| d S r$   )r'   r   r   r&   r   rh   r   r   r   r      s    zFineTuningJob.cancel)NNNT)T)r
   r   r   rN   rM   r   r   r   r   r   r   r   r   rb   r-   r   r   r   r   r   r   r   r   r   a  s>   
    R





r   c                   @   s   e Zd Zedd ZdS )WandbLoggerc                 C   s6   dd l }|jjj|j|j|j|j|jd}t	| d S )Nr   )r&   n_fine_tunesprojectentityforce)
Zopenai.wandb_loggerZwandb_loggerr   syncr&   r   r   r   r   r   )r*   r+   r'   rI   r   r   r   r   '  s    zWandbLogger.syncN)r
   r   r   rN   r   r   r   r   r   r   &  s   r   c                    sh    j ddd} fdd} j|d |d}|jdd	d
dd |jdddddd |jtjd d S )NZToolszConvenience client side toolstitlehelpc                    s       d S r   
print_helpr+   parserr   r   r   :  s    ztools_register.<locals>.helpfunczfine_tunes.prepare_data-f--fileTzJSONL, JSON, CSV, TSV, TXT or XLSX file containing prompt-completion examples to be analyzed.This should be the local file path.requiredr   z-qz--quietF
store_truezWAuto accepts all suggestions, without asking for user input. To be used within scripts.)r  actionr   )add_subparsersset_defaults
add_parseradd_argumentr   r   r   
subparsersr   subr   r   r   tools_register5  s(    
r  c              	      s
   j dd} fdd} j|d |d}|jtjd |d}|jdd	d
d |jtjd |d}|jdd	d
d |jddtd |jtjd |d}|jdd	d
d |jdddd |jdddd |jdddtd |jdddt	d |jdd d!t	d |jd"d#d$td |jd%d&td |jd'd(d |jd)d*d+d,d- |jtj
d |d.}|j  |d/}|d0}|jd1d2d3d4d5d6d
d7 | }|jd8d9d:d |jd)d*d;d |jd"d<d=td |jd>d?d@td |jdddt	d |jdAd d!t	d |jd'dBd |jddCdd |jtjd |dD}|jd8d9dEd |jd)d*dFd |jdddd |jddGdHd |jd>d?dItd |jdddt	d |jdAd d!t	d |jd"d<dJtd |jd%dKtd |jd'd(d |jtjd |dL}|jtjd |dM}|jdd	d
dNd- |jtjd |dO}|jdd	d
dNd- |jtjd |dP}|jd)d*d
dQd- |jdRdSd
dTd- |jtjd |dU}|jtjd |dV}|jdd	d
dQd- |jtjd |dW}|jdd	d
dQd- |jtjd |dX}|jdYdZd
d[d- |jdd\d]d
d^ |jtjd |d_}|jdd	d
d`d- |jtjd |da}|jdd	d
d`d- |jtjd |db}|jtjd |dc}|jtjd |dd}|jdded
dfd- |jdgdhdid |jdjdkdldmdn |jd)d*dod |jdpdqd |jdrddsdt |jdutdvdw |jdxtdydw |jdzt	d{dw |jd|t	d}dw |jd~dddt |jd d |jdtddw |jddd |jdt	ddd |jtjd |d}|jdd	d
dd- |jtjd |d}|jdd	d
dd- |jtjd |d}|jdd	d
dd- |jdRddddt |jtjd |d}|jdd	d
dd- |jtjd |d}|jdd	d
dd- |jtjd |d}|jdd	d
dd- |jtjd |d}|jddGtd
d |jd"dtdd |jdRdtddd |jdtdd |jtjd |d}|jddGtd
d |jd"dtdd |jddtd
dd |jdRdtddd |jdtdd |jd>dtd+dd |jtjd |d}|jd"dtdd |jddtd
dd |jdRdtddd |jdtdd |jtjd |d}|jd)d*tdd |jdYdZtd
d |jdtd |jdtd |jddt	d |jdGtd |jtj d |d}|jd)d*tdd |jdYdZtd
d |jdtd |jdtd |jddt	d |jdGtd |jtj!d |d}|jt"jd |d}|jdded
dd- |jdgdhdd |jdjdkdldmdn |jd)d*dod |jdpdd |jdutdvdw |jt"jd |d}|jdd	d
dd- |jt"jd |d}|jdd	d
dd- |jt"jd |d}|jdd	d
dd- |jdtd+dd |jt"jd |d}|jdd	d
dd- |jt"jd |d}|jdd	d
dd- |jt"jd d S )NzAll API subcommandsr   c                    s       d S r   r   r   r   r   r   r   U  s    zapi_register.<locals>.helpr   zengines.listzengines.get-i--idT)r  zengines.updatez-rz
--replicas)typezengines.generatez--streamzStream tokens as they're ready.r  )r   r  z-cz	--contextz$An optional context to generate fromz-lz--lengthzHow many tokens to generate)r   r  z-tz--temperaturezWhat sampling temperature to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.

Mutually exclusive with `top_p`.z-pz--top_pa  An alternative to sampling with temperature, called nucleus sampling, where the considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10%% probability mass are considered.

            Mutually exclusive with `temperature`.-nz--completionsz4How many parallel completions to run on this contextz
--logprobsa7  Include the log probabilites on the `logprobs` most likely tokens. So for example, if `logprobs` is 10, the API will return a list of the 10 most likely tokens. If `logprobs` is supplied, the API will always return the logprob of the generated token, so there may be up to `logprobs+1` elements in the response.z--stopz3A stop sequence at which to stop generating tokens.z-mz--modelFz\A model (most commonly a model ID) to generate from. Defaults to the engine's default model.r  zchat_completions.createzrequired argumentszoptional argumentsz-gz	--messageappend   )ZROLEZCONTENTzbA message in `{role} {content}` format. Use this argument multiple times to add multiple messages.)r  nargsmetavarr   r  z-ez--enginezThe engine to use. See https://learn.microsoft.com/en-us/azure/cognitive-services/openai/chatgpt-quickstart?pivots=programming-language-python for more about what engines are available.zThe model to use.z--nz6How many completions to generate for the conversation.z-Mz--max-tokensz)The maximum number of tokens to generate.z-PzCA stop sequence at which to stop generating tokens for the message.z!Stream messages as they're ready.zcompletions.createzjThe engine to use. See https://platform.openai.com/docs/engines for more about what engines are available.zIThe model to use. At most one of `engine` or `model` should be specified.z--promptz#An optional prompt to complete fromz(The maximum number of tokens to generatez5How many sub-completions to generate for each prompt.a  Include the log probabilites on the `logprobs` most likely tokens, as well the chosen tokens. So for example, if `logprobs` is 10, the API will return a list of the 10 most likely tokens. If `logprobs` is 0, only the chosen tokens will have logprobs returned.zdeployments.listzdeployments.getzThe deployment IDzdeployments.deletezdeployments.createzThe model IDz-sz--scale_typez-The scale type. Either 'manual' or 'standard'zmodels.listz
models.getzmodels.deletezfiles.creater  r  zFile to uploadz	--purposezcWhy are you uploading this file? (see https://platform.openai.com/docs/api-reference/ for purposes))r   r  z	files.getzThe files IDzfiles.deletez
files.listzfine_tunes.listzfine_tunes.createz--training_filezJSONL file containing prompt-completion examples for training. This can be the ID of a file uploaded through the OpenAI API (e.g. file-abcde12345), a local file path, or a URL that starts with "http".z-vz--validation_filezJSONL file containing prompt-completion examples for validation. This can be the ID of a file uploaded through the OpenAI API (e.g. file-abcde12345), a local file path, or a URL that starts with "http".z--no_check_if_files_existr   store_falsezIf this argument is set and training_file or validation_file are file paths, immediately upload them. If this argument is not set, check if they may be duplicates of already uploaded files before uploading, based on file name and file size.)destr  r   z#The model to start fine-tuning fromz--suffixa  If set, this argument can be used to customize the generated fine-tuned model name.All punctuation and whitespace in `suffix` will be replaced with a single dash, and the string will be lower cased. The max length of `suffix` is 40 chars. The generated name will match the form `{base_model}:ft-{org-title}:{suffix}-{timestamp}`. For example, `openai api fine_tunes.create -t test.jsonl -m ada --suffix "custom model name" could generate a model with the name ada:ft-your-org:custom-model-name-2022-02-15-04-21-04z--no_followzpIf set, returns immediately after creating the job. Otherwise, streams events and waits for the job to complete.r  r   z
--n_epochszlThe number of epochs to train the model for. An epoch refers to one full cycle through the training dataset.)r  r   z--batch_sizezThe batch size to use for training. The batch size is the number of training examples used to train a single forward and backward pass.z--learning_rate_multiplierzThe learning rate multiplier to use for training. The fine-tuning learning rate is determined by the original learning rate used for pretraining multiplied by this value.z--prompt_loss_weightzThe weight to use for the prompt loss. The optimum value here depends depends on your use case. This determines how much the model prioritizes learning from prompt tokens vs learning from completion tokens.z --compute_classification_metricszIf set, we calculate classification-specific metrics such as accuracy and F-1 score using the validation set at the end of every epoch.)r   z--classification_n_classesziThe number of classes in a classification task. This parameter is required for multiclass classification.z--classification_positive_classzThe positive class in binary classification. This parameter is needed to generate precision, recall and F-1 metrics when doing binary classification.z--classification_betas+zIf this is provided, we calculate F-beta scores at the specified beta values. The F-beta score is a generalization of F-1 score. This is only used for binary classification.)r  r  r   zfine_tunes.getzThe id of the fine-tune jobzfine_tunes.resultszfine_tunes.eventszr[DEPRECATED] If set, events will be streamed until the job is done. Otherwise, displays the event history to date.zfine_tunes.followzfine_tunes.cancelzfine_tunes.deletezimage.create)r  r  z--num-imagesr1   )r  defaultz--sizeZ	1024x1024zSize of the output imager  r  r   z--response-formatr   zimage.create_editz-Iz--imagez@Image to modify. Should be a local path and a PNG encoded image.)r  r  r   z--maskzPath to a mask image. It should be the same size as the image you're editing and a RGBA PNG image. The Alpha channel acts as the mask.zimage.create_variationzaudio.transcribez	whisper-1z
--languagezaudio.translatezfine_tuning.job.listzfine_tuning.job.createzJSONL file containing either chat-completion or prompt-completion examples for training. This can be the ID of a file uploaded through the OpenAI API (e.g. file-abcde12345), a local file path, or a URL that starts with "http".zJSONL file containing either chat-completion or prompt-completion examples for validation. This can be the ID of a file uploaded through the OpenAI API (e.g. file-abcde12345), a local file path, or a URL that starts with "http".aM  If set, this argument can be used to customize the generated fine-tuned model name.All punctuation and whitespace in `suffix` will be replaced with a single dash, and the string will be lower cased. The max length of `suffix` is 18 chars. The generated name will match the form `ft:{base_model}:{org-title}:{suffix}:{rstring}` where `rstring` is a random string sortable as a timestamp. For example, `openai api fine_tuning.job.create -t test.jsonl -m gpt-3.5-turbo-0613 --suffix "first finetune!" could generate a model with the name ft:gpt-3.5-turbo-0613:your-org:first-finetune:7p4PqAoYzfine_tuning.job.getzfine_tuning.job.resultszfine_tuning.job.eventsz--limitziThe number of events to return, starting from most recent. If not specified, all events will be returned.zfine_tuning.job.followzfine_tuning.job.cancel)#r  r	  r
  r#   rM   r  r-   intr0   floatrC   _action_groupspopadd_argument_groupadd_mutually_exclusive_grouprO   rb   re   rg   ri   rn   ro   r   r   r   r   r   r   r}   r   r   r   r   r   r   )r   r  r   r  reqoptgroupr   r   r   api_registerQ  s:   











































r(  c                    s    j ddd} fdd} j|d |d}|jdd	d
d |jddtd dd |jdddd |jddd |jdddd |jdddd |jdd |jdd |jtjd d S )NZwandbzeLogging with Weights & Biases, see https://docs.wandb.ai/guides/integrations/openai for documentationr   c                    s       d S r   r   r   r   r   r   r   e  s    zwandb_register.<locals>.helpr   r   r  r  z&The id of the fine-tune job (optional)r  r  z--n_fine_tuneszjNumber of most recent fine-tunes to log when an id is not provided. By default, every fine-tune is synced.r  z	--projectzOpenAI-Fine-TunezeName of the Weights & Biases project where you're sending runs. By default, it is "OpenAI-Fine-Tune".)r  r   z--entityzWeights & Biases username or team name where you're sending runs. By default, your default entity is used, which is usually your username.z--forcer  zFForces logging and overwrite existing wandb run of the same fine-tune.r  z--legacyz1Log results from legacy OpenAI /v1/fine-tunes apiF)r   )legacy)r  r	  r
  r  r  r   r   r  r   r   r   wandb_register`  sH    
r*  )#r   r   r   r   r?   typingr   r   r'   Zopenai.upload_progressr   Zopenai.validatorsr   r   r   r   r   r	   r   r   r"   r#   rO   re   rg   rn   ro   r}   r   r   r   r   r  r(  r*  r   r   r   r   <module>   sD   	4,&*%   F    