o
    ߥi                  
   @   s   d dl mZmZmZmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d	d
lmZ dededeeeef  defddZejeje
jdejeje
jdejeje
jdejeje
jdG dd deeeZdS )    )DictListOptionalTupleUnionN)LlamaForCausalLM)Models)
TorchModel)MODELS)
OutputKeys)Tasks   )MsModelMixinsystemtexthistory
max_lengthc                 C   s6  d|  d}||dddj }|  d}||dddj }|jd |jd  }	|	|kr5td|	 d	| d
}
g }|d d d D ]A\}}t|tsKJ t|tsRJ |  d|  d}||dddj }|	|jd  |krq n||
 }
|g| }|	|jd 7 }	q@||
|g}|g| |g }d
|tj|ddfS )Nz<s>[INST] <<SYS>>
z
<</SYS>>

Fpt)add_special_tokensreturn_tensorsz [/INST]zprepend prompt length z is bigger than max_length  z	 [/INST] z </s><s>[INST] r   )dim)		input_idsstripshapeRuntimeError
isinstancestrjointorchcat)r   r   r   r   	tokenizersystem_prompt
system_idstext_prompttext_idsprompt_lengthhistory_prompthistory_ids_listuserbotround_prompt	round_idsprompt_listprompt_ids_list r0   _/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/llama/text_generation.pyget_chat_prompt!   sJ   

r2   )module_namec                   @   s   e Zd ZdedefddZdS )LlamaForTextGenerationinputreturnc                 C   s  dd l }| |}d|vrd}n	|d }|d d|vr d}n	|d }|d d|vr0g }n| |d }|d d|vrDd|d< t||||d |d\}}	|	| j}
| j|
fi |}|d d |
jd	 d f }|j|d
ddd }| }|	||f t
j|t
j|iS )Nr   r   r   r   r   r   i   )r   r   r   r   r"   r   TF)skip_special_tokensclean_up_tokenization_spaces)copypopr2   todevicegenerater   batch_decoder   appendr   RESPONSEHISTORY)selfr5   r"   r9   
gen_kwargsr   r   r   prompt
prompt_idsr   generate_idsresponser0   r0   r1   chatP   sH   




zLlamaForTextGeneration.chatN)__name__
__module____qualname__r   rH   r0   r0   r0   r1   r4   J   s    r4   )typingr   r   r   r   r   r    transformers.models.llamar   modelscope.metainfor   modelscope.models.baser	   modelscope.models.builderr
   modelscope.outputsr   modelscope.utils.constantr   backboner   r   intr2   register_modulerH   llama2llamatext_generationr4   r0   r0   r0   r1   <module>   s"   
) 