
    9il              	       &   S SK r S SKJrJrJrJrJr  S SKrS SKJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJrJrJr  S SKJrJr  S S	KJr  S S
KJr  S SKJr  S SKJrJr  S SK J!r!J"r"  S SK#J$r$  S SK%J&r&  S SK'J(r(  \$" 5       r)/ SQr*\RV                  " \RX                  \RX                  S9 " S S\\&5      5       r-\RV                  " \R\                  \R^                  S9\RV                  " \R\                  \R`                  S9\RV                  " \R\                  \Rb                  S9\RV                  " \R\                  \R\                  S9 " S S\-5      5       5       5       5       r2\RV                  " \Rf                  SS9 " S S\5      5       r4\RV                  " \Rf                  SS9 " S S\5      5       r5\RV                  " \Rf                  SS9 " S S \5      5       r6\RV                  " \RX                  S!S9 " S" S#\5      5       r7\RV                  " \RX                  S$S9 " S% S&\5      5       r8\RV                  " \RX                  \Rr                  S9 " S' S(\-5      5       r:\RV                  " \Rf                  \Rv                  S9 " S) S*\5      5       r<g)+    N)AnyDictListOptionalUnion)GenerationConfig)snapshot_download)	Pipelines)Model)ModelOutputBase
OutputKeysTokenGeneratorOutput)PipelineTensor)	PIPELINES)Preprocessor)"remove_space_between_chinese_chars)	ModelFileTasks)Configread_config)
get_logger)PipelineStreamingOutputMixin)is_on_same_device)TextGenerationPipelineTextGenerationT5PipelineChatGLM6bTextGenerationPipeline!ChatGLM6bV2TextGenerationPipelineQWenChatPipelineQWenTextGenerationPipelineSeqGPTPipelineLlama2TaskPipeline)module_namec            	          ^  \ rS rSr     SS\\\4   S\\   S\S\4U 4S jjjr	S r
S\\\\4   \4   S	\\\4   4S
 jrS	\4S jrS	\4S jrS	\4S jrS\\\\4   \4   S	\\\4   4S jrSrU =r$ )r   #   modelpreprocessorconfig_filedevicec                   > [         T	U ]  " SUUUUUUR                  SS5      UR                  S0 5      S.UD6  [        U R                  [
        5      (       d   S[        R                   35       eUc2  [        R                  " U R                  R                  4SU0UD6U l        U R                  R                  5         UR                  SS5      U l        U R                  cP  [        U R                  S	5      (       a5  [        U R                  R                  5      nUR!                  S5      U l        U R                  c  S
U l        SU l        g)u  Use `model` and `preprocessor` to create a generation pipeline for prediction.

Args:
    model (str or Model): Supply either a local model dir which supported the text generation task,
    or a model id from the model hub, or a torch model instance.
    preprocessor (Preprocessor): An optional preprocessor instance, please make sure the preprocessor fits for
    the model if supplied.
    kwargs (dict, `optional`):
        Extra kwargs passed into the preprocessor's constructor.

Examples:
    >>> from modelscope.pipelines import pipeline
    >>> pipeline_ins = pipeline(task='text-generation',
    >>>    model='damo/nlp_palm2.0_text-generation_chinese-base')
    >>> sentence1 = '本文总结了十个可穿戴产品的设计原则，而这些原则，同样也是笔者认为是这个行业最吸引人的地方：'
    >>>     '1.为人们解决重复性问题；2.从人开始，而不是从机器开始；3.要引起注意，但不要刻意；4.提升用户能力，而不是取代'
    >>> print(pipeline_ins(sentence1))
    >>> # Or use the dict input:
    >>> print(pipeline_ins({'sentence': sentence1}))

    To view other examples please check tests/pipelines/test_text_generation.py.
compileFcompile_options)r&   r'   r(   r)   auto_collater+   r,   z,please check whether model config exists in Nfirst_sequencepostprocessor	model_dirdecode )super__init__pop
isinstancer&   r   r   CONFIGURATIONr   from_pretrainedr0   r'   evalr/   hasattrr   get
has_logged)
selfr&   r'   r(   r)   r-   r.   kwargscfg	__class__s
            q/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/pipelines/nlp/text_generation_pipeline.pyr4   TextGenerationPipeline.__init__'   s6   < 	 	%#%JJy%0"JJ'8"=	 	 $**e,, 	U:9;R;R:ST	U, , < <

