o
    ®©Zh‹  ã                   @   sN   d dl Z d dlmZmZ d dlmZ d dlmZ e  e	¡Z
G dd„ deƒZdS )é    N)ÚAnyÚOptional)ÚLLM)ÚIpexLLMc                   @   sš   e Zd ZdZe	dddddœdedee dee ded	ee d
e	de
fdd„ƒZe	dddœdedee dee d
e	de
f
dd„ƒZedefdd„ƒZdS )ÚBigdlLLMzÔWrapper around the BigdlLLM model

    Example:
        .. code-block:: python

            from langchain_community.llms import BigdlLLM
            llm = BigdlLLM.from_model_id(model_id="THUDM/chatglm-6b")
    NT)Útokenizer_idÚload_in_4bitÚload_in_low_bitÚmodel_idÚmodel_kwargsr   r   r	   ÚkwargsÚreturnc                K   s&  t  d¡ zddlm}m} ddlm}	m}
 W n ty"   tdƒ‚w |dur,t  d¡ |s2t	dƒ‚|p5i }|p9|}z|	j
|fi |¤Ž}W n tyW   |
j
|fi |¤Ž}Y nw z|j
|fd	d
i|¤Ž}W n tyy   |j
|fd	d
i|¤Ž}Y nw d|v r‡dd„ | ¡ D ƒ}| d||||dœ|¤ŽS )a5  
        Construct object from model_id

        Args:
            model_id: Path for the huggingface repo id to be downloaded or
                      the huggingface checkpoint folder.
            tokenizer_id: Path for the huggingface repo id to be downloaded or
                      the huggingface checkpoint folder which contains the tokenizer.
            model_kwargs: Keyword arguments to pass to the model and tokenizer.
            kwargs: Extra arguments to pass to the model and tokenizer.

        Returns:
            An object of BigdlLLM.
        ú4BigdlLLM was deprecated. Please use IpexLLM instead.r   ©Ú	AutoModelÚAutoModelForCausalLM©ÚAutoTokenizerÚLlamaTokenizerúpCould not import bigdl-llm or transformers. Please install it with `pip install --pre --upgrade bigdl-llm[all]`.Nz°`load_in_low_bit` option is not supported in BigdlLLM and 
                is ignored. For more data types support with `load_in_low_bit`, 
                use IpexLLM instead.zŒBigdlLLM only supports loading in 4-bit mode, i.e. load_in_4bit = True. Please install it with `pip install --pre --upgrade bigdl-llm[all]`.r   TÚtrust_remote_codec                 S   ó   i | ]\}}|d kr||“qS ©r   © ©Ú.0ÚkÚvr   r   úY/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/bigdl_llm.pyÚ
<dictcomp>]   ó    z*BigdlLLM.from_model_id.<locals>.<dictcomp>©r
   ÚmodelÚ	tokenizerr   r   )ÚloggerÚwarningÚbigdl.llm.transformersr   r   Útransformersr   r   ÚImportErrorÚ
ValueErrorÚfrom_pretrainedÚ	ExceptionÚitems)Úclsr
   r   r   r   r	   r   r   r   r   r   Ú_model_kwargsÚ_tokenizer_idr#   r"   r   r   r   Úfrom_model_id   sj   
ÿÿÿÿÿÿÿ
ÿÿÿ
ÿÿÿüûzBigdlLLM.from_model_id)r   c                K   s   t  d¡ zddlm}m} ddlm}m} W n ty"   tdƒ‚w |p&i }	|p*|}
z|j	|
fi |	¤Ž}W n t
yH   |j	|
fi |	¤Ž}Y nw z|j|fi |	¤Ž}W n t
yf   |j|fi |	¤Ž}Y nw d|	v rtdd„ |	 ¡ D ƒ}	| d||||	d	œ|¤ŽS )aê  
        Construct low_bit object from model_id

        Args:

            model_id: Path for the bigdl-llm transformers low-bit model folder.
            tokenizer_id: Path for the huggingface repo id or local model folder
                      which contains the tokenizer.
            model_kwargs: Keyword arguments to pass to the model and tokenizer.
            kwargs: Extra arguments to pass to the model and tokenizer.

        Returns:
            An object of BigdlLLM.
        r   r   r   r   r   r   c                 S   r   r   r   r   r   r   r   r   ž   r    z2BigdlLLM.from_model_id_low_bit.<locals>.<dictcomp>r!   Nr   )r$   r%   r&   r   r   r'   r   r   r(   r*   r+   Zload_low_bitr,   )r-   r
   r   r   r   r   r   r   r   r.   r/   r#   r"   r   r   r   Úfrom_model_id_low_biti   sB   
ÿÿÿÿÿüûzBigdlLLM.from_model_id_low_bitc                 C   s   dS )Nz	bigdl-llmr   )Úselfr   r   r   Ú	_llm_typeª   s   zBigdlLLM._llm_type)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚclassmethodÚstrr   ÚdictÚboolr   r   r0   r1   Úpropertyr3   r   r   r   r   r      sP    	ýùþýûúùø	÷Sýûþýûúù@r   )ÚloggingÚtypingr   r   Z#langchain_core.language_models.llmsr   Z!langchain_community.llms.ipex_llmr   Ú	getLoggerr4   r$   r   r   r   r   r   Ú<module>   s    
