a
     gô  ã                   @   s"   d dl Z d dlZG dd dZdS )é    Nc                   @   s   e Zd Zdd ZeedddZdddd	ejd
dfeeeeeedddZ	dee
eee
e
eeed	ddZdeeeee
e
eeed	ddZdS )ÚWangChanGLMc                 C   s$   t  dĄ| _d| _dddd| _d S )Nu   [^āļ-āđ]+Ú
z1<context>: {input}
<human>: {instruction}
<bot>: z<human>: {instruction}
<bot>: z<human>: {human}
<bot>: {bot})Úprompt_inputÚprompt_no_inputZprompt_chatbot)ÚreÚcompileÚexclude_patternZ
stop_tokenÚPROMPT_DICT)ÚselfĐ r   úH/usr/local/lib/python3.9/dist-packages/pythainlp/generate/wangchanglm.pyÚ__init__	   s    øzWangChanGLM.__init__)ÚtextÚreturnc                 C   s   t | j |ĄS )N)Úboolr   Úsearch)r
   r   r   r   r   Ú
is_exclude   s    zWangChanGLM.is_excludez)pythainlp/wangchanglm-7.5B-sft-en-shardedTFZcudaz./)Ú
model_pathÚreturn_dictÚload_in_8bitÚdeviceÚoffload_folderÚlow_cpu_mem_usagec              	   C   sĪ   ddl }ddlm}	m}
 || _|| _|| _|	j| j||||||d| _|
 | jĄ| _	|j
| j	j Ą ddgd| _| jj | jĄ| jd< | j| jjd	u  j Ą | _dS )
a  
        Load model
        
        :param str model_path: model path
        :param bool return_dict: return dict
        :param bool load_in_8bit: load model in 8bit
        :param str device: device (cpu, cuda or other)
        :param torch_dtype torch_dtype: torch_dtype
        :param str offload_folder: offload folder
        :param bool low_cpu_mem_usage: low cpu mem usage
        r   N)ÚAutoModelForCausalLMÚAutoTokenizer)r   r   Z
device_mapÚtorch_dtyper   r   r   Úidx)Úcolumnsr   T)ZpandasZtransformersr   r   r   r   r   Zfrom_pretrainedÚmodelÚ	tokenizerZ	DataFrameZvocabÚitemsZdfr   Úmapr   r   ÚtolistÚexclude_ids)r
   r   r   r   r   r   r   r   Úpdr   r   r   r   r   Ú
load_model   s$    ų	zWangChanGLM.load_modelé   įffffffî?įÍĖĖĖĖĖė?é2   é   į      ð?)	r   Úmax_new_tokensÚtop_pÚtemperatureÚtop_kÚno_repeat_ngram_sizeÚ	typical_pÚ	thai_onlyÚskip_special_tokensc
                 C   sŪ   | j |dd}
tj| j| jdV |rJ| jj|
d || j|||||d}n| jj|
d ||||||d}W d   n1 s|0    Y  | j j|d t	|
d d d |	d	S )
a
  
        Generate Instruct
        
        :param str text: text
        :param int max_new_tokens: maximum number of new tokens
        :param float top_p: top p
        :param float temperature: temperature
        :param int top_k: top k
        :param int no_repeat_ngram_size: do not repeat ngram size
        :param float typical_p: typical p
        :param bool thai_only: Thai only
        :param bool skip_special_tokens: skip special tokens
        :return: the answer from Instruct
        :rtype: str
        Úpt)Zreturn_tensors)Zdevice_typeZdtypeÚ	input_ids)r5   r,   Zbegin_suppress_tokensr0   r/   r-   r1   r.   )r5   r,   r0   r/   r-   r1   r.   Nr   )r3   )
r   ÚtorchZautocastr   r   r   Úgenerater#   ÚdecodeÚlen)r
   r   r,   r-   r.   r/   r0   r1   r2   r3   ÚbatchZoutput_tokensr   r   r   Úgen_instruct@   s.    ũø$
zWangChanGLM.gen_instructNé   )	ÚinstructÚcontextr.   r-   r/   r0   r1   r2   r3   c                 C   sV   |dv r | j d  |ddĄ}n| j d  ||dĄ}| j||||||||	|
d	}|S )uĘ  
        Generate Instruct
        
        :param str instruct: Instruct
        :param str context: context
        :param int max_new_tokens: maximum number of new tokens
        :param float top_p: top p
        :param float temperature: temperature
        :param int top_k: top k
        :param int no_repeat_ngram_size: do not repeat ngram size
        :param float typical_p: typical p
        :param bool thai_only: Thai only
        :param bool skip_special_tokens: skip special tokens
        :return: the answer from Instruct
        :rtype: str

        :Example:
        ::

                from pythainlp.generate.wangchanglm import WangChanGLM
                import torch

                model = WangChanGLM()

                model.load_model(device="cpu",torch_dtype=torch.bfloat16)

                print(model.instruct_generate(instruct="āļāļ­āļ§āļīāļāļĩāļĨāļāļāđāļģāļŦāļāļąāļ"))
                # output: āļĨāļāļāđāđāļēāļŦāļāļąāļāđāļŦāđāđāļāđāļāļĨ āļāđāļ­āļāļāđāļēāļ­āļĒāđāļēāļāļāđāļ­āļĒāđāļāđāļāļāđāļ­āļĒāđāļ
                # āļāļĢāļąāļāđāļāļĨāļĩāđāļĒāļāļāļĪāļāļīāļāļĢāļĢāļĄāļāļēāļĢāļāļīāļāļ­āļēāļŦāļēāļĢ
                # āļ­āļ­āļāļāđāļēāļĨāļąāļāļāļēāļĒāļ­āļĒāđāļēāļāļŠāļĄāđāđāļēāđāļŠāļĄāļ­
                # āđāļĨāļ°āļāļąāļāļāđāļ­āļāđāļŦāđāđāļāļĩāļĒāļāļāļ­
                # āļāļĩāđāļŠāđāļēāļāļąāļāļāļ§āļĢāļŦāļĨāļĩāļāđāļĨāļĩāđāļĒāļāļ­āļēāļŦāļēāļĢāļāļĩāđāļĄāļĩāđāļāļĨāļ­āļĢāļĩāđāļŠāļđāļ
                # āđāļāđāļ āļ­āļēāļŦāļēāļĢāļāļ­āļ āļ­āļēāļŦāļēāļĢāļĄāļąāļ āļ­āļēāļŦāļēāļĢāļāļĩāđāļĄāļĩāļāđāđāļēāļāļēāļĨāļŠāļđāļ
                # āđāļĨāļ°āđāļāļĢāļ·āđāļ­āļāļāļ·āđāļĄāđāļ­āļĨāļāļ­āļŪāļ­āļĨāđ

        )NÚ r   r?   )ZinstructionÚinputr   )r,   r-   r/   r.   r0   r1   r2   r3   )r	   Ú
format_mapr;   )r
   r=   r>   r,   r.   r-   r/   r0   r1   r2   r3   ÚpromptÚresultr   r   r   Úinstruct_generateu   s&    1
ĸ
ĸũzWangChanGLM.instruct_generate)r&   r'   r(   r)   r*   r+   TT)	Nr&   r(   r'   r)   r*   r<   TT)Ú__name__Ú
__module__Ú__qualname__r   Ústrr   r   r6   Úfloat16r%   ÚintÚfloatr;   rD   r   r   r   r   r      sp   øø*        öö8         õõr   )r   r6   r   r   r   r   r   Ú<module>   s   