o
    )i                     @  s   d dl m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
mZmZmZmZ d dlmZ d dlmZmZ e
rKd d	lmZmZmZ d d
lmZ neZeZeZeZeeZG dd dZG dd dZdS )    )annotationsN)abstractmethod)Sequence)cached_property)TYPE_CHECKINGAnyCallableOptionalUnion)init_logger)import_from_path
is_list_of)ChatCompletionRequestDeltaMessageResponsesRequest)AnyTokenizerc                   @  s`   e Zd ZdZd#ddZed$dd	Zed%ddZed&ddZ	ed'ddZ
ed(d d!Zd"S ))ReasoningParserz
    Abstract reasoning parser class that should not be used directly.
    Provided and methods should be used in derived classes.

    It is used to extract reasoning content from the model output.
    	tokenizerr   c                 C  s
   || _ d S N)model_tokenizer)selfr    r   p/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/reasoning/abs_reasoning_parsers.py__init__%   s   
zReasoningParser.__init__returndict[str, int]c                 C  s
   | j  S r   )r   Z	get_vocab)r   r   r   r   vocab(   s   
zReasoningParser.vocab	input_ids	list[int]boolc                 C     dS )a  
        Check if the reasoning content ends in the input_ids.

        It is used in structured engines like `xgrammar` to check if the
        reasoning content ends in the model output.

        Parameters:
        input_ids: list[int]
            The input_ids of the model output.

        Returns:
        bool
            True if the reasoning content ends in the input_ids.
        Nr   r   r   r   r   r   is_reasoning_end.       z ReasoningParser.is_reasoning_endc                 C  r    )z
        Extract content token ids from the input_ids.
        Parameters:
        input_ids: list[int]
            The input_ids of the model output.
        Returns:
        list[int]
            The extracted content from the input_ids.
        Nr   r!   r   r   r   extract_content_ids?   r#   z#ReasoningParser.extract_content_idsmodel_outputstrrequest.Union[ChatCompletionRequest, ResponsesRequest]#tuple[Optional[str], Optional[str]]c                 C  r    )aE  
        Extract reasoning content from a complete model-generated string.

        Used for non-streaming responses where we have the entire model response
        available before sending to the client.

        Parameters:
        model_output: str
            The model-generated string to extract reasoning content from.

        request: ChatCompletionRequest
            The request object that was used to generate the model_output.

        Returns:
        tuple[Optional[str], Optional[str]]
            A tuple containing the reasoning content and the content.
        Nr   )r   r%   r'   r   r   r   extract_reasoning_contentK   r#   z)ReasoningParser.extract_reasoning_contentprevious_textcurrent_text
delta_textprevious_token_idsSequence[int]current_token_idsdelta_token_idsUnion[DeltaMessage, None]c                 C  r    )az  
        Instance method that should be implemented for extracting reasoning
        from an incomplete response; for use when handling reasoning calls and
        streaming. Has to be an instance method because  it requires state -
        the current tokens/diffs, but also the information about what has
        previously been parsed and extracted (see constructor)
        Nr   )r   r+   r,   r-   r.   r0   r1   r   r   r   #extract_reasoning_content_streamingc   r#   z3ReasoningParser.extract_reasoning_content_streamingN)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/   r0   r/   r1   r/   r   r2   )__name__
__module____qualname____doc__r   r   r   r   r"   r$   r*   r3   r   r   r   r   r      s    
r   c                   @  s`   e Zd ZU i Zded< edddZe			
ddddZe			
		dd ddZed!ddZ	d	S )"ReasoningParserManagerzdict[str, type]reasoning_parsersname
str | Noner   type[ReasoningParser]c                 C  s$   || j v r
| j | S td| d)z
        Get reasoning parser by name which is registered by `register_module`.

        Raise a KeyError exception if the name is not registered.
        zreasoning helper: 'z ' not found in reasoning_parsers)r9   KeyError)clsr:   r   r   r   get_reasoning_parsery   s
   


z+ReasoningParserManager.get_reasoning_parserNTmoduletypemodule_nameOptional[Union[str, list[str]]]forcer   Nonec                 C  s~   t |tstdt| |d u r|j}t|tr|g}|D ]}|s7|| jv r7| j| }t| d|j	 || j|< qd S )Nz4module must be subclass of ReasoningParser, but got z is already registered at )

issubclassr   	TypeErrorrA   r4   
isinstancer&   r9   r=   r5   )r>   r@   rB   rD   r:   Zexisted_moduler   r   r   _register_module   s    


z'ReasoningParserManager._register_moduleUnion[type, None]Union[type, Callable]c                   sz   t tstdt du s%t ts%tts%tdt |dur3 j|d |S  fdd}|S )z
        Register module with the given name or name list. it can be used as a
        decoder(with module as None) or normal function(with module as not
        None).
        z!force must be a boolean, but got NzEname must be None, an instance of str, or a sequence of str, but got r@   rB   rD   c                   s    j | d | S )NrL   )rI   )r@   r>   rD   r:   r   r   	_register   s   z9ReasoningParserManager.register_module.<locals>._register)rH   r   rG   rA   r&   r   rI   )r>   r:   rD   r@   rN   r   rM   r   register_module   s   
z&ReasoningParserManager.register_moduleplugin_pathr&   c                 C  sL   t jt j|d }zt|| W dS  ty%   td|| Y dS w )zq
        Import a user-defined reasoning parser by the path
        of the reasoning parser define file.
        r   z#Failed to load module '%s' from %s.N)ospathsplitextbasenamer   	Exceptionlogger	exception)r>   rP   rB   r   r   r   import_reasoning_parser   s   z.ReasoningParserManager.import_reasoning_parser)r:   r;   r   r<   )NT)r@   rA   rB   rC   rD   r   r   rE   )NTN)r:   rC   rD   r   r@   rJ   r   rK   )rP   r&   r   rE   )
r4   r5   r6   r9   __annotations__classmethodr?   rI   rO   rX   r   r   r   r   r8   v   s   
 !r8   )
__future__r   rQ   abcr   collections.abcr   	functoolsr   typingr   r   r   r	   r
   Zvllm.loggerr   Z
vllm.utilsr   r   Z vllm.entrypoints.openai.protocolr   r   r   Z!vllm.transformers_utils.tokenizerr   r4   rV   r   r8   r   r   r   r   <module>   s$   Y