




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
Prompt
Engineering
Author:LeeBoonstra
PromptEngineering
Acknowledgements
Contentcontributors
MichaelShermanYuanCao
ErickArmbrust
AnantNawalgariaAntonioGulli
SimoneCammel
CuratorsandEditors
AntonioGulli
AnantNawalgariaGraceMollison
TechnicalWriter
JoeyHaymaker
Designer
MichaelLanning
February20252
Tableofcontents
Introduction6
Promptengineering··7
LLMoutputconfiguration8
Outputlength.8
Samplingcontrols9
Temperature9
Top-Kandtop-P··10
Puttingitalltogether11
Promptingtechniques·13
Generalprompting/zeroshot·13
One-shot&few-shot.15
System,contextualandroleprompting18
Systemprompting·19
Roleprompting·21
Contextualprompting23
Step-backprompting25
ChainofThought(CoT)·29
Self-consistency32
TreeofThoughts(ToT)·36
ReAct(reason&act)·37
AutomaticPromptEngineering·40
Codeprompting42
Promptsforwritingcode42
Promptsforexplainingcode··44
Promptsfortranslatingcode46
Promptsfordebuggingandreviewingcode48
Whataboutmultimodalprompting?54
BestPractices·54
Provideexamples54
Designwithsimplicity··55
Bespecificabouttheoutput56
UseInstructionsoverConstraints56
Controlthemaxtokenlength58
Usevariablesinprompts·58
Experimentwithinputformatsandwritingstyles··59
Forfew-shotpromptingwithclassificationtasks,mixuptheclasses59
Adapttomodelupdates·60
Experimentwithoutputformats·60
JSONRepair61
WorkingwithSchemas·62
Experimenttogetherwithotherpromptengineers63
CoTBestpractices·64
Documentthevariouspromptattempts64
Summary·66
Endnotes68
PromptEngineering
Youdon’tneedtobeadata
scientistoramachinelearningengineer–everyonecanwriteaprompt.
Introduction
Whenthinkingaboutalargelanguagemodelinputandoutput,atextprompt(sometimes
accompaniedbyothermodalitiessuchasimageprompts)istheinputthemodeluses
topredictaspecificoutput.Youdon’tneedtobeadatascientistoramachinelearning
engineer–everyonecanwriteaprompt.However,craftingthemosteffectivepromptcanbecomplicated.Manyaspectsofyourpromptaffectitsefficacy:themodelyouuse,themodel’strainingdata,themodelconfigurations,yourword-choice,styleandtone,structure,and
contextallmatter.Therefore,promptengineeringisaniterativeprocess.Inadequatepromptscanleadtoambiguous,inaccurateresponses,andcanhinderthemodel’sabilitytoprovidemeaningfuloutput.
February20256
PromptEngineering
February20257
WhenyouchatwiththeGeminichatbot,1youbasicallywriteprompts,howeverthis
whitepaperfocusesonwritingpromptsfortheGeminimodelwithinVertexAIorbyusingtheAPI,becausebypromptingthemodeldirectlyyouwillhaveaccesstotheconfigurationsuchastemperatureetc.
Thiswhitepaperdiscussespromptengineeringindetail.Wewilllookintothevariouspromptingtechniquestohelpyougettingstartedandsharetipsandbestpracticestobecomeapromptingexpert.Wewillalsodiscusssomeofthechallengesyoucanfacewhilecraftingprompts.
Promptengineering
RememberhowanLLMworks;it’sapredictionengine.Themodeltakessequentialtextas
aninputandthenpredictswhatthefollowingtokenshouldbe,basedonthedataitwas
trainedon.TheLLMisoperationalizedtodothisoverandoveragain,addingthepreviouslypredictedtokentotheendofthesequentialtextforpredictingthefollowingtoken.Thenexttokenpredictionisbasedontherelationshipbetweenwhat’sintheprevioustokensandwhattheLLMhasseenduringitstraining.
Whenyouwriteaprompt,youareattemptingtosetuptheLLMtopredicttherightsequenceoftokens.Promptengineeringistheprocessofdesigninghigh-qualitypromptsthatguide
LLMstoproduceaccurateoutputs.Thisprocessinvolvestinkeringtofindthebestprompt,optimizingpromptlength,andevaluatingaprompt’swritingstyleandstructureinrelationtothetask.InthecontextofnaturallanguageprocessingandLLMs,apromptisaninputprovidedtothemodeltogeneratearesponseorprediction.
PromptEngineering
February20258
Thesepromptscanbeusedtoachievevariouskindsofunderstandingandgenerationtaskssuchastextsummarization,informationextraction,questionandanswering,textclassification,languageorcodetranslation,codegeneration,andcodedocumentationorreasoning.
PleasefeelfreetorefertoGoogle’spromptingguides2,3withsimpleandeffectivepromptingexamples.
Whenpromptengineering,youwillstartbychoosingamodel.Promptsmightneedtobe
optimizedforyourspecificmodel,regardlessofwhetheryouuseGeminilanguagemodelsinVertexAI,GPT,Claude,oranopensourcemodellikeGemmaorLLaMA.
Besidestheprompt,youwillalsoneedtotinkerwiththevariousconfigurationsofaLLM.
LLMoutputconfiguration
Onceyouchooseyourmodelyouwillneedtofigureoutthemodelconfiguration.MostLLMscomewithvariousconfigurationoptionsthatcontroltheLLM’soutput.Effectiveprompt
engineeringrequiressettingtheseconfigurationsoptimallyforyourtask.
Outputlength
Animportantconfigurationsettingisthenumberoftokenstogenerateinaresponse.
GeneratingmoretokensrequiresmorecomputationfromtheLLM,leadingtohigherenergyconsumption,potentiallyslowerresponsetimes,andhighercosts.
PromptEngineering
February20259
ReducingtheoutputlengthoftheLLMdoesn’tcausetheLLMtobecomemorestylisticallyortextuallysuccinctintheoutputitcreates,itjustcausestheLLMtostoppredictingmoretokensoncethelimitisreached.Ifyourneedsrequireashortoutputlength,you’llalso
possiblyneedtoengineeryourprompttoaccommodate.
OutputlengthrestrictionisespeciallyimportantforsomeLLMpromptingtechniques,likeReAct,wheretheLLMwillkeepemittinguselesstokensaftertheresponseyouwant.
Beaware,generatingmoretokensrequiresmorecomputationfromtheLLM,leadingtohigherenergyconsumptionandpotentiallyslowerresponsetimes,whichleadstohighercosts.
Samplingcontrols
LLMsdonotformallypredictasingletoken.Rather,LLMspredictprobabilitiesforwhatthenexttokencouldbe,witheachtokenintheLLM’svocabularygettingaprobability.Thosetokenprobabilitiesarethensampledtodeterminewhatthenextproducedtokenwillbe.
Temperature,top-K,andtop-Parethemostcommonconfigurationsettingsthatdeterminehowpredictedtokenprobabilitiesareprocessedtochooseasingleoutputtoken.
Temperature
Temperaturecontrolsthedegreeofrandomnessintokenselection.Lowertemperatures
aregoodforpromptsthatexpectamoredeterministicresponse,whilehighertemperaturescanleadtomorediverseorunexpectedresults.Atemperatureof0(greedydecoding)is
PromptEngineering
February202510
deterministic:thehighestprobabilitytokenisalwaysselected(thoughnotethatiftwotokenshavethesamehighestpredictedprobability,dependingonhowtiebreakingisimplementedyoumaynotalwaysgetthesameoutputwithtemperature0).
Temperaturesclosetothemaxtendtocreatemorerandomoutput.Andastemperaturegetshigherandhigher,alltokensbecomeequallylikelytobethenextpredictedtoken.
TheGeminitemperaturecontrolcanbeunderstoodinasimilarwaytothesoftmaxfunction
usedinmachinelearning.Alowtemperaturesettingmirrorsalowsoftmaxtemperature(T),emphasizingasingle,preferredtemperaturewithhighcertainty.AhigherGeminitemperaturesettingislikeahighsoftmaxtemperature,makingawiderrangeoftemperaturesaround
theselectedsettingmoreacceptable.Thisincreaseduncertaintyaccommodatesscenarioswherearigid,precisetemperaturemaynotbeessentiallikeforexamplewhenexperimentingwithcreativeoutputs.
Top-Kandtop-P
Top-Kandtop-P(alsoknownasnucleussampling)4aretwosamplingsettingsusedinLLMstorestrictthepredictednexttokentocomefromtokenswiththetoppredictedprobabilities.Liketemperature,thesesamplingsettingscontroltherandomnessanddiversityof
generatedtext.
?Top-KsamplingselectsthetopKmostlikelytokensfromthemodel’spredicted
distribution.Thehighertop-K,themorecreativeandvariedthemodel’soutput;the
lowertop-K,themorerestiveandfactualthemodel’soutput.Atop-Kof1isequivalenttogreedydecoding.
PromptEngineering
February202511
?Top-Psamplingselectsthetoptokenswhosecumulativeprobabilitydoesnotexceedacertainvalue(P).ValuesforPrangefrom0(greedydecoding)to1(alltokensintheLLM’svocabulary).
Thebestwaytochoosebetweentop-Kandtop-Pistoexperimentwithbothmethods(orbothtogether)andseewhichoneproducestheresultsyouarelookingfor.
Puttingitalltogether
Choosingbetweentop-K,top-P,temperature,andthenumberoftokenstogenerate,
dependsonthespecificapplicationanddesiredoutcome,andthesettingsallimpactoneanother.It’salsoimportanttomakesureyouunderstandhowyourchosenmodelcombinesthedifferentsamplingsettingstogether.
Iftemperature,top-K,andtop-Pareallavailable(asinVertexStudio),tokensthatmeet
boththetop-Kandtop-Pcriteriaarecandidatesforthenextpredictedtoken,andthen
temperatureisappliedtosamplefromthetokensthatpassedthetop-Kandtop-Pcriteria.Ifonlytop-Kortop-Pisavailable,thebehavioristhesamebutonlytheonetop-KorPsettingisused.
Iftemperatureisnotavailable,whatevertokensmeetthetop-Kand/ortop-Pcriteriaarethenrandomlyselectedfromtoproduceasinglenextpredictedtoken.
Atextremesettingsofonesamplingconfigurationvalue,thatonesamplingsettingeithercancelsoutotherconfigurationsettingsorbecomesirrelevant.
PromptEngineering
February202512
?Ifyousettemperatureto0,top-Kandtop-Pbecomeirrelevant–themostprobable
tokenbecomesthenexttokenpredicted.Ifyousettemperatureextremelyhigh(above1–generallyintothe10s),temperaturebecomesirrelevantandwhatevertokensmakeitthroughthetop-Kand/ortop-Pcriteriaarethenrandomlysampledtochooseanextpredictedtoken.
?Ifyousettop-Kto1,temperatureandtop-Pbecomeirrelevant.Onlyonetokenpassesthetop-Kcriteria,andthattokenisthenextpredictedtoken.Ifyousettop-Kextremelyhigh,liketothesizeoftheLLM’svocabulary,anytokenwithanonzeroprobabilityofbeingthenexttokenwillmeetthetop-Kcriteriaandnoneareselectedout.
?Ifyousettop-Pto0(oraverysmallvalue),mostLLMsamplingimplementationswillthenonlyconsiderthemostprobabletokentomeetthetop-Pcriteria,makingtemperatureandtop-Kirrelevant.Ifyousettop-Pto1,anytokenwithanonzeroprobabilityofbeingthe
nexttokenwillmeetthetop-Pcriteria,andnoneareselectedout.
Asageneralstartingpoint,atemperatureof.2,top-Pof.95,andtop-Kof30willgiveyou
relativelycoherentresultsthatcanbecreativebutnotexcessivelyso.Ifyouwantespeciallycreativeresults,trystartingwithatemperatureof.9,top-Pof.99,andtop-Kof40.Andifyouwantlesscreativeresults,trystartingwithatemperatureof.1,top-Pof.9,andtop-Kof20.Finally,ifyourtaskalwayshasasinglecorrectanswer(e.g.,answeringamathproblem),startwithatemperatureof0.
NOTE:Withmorefreedom(highertemperature,top-K,top-P,andoutputtokens),theLLMmightgeneratetextthatislessrelevant.
WARNING:Haveyoueverseenaresponseendingwithalargeamountoffillerwords?Thisisalsoknownasthe"repetitionloopbug",whichisacommonissueinLargeLanguage
Modelswherethemodelgetsstuckinacycle,repeatedlygeneratingthesame(filler)word,phrase,orsentencestructure,oftenexacerbatedbyinappropriatetemperatureandtop-k/
PromptEngineering
February202513
top-psettings.Thiscanoccuratbothlowandhightemperaturesettings,thoughfordifferentreasons.Atlowtemperatures,themodelbecomesoverlydeterministic,stickingrigidlytothehighestprobabilitypath,whichcanleadtoaloopifthatpathrevisitspreviouslygenerated
text.Conversely,athightemperatures,themodel'soutputbecomesexcessivelyrandom,
increasingtheprobabilitythatarandomlychosenwordorphrasewill,bychance,leadbacktoapriorstate,creatingaloopduetothevastnumberofavailableoptions.Inbothcases,themodel'ssamplingprocessgets"stuck,"resultinginmonotonousandunhelpfuloutput
untiltheoutputwindowisfilled.Solvingthisoftenrequirescarefultinkeringwithtemperatureandtop-k/top-pvaluestofindtheoptimalbalancebetweendeterminismandrandomness.
Promptingtechniques
LLMsaretunedtofollowinstructionsandaretrainedonlargeamountsofdatasotheycan
understandapromptandgenerateananswer.ButLLMsaren’tperfect;thecleareryour
prompttext,thebetteritisfortheLLMtopredictthenextlikelytext.Additionally,specific
techniquesthattakeadvantageofhowLLMsaretrainedandhowLLMsworkwillhelpyougettherelevantresultsfromLLMs
Nowthatweunderstandwhatpromptengineeringisandwhatittakes,let’sdiveintosomeexamplesofthemostimportantpromptingtechniques.
Generalprompting/zeroshot
Azero-shot5promptisthesimplesttypeofprompt.ItonlyprovidesadescriptionofataskandsometextfortheLLMtogetstartedwith.Thisinputcouldbeanything:aquestion,astartofastory,orinstructions.Thenamezero-shotstandsfor’noexamples’.
PromptEngineering
February202514
Let’suseVertexAIStudio(forLanguage)inVertexAI,6whichprovidesaplaygroundtotestprompts.InTable1,youwillseeanexamplezero-shotprompttoclassifymoviereviews.
Thetableformatasusedbelowisagreatwayofdocumentingprompts.Yourpromptswilllikelygothroughmanyiterationsbeforetheyendupinacodebase,soit’simportanttokeeptrackofyourpromptengineeringworkinadisciplined,structuredway.Moreonthistable
format,theimportanceoftrackingpromptengineeringwork,andthepromptdevelopmentprocessisintheBestPracticessectionlaterinthischapter(“Documentthevariouspromptattempts”).
Themodeltemperatureshouldbesettoalownumber,sincenocreativityisneeded,andweusethegemini-prodefaulttop-Kandtop-Pvalues,whicheffectivelydisablebothsettings(see‘LLMOutputConfiguration’above).Payattentiontothegeneratedoutput.Thewordsdisturbingandmasterpieceshouldmakethepredictionalittlemorecomplicated,asboth
wordsareusedinthesamesentence.
PromptEngineering
February202515
Name
1_1_movie_classification
Goal
Classifymoviereviewsaspositive,neutralornegative.
Model
gemini-pro
Temperature
0.1
TokenLimit
5
Top-K
N/A
Top-P
1
Prompt
ClassifymoviereviewsasPOSITIVE,NEUTRALorNEGATIVE.
Review:"Her"isadisturbingstudyrevealingthedirection
humanityisheadedifAIisallowedtokeepevolving,
unchecked.Iwishthereweremoremovieslikethismasterpiece.Sentiment:
Output
POSITIVE
Table1.Anexampleofzero-shotprompting
Whenzero-shotdoesn’twork,youcanprovidedemonstrationsorexamplesintheprompt,whichleadsto“one-shot”and“few-shot”prompting.Generalprompting/zeroshot
One-shot&few-shot
WhencreatingpromptsforAImodels,itishelpfultoprovideexamples.Theseexamplescanhelpthemodelunderstandwhatyouareaskingfor.Examplesareespeciallyusefulwhenyouwanttosteerthemodeltoacertainoutputstructureorpattern.
Aone-shotprompt,providesasingleexample,hencethenameone-shot.Theideaisthemodelhasanexampleitcanimitatetobestcompletethetask.
Afew-shotprompt7providesmultipleexamplestothemodel.Thisapproachshowsthe
modelapatternthatitneedstofollow.Theideaissimilartoone-shot,butmultipleexamplesofthedesiredpatternincreasesthechancethemodelfollowsthepattern.
PromptEngineering
February202516
Thenumberofexamplesyouneedforfew-shotpromptingdependsonafewfactors,
includingthecomplexityofthetask,thequalityoftheexamples,andthecapabilitiesofthegenerativeAI(genAI)modelyouareusing.Asageneralruleofthumb,youshoulduseatleastthreetofiveexamplesforfew-shotprompting.However,youmayneedtousemoreexamplesformorecomplextasks,oryoumayneedtousefewerduetotheinputlength
limitationofyourmodel.
Table2showsafew-shotpromptexample,let’susethesamegemini-promodel
configurationsettingsasbefore,otherthanincreasingthetokenlimittoaccommodatetheneedforalongerresponse.
Goal
ParsepizzaorderstoJSON
Model
gemini-pro
Temperature
0.1
TokenLimit
250
Top-K
N/A
Top-P
1
Prompt
Parseacustomer'spizzaorderintovalidJSON:
EXAMPLE:
Iwantasmallpizzawithcheese,tomatosauce,andpepperoni.JSONResponse:
、、、
{
"size":"small","type":"normal",
"ingredients":[["cheese","tomatosauce","peperoni"]]}
、、、
Continuesnextpage...
PromptEngineering
February202517
Prompt
EXAMPLE:
CanIgetalargepizzawithtomatosauce,basilandmozzarella
{
"size":"large","type":"normal",
"ingredients":[["tomatosauce","bazel","mozzarella"]]}
Now,Iwouldlikealargepizza,withthefirsthalfcheeseandmozzarella.Andtheothertomatosauce,hamandpineapple.
JSONResponse:
Output
、、、
{
"size":"large",
"type":"half-half",
"ingredients":[["cheese","mozzarella"],["tomatosauce","ham","pineapple"]]
}
、、、
Table2.Anexampleoffew-shotprompting
Whenyouchooseexamplesforyourprompt,useexamplesthatarerelevanttothetaskyouwanttoperform.Theexamplesshouldbediverse,ofhighquality,andwellwritten.Onesmallmistakecanconfusethemodelandwillresultinundesiredoutput.
Ifyouaretryingtogenerateoutputthatisrobusttoavarietyofinputs,thenitisimportanttoincludeedgecasesinyourexamples.Edgecasesareinputsthatareunusualorunexpected,butthatthemodelshouldstillbeabletohandle.
PromptEngineering
February202518
System,contextualandroleprompting
System,contextuaIandroIepromptingareaIItechniquesusedtoguidehowLLMsgeneratetext,buttheyfocusondifferentaspects:
?SystempromptingsetstheoveraIIcontextandpurposefortheIanguagemodeI.It
definesthe,bigpicture,ofwhatthemodeIshouIdbedoing,IiketransIatingaIanguage,cIassifyingareviewetc.
?ContextualpromptingprovidesspecificdetaiIsorbackgroundinformationreIevanttothecurrentconversationortask.ItheIpsthemodeItounderstandthenuancesofwhat,sbeingaskedandtaiIortheresponseaccordingIy.
?RolepromptingassignsaspecificcharacteroridentityfortheIanguagemodeItoadopt.ThishelpsthemodelgenerateresponsesthatareconsistentwiththeassignedroleanditsassociatedknowIedgeandbehavior.
TherecanbeconsiderabIeoverIapbetweensystem,contextuaI,androIeprompting.E.g.apromptthatassignsaroIetothesystem,canaIsohaveacontext.
However,eachtypeofpromptservesasIightIydifferentprimarypurpose:
?Systemprompt:DefinesthemodeI,sfundamentaIcapabiIitiesandoverarchingpurpose.
?ContextuaIprompt:Providesimmediate,task-specificinformationtoguidetheresponse.It,shighIyspecifictothecurrenttaskorinput,whichisdynamic.
?RoIeprompt:FramesthemodeI,soutputstyIeandvoice.ItaddsaIayerofspecificityandpersonaIity.
PromptEngineering
February202519
Distinguishingbetweensystem,contextual,androlepromptsprovidesaframeworkfor
designingpromptswithcIearintent,aIIowingforflexibIecombinationsandmakingiteasiertoanaIyzehoweachprompttypeinfluencestheIanguagemodeI,soutput.
Let,sdiveintothesethreedifferentkindsofprompts.
Systemprompting
Table3containsasystemprompt,whereIspecifyadditionalinformationonhowtoreturntheoutput.IincreasedthetemperaturetogetahighercreativityI
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年軟件考試各類試題及答案
- 行政法學課程教學中的創新要素試題及答案
- 與同事建立良好關系的練習計劃
- 高效人際關系的建立與維護計劃
- 法學概論的法律環境構建與試題及答案
- 軟件設計師考試實例講解試題及答案
- 軟件水平考試備考經典試題及答案
- 2025年軟考網絡管理員核心技能試題及答案
- 班主任奉獻愛心播撒陽光計劃
- 2025冰箱租賃合同模板
- 試卷交接簽字單
- 調壓器技術規范
- 學校生均占地面積
- 《康復醫學》第四章 常見疾病的康復 第二節 腫瘤康復課件
- 2016年度高考全國3卷文綜地理試題(解析版)
- SIPOC培訓教材學習教案
- 2019年重慶江津小升初數學真題及答案
- 《菱形的判定》教學設計(共3頁)
- 配電箱系統圖
- 電纜井工程量計算
- 初中音樂--人聲的分類--(1)pptppt課件
評論
0/150
提交評論