$$!O5C!OGM!OD

#ZZ>%'$**k*J*Jdjj223C!$!9D%!)D    c                     0 U0 4$ Nr2   r=   pipeline_parameterss     rA   _sanitize_parameters+TextGenerationPipeline._sanitize_parameters^       &**rC   inputsreturnc                 |   [         R                  " 5           U R                  R                  " U40 UD6sS S S 5        $ ! [         ab  nU R
                  (       d   [        R                  SU S35        SU l        U R                  R                  " S0 UDUD6s S nAsS S S 5        $ S nAff = f! , (       d  f       g = f)Nz.When inputs are passed directly, the error is z,, which can be ignored if it runs correctly.Tr2   )torchno_gradr&   generateAttributeErrorr<   loggerwarning)r=   rK   forward_paramses       rA   forwardTextGenerationPipeline.forwarda   s    ]]_	Gzz**6D^D _ " GNN(()s +EEF '+DOzz**FVF~FF _G _s3   B->
B*AB%B*B-%B**B--
B;c                 R    U R                   R                  UR                  5       SS9$ )NTskip_special_tokensr'   r1   tolistr=   rK   s     rA   r1   TextGenerationPipeline.decodeo   s+      ''MMO ( 7 	7rC   c                 T    U R                   R                  UR                  5       5      $ rE   r[   r]   s     rA   sentence_piece%TextGenerationPipeline.sentence_pieces   s      ''88rC   c                     U R                   R                  UR                  5       5      nUR                  SS5      R                  SS5      R                  SS5      $ )Nz<q>z. z<mask>z</s> )r'   r1   r\   replace)r=   rK   decodeds      rA   robertaTextGenerationPipeline.robertav   sN    ##**6==?;ud+33H48::A'&":M	NrC   c                    [        U[        [        45      (       a  US   n[        U[        5      (       d  [	        UR
                  5      S:  a  US   n[        X R                  5      " U5      n[        U5      n[        R                  U0$ )zprocess the prediction results

Args:
    inputs (Dict[str, Any]): _description_

Returns:
    Dict[str, str]: the prediction results
	sequences   r   )r6   dictr   listlenshapegetattrr/   r   r   TEXT)r=   rK   postprocess_paramsre   texts        rA   postprocess"TextGenerationPipeline.postprocess{   st     ft_566K(Ffd##s6<<'81'<AYF$ 2 23F;1':&&rC   )r<   r/   r'   )NNgpuTsentence)__name__
