
    .`i)d                        d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	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mZmZmZ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/          Z0 e	j1        e	j2                  Z3 G d de          Z4 G d de          Z5 G d de5          Z6 G d de          Z7 G d de          Z8 G d de          Z9 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ClassVarLiteral)ChatCompletionAudio)
Annotation)Fieldmodel_validator)ChatCompletionMessageParam)AnyResponseFormatDeltaMessageFunctionCallFunctionDefinition!LegacyStructuralTagResponseFormatLogitsProcessorsOpenAIBaseModelStreamOptionsStructuralTagResponseFormatToolCall	UsageInfoget_logits_processors)VLLMValidationError)init_logger)Logprob)BeamSearchParamsRequestOutputKindSamplingParamsStructuredOutputsParamsrandom_uuidc                       e Zd ZU 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
dz  ed<   dZedz  ed<    ee          Zee         ed	<   dZedz  ed
<   dZedz  ed<   	  ed          d             ZdS )ChatMessageroleNcontentrefusalannotationsaudiofunction_calldefault_factory
tool_calls	reasoningreasoning_contentaftermodec                     | j         | _        | S )z?Copy reasoning to reasoning_content for backward compatibility.)r-   r.   )selfs    /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/entrypoints/openai/chat_completion/protocol.py#handle_deprecated_reasoning_contentz/ChatMessage.handle_deprecated_reasoning_contentC   s     "&    )__name__
__module____qualname__str__annotations__r%   r&   r'   OpenAIAnnotationr(   OpenAIChatCompletionAudior)   r   r
   listr,   r   r-   r.   r   r5    r6   r4   r#   r#   5   s         
IIIGS4ZGS4Z+/K!D(///.2E$t+222)-M<$&---!&t!<!<!<JX<<< !IsTz   $(sTz(((._'"""  #"  r6   r#   c                   H    e Zd ZU eed<   dZeed<   dZee	         dz  ed<   dS )ChatCompletionLogProbtokeng    logprobNbytes)
r7   r8   r9   r:   r;   rC   floatrD   r>   intr?   r6   r4   rA   rA   J   sD         JJJGU"E49t"""""r6   rA   c                   j    e Zd ZU dZeee         dz           ed<    ee	          Z
e	e         ed<   dS )ChatCompletionLogProbsContentNfield_namesr*   top_logprobs)r7   r8   r9   rI   r   setr:   r;   r
   r>   rJ   rA   r?   r6   r4   rH   rH   P   sT          .2K#c(T/*11105d0K0K0KL$,-KKKKKr6   rH   c                   0    e Zd ZU dZee         dz  ed<   dS )ChatCompletionLogProbsNr%   )r7   r8   r9   r%   r>   rH   r;   r?   r6   r4   rM   rM   W   s-         :>GT/047>>>>>r6   rM   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<   dZ
ee	z  dz  ed<   dZee         dz  ed<   dS )	ChatCompletionResponseChoiceindexmessageNlogprobsstopfinish_reasonstop_reason	token_ids)r7   r8   r9   rF   r;   r#   rR   rM   rT   r:   rU   rV   r>   r?   r6   r4   rO   rO   [   s         JJJ.2H$t+222 &M3:&&&$(KsT!((( #'ItCy4&&&&&r6   rO   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<   d
Zeee
ef         d
z           d
z  ed<   d
Zee
         d
z  ed<    ed
d          Zeeef         d
z  ed<   d
S )ChatCompletionResponsec                  $    dt                       S Nz	chatcmpl-r    r?   r6   r4   <lambda>zChatCompletionResponse.<lambda>i       ,G,G,G r6   r*   idzchat.completionobjectc                  B    t          t          j                              S NrF   timer?   r6   r4   r[   zChatCompletionResponse.<lambda>k       TY[[1A1A r6   createdmodelchoicesN)autodefaultflexscalepriorityservice_tiersystem_fingerprintusageprompt_logprobsprompt_token_idszKVTransfer parameters.rh   descriptionkv_transfer_params)r7   r8   r9   r
   r]   r:   r;   r^   r   rd   rF   r>   rO   rl   rm   r   ro   dictr   rp   rs   r   r?   r6   r4   rX   rX   h   sE        e$G$GHHHBHHH):FG%&:::5)A)ABBBGSBBBJJJ.////SWL'HIDPWWW%)d
))) ?COT$sG|,t34t;BBB)-d3i$&---05":1 1 1S#X-     r6   rX   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<   dZ
ee	z  dz  ed<   dZee         dz  ed<   dS )"ChatCompletionResponseStreamChoicerP   deltaNrR   rT   rU   rV   )r7   r8   r9   rF   r;   r   rR   rM   rT   r:   rU   rV   r>   r?   r6   r4   rv   rv   z   s         JJJ.2H$t+222 $M3:$$$$(KsT!((("&ItCy4&&&&&r6   rv   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	<    ed
          Zed
z  ed<   d
Zee
         d
z  ed<   d
S )ChatCompletionStreamResponsec                  $    dt                       S rZ   r    r?   r6   r4   r[   z%ChatCompletionStreamResponse.<lambda>   r\   r6   r*   r]   zchat.completion.chunkr^   c                  B    t          t          j                              S r`   ra   r?   r6   r4   r[   z%ChatCompletionStreamResponse.<lambda>   rc   r6   rd   re   rf   N)rh   rn   rp   )r7   r8   r9   r
   r]   r:   r;   r^   r   rd   rF   r>   rv   rn   r   rp   r?   r6   r4   ry   ry      s         e$G$GHHHBHHH/FFG+,FFF5)A)ABBBGSBBBJJJ45555#eD111E9t111)-d3i$&-----r6   ry   c                   4    e Zd ZU dZed         ed<   eed<   dS )ChatCompletionToolsParamfunctiontypeN)r7   r8   r9   r   r   r;   r   r?   r6   r4   r}   r}      s3          *D'*
