
    dhH'                         S SK r S SKJrJrJrJr  S SKJr  S SKJ	r	  S SK
Jr  Sr\ R                  " \5      r " S S\	5      rg)	    N)AnyListMappingOptional)CallbackManagerForLLMRun)LLM)
ConfigDictgpt2c                      \ rS rSr% Sr\r\\S'    Sr	\
\   \S'    Sr\\S'    Sr\\S'    Sr\\S	'    \" S
S9r\ S"SSSS.S\S\
\   S\
\   S\S\
\   S\S\4S jjj5       r\ S"SS.S\S\
\   S\
\   S\S\4
S jjj5       r\      S#S\S\
\   S\S\
\   S\S\
\   S\
\   S\4S jj5       r\S\S\S\S\S\S\4S j5       r\S\\\4   4S j5       r\S\4S j5       r  S$S\S\
\\      S\
\   S\S\4
S  jjr S!r!g)%IpexLLM   zIpexLLM 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          
      ,    U R                  UUSUUUUS9$ )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          Y/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/ipex_llm.pyfrom_model_idIpexLLM.from_model_id'   s0    @ %%+%  
 	
    )r   c          
      ,    U R                  UUSSSUUS9$ )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_bitIpexLLM.from_model_id_low_bitQ   s/    0 % %  
 	
r#   r   c           	          SSK JnJn	  SSKJn
Jn  U=(       d    0 nU=(       d    0 nU=(       d    UnSU;  a  SUS'   US   S;  a  [        SUS    S	35      eUR                  S5      n U
R                  " U40 UD6nS
U;   a-  UR                  5        VVs0 sH  u  nnUS
:w  d  M  UU_M     nnnSSS.nU(       d  Ub  SnUUS'   O
SnUUS'   OSn U R                  U	UUUUS9nUR                  U5        U " SUUUUS.UD6$ ! [         a    [        S5      ef = f! [         a    UR                  " U40 UD6n Nf = fs  snnf ! [         a    U R                  UUUUUS9n Nf = f)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_cacher0   from_pretrainedr   r   load_low_bit)load_function_namer   load_kwargsr   )r   r   r   r    )ipex_llm.transformersr(   r)   transformersr*   r+   ImportError
ValueErrorpopr2   	Exceptionitems_load_model_generalto)r   r   r   r   r   r   r   r   r(   r)   r*   r+   _model_kwargs_tokenizer_idr,   r   kvr5   r4   r   s                        r    r   IpexLLM._load_models   s	   	 C %*2$0=(&+M(#".811>x1H0IL  ""8,	W%55mU}UI
 -/!.!4!4!6!6A!?R:R1!6   !%

 *%6"1@-.%6".:N+!/	++$#5!'* , E" 	 
&	

 
 	
M  	c 	0  	W&66}VVI	W
:  	++#5!'* , E	s;   D +D E 'E E DD=<D=E&%E&model_classr4   r5   c           	           [        X5      nU" U40 0 UEUED6$ ! [         a2  n[        R                  SU R                   SU SU 35         SnAgSnAff = f)z,General function to attempt to load a model.zFailed to load model using r/   z: N)getattrr<   loggererror__name__)rE   r4   r   r5   r   load_functiones          r    r>   IpexLLM._load_model_general   sp    	#KDM M-L-L|-LMM 	LL-''(*<)=RsD 	s    
A(AAc                 4    U R                   U R                  S.$ )zGet the identifying parameters.r   r   rO   selfs    r    _identifying_paramsIpexLLM._identifying_params   s      --
 	
r#   c                     g)Nzipex-llmr6   rP   s    r    	_llm_typeIpexLLM._llm_type   s    r#   promptstoprun_managerc                    U R                   (       a  SSKJn  U R                  R	                  USS9nUR                  U R                  R                  5      nU" U R                  SSS9nUb&  SSKJ	n  SSK
Jn	  U" U	" X R                  5      /5      n
OS n
U R                  R                  " U4UU
S	.UD6nU R                  R                  US   SS
9nU$ U R                  R	                  USS9nUR                  U R                  R                  5      nUb&  SSKJ	n  SSK
Jn	  U" U	" X R                  5      /5      n
OS n
U R                  R                  " U4SU
0UD6nU R                  R                  US   SS
9[        U5      S  nU$ )Nr   )TextStreamerpt)return_tensorsT)skip_promptskip_special_tokens)StoppingCriteriaList)StopSequenceCriteria)streamerstopping_criteria)r_   rc   )r   r8   r[   r   encoder?   r   r,   )transformers.generation.stopping_criteriar`   transformers.tools.agentsra   generatedecodelen)rQ   rW   rX   rY   r   r[   	input_idsrb   r`   ra   rc   outputtexts                r    _callIpexLLM._call   s    >>1--fT-JI!TZZ%6%67I#DdH  K %9)$?@%! %)!ZZ((!"3 	F >>(((MDK--fT-JI!TZZ%6%67I K$8)$?@%! %)!ZZ((->BHF >>(((MFD Kr#   r6   )N)NFNFNN)NN)"rJ   
__module____qualname____firstlineno____doc__DEFAULT_MODEL_IDr   str__annotations__r   r   dictr   r   r   r   boolr	   model_configclassmethodr   r!   r%   r   staticmethodr>   propertyr   rR   rU   r   r   rm   __static_attributes__r6   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 8r#   r   )loggingtypingr   r   r   r   langchain_core.callbacksr   #langchain_core.language_models.llmsr   pydanticr	   rs   	getLoggerrJ   rH   r   r6   r#   r    <module>r      s=     / / = 3   
		8	$[c [r#   