o
    -iM                      @   s  d dl mZ d dlm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 d dlmZ d d	lmZmZ d d
lmZmZmZ d dlmZ d dl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%m&Z& d dl'm(Z( e#e)Z*G dd deZ+G dd de+Z,dS )    )
HTTPStatus)castN)Request)EngineClient)ChatTemplateContentFormatOption)RequestLogger)ChatCompletionRequest)ErrorResponse	UsageInfo)ClassificationServeContextOpenAIServingServeContext)OpenAIServingModels)ClassificationChatRequestClassificationCompletionRequestClassificationDataClassificationRequestClassificationResponse)RenderConfig)init_logger)ClassificationOutputPoolingRequestOutput)PoolingParamsc                   @   sh   e Zd ZU edB ed< eed< eed< dededB fddZ	dede
eB fd	d
ZdedefddZdS )ClassificationMixinNchat_templatechat_template_content_formattrust_request_chat_templatectxreturnc                    s  t t|}z|j}t|trV|}|j}t| dd}| j|j|j	|d}|r)|W S | j
t t|| j||jp:t| ddt tt| dd|j|j|jdI dH \}}||_W dS t|tr|}	|	j}
|
d	v rm| jd
tjdW S t|
trz|
szg |_W dS |  }t ttt B |
}|j|| |	dI dH |_W dS | jdtjdW S  tttjfy } zt !d | t|W  Y d}~S d}~ww )z|
        Process classification inputs: tokenize text, resolve adapters,
        and prepare model-specific inputs.
        r   F)request_chat_templatechat_template_kwargsr   r   Nr   auto)r   r   add_generation_promptcontinue_final_messageadd_special_tokens)N z"Input or messages must be provided)status_code)prompt_or_promptsconfigz#Invalid classification request typez$Error in preprocessing prompt inputs)"r   r   request
isinstancer   messagesgetattr_validate_chat_templater   r    _preprocess_chatr   rendererr   r"   r#   r$   engine_promptsr   inputcreate_error_responser   BAD_REQUESTlist_get_completion_rendererstrrender_prompt_build_render_config
ValueError	TypeErrorjinja2TemplateErrorlogger	exception)selfr   request_objchat_requestr+   r   ret_r0   completion_request
input_datar/   prompt_inpute rH   f/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/vllm/entrypoints/pooling/classify/serving.py_preprocess/   s|   






zClassificationMixin._preprocessc                 C   s   t t|}g }d}t tt |j}t|D ]7\}}t|j}|j	}t
t|}	t| jjdi |	}
t||
|t|d}|| |j}|t|7 }qt||d}t|j|j|j||dS )zu
        Convert model outputs to a formatted classification response
        with probabilities and labels.
        r   id2label)indexlabelprobsnum_classes)prompt_tokenstotal_tokens)idcreatedmodeldatausage)r   r   r4   r   final_res_batch	enumerater   	from_baseoutputsrN   intnpargmaxr,   model_config	hf_configgetr   lenappendprompt_token_idsr
   r   
request_idcreated_time
model_name)r?   r   itemsnum_prompt_tokensfinal_res_batch_checkedidx	final_resclassify_resrN   predicted_indexrM   itemrc   rV   rH   rH   rI   _build_response{   s>   

z#ClassificationMixin._build_responser)   c                 C   s   t | j|j|jdS )N)
max_lengthtruncate_prompt_tokensr$   )r   max_model_lenrq   r$   )r?   r)   rH   rH   rI   r8      s
   z(ClassificationMixin._build_render_config)__name__
__module____qualname__r6   __annotations__r   boolr   r	   rJ   r   ro   r   r   r8   rH   rH   rH   rI   r   *   s   
 
L
/r   c                       s   e Zd ZdZddddddedededB d	edB d
ede	de	ddf fddZ
dededeeB f fddZdee deeB f fddZ  ZS )ServingClassificationclassifyNr!   F)r   r   r   log_error_stackengine_clientmodelsrequest_loggerr   r   r   rz   r   c                   s*   t  j||||d || _|| _|| _d S )N)r{   r|   r}   rz   )super__init__r   r   r   )r?   r{   r|   r}   r   r   r   rz   	__class__rH   rI   r      s   
zServingClassification.__init__r)   raw_requestc                    sD   | j  }| j d| | }t||||d}t |I d H S )N-)r)   r   rf   rd   )r|   rf   request_id_prefix_base_request_idr   r~   handle)r?   r)   r   rf   rd   r   r   rH   rI   create_classify   s   
z%ServingClassification.create_classifyr   c              
      sb   t  |}t|tr|S z
|d| j W |S  ty0 } z| t|W  Y d }~S d }~ww )Nry   )	r~   _create_pooling_paramsr*   r	   verifyr^   r9   r2   r6   )r?   r   pooling_paramsrG   r   rH   rI   r      s   
z,ServingClassification._create_pooling_params)rs   rt   ru   r   r   r   r   r6   r   rw   r   r   r   r   r	   r   r   r   r   __classcell__rH   rH   r   rI   rx      sF    	
rx   )-httpr   typingr   r;   numpyr\   fastapir   vllm.engine.protocolr   vllm.entrypoints.chat_utilsr   vllm.entrypoints.loggerr   0vllm.entrypoints.openai.chat_completion.protocolr   'vllm.entrypoints.openai.engine.protocolr	   r
   &vllm.entrypoints.openai.engine.servingr   r   r   &vllm.entrypoints.openai.models.servingr   *vllm.entrypoints.pooling.classify.protocolr   r   r   r   r   vllm.entrypoints.rendererr   vllm.loggerr   vllm.outputsr   r   vllm.pooling_paramsr   rs   r=   r   rx   rH   rH   rH   rI   <module>   s*    	