***      r6   r}   c                       e Zd ZU eed<   dS )ChatCompletionNamedFunctionnameN)r7   r8   r9   r:   r;   r?   r6   r4   r   r      s         
IIIIIr6   r   c                   4    e Zd ZU eed<   dZed         ed<   dS )"ChatCompletionNamedToolChoiceParamr~   r   N)r7   r8   r9   r   r;   r   r   r?   r6   r4   r   r      s3         )))) *D'*
*****r6   r   c                      e Zd ZU ee         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
<    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<    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e"         dz  ed<   dZ#e$d         e$d         z  e$d         z  e%z  dz  ed<   dZ&e$d         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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Z0eed,<   dZ1eed-<   d	Z2eed.<   d!Z3eed/<   d!Z4eed0<   dZ5e6e ed1ej                  f         dz  ed2<   dZ7edz  ed3<   dZ8ee         dz  ed4<    ee5          Z9ee         ed6<    edd78          Z:eed9<    ed!d:8          Z;eed;<    edd<8          Z<eed=<    edd>8          Z=eed?<    edd@8          Z>eeeef                  dz  edA<    eddB8          Z?edz  edC<    eddD8          Z@eeeAf         dz  edE<    eddF8          ZBeeeAf         dz  edG<    eddH8          ZCeDdz  edI<    ed	dJ8          ZEeedK<    eeFdLM          ZGeedN<    eddO8          ZHeIdz  edP<    eddQ8          ZJedz  edR<    eddS8          ZKedz  edT<    eddU8          ZLedz  edV<    eddW8          ZMeeeAf         dz  edX<    eddY8          ZNeeeez  e	z  eeez  e	z           z  f         dz  edZ<   d)d)d)d	dd[ZOeed\<   ded]ed^ePfd_ZQded`edz  d]ed^eRfdaZS eTdbc          eUdd                         ZV eTdbc          eUde                         ZW eTdbc          eUdf                         ZX eTdbc          eUdg                         ZY eTdbc          eUdh                         ZZ eTdbc          eUdi                         Z[dS )jChatCompletionRequestmessagesNre   g        frequency_penalty
logit_biasFrR   r   rJ   zDmax_tokens is deprecated in favor of the max_completion_tokens field)rh   
deprecated
max_tokensmax_completion_tokens   npresence_penaltyresponse_format)geleseedrS   streamstream_optionstemperaturetop_ptoolsnonerg   requiredtool_choice)lowmediumhighreasoning_effortTinclude_reasoningparallel_tool_callsuseruse_beam_searchtop_kmin_prepetition_penaltyg      ?length_penaltystop_token_idsinclude_stop_str_in_output
ignore_eos
min_tokensskip_special_tokensspaces_between_special_tokenstruncate_prompt_tokensro   allowed_token_idsr*   	bad_wordszaIf true, the new message will be prepended with the last message if they belong to the same role.rq   echozIf true, the generation prompt will be added to the chat template. This is a parameter used by chat template in tokenizer config of the model.add_generation_prompta:  If this is set, the chat will be formatted so that the final message in the chat is open-ended, without any EOS tokens. The model will continue this message rather than starting a new one. This allows you to "prefill" part of the model's response for it. Cannot be used at the same time as `add_generation_prompt`.continue_final_messagezIf true, special tokens (e.g. BOS) will be added to the prompt on top of what is added by the chat template. For most models, the chat template takes care of adding the special tokens so this should be set to false (as is the default).add_special_tokensa#  A list of dicts representing documents that will be accessible to the model if it is performing RAG (retrieval-augmented generation). If the template does not support RAG, this argument will have no effect. We recommend that each document should be a dict containing "title" and "text" keys.	documentszA Jinja template to use for this conversion. As of transformers v4.44, default chat template is no longer allowed, so you must provide a chat template if the tokenizer does not define one.chat_templatezbAdditional keyword args to pass to the template renderer. Will be accessible by the chat template.chat_template_kwargsz.Additional kwargs to pass to the HF processor.mm_processor_kwargsz(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.rk   zThe 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.)r+   rr   
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.rs   zaAdditional request parameters with (list of) string or numeric values, used by custom extensions.
vllm_xargs)r   r   r   r   r   _DEFAULT_SAMPLING_PARAMSdefault_sampling_paramsreturnc                     | j         | j         nd}| j        x}!|                    d| j        d                   }t	          ||| j        || j        | j                  S )Nr   r   )
beam_widthr   r   r   r   r   )r   r   getr   r   r   r   r   )r3   r   r   r   r   s        r4   to_beam_search_paramsz+ChatCompletionRequest.to_beam_search_paramsp  sy     f(DFFa++K4155t<]K K  !#.'+'F
 
 
 	
