a
    bg<'                     @   sf   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 dl
mZ dZe eZG dd de	ZdS )	    N)AnyListMappingOptional)CallbackManagerForLLMRun)LLM)
ConfigDictZgpt2c                   @   sZ  e Zd ZU dZeZeed< dZe	e
 ed< dZeed< dZeed< dZeed< e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	streamingZforbid)extra)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   o/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/ipex_llm.py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_bitQ   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y>   tdY n0 |pFi }|pNi }|pV|}d|vrhd|d< |d dvrtd|d  d	|d}z|
j	|fi |}W n$ t
y   |j	|fi |}Y n0 d
|v rdd | D }ddd}|s|d urd}||d< nd}||d< nd}z| j|	||||d}W n( t
yb   | j|||||d}Y n0 || | 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   s   s|    






zIpexLLM._load_model)model_classr+   r
   r,   r   r   c                 C   sl   z$t | |}||fi i ||W S  tyf } z*td| j d| d|  W Y d}~n
d}~0 0 dS )z,General function to attempt to load a model.zFailed to load model using r$   z: N)getattrr1   loggererror__name__)r5   r+   r
   r,   r   Zload_functioner   r   r   r3      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 u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 u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)rE   rI   )r   r-   rC   r   encoder4   r   r"   Z)transformers.generation.stopping_criteriarF   Ztransformers.tools.agentsrG   generatedecodelen)r=   r@   rA   rB   r   rC   Z	input_idsrH   rF   rG   rI   outputtextr   r   r   _call   sX    
zIpexLLM._call)N)N)NFNFNN)NN) r9   
__module____qualname____doc__DEFAULT_MODEL_IDr
   str__annotations__r   r   dictr   r   r   r   boolr   Zmodel_configclassmethodr   r   r   r   staticmethodr3   propertyr   r>   r?   r   r   rP   r   r   r   r   r	      s   
	 ) !      _  
r	   )loggingtypingr   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zpydanticr   rT   	getLoggerr9   r7   r	   r   r   r   r   <module>   s   
