发挥LLM大语言模型的潜力快速工程指南
近年来,语言模型得到了迅速改进,GPT3和GPT4等大型语言模型(LLM)占据了中心舞台。这些模型因其以令人不可思议的执行各种任务的能力而流行。随着这些模型的参数数量(数十亿!)增加,这些模型也意外地获得了新能力。
在本文中,我们将探讨LLM、它们可以执行的任务、它们的缺点以及各种提示工程策略。LLM是什么?
LLM是经过大量文本数据训练的神经网络。训练过程允许模型学习文本中的模式,包括语法、句法和单词关联。这些模型使用这些学习到的模式来生成类似人类的文本,使其成为自然语言处理(NLP)任务的理想选择。哪些LLM可用?
有多种LLM可用,其中GPT4最受欢迎。其他模型包括LLaMA、PaLM、BERT和T5。每个模型都有其优点和缺点,其中一些是开放的,另一些是封闭的,只能通过API使用。LLM的缺点
尽管表现出色,但LLM有一些局限性。一个显着的缺点是他们无法超越提示中提供的信息进行推理。此外,LLM可以根据他们训练数据生成有偏见的文本。控制LLM的输出也具有挑战性,因此有必要使用及时的工程策略来实现所需的输出。可以执行哪些任务?
以下是这些任务的列表以及每个任务的示例。文本摘要
LLM可以生成长文本的摘要,使用提示模板来总结一段文本:FulltextSummarizethetextabove:ExplainthetextaboveinNsentences:
例如,我们可以要求使用以下提示来总结这段文本:
PromptInamechanicalwatchthewatchsgearsareturnedbyaspiralspringcalledamainspring。Inamanualwatchenergyisstoredinthemainspringbyturningaknob,thecrownonthesideofthewatch。Thentheenergyfromthemainspringpowersthewatchmovementuntilitrunsdown,requiringthespringtobewoundagain。Aselfwindingwatchmovementhasamechanismwhichwindsthemainspringusingthenaturalmotionsofthewearersbody。Thewatchcontainsanoscillatingweightthatturnsonapivot。Thenormalmovementsofthewatchintheuserspocket(forapocketwatch)orontheusersarm(forawristwatch)causetherotortopivotonitsstaff,whichisattachedtoaratchetedwindingmechanism。Themotionofthewatchistherebytranslatedintocircularmotionoftheweightwhich,throughaseriesofreverserandreducinggears,eventuallywindsthemainspring。Therearemanydifferentdesignsformodernselfwindingmechanisms。Somedesignsallowwindingofthewatchtotakeplacewhiletheweightswingsinonlyonedirectionwhileother,moreadvanced,mechanismshavetworatchetsandwindthemainspringduringbothclockwiseandanticlockwiseweightmotions。Explainthetextabovein2sentences:
给出以下输出:
OutputAmechanicalwatchusesamainspringtopowerthemovementofthewatchsgears。Inamanualwatch,themainspringiswoundbyturningaknob,whileaselfwindingwatchusesthenaturalmotionofthewearersbodytowindthemainspringthroughanoscillatingweightandratchetedwindingmechanism。信息提取
从大量文本中识别和提取特定信息。这在新闻和商业等领域很有用。FulltextMentionXinthetextabove:ListXinthetextabove:
例如,我们要求从以下文本中提取地名:
PromptTheearliestcredibleevidenceforasuccessfuldesignisthewatchmadebytheSwisswatchmakerAbrahamLouisPerrelet,wholivedinLeLocle。Inlate1776orearly1777,heinventedaselfwindingmechanismforpocketwatchesusinganoscillatingweightinsidethewatchthatmovedupanddown。TheGenevaSocietyofArts,reportingonthiswatchin1777,statedthat15minuteswalkingwasnecessarytofullywindthewatch。Listthenamesofplacesinthetextabove:
输出是:
OutputLeLocle,Geneva。问答
根据提示中提供的信息回答问题。
TemplateAnswerthequestionbasedonthecontextbelow。Otherinstructions。RespondNotsureifnotsureabouttheanswer。Context:ContextQ:QuestionA:
PromptAnswerthequestionbasedonthecontextbelow。Answerinasinglesentence。RespondNotsureifnotsureabouttheanswer。Context:Anautomaticwatch,alsoknownasaselfwindingwatchorsimplyanautomatic,isamechanicalwatchwherethenaturalmotionofthewearerprovidesenergytowindthemainspring,makingmanualwindingunnecessaryifwornenough。Itisdistinguishedfromamanualwatchinthatamanualwatchmusthaveitsmainspringwoundbyhandatregularintervals。Q:Doautomaticwatchesneedtobemanuallywound?A:
ChatGPT回应:
OutputNo,automaticwatchesdonotneedtobemanuallywoundifwornenoughbecausethenaturalmotionofthewearerprovidesenergytowindthemainspring。文本分类
LLM可以将文本分类到预定义的类别中,例如情绪分析或垃圾邮件检测。
TemplateClassifythetextintoListofchoices:Text:FulltextClassification:
例如,我们可以尝试根据其情绪是正面、中性还是负面来对文本进行分类:
PromptClassifythetextintopositive,neutralornegative:Text:ThatshotselectionwasawfulClassification:
OutputNegative对话
LLM可以生成类似人类的对话,在聊天机器人和客户服务应用程序中很有用。我们可以指示LLM以特定方式跟踪对话。
TemplateThefollowingisaconversationwith。TheshouldanswerFurtherinstructions。Person:Conversation:ResponsePerson:Conversation:
LLM将按照我们指示的参与者类型作出响应,通过将整个对话与新的响应再次传递给它来继续对话。例如:
PromptThefollowingisaconversationwithalawyer。Thelawyershouldanswersimplyandinlaymanterms。Person:Hello,IwouldliketoknowhowtodisputeaparkingticketLawyer:
OutputSure,Icanhelp。Thefirststepistochecktheticketforanyerrorsormistakes。Ifyoufindany,youcanusethoseasgroundstodisputetheticket。Iftherearenomistakes,youcanstilldisputetheticketbyprovidingevidencetosupportyourcase,suchasphotosorwitnessstatements。Youcanalsorequestahearingtopresentyourcaseinperson。
PromptThefollowingisaconversationwithalawyer。Thelawyershouldanswersimplyandinlaymanterms。Person:Hello,IwouldliketoknowhowtodisputeaparkingticketLawyer:Sure,Icanhelp。Thefirststepistochecktheticketforanyerrorsormistakes。Ifyoufindany,youcanusethoseasgroundstodisputetheticket。Iftherearenomistakes,youcanstilldisputetheticketbyprovidingevidencetosupportyourcase,suchasphotosorwitnessstatements。Youcanalsorequestahearingtopresentyourcaseinperson。Person:HowdoIrequestahearing?Lawyer:
OutputTorequestahearing,youllneedtofollowtheinstructionsonthebackoftheticketoronthewebsiteprovided。Usually,youllneedtosubmitawrittenrequestwithinacertaintimeframe,alongwithanyevidenceorsupportingdocumentsyouhave。Onceyourrequestisreceived,youllbenotifiedofthedate,time,andlocationofyourhearing。代码生成
LLM可以生成代码,开发人员更容易创建复杂的软件程序。我们可以写一个代码注释来描述我们希望代码做什么,LLM将能够尽最大努力为我们做这件事。我们希望代码做的事情越复杂,我们就需要在提示注释中提供越具体和描述性的信息。
模板可能因我们要生成的编程语言而异,但建议大致遵循此模板:
TemplateCommentsymbolofyourlanguageofchoice(oror)Method:NameofthemethodLanguage:ProgramminglanguageDescription:DescriptionofwhatthemethodmustdoParameters:ParametersthatthemethodwillreceiveContext:Furtherinformationneededtoimplementthemethod(URLs,databases,etc。)Output:OutputofthemethodCommentsymbolofyourlanguageofchoice(oror)
例如,如果想获取带有分页的API,编写以下注释:
PromptMethod:fetchFirst5PagesLanguage:JavascriptDescription:Fetchthefirst5pagesfromthispaginatedAPI:http:example。compages?nxContext:ThisAPIacceptsthefollowingURLparameters:n:thenumberofthepagetofetchOutput:thecontentofthefirst5pages
OutputHeresanexampleimplementationofthefetchFirst5PagesmethodinJavaScript,assumingtheuseofthefetchfunction:asyncfunctionfetchFirst5Pages(){constbaseUrlhttp:example。compages?n;constpages〔〕;for(leti1;i5;i){consturlbaseUrli;constresponseawaitfetch(url);if(!response。ok){thrownewError(Errorfetchingpage{i});}constpageDataawaitresponse。json();pages。push(pageData);}returnpages;}Thisfunctionusesalooptofetchthefirst5pagesfromtheAPIbyappendingthepagenumbertothebaseURL。Itchecksiftheresponsefromtheserverisokayandthrowsanerrorifitsnot。Then,itparsestheJSONdatafromtheresponseandaddsittoanarrayofpages。Finally,itreturnsthearrayofpages。推理
LLM可能难以完成一些基本任务,例如算术问题。但是,如果我们要求LLM根据其响应进行推理,我们可以帮助它找到正确的答案。这是推荐的模板:
TemplateQ:QuestionA:Letsworkthisoutinastepbystepwaytobesurewehavetherightanswer。First,Instructions。
在这个例子中,如果我们不加推理地提出一个简单的算术问题,它就会失败:
PromptAnswerthefollowingquestion。RespondNotsureifnotsureabouttheanswer。Q:Thesumofprimenumbersinthefollowinglistareamultipleof3:〔2,4,6,7,9,14〕A:〔YesorNo〕
OutputNo。Thesumofprimenumbersinthelist〔2,4,6,7,9,14〕isnotamultipleof3。Therefore,theanswerisNo。
但是,如果我们指示LLM对其答案进行推理,我们会得到截然不同的结果:
PromptQ:Thesumofprimenumbersinthefollowinglistareamultipleof3:〔2,4,6,7,9,14〕A:Letsworkthisoutinastepbystepwaytobesurewehavetherightanswer。First,wefindtheprimenumbersinthelistandaddthemtogether。Thenwecheckwhethertheresultingnumbercanbepidedby3。
OutputSure,letsworkthisoutstepbystep。Theprimenumbersinthelistare2and7。Sowecanaddthemtogether:279Theresultingnumberis9。Nowweneedtocheckwhetheritisamultipleof3。Wecandothisbycheckingiftheremainderof9pidedby3isequalto0:930Sincetheremainderis0,weknowthat9isindeedamultipleof3。Therefore,theanswerisYes,thesumofprimenumbersinthelist〔2,4,6,7,9,14〕isamultipleof3。攻略
虽然这些模板充分发挥LLM的潜力以满足你的需求,但有时它们仍然难以完成某些复杂的任务。已经开发了一些技术来帮助LLM获得满意的答案。到目前为止,我们只使用了零样本提示,但是,我们可以使用更复杂的技术。零次提示
我们一直在对本文中的所有示例使用零样本提示。零样本提示意味着向LLM传递一个提示,只包含我们希望它回答的问题,而没有任何示例说明它在其他情况下应该如何响应。例如,在之前的例子中我们做了:
PromptClassifythetextintopositive,neutralornegative:Text:ThatshotselectionwasawfulClassification:
OutputNegative
对于模型来说,这可能不足以准确回答我们想要的结果。那我们还有别的办法吗?小提示
与零样本提示相反,使用少样本提示,提出问题之前给LLM一些它应该如何回应的例子。例如,假设要对文本的情感进行分类。但我们希望它每次都能以非常具体的格式给我们答案。我们可以像这样预先给它举几个例子:
PromptClassifythetextintopositive,neutralornegative:Text:TodaytheweatherisfantasticClassification:PosText:Thefurnitureissmall。Classification:NeuText:IdontlikeyourattitudeClassification:NegText:ThatshotselectionwasawfulClassification:
OutputNeg思维链提示
对于更复杂的任务,可以将小样本提示与让模型推理其答案相结合。我们可以使用与之前相同的示例来查看推理和小样本提示如何影响结果:
PromptQ:Thesumofprimenumbersinthefollowinglistareamultipleof3:〔2,4,6,7,9,14〕A:Theprimenumbersinthelistare2and7。Theirsumis9。930。Theanswerisyes。Q:Thesumofprimenumbersinthefollowinglistareamultipleof3:〔1,2,4,7,8,10〕A:Theprimenumbersinthelistare1,2and7。Theirsumis10。1031。Theanswerisno。Q:Thesumofprimenumbersinthefollowinglistareamultipleof3:〔1,2,3,4〕A:Theprimenumbersinthelistare1,2and3。Theirsumis6。630。Theanswerisyes。Q:Thesumofprimenumbersinthefollowinglistareamultipleof3:〔5,6,7,8〕A:
OutputTheprimenumbersinthelistare5and7。Theirsumis12。1230。Theanswerisyes。最后说明
LLM彻底改变了自然语言处理领域,但要最大限度地发挥其潜力,及时工程至关重要。通过了解LLM可以执行的任务、它们的缺点以及各种及时的工程策略,开发人员可以利用LLM的力量来创建创新和有效的解决方案。在不久的将来可能会开发出更多的策略和技术,因此请关注该领域的进一步发展,以最大限度地发挥LLM的潜力。此外,随着LLM继续变得越来越大,增加了数十亿个参数,我们现在甚至无法想象的更多任务很可能会成为可能。想想使用这些新工具可以做什么,以及它们将来会为我们服务哪些用例,真是令人惊叹。