
    Ng<'                         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dS )	    N)AnyListMappingOptional)CallbackManagerForLLMRun)LLM)
ConfigDictgpt2c                   p   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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	streamingforbid)extra)tokenizer_idload_in_4bitload_in_low_bitr   r   r   kwargsreturnc          	      :    |                      ||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/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/ipex_llm.pyfrom_model_idzIpexLLM.from_model_id'   s5    @ %%+%  
 
 	
    )r   c          	      :    |                      ||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   s4    0 % %  
 
 	
r!   Fr   c                    	 ddl m}m}	 ddlm}
m} n# t          $ r t          d          w xY w|pi }|pi }|p|}d|vrd|d<   |d         dvrt          d|d          d	          |                    d          }	  |
j	        |fi |}n# t          $ r  |j	        |fi |}Y nw xY wd
|v rd |                                D             }ddd}|s|d}||d<   n
d}||d<   nd}	 |                     |	||||          }n*# t          $ r |                     |||||          }Y nw xY w|                    |            | d||||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*   xpuzXIpexLLMBgeEmbeddings currently only supports device to be 'cpu' or 'xpu', but you have: .trust_remote_codec                 &    i | ]\  }}|d k    ||S )r-    ).0kvs      r   
<dictcomp>z'IpexLLM._load_model.<locals>.<dictcomp>   s0       A!?R:R:R1:R:R:Rr!   T)	use_cacher-   from_pretrainedr   r   load_low_bit)load_function_namer   load_kwargsr   )r   r   r   r   r/   )ipex_llm.transformersr%   r&   transformersr'   r(   ImportError
ValueErrorpopr5   	Exceptionitems_load_model_generalto)r   r   r   r   r   r   r   r   r%   r&   r'   r(   _model_kwargs_tokenizer_idr)   r   r8   r7   r   s                      r   r   zIpexLLM._load_models   s   	        CBBBBBBBB 	 	 	c  	 %*2$0=((&+M(#".88L1>x1HL L L   ""8,,	W55mUU}UUII 	W 	W 	W66}VVVVIII	W -// !.!4!4!6!6  M !%
 

  	0*%6"1@-..%6".:N++!/	++$#5!'* ,  EE  	 	 	++#5!'* ,  EEE	 	s 
&	
 

 
 
 	
s,    ->B B('B()D $D+*D+model_classr7   r8   c           	          	 t          | |          } ||fi i ||S # t          $ r3}t                              d| j         d| d|            Y d}~dS d}~ww xY w)z,General function to attempt to load a model.zFailed to load model using r,   z: N)getattrr>   loggererror__name__)rD   r7   r   r8   r   load_functiones          r   r@   zIpexLLM._load_model_general   s    	#K1CDDM =MM-L-L|-LMMM 	 	 	LLD'D D*<D D@AD D        	s    
A(AAc                      | j         | j        dS )zGet the identifying parameters.r   r   rM   selfs    r   _identifying_paramszIpexLLM._identifying_params   s      -
 
 	
r!   c                     dS )Nzipex-llmr/   rN   s    r   	_llm_typezIpexLLM._llm_type   s    zr!   promptstoprun_managerc                    | j         rddlm} | j                            |d          }|                    | j        j                  } || j        dd          }|(ddlm	} ddl
m}	  | |	|| j                  g          }
nd }
 | j        j        |f||
d	|}| j                            |d         d
          }|S | j                            |d          }|                    | j        j                  }|(ddlm	} ddl
m}	  | |	|| j                  g          }
nd }
 | j        j        |fd|
i|}| j                            |d         d
          t          |          d          }|S )Nr   )TextStreamerpt)return_tensorsT)skip_promptskip_special_tokens)StoppingCriteriaList)StopSequenceCriteria)streamerstopping_criteria)r[   r_   )r   r:   rW   r   encoderA   r   r)   )transformers.generation.stopping_criteriar\   transformers.tools.agentsr]   generatedecodelen)rO   rS   rT   rU   r   rW   	input_idsr^   r\   r]   r_   outputtexts                r   _callzIpexLLM._call   s:    > 1	111111--fT-JJI!TZ%677I#|Dd  H       KJJJJJ %9$8))$??@% %!! %)!(TZ(!"3  	 F >(((MMDK--fT-JJI!TZ%677I      KJJJJJ$8$8))$??@% %!! %)!(TZ( ->BH F >(((MMFD Kr!   )N)NFNFNN)NN) rI   
__module____qualname____doc__DEFAULT_MODEL_IDr   str__annotations__r   r   dictr   r   r   r   boolr	   model_configclassmethodr   r    r#   r   staticmethodr@   propertyr   rP   rR   r   r   ri   r/   r!   r   r   r      sT          %Hc$$$*#'L(4.'''0E3Is&It8:  L  (,'

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

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

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

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

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