
    .`iE                        d dl Z d dlZd dlmZ d dlmZmZmZ d dlZd dl	m
Z
mZ d dlmZmZmZmZmZ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mZmZmZ d d
l m!Z!  ee"          Z# ej$        ej%                  Z& G d de          Z' G d de          Z( G d de          Z) G d de          Z* G d de          Z+ G d de          Z,dS )    N)replace)	AnnotatedAnyLiteral)Fieldmodel_validator)AnyResponseFormat!LegacyStructuralTagResponseFormatLogitsProcessorsOpenAIBaseModelStreamOptionsStructuralTagResponseFormat	UsageInfoget_logits_processors)VLLMValidationError)init_logger)Logprob)BeamSearchParamsRequestOutputKindSamplingParamsStructuredOutputsParamsrandom_uuidc            	          e Zd ZU dZedz  ed<   dZee         eee                  z  ez  ee         z  dz  ed<   dZ	e
dz  ed<   dZedz  ed<   dZeeef         dz  ed<   dZedz  ed	<   d
Zedz  ed<   dZeed<   dZedz  ed<    edej        ej                  Zedz  ed<   g Zeee         z  dz  ed<   dZe
dz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZ e
ed<   dZ!edz  ed<   dZ"edz  ed<   dZ#edz  ed<   dZ$eed<   g Z%ee         dz  ed<   dZ&e
ed<   dZ'e
ed <   d!Z(eed"<   d#Z)e
ed$<   d#Z*e
ed%<   dZ+e,e ed&ej                  f         dz  ed'<   dZ-ee         dz  ed(<   dZ.edz  ed)<   dZ/e0ee0         z  dz  ed*<    ed#d+,          Z1e
ed-<    edd.,          Z2e3dz  ed/<    edd0,          Z4e5dz  ed1<    ed!d2,          Z6eed3<    ee7d45          Z8eed6<    edd7,          Z9e:dz  ed8<    edd9,          Z;e
dz  ed:<    edd;,          Z<e
dz  ed<<    edd=,          Z=edz  ed><    edd?,          Z>eee?f         dz  ed@<    eddA,          Z@eeeez  ez  f         dz  edB<   dddd!ddCZAeedD<   	 dQdedEedz  dFeBfdGZC	 dQdedHedz  dEedz  dFeDfdIZE eFdJK          eGdL                         ZH eFdJK          eGdM                         ZI eFdJK          eGdN                         ZJ eFdJK          eGdO                         ZK eFdJK          eGdP                         ZLdS )RCompletionRequestNmodelpromptFechog        frequency_penalty
logit_biaslogprobs   
max_tokens   npresence_penalty)geleseedstopstreamstream_optionssuffixtemperaturetop_puseruse_beam_searchtop_kmin_prepetition_penalty      ?length_penaltystop_token_idsinclude_stop_str_in_output
ignore_eosr   
min_tokensTskip_special_tokensspaces_between_special_tokenstruncate_prompt_tokensallowed_token_idsprompt_logprobsprompt_embedszMIf true (the default), special tokens (e.g. BOS) will be added to the prompt.defaultdescriptionadd_special_tokenszSimilar to chat completion, this parameter specifies the format of output. Only {'type': 'json_object'}, {'type': 'json_schema'}, {'type': 'structural_tag'}, or {'type': 'text' } is supported.response_formatz(Additional kwargs for structured outputsstructured_outputszThe priority of the request (lower means earlier handling; default: 0). Any priority other than 0 will raise an error if the served model does not use priority scheduling.priorityzThe request_id related to this request. If the caller does not set it, a random_uuid will be generated. This id is used through out the inference process and return in response.)default_factoryrD   
request_ida  A list of either qualified names of logits processors, or constructor objects, to apply when sampling. A constructor is a JSON object with a required 'qualname' field specifying the qualified name of the processor class/factory, and optional 'args' and 'kwargs' fields containing positional and keyword arguments. For example: {'qualname': 'my_module.MyLogitsProcessor', 'args': [1, 2], 'kwargs': {'param': 'value'}}.logits_processorszIf specified with 'logprobs', tokens are represented  as strings of the form 'token_id:{token_id}' so that tokens that are not JSON-encodable can be identified.return_tokens_as_token_idsa(  If specified, the result will include token IDs alongside the generated text. In streaming mode, prompt_token_ids is included only in the first chunk, and token_ids contains the delta tokens for each chunk. This is useful for debugging or when you need to map generated text back to input tokens.return_token_idsa/  If specified, the prefix cache will be salted with the provided string to prevent an attacker to guess prompts in multi-user environments. The salt should be random, protected from access by 3rd parties, and long enough to be unpredictable (e.g., 43 characters base64-encoded, corresponding to 256 bit).
cache_saltz5KVTransfer parameters used for disaggregated serving.kv_transfer_paramszWAdditional request parameters with string or numeric values, used by custom extensions.
vllm_xargs)r4   r.   r/   r2   r3   _DEFAULT_SAMPLING_PARAMSdefault_sampling_paramsreturnc                     |i }| j         | j         nd}| j        x}|                    dd          }t          ||| j        || j        | j                  S )Nr$   r.   r5   )
beam_widthr#   r9   r.   r6   r8   )r%   r.   getr   r9   r6   r8   )selfr#   rR   r%   r.   s        /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/entrypoints/openai/completion/protocol.pyto_beam_search_paramsz'CompletionRequest.to_beam_search_params   sv    
 #*&(#f(DFFa++K4155mSIIK!#.'+'F
 
 
 	
    logits_processor_patternc           	         |i }| j         x}!|                    d| j        d                   }| j        x}!|                    d| j        d                   }| j        x}!|                    d| j        d                   }| j        x}!|                    d| j        d                   }| j        x}!|                    d| j        d                   }| j        }	|	| j        r| j	        }	| j        o
| j
        dk    }
| j        }|t          t          t          f                     }|j        dk    rd|d<   n{|j        d	k    r|j        }|J |j        |d
<   nZ|j        dk    rO|}|t#          |t$          t&          f          sJ |                    d          }t+          j        |          |d<   t/          |          dk    r*| j        t3          d%i |nt5          | j        fi || _        | j        r| j        ni }| j        r
| j        |d<   t;          j        d%i d| j        d| j         d| j!        d|d|d|d|d|d| j"        d| j#        d| j$        d| j	        d| j%        d|
s|ndd| j&        d|	d| j'        d| j(        d| j)        dtU          | j+        |          d| j,        d| j-        rt\          j/        nt\          j0        d | j        d!| j1        d"| j2        d#|pd d$dS )&Nr4   r.   r/   r2   r3   r   json_objectTjson_schemajsonstructural_tag)by_aliasrO   r%   r&   r   r)   r*   r7   r!   r9   r#   r$   r:   r@   r;   r<   r8   rK   r>   output_kindrG   r    r?   
extra_args
skip_clone )3r4   rV   rQ   r.   r/   r2   r3   r@   r   r!   r#   rF   dictstrr   typer^   
isinstancer
   r   
model_dumpr_   dumpslenrG   r   r   rP   rO   r   from_optionalr%   r&   r   r)   r*   r7   r9   r:   r;   r<   r8   r   rK   r>   r+   r   DELTA
FINAL_ONLYr    r?   )rW   r#   r[   rR   r4   r.   r/   r2   r3   r@   echo_without_generationrF   structured_outputs_kwargsr^   r`   	s_tag_objrc   s                    rX   to_sampling_paramsz$CompletionRequest.to_sampling_params   sg    #*&(# #'"99B!8!<!<$-.BC" "  ++K4155t<]K K ZE(+//6w? E ZE(+//6w? E ZE(+//6w? E ."ty""mO"&)"D10D.&(,S#X(8(8% #}44;?)-88 %66-9"...4?4K)&11 %)999!0%1j"937 711  +55t5DD	>Bj>S>S)*:; ,--11 .6 ,HH.GHHH !8VV<UVV ' 9=%OT__R
" 	G/3/FJ+,+  
  
  
ff 
!22 
 #44 
  21	 

 $ 
 % 
 % 
 % 
  
  
  .. 
 ]] 
  
 *AGzza 
  
  ,O! 
