
    Ng                     r    d dl Z d dlmZmZ d dlmZ d dlmZ  e j        e	          Z
 G d de          ZdS )    N)AnyOptional)LLM)IpexLLMc                       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            Ze	 ddddedee         dee         d
e	de
f
d            Zedefd            ZdS )BigdlLLMzWrapper 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                   t                               d           	 ddlm}m} ddlm}	m}
 n# t          $ r t          d          w xY w|t                               d           |st          d          |pi }|p|}	  |	j
        |fi |}n# t          $ r  |
j
        |fi |}Y nw xY w	  |j
        |fd	d
i|}n # t          $ r  |j
        |fd	d
i|}Y nw xY wd|v r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LlamaTokenizerpCould 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.zBigdlLLM 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                 &    i | ]\  }}|d k    ||S r    .0kvs      ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/bigdl_llm.py
<dictcomp>z*BigdlLLM.from_model_id.<locals>.<dictcomp>]   0       A!?R:R:R1:R:R:R    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&   s                  r!   from_model_idzBigdlLLM.from_model_id   s=   2 	MNNN	        CBBBBBBBB 	 	 	W  	 &NN(    	W   %*$0	W55mUU}UUII 	W 	W 	W66}VVVVIII	W	8(8 '+/< EE  	 	 	-I- '+/< EEE	
 -// !.!4!4!6!6  M s 
&	
 

 
 
 	
s-   - A B B*)B*.B? ?CC)r	   c                   t                               d           	 ddlm}m} ddlm}m} n# t          $ r t          d          w xY w|pi }	|p|}
	  |j	        |
fi |	}n# t          $ r  |j	        |
fi |	}Y nw xY w	  |j        |fi |	}n# t          $ r  |j        |fi |	}Y nw xY wd|	v r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                 &    i | ]\  }}|d k    ||S r   r   r   s      r!   r"   z2BigdlLLM.from_model_id_low_bit.<locals>.<dictcomp>   r#   r$   r%   r   )r(   r)   r*   r   r   r+   r   r   r,   r.   r/   load_low_bitr0   )r1   r   r   r	   r   r   r   r   r   r2   r3   r'   r&   s                r!   from_model_id_low_bitzBigdlLLM.from_model_id_low_biti   s   0 	MNNN	        CBBBBBBBB 	 	 	W  	 %*$0	W55mUU}UUII 	W 	W 	W66}VVVVIII	W	F5(5hPP-PPEE 	F 	F 	F*I*8EE}EEEEE	F -// !.!4!4!6!6  M s 
&	
 

 
 
 	
s-   - AA" "A=<A=B B+*B+c                     dS )Nz	bigdl-llmr   )selfs    r!   	_llm_typezBigdlLLM._llm_type   s    {r$   )N)__name__
__module____qualname____doc__classmethodstrr   dictboolr   r   r4   r8   propertyr;   r   r$   r!   r   r      sU          (,Q

 '+!)-Q
 Q
 Q
Q
 tnQ

 smQ
 Q
 "#Q
 Q
 
Q
 Q
 Q
 [Q
f  (,>

 '+>
 >
 >
>
 tn>

 sm>
 >
 
>
 >
 >
 [>
@ 3    X  r$   r   )loggingtypingr   r   #langchain_core.language_models.llmsr   !langchain_community.llms.ipex_llmr   	getLoggerr<   r(   r   r   r$   r!   <module>rJ      s                     3 3 3 3 3 3 5 5 5 5 5 5		8	$	$a a a a aw a a a a ar$   