r6   logits_processor_patternc           	         | 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
        }
|
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        t1          d%i |nt3          | j        fi || _        | j        r| j        ni }| j        r
| j        |d<   t9          j        d%i d| j        d| j        d| j         d|d|d|d|d|d| j!        d| j"        d| j#        d| j$        r| j	        nd d|	d| j%        d|d| j&        d| j'        d| j(        dtS          | j*        |          d| j+        d| j,        d| j-        rt\          j/        nt\          j0        d| j        d | j1        d!| j2        d"| j3        d#|pd d$dS )&Nr   r   r   r   r   json_objectTjson_schemajsonstructural_tag)by_aliasr   rs   r   r   r   r   rS   r   rR   ro   r   r   r   r   r   r   r   r   output_kindr   r   r   r   
extra_args
skip_cloner?   )4r   r   r   r   r   r   r   ro   r   rJ   r   rt   r:   r   r   r   
isinstancer   r   
model_dumpr   dumpslenr   r   r   r   rs   r   from_optionalr   r   r   r   rS   r   rR   r   r   r   r   r   r   r   r   r   r   DELTA
FINAL_ONLYr   r   r   )r3   r   r   r   r   r   r   r   r   ro   r   structured_outputs_kwargsr   r   	s_tag_objr   s                   r4   to_sampling_paramsz(ChatCompletionRequest.to_sampling_params  sY    #'"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""/O.&(,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	!

 $!
 %!
 %!
 %!
 !
 !
  ..!
 +/-AT&&T!
 ,O!
 !
 "z!
  !!
" !% 8 8#!
$ +/*L*L%!
& 4&(@  '!
, (,'F'F-!
. $(#>#>/!
2 {.)//"-5!
6  $667!
8 9!
: nn;!
< #44=!
> ")T?!
@ tA!
 !	
r6   beforer0   c                 |    |                     d          r&|                     d          st          dd          |S )Nr   r   z6Stream options can only be defined when `stream=True`.	parameterr   r   clsdatas     r4   validate_stream_optionsz-ChatCompletionRequest.validate_stream_options  sP     88$%% 	dhhx.@.@ 	%H*   
 r6   c                    |                     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}P|dk     r|dk    rt          d
d	|          |dk    s|dk    r&|                     d          st          dd	          |S )Nro   r   r   r   z7`prompt_logprobs` are not available when `stream=True`.r   z1`prompt_logprobs` must be a positive value or -1.)r   valuerJ   z.`top_logprobs` must be a positive value or -1.rR   z:when using `top_logprobs`, `logprobs` must be set to true.r   )r   r   ro   rJ   s       r4   check_logprobsz$ChatCompletionRequest.check_logprobs  s3     $xx(9:::OGxx!! ':':oQS>S>S)M/   
 """'<'<)G/)   
 !HH^444LAaLB$6$6)D,&    ""lQ&6&6@T@T&6)P,   
 r6   c                 0   t          |t                    r||                    dd           |S |d         t          fddD                       }|dk    rt          d          |dk    r'|                    dd          dvrt          d	          |S )
Nr   c              3   F   K   | ]}                     |          d uV  d S r`   )r   ).0kr   s     r4   	<genexpr>zGChatCompletionRequest.check_structured_outputs_count.<locals>.<genexpr>  sJ       
 
 &))!,,D8
 
 
 
 
 
r6   )r   regexchoicer   z^You can only use one kind of constraints for structured outputs ('json', 'regex' or 'choice').r   r   )r   rg   r   zNYou can only either use constraints for structured outputs or tools, not both.)r   
ValueErrorr   sum)r   r   countr   s      @r4   check_structured_outputs_countz4ChatCompletionRequest.check_structured_outputs_count  s     dJ'' 	J88($//7K$()=$>! 
 
 
 
0
 
 
 
 

 1999  
 199-88 A
 
 

 &   r6   c                    d|vr|                     d          rd|d<   d|v r|d         dk    r|S d|v r|d         d|vs|d         t          d          |d         dvr4t          |d         t                    st          d|d          d          |d         d	k    r>t          |d         t                    r#t          |d                   d
k    r
d|d<   |d= |S d}t          |d         t                    rd}|d                              d          }t          |t                    st          d| d|           d|vrt          d|           |d         }t          |t                    rt          |          d
k    rt          d| d|           |d         D ]}|d         d         |k    rd} n|st          d          |S )Nr   r   rg   r   z.When using `tool_choice`, `tools` must be set.)rg   r   z!Invalid value for `tool_choice`: z?! Only named tools, "none", "auto" or "required" are supported.r   r   zJCorrect usage: `{"type": "function", "function": {"name": "my_function"}}`Fr~   zInvalid value for `function`: `z` in `tool_choice`! r   z6Expected field `name` in `function` in `tool_choice`! zInvalid `name` in `function`: `TzOThe tool specified in `tool_choice` does not match any of the specified `tools`)r   r   r   rt   r>   r   r:   )r   r   correct_usage_message
valid_toolr~   function_nametools          r4   check_tool_usagez&ChatCompletionRequest.check_tool_usage5  s   
 $$'):):$"(D D  T-%8F%B%BK D  T-%8%Dd""d7m&; !QRRR M"*>>>z]#TH H> !%]8K % % %   ]#z11tG}d33 2W&&!++&,]#M
9 " $}-t44 "
.22:>>!(D11 $B( B B*?B B   ))$B*?B B   !) 0!-55 ]9K9Kq9P9P$F- F F.CF F   !M  DJ'/=@@%)
 A " $4   r6   c                 x    |                     d          r$|                     d          rt          d          |S )Nr   r   zMCannot set both `continue_final_message` and `add_generation_prompt` to True.)r   r   r   s     r4   check_generation_promptz-ChatCompletionRequest.check_generation_prompt  sK     88,-- 	$((;R2S2S 	3   r6   c                     |                     d          2t          |d         t                    r|d         st          d          |S )Nr   z>Parameter 'cache_salt' must be a non-empty string if provided.)r   r   r:   r   r   s     r4   check_cache_salt_supportz.ChatCompletionRequest.check_cache_salt_support  sX     88L!!-4-s33 .;?;M . P   r6   )\r7   r8   r9   r>   r   r;   re   r:   r   rE   r   rt   rR   boolrJ   rF   r
   r   r   r   r   r   r   
_LONG_INFOminmaxr   rS   r   r   r   r   r   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ro   r   r   r   r   r   r   r   r   r   r   r   r   r   rk   r!   r   r   r   r   r   r   rs   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r?   r6   r4   r   r      sx	         -....E3:&)ut|)))*.JS%Z 4'...!HdTk!!! L#*   "U*  Jd
   
 )-3:,,,AsTz%(edl(((04O&-444uTjnHHHD#*HHH#%D#S	/D
 %%%FD4K+/NMD(/// $K$$$E54<37E4()D0777 	 
&/	
*
	 -	- 		   AEg56=DDD"t"""'++++ D#* "OT!!!E3:E54<'++++NE')NDI$)))',,,,JJ $$$$*.!4... Ic55B:>+J+J+J&JKdR    #'OS4Z&&&*.tCy4'... 5666ItCy666 /  D$    #(%# # #4    $)5J	$ 	$ 	$D 	 	 	  %u	  	  	  	 	 	 .3U'	. 	. 	.ItDcN#d* 	 	 	 !&#! ! !M3:    38%73 3 3$sCx.4/    27E2 2 2c3h$.    :?>: : :/$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-   
 QVPU9Q Q QJS#)e+d39u3D.EEEFM    "& &d   

8<
	
 
 
 
$f
f
 #&*f
 "&	f

 
f
 f
 f
 f
P _(###  [ $# _(###  [ $#< _(###  [ $#< _(###I I [ $#IV _(###  [ $# _(###  [ $#  r6   r   )@r   rb   dataclassesr   typingr   r   r   r   torch'openai.types.chat.chat_completion_audior   r=   )openai.types.chat.chat_completion_messager	   r<   pydanticr
   r   vllm.entrypoints.chat_utilsr   'vllm.entrypoints.openai.engine.protocolr   r   r   r   r   r   r   r   r   r   r   r   vllm.exceptionsr   vllm.loggerr   vllm.logprobsr   vllm.sampling_paramsr   r   r   r   
vllm.utilsr!   r7   loggeriinfolongr   r#   rA   rH   rM   rO   rX   rv   ry   r}   r   r   r   r?   r6   r4   <module>r     s           4 4 4 4 4 4 4 4 4 4 4 4       U T T T T T       
 C B B B B B                            0 / / / / / # # # # # # ! ! ! ! ! !            # " " " " "	X		 U[$$
    /   *# # # # #O # # #L L L L L$9 L L L? ? ? ? ?_ ? ? ?
' 
' 
' 
' 
'? 
' 
' 
'    _   $' ' ' ' ' ' ' '. . . . .? . . .! ! ! ! ! ! ! !
    /   + + + + + + + +
x x x x xO x x x x xr6   