" !% 8 8# 
$ +/*L*L% 
& (,'F'F' 
( 4&(@  ) 
. $(#>#>/ 
2 {.)//"-5 
6  $667 
8 9 
: #44; 
< ")T= 
> t? 
  	
rZ   before)modec                     |                     dd           |S |d         t          fddD                       }|dk    rt          dd          |S )NrG   c              3   F   K   | ]}                     |          d uV  d S N)rV   ).0krq   s     rX   	<genexpr>zCCompletionRequest.check_structured_outputs_count.<locals>.<genexpr>F  sJ       
 
 &))!,,D8
 
 
 
 
 
rZ   )r_   regexchoicer$   z^You can only use one kind of constraints for structured outputs ('json', 'regex' or 'choice').	parameter)rV   sumr   )clsdatacountrq   s      @rX   check_structured_outputs_countz0CompletionRequest.check_structured_outputs_count?  s     88($//7K$()=$>! 
 
 
 
0
 
 
 
 
 199%9.   
 rZ   c                 2   |                     d          x}P|                     d          r|dk    s|dk    rt          dd          |dk     r|dk    rt          dd|          |                     d	          x}|dk     rt          d
d	|          |S )Nr@   r+   r   r=   z7`prompt_logprobs` are not available when `stream=True`.r~   z1`prompt_logprobs` must be a positive value or -1.)r   valuer!   z$`logprobs` must be a positive value.rV   r   )r   r   r@   r!   s       rX   check_logprobsz CompletionRequest.check_logprobsR  s      $xx(9:::OGxx!! ':':oQS>S>S)M/   
 """'<'<)G/)   
 ,,,H9hll%6$    rZ   c                 |    |                     d          r&|                     d          st          dd          |S )Nr,   r+   z6Stream options can only be defined when `stream=True`.r~   r   r   r   s     rX   validate_stream_optionsz)CompletionRequest.validate_stream_optionsk  sP     88$%% 	dhhx.@.@ 	%H*   
 rZ   c                    |                     d          }|                     d          }|d u pt          |t                    o|dk    }|d u p't          |t                    ot	          |          dk    }|r|rt          d          |S )Nr   rA    r   z>Either prompt or prompt_embeds must be provided and non-empty.)rV   ri   rg   listrl   
ValueError)r   r   r   rA   prompt_is_emptyembeds_is_emptys         rX   !validate_prompt_and_prompt_embedsz3CompletionRequest.validate_prompt_and_prompt_embedsv  s     (##11 D.VZ-D-D-USU'4/ 
}d++GM0B0Ba0G 	  	 	P   rZ   c                     |                     d          2t          |d         t                    r|d         st          d          |S )NrN   z>Parameter 'cache_salt' must be a non-empty string if provided.)rV   ri   rg   r   r   s     rX   check_cache_salt_supportz*CompletionRequest.check_cache_salt_support  sX     88L!!-4-s33 .;?;M . P   rZ   rx   )M__name__
__module____qualname__r   rg   __annotations__r   r   intr   boolr   floatr    rf   r!   r#   r%   r&   r   
_LONG_INFOminmaxr)   r*   r+   r,   r   r-   r.   r/   r0   r1   r2   r3   r4   r6   r7   r8   r9   r:   r;   r<   r>   r   r?   r@   rA   bytesrE   rF   r	   rG   r   rH   r   rJ   rK   r   rL   rM   rN   rO   r   rP   rQ   r   rY   r   rs   r   classmethodr   r   r   r   r   re   rZ   rX   r   r   ,   s'         E3:CGFDIT#Y'#-S	9D@GGGD$+&)ut|)))*.JS%Z 4'...HcDjJd
AsJJJ%(edl(((uTjnHHHD#*HHH#%D#S	/D
 %%%FD4K+/NMD(///FC$J $K$$$E54<D#* "OT!!!E3:E54<'++++NE')NDI$)))',,,,JJ $$$$*.!4... Ic55B:>+J+J+J&JKdR    +/tCy4'..."&OS4Z&&& 15M54;&-444$u         16O1 1 1O&-    :?>: : :/$6    ED  Hc    e#H  J    27#2 2 2'$.    /4e=/ / /t    %*E?	% 	% 	%dTk 	 	 	 #U
 
 
Jd
 
 
 
 16K1 1 1S#X-   
 7<e97 7 7JS#)e++,t3    "& &d    04
 

 "&
 
	
 
 
 
2 04	j
 j
j
 #&*j
 "&	j

 
j
 j
 j
 j
X _(###  [ $#" _(###  [ $#. _(###  [ $# _(###  [ $#  _(###  [ $#  rZ   r   c                       e Zd ZU  ee          Zee         ed<    ee          Zee	dz           ed<    ee          Z
ee         ed<    ee          Zeeee	f         dz           ed<   dS )CompletionLogProbsrI   text_offsetNtoken_logprobstokenstop_logprobs)r   r   r   r   r   r   r   r   r   r   r   rg   r   rf   re   rZ   rX   r   r     s         "U4888Kc888).t)D)D)DND&DDDd333FDI33327%2M2M2ML$tCJ'$./MMMMMrZ   r   c                       e Zd ZU eed<   eed<   dZedz  ed<   dZedz  ed<    e	dd          Z
eez  dz  ed<   dZee         dz  ed	<   dZeeeef         dz           dz  ed
<   dZee         dz  ed<   dS )CompletionResponseChoiceindextextNr!   finish_reasonThe stop string or token id that caused the completion to stop, None if the completion finished for some other reason including encountering the EOS tokenrB   stop_reason	token_idsr@   prompt_token_ids)r   r   r   r   r   rg   r!   r   r   r   r   r   r   r@   rf   r   r   re   rZ   rX   r   r     s         JJJ
III*.H 4'... $M3:$$$$)E3% % %KsT!    #'ItCy4&&&>BOT$sG|,t34t;BBB)-d3i$&-----rZ   r   c                   
   e Zd ZU  ed           Zeed<   dZed         ed<    ed           Z	e
ed<   eed<   ee         ed	<   d
Zed         d
z  ed<   d
Zed
z  ed<   eed<    ed
d          Zeeef         d
z  ed<   d
S )CompletionResponsec                  $    dt                       S Nzcmpl-r   re   rZ   rX   <lambda>zCompletionResponse.<lambda>      ,CKMM,C,C rZ   r   idtext_completionobjectc                  B    t          t          j                              S rx   r   timere   rZ   rX   r   zCompletionResponse.<lambda>      TY[[1A1A rZ   createdr   choicesN)autorC   flexscalerH   service_tiersystem_fingerprintusagezKVTransfer parameters.rB   rO   )r   r   r   r   r   rg   r   r   r   r   r   r   r   r   r   r   rO   rf   r   re   rZ   rX   r   r     s        e$C$CDDDBDDD):FG%&:::5)A)ABBBGSBBBJJJ*++++SWL'HIDPWWW%)d
))) 16":1 1 1S#X-     rZ   r   c                       e Zd ZU eed<   eed<   dZedz  ed<   dZedz  ed<    e	dd          Z
eez  dz  ed<   dZee         dz  ed	<   dZee         dz  ed
<   dS )CompletionResponseStreamChoicer   r   Nr!   r   r   rB   r   r   r   )r   r   r   r   r   rg   r!   r   r   r   r   r   r   r   re   rZ   rX   r   r     s         JJJ
III*.H 4'... $M3:$$$$)E3% % %KsT!    *.d3i$&---"&ItCy4&&&&&rZ   r   c                       e Zd ZU  ed           Zeed<   dZeed<    ed           Ze	ed<   eed<   e
e         ed	<    ed
          Zed
z  ed<   d
S )CompletionStreamResponsec                  $    dt                       S r   r   re   rZ   rX   r   z!CompletionStreamResponse.<lambda>  r   rZ   r   r   r   r   c                  B    t          t          j                              S rx   r   re   rZ   rX   r   z!CompletionStreamResponse.<lambda>  r   rZ   r   r   r   N)rC   r   )r   r   r   r   r   rg   r   r   r   r   r   r   r   r   re   rZ   rX   r   r     s         e$C$CDDDBDDD#FC###5)A)ABBBGSBBBJJJ01111#eD111E9t11111rZ   r   )-r_   r   dataclassesr   typingr   r   r   torchpydanticr   r   'vllm.entrypoints.openai.engine.protocolr	   r
   r   r   r   r   r   r   vllm.exceptionsr   vllm.loggerr   vllm.logprobsr   vllm.sampling_paramsr   r   r   r   
vllm.utilsr   r   loggeriinfolongr   r   r   r   r   r   r   re   rZ   rX   <module>r      s           * * * * * * * * * *        
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 0 / / / / / # # # # # # ! ! ! ! ! !            # " " " " "	X		 U[$$
e e e e e e e ePN N N N N N N N. . . . . . . .$        ' ' ' ' '_ ' ' '&2 2 2 2 2 2 2 2 2 2rZ   