大模型实战篇:,的左右互搏之术
文章通过实际源码详细介绍了BasicReflection模式的实现方法,包括构建Generator和Reflector的过程。阅读本文可以帮助读者更好地理解BasicReflection的概念和实现过程。
在前面几篇文章中,风叔依次介绍了REWOO、Plan-and-Execute和LLMCompiler三种更侧重规划能力的AIAgent设计模式。
从最初的ReAct模式出发,加入规划能力即演变成REWOO;再加上Replan能力即演变成Plan-and-Execute;最后再加上DAG和并行处理能力,即演变成LLMCompiler。
从这篇文章开始,我们将转向另外几种侧重反思的AIAgent模式,我们首先从BasicReflection开始。
一、BasicReflection的概念
BasicReflection可以类比于左右互博。左手是Generator,负责根据用户指令生成结果;右手是Reflector,来审查Generator的生成结果并给出建议。在左右互搏的情况下,Generator生成的结果越来越好,Reflector的检查越来越严格,输出的结果也越来越有效。
下图是BasicReflection的原理,非常简单。
二、BasicReflection的实现过程
下面,风叔通过实际的源码,详细介绍BasicReflection模式的实现方法,具体的源代码地址在文章结尾处获取。
第一步构建Generator
在下面的例子中,我们先构建一个能够生成5段话的文章生成器。
首先,我们给Generator约定了Prompt,大模型实战篇:,的左右互搏之术告诉Generator具体的角色和目标,并且要求如果用户给出建议,需要给出修改后的版本
然后选择LLM模型,并构建Generator
最后要求输出Generator生成的内容
fromlangchain_core.messagesimportAIMessage,BaseMessage,HumanMessage
fromlangchain_core.promptsimportChatPromptTemplate,MessagesPlaceholder
fromlangchain_fireworksimportChatFireworks
下面是Generator的InitialResponse,可以看出Generator虽然按照我们的要求生成了初始回复,但是回复的整体质量还有很大的提升空间。
Title:TheRelevanceofTheLittlePrinceinModernChildhood
TheLittlePrince,anovellabyAntoinedeSaint-Exupéry,hasbeenachildhoodfavoriteforgenerations.Despitebeingpublishedoversevendecadesago,itstimelessthemescontinuetoresonatewithmodernchildren,makingithighlyrelevantincontemporarychildhood.
Firstly,thestoryexploresthecomplexnatureofhumanrelationships,whichisparticularlyrelevantformodernchildrengrowingupinanincreasinglyconnectedyetimpersonalworld.Throughthelittleprince’sencounterswithvariousgrown-upsondifferentplanets,thebookhighlightstheimportanceofgenuineconnectionsandunderstanding.Inanagewheredigitalcommunicationoftenreplacesface-to-faceinteraction,thismessageismorepertinentthanever.TheLittlePrinceencourageschildrentolookbeyondsuperficialrelationshipsandseekdeeperconnections,fosteringempathyandemotionalintelligence.
Secondly,thebookdealswiththeconceptofresponsibilityandself-discovery,elementsthatareintegraltoachild’sgrowth.Thelittleprince’sjourneyisessentiallyaquestforself-discovery,leadinghimtorealizehisresponsibilitytowardshisbelovedrose.Thisnarrativeencouragesmodernchildrentoembracetheirindividualitywhileunderstandingthesignificanceoftheiractions.Inasocietythatoftenoverlookstheemotionalwell-beingofchildren,TheLittlePrinceoffersarefreshingperspectiveonpersonalgrowthandresponsibility.
Thirdly,thebookaddressesthechallengingthemeoflossandbereavement.Thelittleprince’sdeparturefromhisasteroidandhissubsequentencounterswiththefoxandthesnakeareprofoundreflectionsontheinevitabilityoflossandtheimportanceofcherishingrelationships.Inatimewhenchildrenareexposedtovariousformsofloss,fromthedeathoflovedonestoenvironmentaldegradation,TheLittlePrinceprovidesagentleyetpowerfulwaytounderstandandcopewiththeseexperiences.
However,somecriticsarguethatthebook’spaceandabstractconceptsmightbechallengingformodernchildrenwithshortattentionspans.Toaddressthis,arevisedversioncouldincorporatemorevisualelementsandinteractiveactivitiestoengageyoungreadersbetter.Additionally,supplementarymaterialsexplainingthebook’sthemesinsimplertermscouldbeprovidedforparentsandeducatorstouseindiscussionswithchildren.
Inconclusion,TheLittlePrinceremainsrelevantinmodernchildhoodduetoitsexplorationofhumanrelationships,self-discovery,andloss.Thesethemes,wrappedinacaptivatingnarrative,offervaluablelessonsformodernchildren.Whilesomeadaptationsmaybenecessarytocatertothepreferencesoftoday’schildren,theessenceofthestoryremainsapowerfultoolforteachingemotionalintelligence,personalgrowth,andresilience.
第二步构建Reflector
针对Generator,我们构建一个专门的Reflector。
同样的,我们要先给Reflector约定Prompt,告诉Reflector“你是一名正在给论文评分的老师。请对用户的论文提出批评和建议,包括长度、深度、风格等要求。”
然后选择LLM模型,并构建Reflector
下面是针对Generator的InitialResponse,Reflector反馈的评语和建议。Reflector给出了B 的评级,并且按照我们的要求,从文章的长度、深度、风格等维度给出了具体的建议和总结。
EssayGrade:B
Theessayyousubmittedprovidesaclearandwell-structuredargumentabouttherelevanceofTheLittlePrinceinmodernchildhood.Youhavedemonstratedastrongunderstandingofthetextanditsthemes,andhaveeffectivelyappliedthemtothecontextofcontemporarychildhood.However,therearesomeareaswhereimprovementcouldbemadetoenhancethedepth,style,andoverallflowofyouressay.
1.Length:Whileyouressayiswell-writtenandinformative,itisrelativelybrief.Expandingoneachpointwithmoredetailedanalysisandexampleswouldstrengthenyourargumentanddemonstrateamorecomprehensiveunderstandingofthetext.Aimforaminimumof500wordstoallowforamorein-depthexplorationofyourideas.
2.Depth:Althoughyouhavetouchedupontherelevanceofthenovel’sthemes,furtheranalysisisneededtotrulyestablishitssignificanceinmodernchildhood.Forexample,whendiscussingthecomplexnatureofhumanrelationships,delveintohowthedigitalageaffectschildren’scommunicationskills,andhowTheLittlePrinceaddressesthisissue.Providingconcreteexamplesfromthetextandconnectingthemtoreal-worldscenarioswillmakeyourargumentmorecompelling.
3.Style:Toengageyourreadersmoreeffectively,considervaryingyoursentencestructureandlength.Usingamixofsimple,compound,andcomplexsentenceswillimprovetheflowofyouressayandmakeitmoreengagingtoread.Additionally,watchyourtenseconsistency.Ensurethatyoumaintainthesametensethroughoutyouressaytoavoidconfusion.
4.Recommendations:Whileyoursuggestionsforadaptationareagoodstart,theycouldbeexpandedupontoprovidemorecomprehensiverecommendations.Forexample,youmaywanttodiscussdifferentmethodsofincorporatingvisualelementsandinteractiveactivities,suchasillustrations,quizzes,ordiscussionquestions.Thiswilldemonstratethatyouhavethoughtfullyconsideredtheneedsofmodernchildrenandhavedevelopedstrategiestoaddressthesechallenges.
5.Conclusion:YourconclusioncouldbenefitfromastrongersummarizationofyourkeypointsandanassertivefinalstatementabouttherelevanceofTheLittlePrinceinmodernchildhood.Tyingallyourargumentstogetherinaconciseandpowerfulmannerwillleavealastingimpressiononyourreadersandsolidifyyourposition.
Overall,youressayiswell-researchedandprovidesasolidfoundationforacompellingargumentabouttherelevanceofTheLittlePrinceinmodernchildhood.Withsomeexpansion,deeperanalysis,andstylisticimprovements,youressaycanachieveanevenhigherlevelofexcellence.
第三步循环执行
接下来,我们就可以循环执行这个“生成–检查”的过程,重复规定的次数,或者约定当Generator的生成结果达到多少分时,停止循环。
大家可以看到,在循环过程中,我们也加入了人类的反馈建议,有助于Generator和Reflector学习迭代。
forchunkingenerate.stream(
{“messages”:[request,AIMessage(content=essay),HumanMessage(content=reflection)]}
):
print(chunk.content,end=””)
下面,我们构建流程图,将Generator、Reflector等节点添加进来,循环执行并输出结果。
classState(TypedDict):
messages:Annotated[list,add_messages]
asyncdefgeneration_node(state:Sequence[BaseMessage]):
returnawaitgenerate.ainvoke({“messages”:state})
builder=StateGraph(State)
builder.add_node(“generate”,generation_node)
builder.add_node(“reflect”,reflection_node)
builder.add_edge(START,“generate”)
defshould_continue(state:List[BaseMessage]):
iflen(state)>6:
#Endafter3iterations
returnEND
return“reflect”
builder.add_conditional_edges(“generate”,should_continue)
builder.add_edge(“reflect”,“generate”)
graph=builder.compile()
asyncforeventingraph.astream(
[
HumanMessage(
content=”GenerateanessayonthetopicalityofTheLittlePrinceanditsmessageinmodernlife”
)
],
print(event)
print(“—“)
至此,BasicReflection的完整流程就介绍完了,非常简单,相信哪怕是没有AI基础的同学也能看懂。
BasicReflection的架构,非常适合于进行相对比较发散的内容生成类工作,比如文章写作、图片生成、代码生成等等。
总体而言,BasicReflection是一种非常高效的反思类AIAgent设计模式。BasicReflection的思路非常朴素,使用成本较低。但是在实际应用中,BasicReflection也面临着一些缺陷:
对于一些比较复杂的问题,显然需要Generator具备更强大的推理能力
Generator生成的结果可能会过于发散,和我们要求的结果相去甚远
在一些复杂场景下,Generator和Reflector之间的循环次数不太好定义,如果次数太少,生成效果不够理想;如果次数太多,对token的消耗会很大。
我们有两种方法来优化BasicReflection,一种是边推理边执行的SelfDiscover模式,一种是增加了强化学习的Reflexion模式。
题图来自Unsplash,基于CC0协议。