
    7|h<'                         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 j                  e      Z G d de	      Zy)	    N)AnyListMappingOptional)CallbackManagerForLLMRun)LLM)
ConfigDictgpt2c                      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	
      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!dede	e   dede	e   dede	e
   de	e
   defd       Zedededede
de
defd       Zedeeef   fd       Zedefd       Z	 	 d"dede	ee      de	e   dedef
dZy)#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	streamingforbid)extra)tokenizer_idload_in_4bitload_in_low_bitr   r   r   kwargsreturnc          	      2    | j                  ||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   s          `/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/ipex_llm.pyfrom_model_idzIpexLLM.from_model_id'   s0    @ %%+%  
 	
    )r   c          	      2    | j                  ||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   s        r   from_model_id_low_bitzIpexLLM.from_model_id_low_bitQ   s/    0 % %  
 	
r!   r   c                    	 ddl m}m}	 ddlm}
m} |xs i }|xs i }|xs |}d|vrd|d<   |d   dvrt        d|d    d	      |j                  d      }	  |
j                  |fi |}d
|v r)|j                         D ci c]  \  }}|d
k7  s|| }}}ddd}|s|d}||d<   n
d}||d<   nd}	 | j                  |	||||      }|j                  |        | d||||d|S # t        $ r t        d      w xY w# t        $ r  |j                  |fi |}Y w xY wc c}}w # t        $ r | j                  |||||      }Y w xY w)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*   xpuzXIpexLLMBgeEmbeddings currently only supports device to be 'cpu' or 'xpu', but you have: .trust_remote_codeT)	use_cacher-   from_pretrainedr   r   load_low_bit)load_function_namer   load_kwargsr   )r   r   r   r    )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(   _model_kwargs_tokenizer_idr)   r   kvr2   r1   r   s                        r   r   zIpexLLM._load_models   s   	 C %*2$0=(&+M(#".811>x1H0IL  ""8,	W555mU}UI
 -/!.!4!4!6A!?R:R1M  !%

 *%6"1@-.%6".:N+!/	++$#5!'* , E" 	 
&	

 
 	
M  	c 	0  	W666}VVI	W
:  	++#5!'* , E	s;   C( D  D"D"4D( (C= DD(E
	E
model_classr1   r2   c           	          	 t        | |      } ||fi i ||S # t        $ r2}t        j                  d| j                   d| d|        Y d}~yd}~ww xY w)z,General function to attempt to load a model.zFailed to load model using r,   z: N)getattrr9   loggererror__name__)rA   r1   r   r2   r   load_functiones          r   r;   zIpexLLM._load_model_general   ss    	#K1CDM M-L-L|-LMM 	LL-''(*<)=RsD 	s    	A(AAc                 4    | j                   | j                  dS )zGet the identifying parameters.r   r   rJ   selfs    r   _identifying_paramszIpexLLM._identifying_params   s      --
 	
r!   c                      y)Nzipex-llmr3   rK   s    r   	_llm_typezIpexLLM._llm_type   s    r!   promptstoprun_managerc                 "   | j                   rddlm} | j                  j	                  |d      }|j                  | j                  j                        } || j                  dd      }|'ddlm	} ddl
m}	  | |	|| j                        g      }
nd }
 | j                  j                  |f||
d	|}| j                  j                  |d   d
      }|S | j                  j	                  |d      }|j                  | j                  j                        }|'ddlm	} ddl
m}	  | |	|| j                        g      }
nd }
 | j                  j                  |fd|
i|}| j                  j                  |d   d
      t        |      d  }|S )Nr   )TextStreamerpt)return_tensorsT)skip_promptskip_special_tokens)StoppingCriteriaList)StopSequenceCriteria)streamerstopping_criteria)rX   r\   )r   r5   rT   r   encoder<   r   r)   )transformers.generation.stopping_criteriarY   transformers.tools.agentsrZ   generatedecodelen)rL   rP   rQ   rR   r   rT   	input_idsr[   rY   rZ   r\   outputtexts                r   _callzIpexLLM._call   s    >>1--fT-JI!TZZ%6%67I#DdH  K %9)$?@%! %)!(TZZ((!"3 	F >>(((MDK--fT-JI!TZZ%6%67I K$8)$?@%! %)!(TZZ((->BHF >>(((MFD Kr!   )N)NFNFNN)NN) rF   
__module____qualname____doc__DEFAULT_MODEL_IDr   str__annotations__r   r   dictr   r   r   r   boolr	   model_configclassmethodr   r    r#   r   staticmethodr;   propertyr   rM   rO   r   r   rf   r3   r!   r   r   r      s    %Hc$*#'L(4.'0E3Is&It8L  (,'

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

 sm'
 '
 "#'
 '
 
'
 '
R  (,

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

 sm
 
 

 
B  '+")-#'+!%]
]
 sm]
 	]

 "#]
 ]
 tn]
 ]
 
]
 ]
~   	
  
 " 
WS#X%6 
 
 3   %):>	88 tCy!8 67	8
 8 
8r!   r   )loggingtypingr   r   r   r   langchain_core.callbacksr   #langchain_core.language_models.llmsr   pydanticr	   rj   	getLoggerrF   rD   r   r3   r!   r   <module>ry      s?     / / = 3   
		8	$[c [r!   