
    -`i                     <   d dl Z d dl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 erd dlmZ ne j        Zd Zde j        d	eeef         fd
Zd	efdZd	efdZdedz  deded	dfdZded	efdZ G d de          Z G d de          Zd	ee         fdZdS )    N)TYPE_CHECKING)OpenAI)ChatCompletionMessageParam)CLISubcommand)FlexibleArgumentParserc                      d } t          j         t           j        |            t          j         t           j        |            d S )Nc                 .    t          j        d           d S )Nr   )sysexit)sigframes     o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/entrypoints/cli/openai.pysignal_handlerz1_register_signal_handlers.<locals>.signal_handler   s        )signalSIGINTSIGTSTP)r   s    r   _register_signal_handlersr      sA       M&-000
M&..11111r   argsreturnc                 <   t                       | j        }| j        pt          j                            dd          }t          ||          }| j        r| j        }n+|j        	                                }|j
        d         j        }t          d|            ||fS )NOPENAI_API_KEYEMPTY)api_keybase_urlr   zUsing model: )r   urlr   osenvirongetr   
model_namemodelslistdataidprint)r   r   r   openai_clientr    available_modelss         r   _interactive_clir(      s    xHlGbjnn-=wGGG7X>>>M 1_

(/4466%*1-0
	
&*
&
&'''}$$r   c                     d}| D ]<}|j         d         j        }|j        r!||j        z  }t          |j        dd           =t                       |S N r   T)endflush)choicesdeltacontentr%   )streamoutputchunkr/   s       r   _print_chat_streamr4   /   sa    F 5 5a &= 	5em#F%-Rt4444	GGGMr   c                     d}| D ]-}|j         d         j        }|||z  }t          |dd           .t                       |S r*   )r.   textr%   )r1   r2   r3   r6   s       r   _print_completion_streamr7   :   sX    F , ,}Q$dNF$Bd++++	GGGMr   system_promptr    clientc                 b   g }| |                     d| d           t          d           	 	 t          d          }n# t          $ r Y d S w xY w|                     d|d           |j        j                            ||d          }t          |          }|                     d|d           )	Nsystemroler0   *Please enter a message for the chat model:T> usermodelmessagesr1   	assistant)appendr%   inputEOFErrorchatcompletionscreater4   )r8   r    r9   conversationinput_messager1   r2   s          r   rH   rH   E   s    57L X-HHIII	
6777F	!$KKMM 	 	 	EE	VFFGGG(//|D 0 
 
 $F++[VDDEEEFs   > 
AAparserc                     |                      dt          dd           |                      dt          d d           |                      dt          d d           | S )	Nz--urlzhttp://localhost:8000/v1z7url of the running OpenAI-Compatible RESTful API servertypedefaulthelpz--model-namez]The model name used in prompt completion, default to the first model in list models API call.z	--api-keyaK  API key for OpenAI services. If provided, this api key will overwrite the api key obtained through environment variables. It is important to note that this option only applies to the OpenAI-compatible API endpoints and NOT other endpoints that may be present in the server. See the security guide in the vLLM docs for more details.)add_argumentstrrM   s    r   _add_query_optionsrV   Y   s    
*F	     7           Mr   c                   z    e Zd ZdZdZedej        ddfd            Zede	de	fd            Z
d	ej        de	fd
ZdS )ChatCommandz'The `chat` subcommand for the vLLM CLI.rH   r   r   Nc                 r   t          |           \  }}| j        }g }||                    d|d           | j        rh|                    d| j        d           |j        j                            ||d          }t          |          }|                    d|d           d S t          d           	 	 t          d          }n# t          $ r Y d S w xY w|                    d|d           |j        j                            ||d          }t          |          }|                    d|d           )	Nr;   r<   r@   TrA   rD   r>   r?   )r(   r8   rE   quickrH   rI   rJ   r4   r%   rF   rG   )r   r    r9   r8   rK   r1   r2   rL   s           r   cmdzChatCommand.cmd~   s}   -d33
F*9;$m L LMMM: 	DJ G GHHH[,33 < 4  F (//F H HIIIF:;;;	J %d   M J JKKK[,33 < 4  F (//F H HIII	Js   6C 
CCrM   c                     t          |            |                     dt          dd           |                     ddt          dd	           | S )
z'Add CLI arguments for the chat command.z--system-promptNz`The system prompt to be added to the chat template, used for models that support system prompts.rO   -q--quickMESSAGEzBSend a single prompt as MESSAGE and print the response, then exit.rP   metavarrR   )rV   rS   rT   rU   s    r   add_cli_argszChatCommand.add_cli_args   sr     	6"""? 	 	
 	
 	
 	V 	 	
 	
 	
 r   
subparsersc                 h    |                     dddd          }t                              |          S )NrH   z5Generate chat completions via the running API server.zvllm chat [options]rR   descriptionusage)
add_parserrX   rb   selfrc   rM   s      r   subparser_initzChatCommand.subparser_init   s@     &&HO'	 ' 
 
 ''///r   __name__
__module____qualname____doc__namestaticmethodargparse	Namespacer[   r   rb   _SubParsersActionrk    r   r   rX   rX   y   s        11DJ($ J J J J \J@ 3 8N    \*	0"4	0		0 	0 	0 	0 	0 	0r   rX   c                   z    e Zd ZdZdZedej        ddfd            Zede	de	fd            Z
d	ej        de	fd
ZdS )CompleteCommandz+The `complete` subcommand for the vLLM CLI.completer   r   Nc                 `   t          |           \  }}|dd}| j        r
| j        |d<   | j        r* |j        j        dd| j        i|}t          |           d S t          d           	 	 t          d          }n# t          $ r Y d S w xY w |j        j        dd|i|}t          |           F)NT)rB   r1   
max_tokenspromptz Please enter prompt to complete:r?   rv   )	r(   r{   rZ   rI   rJ   r7   r%   rF   rG   )r   r    r9   kwargsr1   input_prompts         r   r[   zCompleteCommand.cmd   s    -d33
F  
 
 ? 	3#'?F< : 	.V'.KKdjKFKKF$V,,,F0111	-$T{{   .V'.MMlMfMMF$V,,,	-s   +A; ;
B	B	rM   c                     t          |            |                     dt          d           |                     ddt          dd           | S )	z+Add CLI arguments for the complete command.z--max-tokensz9Maximum number of tokens to generate per output sequence.)rP   rR   r]   r^   PROMPTz@Send a single prompt and print the completion output, then exit.r`   )rV   rS   intrT   rU   s    r   rb   zCompleteCommand.add_cli_args   sm     	6"""L 	 	
 	
 	

 	S 	 	
 	
 	
 r   rc   c                 h    |                     dddd          }t                              |          S )Nry   zOGenerate text completions based on the given prompt via the running API server.zvllm complete [options]re   )rh   rx   rb   ri   s      r   rk   zCompleteCommand.subparser_init   sF     &&.. , ' 
 
 ++F333r   rl   rv   r   r   rx   rx      s        55D-($ - - - - \-0 3 8N    \"4"44	4 4 4 4 4 4r   rx   c                  :    t                      t                      gS )N)rX   rx   rv   r   r   cmd_initr     s    MM?,,--r   )rs   r   r   r
   typingr   openair   openai.types.chatr   vllm.entrypoints.cli.typesr   vllm.utils.argparse_utilsr   ArgumentParserr   rt   tuplerT   r(   r4   r7   rH   rV   rX   rx   r"   r   rv   r   r   <module>r      s    				  



                   8 8 8 8 8 8 4 4 4 4 4 4 5@@@@@@@%42 2 2%8- %%V2D % % % %$#        Fd
 F FV F F F F F(5 :P    @E0 E0 E0 E0 E0- E0 E0 E0P?4 ?4 ?4 ?4 ?4m ?4 ?4 ?4D.$}% . . . . . .r   