o
    #1 i                     @   s   d dl 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mZmZ d dlmZ d dlmZ d dlmZ G d	d
 d
eZG dd deeZG dd deZdefddZdS )    N)Enum)Path)AnyListOptionalUnion)	BaseModel
ConfigDictfield_validator)BaseSettings)ValidationMode)MistralTokenizerc                   @   s   e Zd ZU dZeeeeeeeeeeeeef f f  f f  e	d< dZ
eeeeef   e	d< eddZdeeef fdd	ZdS )
OpenAIChatCompletionRequestaS  OpenAI chat completion request.

    This class is used to parse the request body for the OpenAI chat completion endpoint.

    Attributes:
        messages: The messages to use for the chat completion.
        tools: The tools to use for the chat completion.

    Note:
        This class accepts extra fields that are not validated.
    messagesNtoolsZallow)extrareturnc                    s8   fdd   D   fddj D _ S )zDrop extra fields from the model.

        This method is used to drop extra fields from the model, which are not defined in the model fields.

        Returns:
            The extra fields that were dropped from the model.
        c                    s$   i | ]\}}|t  jvr||qS  )typeZmodel_fields).0fieldvalueselfr   r/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/mistral_common/experimental/app/models.py
<dictcomp>)   s    zAOpenAIChatCompletionRequest.drop_extra_fields.<locals>.<dictcomp>c                    s   i | ]\}}| vr||qS r   r   )r   kv)extra_fieldsr   r   r   -   s    )Z
model_dumpitems__dict__r   r   )r   r   r   drop_extra_fields!   s
   

z-OpenAIChatCompletionRequest.drop_extra_fields)__name__
__module____qualname____doc__r   dictstrr   r   __annotations__r   r   r	   Zmodel_configr!   r   r   r   r   r      s   
 8
r   c                   @   s   e Zd ZdZdZdS )EngineBackendzZThe engine backend to use.

    Attributes:
        llama_cpp: The llama.cpp backend.
    	llama_cppN)r"   r#   r$   r%   r*   r   r   r   r   r)   1   s    r)   c                       s  e Zd ZU dZdZeed< ej	dZ
eed< dZeed< ejZeed< d	Zeed
< dZeed< edddededefddZedddedeeef defddZdeddf fddZdeeef deddfddZedefddZejdeddfddZ  Z S ) Settingsa<  Settings for the Mistral-common API.

    Attributes:
        app_name: The name of the application.
        app_version: The version of the application.
        engine_url: The URL of the engine.
        engine_backend: The backend to use for the engine.
        timeout: The timeout to use for the engine API.
    zMistral-common APIapp_namezmistral-commonapp_versionz	127.0.0.1
engine_urlengine_backend api_key<   timeoutbeforemoder   r   c                 C   s$   t |tr|dr|d d }|S )N/)
isinstancer'   endswithclsr   r   r   r   _validate_engine_urlM   s   zSettings._validate_engine_urlc                 C   s   t |tr	t|}|S N)r9   r'   r)   r;   r   r   r   _validate_backendT   s   
zSettings._validate_backendcontextNc                    s   t  | d | _d S r>   )supermodel_post_init
_tokenizer)r   r@   	__class__r   r   rB   [   s   
zSettings.model_post_inittokenizer_pathvalidation_modec                 C   s~   |dkrt d| jd urt dt|tr t|}| r |}t|tr3| r3tj||d| _d S tjt||d| _d S )Nr0   zITokenizer path must be set via the environment variable `TOKENIZER_PATH`.z'Tokenizer has already been initialized.r5   )	
ValueErrorrC   r9   r'   r   existsr   	from_fileZfrom_hf_hub)r   rF   rG   Zcandidate_tokenizer_pathr   r   r   _load_tokenizer_   s   

zSettings._load_tokenizerc                 C   s   | j d u r	td| j S )NzTokenizer not initialized.)rC   rH   r   r   r   r   	tokenizero   s   
zSettings.tokenizerc                 C   s   t |ts	td|| _d S )Nz2Tokenizer must be an instance of MistralTokenizer.)r9   r   rH   rC   )r   r   r   r   r   rL   u   s   

)!r"   r#   r$   r%   r,   r'   r(   	importlibmetadataversionr-   r.   r)   r*   r/   r1   r3   intr
   classmethodr=   r   r?   r   rB   r   r   rK   propertyr   rL   setter__classcell__r   r   rD   r   r+   ;   s(   
 


r+   r   c                   C   s   t  S )z,Get the settings for the Mistral-common API.)r+   r   r   r   r   get_settings|   s   rU   )importlib.metadatarM   enumr   pathlibr   typingr   r   r   r   Zpydanticr   r	   r
   Zpydantic_settingsr   Z*mistral_common.protocol.instruct.validatorr   Z(mistral_common.tokens.tokenizers.mistralr   r   r'   r)   r+   rU   r   r   r   r   <module>   s    $
A