__module____qualname____firstlineno__r   r   strr   r   r4   rH   r   r   r   rV   r1   r`   rf   r   rs   __static_attributes____classcell__r@   s   @rA   r   r   #   s     9=$($" *5 eSj)5 '55  "5  	5  5 n+GeDcNF$:; G%)#s(^G7 79 9N N
'%S&[(9(<)= #> '-1#s(^' 'rC   r   c                      ^  \ rS rSr  SS\\\4   S\\   4U 4S jjjr	S r
S\\\4   4U 4S jjrS\\\4   S\\\4   4S	 jrS
rU =r$ )r      r&   r'   c                 <  > [         TU ]  " X40 UD6  X0l        U R                  [	        U R
                  SS 5      S5      U l        U R                  [	        U R
                  SS 5      S5      U l        U R                  [	        U R
                  SS 5      S5      U l        g )Nr0   task_specific_params
min_length
max_length)	r3   r4   sub_task_parse_specific_model_paramsro   r&   r   r   r   )r=   r&   r'   r   r>   r@   s        rA   r4   !TextGenerationT5Pipeline.__init__   s    
 	77 $($E$EDJJT24J%L!;;DJJT2LB;;DJJT2LBrC   c                     Uc  g [        U5      nUR                  SU 35      nUc:  [        [        R                  R	                  US5      5      nUR                  U5      nU$ )Nzmodel.zconfig.json)r   safe_getospathjoin)r=   r0   keyr?   paramss        rA   r   5TextGenerationT5Pipeline._parse_specific_model_params   sZ    !),sen->%bggll9m&LMC\\#&FrC   rL   c                   > [        U[        5      (       d  [        S[        U5       35      eU R                  b  U R
                  =(       d     U R                  R                  R                  nX0R                  ;   aa  U R                  R                  R                  U R                  U   5        SU R                  U   ;   a  U R                  U   R                  U-   n[        TU ]0  " U40 UD6$ )NzNot supported input type: prefix)r6   r{   
ValueErrortyper   r   r&   pipelineconfigupdater   r3   
preprocess)r=   rK   preprocess_paramsr   r@   s       rA   r   #TextGenerationT5Pipeline.preprocess   s    &#&&9$v,HII$$0}}@

(;(;(@(@H444

!!(()B)B8)LMt88BB!66x@GG&PFw!&>,=>>rC   rK   c                 *   UR                  SU R                  5      nUR                  SU R                  5      nUb  X2S'   Ub  XBS'   [        R                  " 5          U R
                  R                  " S0 UDUD6sS S S 5        $ ! , (       d  f       g = f)Nr   r   r2   )r;   r   r   rN   rO   r&   rP   )r=   rK   rT   r   r   s        rA   rV    TextGenerationT5Pipeline.forward   sy     $''dooF
#''dooF
!+5<(!+5<(]]_::&&BB>B __s   B
B)r   r   r   r   )NN)rw   rx   ry   rz   r   r   r{   r   r   r4   r   r   r   r   rV   r|   r}   r~   s   @rA   r   r      s}     9=BeSj)B'5B B	?c3h ?Cd38n C%)#s(^C CrC   r   zchatglm6b-text-generation)	group_keyr#   c                      ^  \ rS rSr  SS\\\4   4U 4S jjjrS rS\	\\
4   4S jrS\	S\	\\
4   4S jrS\	\\
4   4S	 jrS
rU =r$ )r      r&   c                   > SSK Jn  [        U[        5      (       a  [        R
                  R                  U5      (       d  [        U5      OUnUR                  U5      R                  5       n[        R                  R                  5       (       a  UR                  5       nUb  UR                  U5      nU(       a  UR                  5       nXl        U R                  R!                  5         ["        TU ]H  " SSU0UD6  g )Nr   )ChatGLMForConditionalGenerationr&   r2   )-modelscope.models.nlp.chatglm.text_generationr   r6   r{   r   r   existsr	   r8   halfrN   cudais_availablequantizebfloat16r&   r9   r3   r4   )r=   r&   quantization_bituse_bf16r>   r   r0   r@   s          rA   r4   (ChatGLM6bTextGenerationPipeline.__init__   s    
	-eS!! ggnnU33 *9> 3CC46 zz&&((

'NN#34ENN$E


/u//rC   c                     0 U0 4$ rE   r2   rF   s     rA   rH   4ChatGLM6bTextGenerationPipeline._sanitize_parameters   rJ   rC   rL   c                     U$ rE   r2   r=   rK   r   s      rA   r   *ChatGLM6bTextGenerationPipeline.preprocess       rC   rK   c                 Z    UR                  U5        U R                  R                  U5      $ rE   )r   r&   chatr=   rK   rT   s      rA   rV   'ChatGLM6bTextGenerationPipeline.forward   s!    n%zzv&&rC   c                     U$ rE   r2   r=   inputr>   s      rA   rs   +ChatGLM6bTextGenerationPipeline.postprocess       rC   )r&   )NFrw   rx   ry   rz   r   r   r{   r4   rH   r   r   r   rV   rs   r|   r}   r~   s   @rA   r   r      ss     #'0eSj)0 0.+c3h 'd 'c3h '
d38n  rC   r   zchatglm2_6b-text-generationc                      ^  \ rS rSr   SS\\\4   S\\   4U 4S jjjr	S r
S\\\4   4S jrS\S\\\4   4S	 jrS\\\4   4S
 jrSrU =r$ )r      r&   trust_remote_codec                   > SSK Jn  UR                  SS5      n[        U[        5      (       a  UR                  SS 5      n[
        R                  R                  U5      (       d
  [        UUS9OUn	S n
UR                  S5      (       d  UR                  S5      (       a  SS0n
UR                  S	U
5      nS nU(       a  [        R                  nUR                  S
U5      n[        R                  " U	UUUS9nOcUR                  S5      (       d  UR                  S5      (       a   [        U5      (       a  UR                  5         U(       a  UR                  5         Ub  UR!                  U5      nXl        U R"                  R%                  5         UR                  U R"                  R&                  US9U l        [*        TU ]X  " SSU0UD6  g )Nr   AutoTokenizerr)   ru   revision)r   r   rc   
device_maptorch_dtype)r   r   r   r   r&   r2   )
modelscoper   r;   r6   r{   r   r   r   r	   
startswithrN   r   r   r8   r   r   r   r&   r9   r0   	tokenizerr3   r4   )r=   r&   r   r   r   r>   r   r)   r   r0   default_device_mapr   default_torch_dtyper   r@   s                 rA   r4   *ChatGLM6bV2TextGenerationPipeline.__init__   s    	-jj51eS!!zz*d3H +-''..*?*? *!#EJ  "&  ''6+<+<V+D+D&(!W"L2DEJ"&&+nn# **]4GHK))"3%'	)E ""5))V->->v-F-F)%00

 'NN#34E


&66JJ  4E 7 G 	/u//rC   c                     0 U0 4$ rE   r2   rF   s     rA   rH   6ChatGLM6bV2TextGenerationPipeline._sanitize_parameters&  rJ   rC   rL   c                     U$ rE   r2   r   s      rA   r   ,ChatGLM6bV2TextGenerationPipeline.preprocess)  r   rC   rK   c                 n    UR                  U5        U R                  R                  XR                  5      $ rE   )r   r&   r   r   r   s      rA   rV   )ChatGLM6bV2TextGenerationPipeline.forward-  s%    n%zzv~~66rC   c                     U$ rE   r2   r   s      rA   rs   -ChatGLM6bV2TextGenerationPipeline.postprocess2  r   rC   r&   r   )NFN)rw   rx   ry   rz   r   r   r{   r   boolr4   rH   r   r   r   rV   rs   r|   r}   r~   s   @rA   r   r      s     #'59	(0eSj)(0 %-TN	(0 (0T+c3h 7d 7c3h 7
d38n  rC   r   z	qwen-chatc                      ^  \ rS rSrS\\\4   4U 4S jjrS rS\	\\
4   4S jrS\\	\4   S\	\\
4   4S jrS\	\\
4   4S	 jrS
rU =r$ )r   i6  r&   c                 H  > SSK JnJn  UR                  S[        R
                  5      nUR                  SS5      nUR                  SS5      nUR                  SS	5      nUR                  S
S 5      n	U(       am  [        [        R                  R                  5       S   S-  5      S-
   S3n
[        R                  R                  5       n[        U5       Vs0 s H  oU
_M     n
nOS n
US:X  d  U[        R
                  :X  a  SnOSn[        U[        5      (       a_  UR                  XU	S9U l        UR                  UUUU	US9R!                  5       U l        [$        R                  " UU	S9U R"                  l        [(        TU ]T  " SSU R"                  0UD6  SU l        g s  snf )Nr   AutoModelForCausalLMr   r   r   autouse_max_memoryFmodel_revisionzv.1.0.5r      @   GBbf16Tr   r   )r   r   r   fp16r   r&   r2   )r   r   r   r;   rN   r   r5   intr   mem_get_infodevice_countranger6   r{   r8   r   r9   r&   r   generation_configr3   r4   _model_preparer=   r&   r>   r   r   r   r   r   r   r   
max_memoryn_gpusir   r@   s                 rA   r4   QWenChatPipeline.__init__9  s   Bjj?ZZf5
$4e<::.	:"JJ':DA

 7 7 9! <y HIAMNbQJZZ,,.F16v?AZ-J?JJ& K5>>$ADDeS!!*::<M ; ODN-==%!"3 > 
  46 J ,<+K+K"3,5DJJ( 	4tzz4V4"/ @s   Fc                     0 U0 4$ rE   r2   rF   s     rA   rH   %QWenChatPipeline._sanitize_parameters]  rJ   rC   rL   c                     U$ rE   r2   r   s      rA   r   QWenChatPipeline.preprocess`  r   rC   rK   c                 L   [        U[        5      (       a%  UR                  SS 5      nUR                  SS 5      nOUnUR                  SS 5      nUR                  SS5      nUR                  SS5      nU R                  R	                  U R
                  X4UU5      nUS   US   S	.$ )
Nrr   historysystemzYou are a helpful assistant.append_historyTr   rj   )responser   )r6   r   r;   r&   r   r   )r=   rK   rT   rr   r   r   r   ress           rA   rV   QWenChatPipeline.forwardd  s    fd##::fd+DjjD1GD$((D9G##H.LM'++,<dCjjoodnndV,.Fs1v66rC   c                     U$ rE   r2   r   s      rA   rs   QWenChatPipeline.postprocesss  r   rC   r   r&   r   r   r~   s   @rA   r   r   6  sp    "#eE3J/ "#H+c3h 7eD#I. 7%)#s(^7d38n  rC   r   zqwen-text-generationc                      ^  \ rS rSrS\\\4   4U 4S jjrS rS\	\\
4   4S jrS\S\	\\
4   4S jrS\	\\
4   4S	 jrS
rU =r$ )r    iw  r&   c                   > SSK JnJn  UR                  S[        R
                  5      nUR                  SS5      nUR                  SS5      nUR                  SS	5      nUR                  S
S 5      n	U(       am  [        [        R                  R                  5       S   S-  5      S-
   S3n
[        R                  R                  5       n[        U5       Vs0 s H  oU
_M     n
nOS n
US:X  d  U[        R
                  :X  a  SnOSn[        U[        5      (       aa  UR                  UUUU	US9R                  5       U l        UR                  XU	S9U l        [$        R                  " U5      U R                   l        OXl        UR                  SS 5      U l        [(        TU ]T  " SSU R                   0UD6  SU l        g s  snf )Nr   r   r   r   r   r   Fr   zv.1.0.4r   r   r   r   r   T)r   r   r   r   r   r   r&   r2   )r   r   r   r;   rN   r   r5   r   r   r   r   r   r6   r{   r8   r9   r&   r   r   r   r3   r4   r   r   s                 rA   r4   #QWenTextGenerationPipeline.__init__{  s   Bjj?ZZf5
$4e<::.	:"JJ':DA

 7 7 9! <y HIAMNbQJZZ,,.F16v?AZ-J?JJ& K5>>$ADDeS!!-==%!"3 > 
  46 J +::<M ; ODN+;+K+K,DJJ( J#ZZT:DN4tzz4V4"3 @s   F>c                     0 U0 4$ rE   r2   rF   s     rA   rH   /QWenTextGenerationPipeline._sanitize_parameters  rJ   rC   rL   c                     U$ rE   r2   r   s      rA   r   %QWenTextGenerationPipeline.preprocess  r   rC   rK   c                     U R                  USS9R                  S5      n[        R                  U R                   R	                  U R
                  R                  " S0 UD6R                  5       S   SS90$ )Npt)return_tensorszcuda:0r   TrY   r2   )r   tor   rp   r1   r&   rP   cpur   s      rA   rV   "QWenTextGenerationPipeline.forward  sm    t<??IOONN!!

##-f-113A6$( " *
 	
rC   c                     U$ rE   r2   r   s      rA   rs   &QWenTextGenerationPipeline.postprocess  r   rC   r   r   r~   s   @rA   r    r    w  sf    $#eE3J/ $#L+c3h 
c 
S#X 
d38n  rC   r    seqgptc                      ^  \ rS rSrS\\\4   4U 4S jjrS rS\	\\
4   4S jrS\S\	\\
4   4S jrS\	\\
4   4S	 jrS
rU =r$ )r!   i  r&   c                 Z  > SSK Jn  [        U[        5      (       aG  [        R
                  R                  U5      (       d  [        U5      OUn[        R                  " U5      nXl
        U R                  R                  5         UR                  W5      U l        [        TU ]8  " SSU0UD6  g )Nr   r   r&   r2   )r   r   r6   r{   r   r   r   r	   r   r8   r&   r9   r   r3   r4   )r=   r&   r>   r   r0   r@   s        rA   r4   SeqGPTPipeline.__init__  s    ,eS!! ggnnU33 *9> )))4E


&66yA/u//rC   c                     0 U0 4$ rE   r2   rF   s     rA   rH   #SeqGPTPipeline._sanitize_parameters  rJ   rC   rL   c                     U$ rE   r2   r   s      rA   r   SeqGPTPipeline.preprocess  r   rC   promptc                 `   U R                  XR                  SS5      -   SSSSS9nUR                  R                  U R                  R
                  5      nU R                  R                  USSS	S
9nU R                   R                  USS9nUS   nU[        U5      S  n[        R                  U0$ )N	gen_tokenrc   r  Ti   )r  padding
truncationr      F   )	num_beams	do_samplemax_new_tokensrY   r   )r   r;   	input_idsr  r&   r)   rP   batch_decoderm   r   rp   )r=   r  rT   r  outputsdecoded_sentencesdecoded_sentences          rA   rV   SeqGPTPipeline.forward  s     NN''R88 # 	 ''**4::+<+<=	**%%eC & I NN77 8 /,Q/+CKL9!122rC   c                     U$ rE   r2   r   s      rA   rs   SeqGPTPipeline.postprocess  r   rC   r   r   r~   s   @rA   r!   r!     se    0eE3J/ 0+c3h 3c 3S#X 3&d38n  rC   r!   c                      ^  \ rS rSr    SS\\\4   S\S\S\4U 4S jjjrS\	\\
4   4S jrS	 r        SS
\S\S\S\S\S\S\S\S\S\	\\
4   4S jjrS\	\\
4   4S jrSrU =r$ )r"   i  r&   r'   r(   r)   c                    > [         R                  " US[        R                  S9U l        SSKJn  UR                  U5      U l        [        TU ]$  " SSU R                  0UD6  g)u  Use `model` and `preprocessor` to create a generation pipeline for prediction.

Args:
    model (str or Model): Supply either a local model dir which supported the text generation task,
    or a model id from the model hub, or a torch model instance.
    preprocessor (Preprocessor): An optional preprocessor instance, please make sure the preprocessor fits for
    the model if supplied.
    kwargs (dict, `optional`):
        Extra kwargs passed into the preprocessor's constructor.
Examples:
    >>> from modelscope.utils.constant import Tasks
    >>> import torch
    >>> from modelscope.pipelines import pipeline
    >>> from modelscope import snapshot_download, Model
    >>> model_dir = snapshot_download("modelscope/Llama-2-13b-chat-ms",
    >>>     ignore_file_pattern = [r'\w+\.safetensors'])
    >>> pipe = pipeline(task=Tasks.text_generation, model=model_dir, device_map='auto',
    >>>     torch_dtype=torch.float16)
    >>> inputs="咖啡的作用是什么？"
    >>> result = pipe(inputs,max_length=200, do_sample=True, top_p=0.85,
    >>>     temperature=1.0, repetition_penalty=1., eos_token_id=2, bos_token_id=1, pad_token_id=0)
    >>> print(result['text'])

    To view other examples please check tests/pipelines/test_llama2_text_generation_pipeline.py.
r   r   r   r   Llama2Tokenizerr&   Nr2   )
r   r8   rN   float16r&   modelscope.models.nlp.llama2r(  r   r3   r4   )	r=   r&   r'   r(   r)   r-   r>   r(  r@   s	           rA   r4   Llama2TaskPipeline.__init__  sR    @ **f%--A
@(88?4tzz4V4rC   rL   c                     U$ rE   r2   r   s      rA   r   Llama2TaskPipeline.preprocess  r   rC   c                     0 U0 4$ rE   r2   rF   s     rA   rH   'Llama2TaskPipeline._sanitize_parameters  rJ   rC   rK   r   r  top_ptemperaturerepetition_penaltyeos_token_idbos_token_idpad_token_idc
                     0 nU R                  USSS9nU R                  R                  " UR                  R	                  S5      4UUUUUUUU	S.U
D6nU R                   R                  USSS9S   nXS	'   U$ )
NFr  )add_special_tokensr  r   )r   r  r0  r1  r2  r3  r4  r5  T)rZ   clean_up_tokenization_spacesr   rr   )r   r&   rP   r  r  r  )r=   rK   r   r  r0  r1  r2  r3  r4  r5  rT   outputgenerate_idsouts                 rA   rV   Llama2TaskPipeline.forward  s     uT   Czz**'
!#1%%%
 
 nn)) $). * 0 123 vrC   c                     U$ rE   r2   r   s      rA   rs   Llama2TaskPipeline.postprocess7  r   rC   r   NNru   T)   F?333333?      ?r   rj   r   )rw   rx   ry   rz   r   r   r{   r   r4   r   r   r   rH   r   r   floatrV   rs   r|   r}   r~   s   @rA   r"   r"     s    /3$($"$5eSj)$5+$5 "$5 	$5 $5Lc3h +
 #'"'"%(,.$%$%$%   	
 # %* " " " &*#s(^Bd38n  rC   r"   c                      ^  \ rS rSrSr    S#S\\\4   S\S\S\S\	S\
\\4   4U 4S	 jjjrS\
\\4   4S
 jrS rSSSSSSSSS/ 4
S\S\S\	S\S\S\S\S\S\S\S\S\
\\4   4S  jjrS\
\\4   4S! jrS"rU =r$ )$Llama2chatTaskPipelinei;  a  Use `model` and `preprocessor` to create a generation pipeline for prediction.

Args:
    model (str or Model): Supply either a local model dir which supported the text generation task,
    or a model id from the model hub, or a torch model instance.
    preprocessor (Preprocessor): An optional preprocessor instance, please make sure the preprocessor fits for
    the model if supplied.
    kwargs (dict, `optional`):
        Extra kwargs passed into the preprocessor's constructor.
Examples:
    >>> from modelscope.utils.constant import Tasks
    >>> import torch
    >>> from modelscope.pipelines import pipeline
    >>> from modelscope import Model
    >>> pipe = pipeline(task=Tasks.chat, model="modelscope/Llama-2-7b-chat-ms", device_map='auto',
    >>> torch_dtype=torch.float16, ignore_file_pattern = [r'.+\.bin$'], model_revision='v1.0.5')
    >>> inputs = 'Where is the capital of Zhejiang?'
    >>> result = pipe(inputs,max_length=512, do_sample=False, top_p=0.9,
    >>> temperature=0.6, repetition_penalty=1., eos_token_id=2, bos_token_id=1, pad_token_id=0)
    >>> print(result['response'])
    >>> inputs = 'What are the interesting places there?'
    >>> result = pipe(inputs,max_length=512, do_sample=False, top_p=0.9,
    >>> temperature=0.6, repetition_penalty=1., eos_token_id=2, bos_token_id=1,
    >>> pad_token_id=0, history=result['history'])
    >>> print(result['response'])
    >>> inputs = 'What are the company there?'
    >>> history_demo = [('Where is the capital of Zhejiang?',
    >>> 'Thank you for asking! The capital of Zhejiang Province is Hangzhou.')]
    >>> result = pipe(inputs,max_length=512, do_sample=False, top_p=0.9,
    >>> temperature=0.6, repetition_penalty=1., eos_token_id=2, bos_token_id=1,
    >>> pad_token_id=0, history=history_demo)
    >>> print(result['response'])

    To view other examples please check tests/pipelines/test_llama2_text_generation_pipeline.py.
r&   r'   r(   r)   r-   rL   c                    > UR                  SS 5      nUR                  SS 5      n[        R                  " XUS9U l        SSKJn	  U	R                  U5      U l        [        T
U ]   " SSU R                  0UD6  g )Nr   r   r&  r   r'  r&   r2   )	r;   r   r8   r&   r*  r(  r   r3   r4   )r=   r&   r'   r(   r)   r-   r>   r   r   r(  r@   s             rA   r4   Llama2chatTaskPipeline.__init__b  sk     ZZd3
jj5**kC
@(88?4tzz4V4rC   c                     U$ rE   r2   r   s      rA   r   !Llama2chatTaskPipeline.preprocessq  r   rC   c                     0 U0 4$ rE   r2   rF   s     rA   rH   +Llama2chatTaskPipeline._sanitize_parameterst  rJ   rC   r@  FrA  rB  rC  r   rj   r   zyou are a helpful assistant!rK   r   r  r0  r1  r2  r3  r4  r5  r   r   c                     UnXS'   X-S'   X=S'   XMS'   X]S'   XmS'   X}S'   XS'   XS	'   XS
'   XS'   U R                   R                  XR                  5      nU$ )Nrr   r   r  r0  r1  r2  r3  r4  r5  r   r   )r&   r   r   )r=   rK   r   r  r0  r1  r2  r3  r4  r5  r   r   rT   inputs_dictr9  s                  rA   rV   Llama2chatTaskPipeline.forwardw  s}     %$F$.L!#,K $G%0M",>()&2N#&2N#&2N# &H!(Inn=rC   c                     U$ rE   r2   r   s      rA   rs   "Llama2chatTaskPipeline.postprocess  r   rC   r   r?  )rw   rx   ry   rz   __doc__r   r   r{   r   r   r   r   r4   r   rH   r   rD  r   rV   rs   r|   r}   r~   s   @rA   rF  rF  ;  s=   "L /3$($&*5eSj)5+5 "5 	5
  $5 #38n5 5c3h +
 #'"'"%(,.$%$%$%< "   	
 # %* " " "   &*#s(^:d38n  rC   rF  )=r   typingr   r   r   r   r   rN   transformersr   r   r	   modelscope.metainfor
   modelscope.models.baser   modelscope.outputsr   r   r   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.chinese_utilsr   modelscope.utils.constantr   r   modelscope.utils.hubr   r   modelscope.utils.loggerr   !modelscope.utils.streaming_outputr   modelscope.utils.torch_utilsr   rR   __all__register_moduletext_generationr   text2text_generationtranslation_en_to_detranslation_en_to_rotranslation_en_to_frr   r   r   r   r   r    r!   llama2_text_generation_pipeliner"   $llama2_text_generation_chat_pipelinerF  r2   rC   rA   <module>rj     s   
 3 3  ) ( ) (6 6 6 2 1 M 6 4 . J :	 	y'@'@Bg'X'C g'Bg'T 	I,J,JL
	I,J,JL
	I,J,JL
	I,J,JL3C5 3CLLLL3Cl jj&AC&h &C&R jj&CE9 9E9x UZZ[I=x = J=@ ##1GI: :I:z ##;*X *;*Z 	99;P/ P;Pf 	JJIJJLXX XLXrC   