o cgeB@sddlZddlmZddlZddlmZmZmZddlZddl Z GdddZ ej fddZ Gdd d e Z Gd d d e ZGd d d e Zedkr\eddddddddZe dZdSdS)N)Prompt) one_paragraphfirst_sentencemake_asc@s eZdZdZddidddddfdeddfddZdefd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZeddZiifdedefddZdd Zd!d"d#d"fd7d$d%Zd&d'Zd(d)Zd8d7d*d+Zd,d-Zd.d"d/d"d0fd1d2Zd3d4Zd9d5d6ZdS):ModuleNF prompt_makerreturnc Cs||_d|_||||_d|_g|_i|_d|_||_||_ ||_ t |_ ||_ d|_i|_tj|_tjd7_d|_d|_d|_dS)NTFrr) 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 pipeliner rrr output_asrr"A/Users/shenjiajun/Desktop/New/CiteLab/citekit/cite_modules/LLM.py__init__ s(  zModule.__init__cCs(|jr |jd|jdSd|jdS)N-[]zUnknown-type module-[) model_typerrr"r"r#__str__"szModule.__str__cCstd|ddddd}|dkr!|jj|jjd}|j}||dS|dkr.dt|jd iS|d vr@||}td |t||Std |d )Nzget_json_config:rprompt destinationr)max turnr*r+ global prompttemplate components) prompt_info self_infor)r,r-zgetting the config:zget_json_config for  is not implemented) printr r/r0r strrgetattrNotImplementedError)rconfigZavaliable_mappingr1r2r"r"r#get_json_config(s,   zModule.get_json_configcC|jSN)rr(r"r"r#get_destinationsFzModule.get_destinationsc Cs0|dkr(|d}|d}|d}ddl}||j|_|jj||d||_dS|dkrUtd|dd |d |d d krF||ddS|j|dt|d d dS|d krzt|j D]\}}t |t |krw|j ||j |=dSq^dS|dkr|j |dddS|dkr||_dStd|d)Nr*r/r0r rr.r+zupdate destination:zpost_processing:rNone)post_processingZdelete_destinationZheaderT)subr,z update for r3)copyZdeepcopyr updater r4 set_targetr enumeraterr5remover add_to_headrr7) rr8Z update_infor/r0r rAidr"r"r#rBIs4    z Module.updatecCsdSr;r"r(r"r"r# end_multijzModule.end_multicCst|tsJ||_dSr;) isinstanceboolr )rZuser"r"r#set_use_head_promptms zModule.set_use_head_promptcCsd|_d|_dSNFr)rrr(r"r"r#resetq z Module.resetcCs|rg|_nd|_||_dS)Nr )rr)rZ bool_valuer"r"r#change_to_multi_processus zModule.change_to_multi_processcCr:r;)r r(r"r"r#get_use_head_prompt{szModule.get_use_head_prompt head_promptdynamic_promptcCstr;)r7)rrSrTr"r"r#generaterJzModule.generatecCs.|jD]}|j|d}||r|SqdS)N conditionrr)rr+condr"r"r#sends z Module.sendcCdSNTr"r(r"r"r#zModule.cC|Sr;r"xr"r"r#r\r]cCs.||d|j|<|g|j|_||jdS)N)rVr?)rrrr )rr+rVr?r"r"r#rCszModule.set_targetcCsg|_i|_dSr;rWr(r"r"r#clear_destinationrPzModule.clear_destinationcC|jrE|js9|j|jjvr|jj|j||idS|jj|jd7<|jj|j||7<dS|||jj|j<dSdSN rhead_subrr headkeysrBrroutputsr"r"r#add_output_to_head zModule.add_output_to_headcCs||_|r |j|dSdSr;)r moduleappend)rr r"r"r#rszModule.connect_tocCsxd}|jD]2\}}||r9|s2|js!|jj|d|jn|jj|dd|jd}|dr9d|_qdS)NFr?r Tr) ritemsrr outputrnrjoinrrZoutedrXZ post_and_endr"r"r#rpsz Module.outputcCrZr[r"r(r"r"r#r\r]cCr^r;r"r_r"r"r#r\r]TcC||d|j|<dSN)r?rrrrXr?rr"r"r# set_outputzModule.set_outputcCr^r;r"r(r"r"r#get_first_modulerJzModule.get_first_modulecC$d|_||_||_|r||_dSdSr[rrrfrrZdatakeyr@processr"r"r#rF  zModule.add_to_head)r Nr;FN)__name__ __module__ __qualname__rrr$r5r)r9r<rBrIrMrOrQpropertyrRdictrUrYrCrarkrrprwryrFr"r"r"r#r s.$!   rcCs:ddlm}m}|j||dd}||}|||fSNr)AutoModelForCausalLM AutoTokenizerZauto)Z torch_dtypeZ device_mapZ transformersrrZfrom_pretrainedeval)model_name_or_pathdtyperrmodel tokenizerr"r"r# load_model rcseZdZdZddddiddddddddddddd fd ed dffd d ZddZd efddZd efddZ e j fddZ ddZ ddZiifdedefddZddZd d!Zd"d#d$d#dfd%d&Zd'd(Zd+d)d*ZZS),LLM GeneratorNcpu?rFTAnswerdocsr r cstj|||| ||d| |_|r||_nfdd|_|r!||_||_d|_||_t|_ | |_ | r5||_ |rgd| vra| sMt d||\|_|_n t d| j| j|_|_||_||_ntd|_i|_d|_d |_dS) N)rcs|iSr;r"r_r!r"r#r\szLLM.__init__..Fgptzloading model...zsharing model...ZOPENAI_API_KEYr)superr$rr? model_namestoprnoisyrr auto_cite cite_fromlowerr4rrr temperaturedeviceosgetenv openai_keyrr token_used)rrr r r?r rrrrshare_model_withrrrprrrr!Zauto_cite_from __class__rr#r$s6   z LLM.__init__cCsd|_d|_d|_dSrN)rrrr(r"r"r#rOs z LLM.resetcCs|jr |jd|jdSdS)Nr%r&z unknown model)rrr(r"r"r#r)sz LLM.__str__cCs.|jd|dddd|jDdgS)Nz | | V /cSsg|]}t|qSr")r5).0Zdesr"r"r# sz LLM.__repr__..rp)r rqrr(r"r"r#__repr__s.z LLM.__repr__cCs:ddlm}m}|j||dd}||}|||fSrr)rrrrrrrr"r"r#rrzLLM.load_modelcCs||_d|_dSr[)rr)rkeyr"r"r#set_citerPz LLM.set_citecCsd|jvr@ddl}|j|_dddd|dg}|jj|j|d|jd}|j|d d |d d 7_|d dd dS|j |dd |j j }|jdurSgn|j}|j j di|dd|jd}|jt|d7_|j j|d|ddddd}t|S)Nrrsystemz1You are a good helper who follow the instructions)Zrolecontentuseri)rZmessagesZ max_tokensrZusageZcompletion_tokensZ prompt_tokensZchoicesmessagerZpt)Zreturn_tensorsT)Z do_sampleZmax_new_tokensrZ input_idsr)Zskip_special_tokensr")rropenairZapi_keyZChatCompletionZcreaterrrZtorrrUrlendecodesizerr4)rr*rZresponseZinputsrrjr"r"r#generate_content s8"  &zLLM.generate_contentrSrTcCs|jr |||j|}n||j|}|jr"tdt|d|d|jd7_||}|jr9tdt||jrI| i||j||}|j rS|j |n||_ | ||}|j|jkrhd|_||jvrv|j|d|S||S)Nz prompt to z: z rzOUTPUT:Tr?)r r r rr4r5rrrcite_from_promptrrrnrkrYrrrr?)rrSrTr*rjr+r"r"r#rU/s,     z LLM.generatecCrbrcrerir"r"r#rkQrlzLLM.add_output_to_headcCs~d}|jD]5\}}||r<|s5|js$|js$|jj|d|jn|jj|dd|jd}|drcCr^r;r"r_r"r"r#r\ir]cCrsrtrurvr"r"r#rwirxzLLM.set_outputcCs`t|}||j}td|}d}|rd|}nd}t|d|d|}||vr.||7}|S)Nz\[\d+\]z ([.!?])\s*$r rz\1 )rrreZfindallrqr@)rZ prompt_dictinputZ cite_docsZrefspatternZciterpr"r"r#rms   zLLM.cite_from_promptcCrzr[r{r|r"r"r#rFzr~zLLM.add_to_headr)rrrr'rr$rOr5r)rtorchfloat16rrrrrUrkrprwrrF __classcell__r"r"rr#rs<! #"   rc sLeZdZdddddidddddddf d ed dffd d Zd dZZS)TestLLMzgpt-4NcCr^r;r"r_r"r"r#r\r]zTestLLM.rrrFr r c s`tj||||| | d| |_||_||_d|_||_i|_d|_d|_ | s+d|_ dS| |_ dS)N)r rrr Frz5Strain[1], turns:, heat[2][4]. Sent2[5]. Sent3. rdd) rr$rr?rrrrrrans) rrr r r?r rrrrrrrrr"r#r$szTestLLM.__init__cCr:r;)r)rr*r"r"r#rr=zTestLLM.generate_content)rrrrr$rrr"r"rr#rs4 rcsTeZdZddddidddddddddddfdeddffd d Zfd d ZZS) AutoAISLLMNrrrFrr r csNt||||||||| | | | | |||tdddddd|_ddi|_dS) Nz Answer: {INST} zPremise: {premise} zClaim: {claim} )INSTpremiseclaim)r0rzIn 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 )rrr r r?r rrrrrrrrprrr!rr"r#r$s* zAutoAISLLM.__init__cst||d}|dS)N)rrr)rrUget)rrrZ dict_answerrr"r#rUs zAutoAISLLM.generate)rrrrr$rUrr"r"rr#rs8 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})rZQuestionZDocsZfeedbackrr.r)rcitekit.prompt.promptrrcitekit.utils.utilsrrrZrandomrrrrrrrrr*mr"r"r"r#s, 4 :