/mheB ddlZddlmZddlZddlmZmZmZddlZddl Z GddZ ejfdZ Gdde Z Gd d e ZGd d e Zed k(redddddddZe dZyy)N)Prompt) one_paragraphfirst_sentencemake_asceZdZdZddidddddfdeddfdZdefdZd Zd Z d Z d Z d Z dZ dZedZiifdedefdZdZddfd"dZdZdZd#d"dZdZdddfdZd Zd$d!Zy)%ModuleNF prompt_makerreturnc x||_d|_|j|||_d|_g|_i|_d|_||_||_ ||_ t|_ ||_ d|_i|_t j"|_t xj"dz c_d|_d|_d|_y)NTFr r) self_promptuse_head_prompt connect_tor last_message destinations conditionshead_keyparallel iterativemerger head_processmax_turn multi_process output_condr module_countcountif_add_output_to_headturnsend) selfr pipelinerrrr output_asrs A/Users/shenjiajun/Desktop/2code/2code/citekit/cite_modules/LLM.py__init__zModule.__init__ s&# !(   " )  "(( q %*" cr|jr|jd|jdSd|jdS)N-[]zUnknown-type module-[) model_typerr"s r%__str__zModule.__str__"s8 ??oo&b A6 6*4::,a8 8r'cLtd|ddddd}|dk(r>|jj|jjd}|j}||dS|dk(rdt |j d iS|d vr||}td |t||Std |d )Nzget_json_config:rprompt destinationr)max turnr/r0 global prompttemplate components) prompt_info self_infor)r1r2zgetting the config:zget_json_config for  is not implemented) printr r4r5rstrrgetattrNotImplementedError)r"configavaliable_mappingr6r7s r%get_json_configzModule.get_json_config(s  ("('   X  --66"//::K((I +& } $s4#4#4Q#78 3 3&v.F ' /4( (%( Z1~' A/ A TU@WX + +!$"3"34 1q6S--%%,,Q/*   x    [  5 z !'DM% F8;N&OP Pr'cyrAr,s r% end_multizModule.end_multijsr'c6t|tsJ||_yrA) isinstanceboolr)r"uses r%set_use_head_promptzModule.set_use_head_promptms#d###"r'c d|_d|_yNFr)r!r r,s r%resetz Module.resetqs r'cB|rg|_||_yd|_||_y)Nr)rr)r" bool_values r%change_to_multi_processzModule.change_to_multi_processus)  "D (!#D 'r'c|jSrA)rr,s r%get_use_head_promptzModule.get_use_head_prompt{s###r' head_promptdynamic_promptctrA)r<)r"rcrds r%generatezModule.generates!!r'cb|jD] }|j|d}||s|cSy)N conditionrr)r"r0conds r%sendz Module.sends<,, #K??;/ zModule.r'c|SrArTxs r%rnzModule.s_`r'c||d|j|<|g|jz|_|j|jy)N)rhrE)rrrr#)r"r0rhrEs r%rLzModule.set_targets=5>Tc'd $(MD,=,==t}}-r'c g|_i|_yrArir,s r%clear_destinationzModule.clear_destinationsr'cJ|jr|js|j|jjj vrA|jjj |j|j|iy|jj|jxxdz cc<|jj|jxx|j|z cc<y|j||jj|j<yyN rhead_subrr#headkeysrKrr"outputss r%add_output_to_headzModule.add_output_to_head  % %==== (:(:(?(?(AAMM&&--t}}d>O>OPW>X.YZMM&&t}}5=5MM&&t}}59J9J79SS5484E4Eg4N ""4==1 &r'cN||_|r|jj|yyrA)r#moduleappend)r"r#s r%rzModule.connect_tos#   OO " "4 ( r'cd}|jjD]\}}||s|s|js9|jjj |d|j nG|jjj |ddj|j d}|dsd|_y)NFrErTr!) ritemsrr#outputrrjoinr!r"outedrj post_and_ends r%rz Module.outputs"&"2"2"8"8": % D,Dz:: ,,334SLAR4STXTeTe4fg ,,334SLAR4STVT[T[\`\m\mTn4op E& $DH %r'cyrmrTr,s r%rnzModule.ror'c|SrArTrqs r%rnzModule.ar'Tc(||d|j|<yN)rEr!rr"rjrEr!s r% set_outputzModule.set_output5Dc!Rr'c|SrArTr,s r%get_first_modulezModule.get_first_modules r'cBd|_||_||_|r||_yyrmrrrzrr"datakeyrHprocesss r%rOzModule.add_to_head(%)"   'D  r')r NrAFN)__name__ __module__ __qualname__rrr&r:r-r?rBrKrUrZr]r`propertyrbdictrfrkrLrurrrrrrOrTr'r%rr sL-1dRTbgqvCDRVchFmq,99 Z<!QB#( $$,.b"D"t"2CV`.  O) %!2ZW[S(r'rcddlm}m}|j||d}|j|}|j ||fSNr)AutoModelForCausalLM AutoTokenizerauto) torch_dtype device_map transformersrrfrom_pretrainedeval)model_name_or_pathdtyperrmodel tokenizers r% load_modelrsJ@ 0 0 1 E --.@AI JJL ) r'ceZdZdZddddiddddddddddddd fd ed dffd Zd Zd efdZd efdZ e jfdZ dZ dZiifdedefdZdZdZdddfdZdZddZxZS)LLM GeneratorNcpu?r FTAnswerdocsr r c2t||||| ||| |_|r||_n fd|_|r||_||_d|_||_t|_ | |_ | r||_ |rd|jvri| s*td|j|\|_|_n.td| j| j c|_|_||_||_nt'j(d|_i|_d|_d|_y) N)rc |iSrArT)rrr$s r%rnzLLM.__init__..s i]r'Fgptzloading model...zsharing model...OPENAI_API_KEYr)superr&rrE model_namestoprnoisyrr auto_cite cite_fromlowerr9rrr temperaturedeviceosgetenv openai_keyrr token_used)r"rr r#rErrrrrshare_model_withrrrrrrr$auto_cite_from __class__s ` r%r&z LLM.__init__s h{IuYab  #2D #:D #DO " )" +DN EKKM)',-151G.DJ,-1A1G1GIYIcIc.DJ#. $ "$)),<"=%*"r'c.d|_d|_d|_yr\)r!r rr,s r%r]z LLM.resets r'cT|jr|jd|jdSy)Nr)r*z unknown model)rrr,s r%r-z LLM.__str__s( ??oo&b A6 6"r'c |jd|ddj|jDcgc] }t|c}dgzzScc}w)Nz | | V /r)r rrr:)r"dess r%__repr__z LLM.__repr__sX$$%[kBCHHbfbsbsMt[^cRUhMtv~uMEAA BMtsA cddlm}m}|j||d}|j|}|j ||fSrr)r"rrrrrrs r%rzLLM.load_modelsJD$445 "112DE  ir'c ||_d|_yrm)rr)r"keys r%set_citez LLM.set_citesr'cd|jjvrddl}|j|_dddd|dg}|j j |j|d|j}|xj|d d |d d zz c_|d dd dS|j|dj|jj}|jgn |j}|jjdi|dd|jd}|xjt|dz c_|jj!|d|dj#ddd}t%|S)Nrrsystemz1You are a good helper who follow the instructions)rolecontentuseri)rmessages max_tokensrusagecompletion_tokens prompt_tokenschoicesmessagerpt)return_tensorsT) do_samplemax_new_tokensr input_idsr )skip_special_tokensrT)rropenairapi_keyChatCompletioncreaterrrtorrrfrlendecodesizerr9)r"r/rresponseinputsrr~s r%generate_contentzLLM.generate_content s DOO))+ + !__FN% SU#7F ,,33ooyy 4H OOx01DEQXHYZiHjj jOI&q))4Y? ?^^F4^@CCDJJDUDUVF*2 D)djj)) $%("&"2"2 G OOs71: .Onn++GAJvk7J7O7OPQ7R7S,Tjn+oG ) )r'rcrdc|jr|j||j|}n|j|j|}|jrt dt |d|d|xj dz c_|j|}|jrt dt ||jr"|ji||j||}|jr|jj|n||_ |j||j}|j |jkDrd|_||j"vr|j"|d|S|j%|S)Nz prompt to z: z r zOUTPUT:TrE)rr rrr9r:r rrcite_from_promptrrrrrkrr!rrE)r"rcrdr/r~r0s r%rfz LLM.generate/sQ   &&{43C3CNSF&&t'7'7GF :: Js4yk-fV < a ''/ :: )  'N >>++,_{,_T=M=M,_P^,_`ghG       $ $W - 'D  (iik :: %DH $// )B4??;/0AB7K K''0 0r'cJ|jr|js|j|jjj vrA|jjj |j|j|iy|jj|jxxdz cc<|jj|jxx|j|z cc<y|j||jj|j<yyrwryr}s r%rzLLM.add_output_to_headQrr'cd}|jjD]\}}||s|s|jsE|js9|jj j |d|jnG|jj j |ddj|jd}|dsd|_ y)NFrE Tr!) rrrrr#rrrrr!rs r%rz LLM.output\s"&"2"2"8"8": % D,Dz::dnn ,,334SLAR4STXTeTe4fg ,,334SLAR4STWT\T\]a]n]nTo4pq E& $DH %r'cyrmrTr,s r%rnz LLM.iror'c|SrArTrqs r%rnz LLM.irr'c(||d|j|<yrrrs r%rzLLM.set_outputirr'ct|}||j}tjd|}d}|rdj |}nd}tj |d|d|}||vr||z }|S)Nz\[\d+\]z ([.!?])\s*$rrz\1 )rrrefindallrrH)r" prompt_dictinput cite_docsrefspatternciters r%rzLLM.cite_from_promptmstu%/ zz*i0  774=DDAdV37 v  dNF r'cBd|_||_||_|r||_yyrmrrs r%rOzLLM.add_to_headzrr'r)rrrr+rr&r]r:r-rtorchfloat16rrrrrfrrrrrO __classcell__rs@r%rrsJ#4DdhxzFKZ]fjwxMQ_drwBFOT^botAI\b6gkB ## B#B5:MM   F,.b 1D 1t 1D O %!2ZW[S (r'rc JeZdZddddidddddddf ded dffd Zd ZxZS) TestLLMzgpt-4Nc|SrArTrqs r%rnzTestLLM.slmr'rrr Fr r c t |||||| | | |_||_||_d|_||_i|_d|_d|_ | sd|_ y| |_ y)N)rrrrFrz5Strain[1], turns:, heat[2][4]. Sent2[5]. Sent3. rdd) rr&rrErrrrrrans)r"rr r#rErrrrrrrrrs r%r&zTestLLM.__init__ss |H^nzC D  . %*"TWL]`r'c|jSrA)r)r"r/s r%rzTestLLM.generate_contents xxr')rrrrr&rr r s@r%r r sa$TDbm{}GLZ]dhstHLY^fj aF aos ar'r cTeZdZddddidddddddddddfdeddffd Zfd ZxZS) AutoAISLLMNrrr Frr r ct|||||||||| | | | | |||tddddd|_ddi|_y) Nz Answer: {INST} zPremise: {premise} zClaim: {claim} )INSTpremiseclaim)r5rzIn this task, you will be presented a premise and a claim. If the premise entails the claim, output "1", otherwise output "1". Your answer should only contains one number without any other letters and punctuations.)rr&rr r)r"rr r#rErrrrrrrrrrrr$rs r%r&zAutoAISLLM.__init__s  hV\^ikoqy|LNWYbdjlqsxzC D"#F.&S   !#{|r'cJt|||d}|jdS)N)rrr)rrfget)r"rr dict_answerrs r%rfzAutoAISLLM.generates&g&'%'HI x((r')rrrrr&rfr r s@r%rrs!$_cqs}BPSZ^ij}AMR^clpx}EJV^||cg|))r'r__main__z(rzQuestion:{Question} z{Docs} z7Here is the feed back of your last response:{feedback} z5Here is answer and you have to give feedback:{Answer})rQuestionDocsfeedbackrr3r)rcitekit.prompt.promptrrcitekit.utils.utilsrrrrandomrrrrrr rrr/mrTr'r%r#s ( FF p(p(f+0-- u(&u(rc" ) )" z G[g_x[e`Z^U TVWF E A r'