o
    c۷iN                     @   s   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 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)	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f B f  B f  ed< dZ	eeeef  dB 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tools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  )typemodel_fields).0fieldvalueselfr   \/home/ubuntu/vllm_env/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    )
model_dumpitems__dict__r   r   )r   r   r   drop_extra_fields!   s
   

z-OpenAIChatCompletionRequest.drop_extra_fields)__name__
__module____qualname____doc__listdictstrr   __annotations__r   r   model_configr!   r   r   r   r   r      s   
 0
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B defddZdeddf fddZdeeB 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)r;   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   rB   	__class__r   r   rD   [   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 )Nr2   zITokenizer path must be set via the environment variable `TOKENIZER_PATH`.z'Tokenizer has already been initialized.r7   )	
ValueErrorrE   r;   r(   r   existsr
   	from_filefrom_hf_hub)r   rH   rI   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.)rE   rJ   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.)r;   r
   rJ   rE   )r   r   r   r   r   rP   u   s   

) r"   r#   r$   r%   r.   r(   r)   	importlibmetadataversionr/   r0   r+   r,   r1   r3   r5   intr   classmethodr?   rA   r   rD   r   r	   rO   propertyr
   rP   setter__classcell__r   r   rF   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   rY   )importlib.metadatarQ   enumr   pathlibr   typingr   pydanticr   r   r   pydantic_settingsr   *mistral_common.protocol.instruct.validatorr	   (mistral_common.tokens.tokenizers.mistralr
   r   r(   r+   r-   rY   r   r   r   r   <module>   s    $
A