U
    h&                     @   sZ   d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	 dZ
e eZG dd de	ZdS )    N)AnyListMappingOptional)CallbackManagerForLLMRun)LLMZgpt2c                   @   sV  e Zd ZU dZeZeed< dZe	e
 ed< eed< eed< dZeed< G d	d
 d
Zed"ddddee	e
 e	e ee	e eedddZed#ddee	e
 e	e eedddZed$ee	e ee	e ee	e
 e	e
 edddZeeeee
e
edddZeeeef dddZeedddZd%ee	ee  e	e eedd d!ZdS )&IpexLLMzIpexLLM model.

    Example:
        .. code-block:: python

            from langchain_community.llms import IpexLLM
            llm = IpexLLM.from_model_id(model_id="THUDM/chatglm-6b")
    model_idNmodel_kwargsmodel	tokenizerT	streamingc                   @   s   e Zd ZdZdS )zIpexLLM.ConfigZforbidN)__name__
__module____qualname__extra r   r   E/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/ipex_llm.pyConfig"   s   r   )tokenizer_idload_in_4bitload_in_low_bit)r	   r
   r   r   r   kwargsreturnc             	   K   s   | j ||d||||dS )a  
        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.
            load_in_4bit: "Whether to load model in 4bit.
                      Unused if `load_in_low_bit` is not None.
            load_in_low_bit: Which low bit precisions to use when loading model.
                      Example values: 'sym_int4', 'asym_int4', 'fp4', 'nf4', 'fp8', etc.
                      Overrides `load_in_4bit` if specified.
            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 IpexLLM.

        Fr	   r   low_bit_modelr   r   r
   r   _load_model)clsr	   r
   r   r   r   r   r   r   r   from_model_id%   s     zIpexLLM.from_model_id)r   )r	   r
   r   r   r   c             	   K   s   | j ||ddd||dS )a  
        Construct low_bit object from model_id

        Args:

            model_id: Path for the ipex-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 IpexLLM.
        TFNr   r   )r   r	   r
   r   r   r   r   r   from_model_id_low_bitO   s    zIpexLLM.from_model_id_low_bitF)r	   r   r   r   r   r
   r   r   c              
   C   s  z$ddl m}m}	 ddlm}
m} W n tk
r@   tdY nX |pHi }|pPi }|pX|}d|krjd|d< |d dkrtd|d  d	|d}z|
j	|f|}W n" t
k
r   |j	|f|}Y nX d
|krdd | D }ddd}|s|d k	rd}||d< nd}||d< nd}z| j|	||||d}W n* t
k
r`   | j|||||d}Y nX || | f ||||d|S )Nr   )	AutoModelAutoModelForCausalLM)AutoTokenizerLlamaTokenizerzCould not import ipex-llm. Please install `ipex-llm` properly following installation guides: https://github.com/intel-analytics/ipex-llm?tab=readme-ov-file#install-ipex-llm.devicecpu)r&   ZxpuzXIpexLLMBgeEmbeddings currently only supports device to be 'cpu' or 'xpu', but you have: .trust_remote_codec                 S   s   i | ]\}}|d kr||qS )r(   r   ).0kvr   r   r   
<dictcomp>   s      z'IpexLLM._load_model.<locals>.<dictcomp>T)Z	use_cacher(   from_pretrainedr   r   Zload_low_bit)load_function_namer	   load_kwargsr
   )r	   r   r   r
   )Zipex_llm.transformersr!   r"   transformersr#   r$   ImportError
ValueErrorpopr-   	Exceptionitems_load_model_generalto)r   r	   r   r   r   r   r
   r   r!   r"   r#   r$   Z_model_kwargsZ_tokenizer_idr%   r   r/   r.   r   r   r   r   r   q   sx    






zIpexLLM._load_model)model_classr.   r	   r/   r
   r   c                 C   sb   zt | |}||f||W S  tk
r\ } z"td| j d| d|  W 5 d}~X Y nX dS )z,General function to attempt to load a model.zFailed to load model using r'   z: N)getattrr4   loggererrorr   )r8   r.   r	   r/   r
   Zload_functioner   r   r   r6      s    	
zIpexLLM._load_model_general)r   c                 C   s   | j | jdS )zGet the identifying parameters.r	   r
   r=   selfr   r   r   _identifying_params   s    zIpexLLM._identifying_paramsc                 C   s   dS )Nzipex-llmr   r>   r   r   r   	_llm_type   s    zIpexLLM._llm_type)promptstoprun_managerr   r   c                 K   s@  | j rddlm} | jj|dd}|| jj}|| jddd}|d k	rtddlm	} ddl
m}	 ||	|| jg}
nd }
| jj|f||
d	|}| jj|d dd
}|S | jj|dd}|| jj}|d k	rddlm	} ddl
m}	 ||	|| jg}
nd }
| jj|fd|
i|}| jj|d dd
t|d  }|S d S )Nr   )TextStreamerpt)Zreturn_tensorsT)Zskip_promptskip_special_tokens)StoppingCriteriaList)StopSequenceCriteria)streamerstopping_criteria)rG   rK   )r   r0   rE   r   encoder7   r   r%   Z)transformers.generation.stopping_criteriarH   Ztransformers.tools.agentsrI   generatedecodelen)r?   rB   rC   rD   r   rE   Z	input_idsrJ   rH   rI   rK   outputtextr   r   r   _call   s\      zIpexLLM._call)N)N)NFNFNN)NN)r   r   r   __doc__DEFAULT_MODEL_IDr	   str__annotations__r
   r   dictr   r   boolr   classmethodr   r   r    r   staticmethodr6   propertyr   r@   rA   r   r   rR   r   r   r   r   r      s   
	 ) !      _  
r   )loggingtypingr   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   rT   	getLoggerr   r:   r   r   r   r   r   <module>   s   
