U
    ·ÜŸj  ã                   @  sÚ   d dl mZ d dlZd dlmZmZ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 d	dlmZ ddlmZ ddlmZ d	dlmZ er¨d dlmZ dddœdd„ZG dd„ deƒZG dd„ dƒZdS )é    )ÚannotationsN)ÚTYPE_CHECKINGÚOptionalÚcast)ÚArgumentParser)Úpartial)Ú
Completioné   )Ú
get_clienté   )Ú	NOT_GIVENÚ
NotGivenOr)Úis_given)ÚCLIError)Ú	BaseModel)ÚStream)Ú_SubParsersActionz!_SubParsersAction[ArgumentParser]ÚNone)Ú	subparserÚreturnc                 C  s  |   d¡}|jd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td� |jd d!dd� |jd"d#td� |jd$d%td� |jd&d'd
� |jd(d)d
� |jd*d+d
� |jtjtd,� d S )-Nzcompletions.createz-mz--modelzThe model to useT)ÚhelpÚrequiredz-pz--promptz#An optional prompt to complete from)r   z--streamzStream tokens as they're ready.Ú
store_true)r   Úactionz-Mz--max-tokensz(The maximum number of tokens to generate)r   Útypez-tz--temperaturezÜWhat 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`.z-nz--nz5How many sub-completions to generate for each prompt.z
--logprobsa  Include the log probabilities 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	--best_ofz”Generates `best_of` completions server-side and returns the 'best' (the one with the highest log probability per token). Results cannot be streamed.z--echoz2Echo back the prompt in addition to the completionz--frequency_penaltyz�Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.z--presence_penaltyz�Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.z--suffixz:The suffix that comes after a completion of inserted text.z--stopz3A stop sequence at which to stop generating tokens.z--userzbA unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.)ÚfuncZ
args_model)Ú
add_parserÚadd_argumentÚintÚfloatÚset_defaultsÚCLICompletionsÚcreateÚCLICompletionCreateArgs)r   Úsub© r%   ú?/tmp/pip-unpacked-wheel-2qj3ofc_/openai/cli/_api/completions.pyÚregister   sx    
üúúüýýýýýþr'   c                   @  s¾   e Zd ZU ded< dZded< dZded< eZd	ed
< eZded< eZ	ded< eZ
ded< eZded< eZd	ed< eZded< eZd	ed< eZd	ed< eZded< eZded< eZded< dS )r#   ÚstrÚmodelFÚboolÚstreamNzOptional[str]ÚpromptzNotGivenOr[int]ÚnzNotGivenOr[str]ÚstopÚuserzNotGivenOr[bool]ÚechoÚsuffixÚbest_ofzNotGivenOr[float]Útop_pÚlogprobsÚ
max_tokensÚtemperatureÚpresence_penaltyÚfrequency_penalty)Ú__name__Ú
__module__Ú__qualname__Ú__annotations__r+   r,   r   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r%   r%   r%   r&   r#   ]   s   
r#   c                   @  sH   e Zd Zedddœdd„ƒZedddœdd	„ƒZed
ddœdd„ƒZdS )r!   r#   r   )Úargsr   c                 C  s–   t | jƒr"| jdkr"| jr"tdƒ‚ttƒ jj| j| j| j	| j
| j| j| j| j| j| j| j| j| j| jd�}| jrŠt ttt |dd�ƒ¡S t |ƒ ¡S )Né   z6Can't stream completions with n>1 with the current CLI)r-   r0   r.   r/   r)   r3   r,   r1   r2   r4   r5   r6   r7   r8   T)r+   )r   r-   r+   r   r   r
   Zcompletionsr"   r0   r.   r/   r)   r3   r,   r1   r2   r4   r5   r6   r7   r8   r!   Ú_stream_creater   r   r   Ú_create)r=   Zmake_requestr%   r%   r&   r"   q   s0    ñþzCLICompletions.creater   )Ú
completionr   c                 C  sj   t | jƒdk}| jD ]P}|r0tj d |j¡¡ tj |j¡ |sN|j d¡sZtj d¡ tj 	¡  qd S )Nr>   z===== Completion {} =====
Ú
)
ÚlenÚchoicesÚsysÚstdoutÚwriteÚformatÚindexÚtextÚendswithÚflush)rA   Úshould_print_headerÚchoicer%   r%   r&   r@   �   s    
zCLICompletions._createzStream[Completion])r+   r   c                 C  s€   | D ]j}t |jƒdk}t|jdd„ d�D ]D}|rDtj d |j¡¡ tj |j¡ |rbtj d¡ tj 	¡  q(qtj d¡ d S )Nr>   c                 S  s   | j S )N)rI   )Úcr%   r%   r&   Ú<lambda>¢   ó    z/CLICompletions._stream_create.<locals>.<lambda>)Úkeyz===== Chat Completion {} =====
rB   )
rC   rD   ÚsortedrE   rF   rG   rH   rI   rJ   rL   )r+   rA   rM   rN   r%   r%   r&   r?   ž   s    zCLICompletions._stream_createN)r9   r:   r;   Ústaticmethodr"   r@   r?   r%   r%   r%   r&   r!   p   s   r!   )Ú
__future__r   rE   Útypingr   r   r   Úargparser   Ú	functoolsr   Zopenai.types.completionr   Ú_utilsr
   Ú_typesr   r   r   Ú_errorsr   Z_modelsr   Z
_streamingr   r   r'   r#   r!   r%   r%   r%   r&   Ú<module>   s    H