版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
DevelopingaChatbotinPython
-MrityunjayaSingh,Scientist-ENIC-MoHUADivision
NewDelhi
Chatbots
Chatbotsarecomputerprogramthatsimulateshumanconversationthroughvoicecommandsortextchatsorbothinnaturallanguage,understandtheuser’sintentandsendresponsesbasedontheapplication’sbusinessrulesanddata.
Theycandecipherverbalorwrittenquestionsandprovideresponseswithappropriateinformationordirection.
ItcanbeconsideredasanenhancedchannelofuserinteractionwhichwouldmovefromInteractiveVoiceResponsetoIntelligentAssistantResponse.
TypesofChatbots
Therearetwokindsofchatbots:ScriptedbotsandArtificialIntelligence(AI)bots.
Scriptedbotislikearule-basedguidedconversationandperformslikeadecisiontreewhereeachactionbytheuserpromptsthebottotakeactionorrespond.Itengagesuserswithquestionsaboutpreferencesandservesupcontentandoffersrelevanttotheirresponses.
AIbotsarebuiltonMachineLearning(ML)andNaturalLanguageProcessing(NLP)capabilities.Theyarebasedonthehumancapabilityoflearningandabsorbinginformationowingtowhichtheyaremoreefficientandcanprocessmuchfasterthanhumansandmaycomeupwithmoresubtleresults.Themoretheconsumerengageswiththebotovertime,thesmarterthebotbecomesaboutconsumerpreferencesandisabletoservepersonalizedcontentandoffers.
TypesofChatbotFrameworks
TherearedifferenttypesofChatbotFrameworkssuchas–
1.Dialogflow2.Alexa3.Luis4.Wit5.Rasa
OftheseframeworksRasaisanOpenSourceframeworkfordevelopingtext-andvoice-basedchatbotsandassistants.Itcomprisesofmainlytwothings,RasaNLU,andRasaCore.ThebenefitofRasaStackisdataprivacyandzerorunningcost.Inotherframeworks,forcorpusdatatobeprocessedthroughtheirNLPengine,userchatsneedtobemovedthroughtheirservers.AndalsobeingopensourcenocostofusingtheplatforminRasa,thoughdeveloperscostwillbehigherashereyouneedexperienceddeveloperstodevelopandmaintainthechatbot.
Onecanchoosedialogfloworalexaifoneneedaquicksolution.Butforhighlyscalable,data
sensitiveassistantRasaStackisbetter.
AIChatbotArchitecture
AsintheAIbotarchitecturediagramsayforChattingPlatformSlackthechatstartsfromchattingplatformasinputrequestandendatchatplatformwithanautoresponse.Inbetweenthemessagepassesintosomanystages.HereNLUmodelsarechatbotAPIswhichextracttheinformationfromuserrequest.TheWeb-hookbackendissimplewebapplicationwhichprovidestherestfulAPI.NLUmodelextracttheinformationintheformofentitywhichworksasparameterintheserestfulAPIs.
AIbotArchitecture
AIChatbotArchitecture…
Itproducessomeresponsethatyougetasrobotresponse.TheonlymagiccomeswiththeNatural
LanguageProcessing(NLP)appliesintheseNLUs.Letsunderstanditwithanexamplebyte–”Order3
rosesformeoncomingMonday”.HereNLUshouldextractthreeinformation–
Product-Rose
User–CurrentLogin
ProductQuantity-3
Time–DatecorrespondingtoMonday
Ifyoursystemisintelligentenoughtoextracttheseparameter,Youcaneasilydevelopthebackendforit.Chatbotcansavesomuchtimeforconsumertounderstandtheproductuserinterface.TheseNLUAPIalsoprovidevoiceinterfacetouser.Onceyouspeakanythingtobot,itfirstconvertsspeechtotext.Onceitgetsthetext,NLPcomesintopicture.
ChatbotsprocessingLanguages!
Chatbotcanlooklikeanormalapp.Thereisanapplicationlayer,acorpusdatabaseandAPIsto
callexternalservices.Inacaseofthechatbot,UIisreplacedwithchatinterface.
Challengetoabotishowcanitunderstandtheintentoftheuser.Thebotsarefirsttrainedwiththeactualdata.Developersuselogsofconversationsalreadyavailabletoanalyzewhatusersaretryingtoaskandwhatdoesthatmean.Withacombinationof
MachineLearning
modelsandtoolsbuilt,developersmatchquestionsthatuserasksandanswerswiththebestsuitableanswer.Forexample:Ifauserisasking“Whereismypaymentreceipt?”and“Ihavenotreceivedapaymentreceipt”,meanthesamething.
Developersstrengthisintrainingthemodelssothatthechatbotisabletoconnectbothofthosequestionstocorrectintentandasanoutputproducesthecorrectanswer.Ifthereisnoextensivedataavailable,differentAPIsdatacanbeusedtotrainthechatbot.
HowChatbotActuallyWorks?
Thechatbotsworkbyadopting3classificationmethods:
PatternMatching
Botsusepatternmatchingtoclassifythetextandproduceasuitableresponsefortheusers.Astandardstructureofthesepatternsis“ArtificialIntelligenceMarkupLanguage”(AIML).Asimplepatternmatchingexampleis:
PatternMatching…
Themachinethengivesanoutput:
Human:DoyouknowwhoAbrahamLincolnis?
Robot:AbrahamLincolnwastheUSPresidentduringAmericancivilwar.
Chatbot
knowstheansweronlybecausehisorhernameisintheassociatedpattern.Similarly,chatbotsrespondtoanythingrelatingittotheassociatedpatterns.Butitcannotgobeyondtheassociatedpattern.Totakeittoanadvancedlevelalgorithmscanhelp.
HowChatbotActuallyWorks…
Algorithms
Foreachkindofquestion,auniquepatternmustbeavailableinthedatabasetoprovideasuitableresponse.Withlotsofcombinationonpatterns,itcreatesahierarchicalstructure.Weusealgorithmstoreducetheclassifiersandgeneratethemoremanageablestructure.Computerscientistscallita“Reductionist”approach-inordertogiveasimplifiedsolution,itreducestheproblem.
MultinomialNaiveBayesistheclassicalgorithmfortextclassificationandNLP.Foraninstance,let’sassumeasetofsentencesaregivenwhicharebelongingtoaparticularclass.Withnewinputsentence,eachwordiscountedforitsoccurrenceandisaccountedforitscommonalityandeachclassisassignedascore.Thehighestscoredclassisthemostlikelytobeassociatedwiththeinputsentence.
Algorithms…
ForexampleSampleTrainingset
class:greeting“Howyoudoing?”“goodmorning”“hithere”
FewsampleInputsentenceclassification:
input:“Hellogoodmorning”term:“hello”(nomatches)Term:“good”(class:greeting)term:“morning”(class:greeting)classification:greeting(score=2)
Algorithms…
Withthehelpofequation,wordmatchesarefoundforgivensomesamplesentencesforeachclass.Classificationscoreidentifiestheclasswiththehighesttermmatchesbutitalsohassomelimitations.Thescoresignifieswhichintentismostlikelytothesentencebutdoesnotguaranteeitistheperfectmatch.Highestscoreonlyprovidestherelativitybase.
HowChatbotActuallyWorks…
ArtificialNeuralNetworks
NeuralNetworksareawayofcalculatingtheoutputfromtheinputusingweightedconnectionswhicharecalculatedfromrepeatediterationswhiletrainingthedata.Eachstepthroughthetrainingdataamendstheweightsresultingintheoutputwithaccuracy.
ArtificialNeuralNetworks…
Asdiscussedearlierherealso,eachsentenceisbrokendownintodifferentwordsandeachwordthenisusedasinputfortheneuralnetworks.Theweightedconnectionsarethencalculatedbydifferentiterationsthroughthetrainingdatathousandsoftimes.Eachtimeimprovingtheweightstomakingitaccurate.Thetraineddataofneuralnetworkisacomparablealgorithmmoreandlesscode.Whenthereisacomparablysmallsample,wherethetrainingsentenceshave200differentwordsand20classes,thenthatwouldbeamatrixof200×20.Butthismatrixsizeincreasesbyntimesmoregraduallyandcomputationaleffortincreases.Inthiskindofsituation,processingspeedshouldbeconsiderablyhigh.
Therearemultiplevariationsinneuralnetworks,algorithmsaswellaspatternsmatchingcode.Complexitymayalsoincreaseinsomeofthevariations.Butthefundamentalremainsthesame,andtheimportantworkisthatofclassification.
NaturalLanguageUnderstanding(NLU)
Ithas3specificconceptslike:
Entities:EntitybasicallyrepresentsaconceptinyourChatbot.ItmightbeapaymentsysteminEcommerceChatbot,NGOinNGODarpanPortaletc.
Intents:Itisbasicallytheactionchatbotshouldperformwhentheusersaysomething.Forinstance,intentcantriggersamethingifusertypes“Iwanttoorderaredpairofshoes”,“Doyouhaveredshoes?Iwanttoorderthem”or“Showmesomeredpairofshoes”,alloftheseuser’stextshowtriggersinglecommandgivingusersoptionsforRedpairofshoes.
Context:WhenaNLUalgorithmanalyzesasentence,itdoesnothavethehistoryoftheuserconversation.Itmeansthatifitreceivestheanswertoaquestionithasjustasked,itwillnotrememberthequestion.Fordifferentiatingthephasesduringthechatconversation,it’sstateshouldbestored.Itcaneitherbeflagslike“OrderingPizza”orparameterslike“Restaurant:‘Dominos’”.Withcontext,youcaneasilyrelateintentswithnoneedtoknowwhatwasthepreviousquestion.
NaturalLanguageProcessing(NLP)
NaturalLanguageProcessing(NLP)Chatbottakessomecombinationofstepstoconverttheuser’stextorspeechintostructureddatathatisusedtoselecttherelatedanswer.SomeoftheNaturalLanguageProcessingstepsare:
Tokenization:TheNLPdividesastringofwordsintopiecesortokensthatarelinguisticallysymbolicorare
differentlyusefulfortheapplication.
NamedEntityRecognition:Thechatbotprogrammodellooksforcategoriesofwords,likethenameoftheproduct,theuser’snameoraddress,whateverdataisrequired.
Normalization:TheChatbotprogrammodelprocessesthetextinanefforttofindcommonspellingmistakesortypographicalerrorsthatmighteffectwhattheuserintentstoconvey.ThisgivesmorehumanlikeeffectoftheChatbottotheusers.
DependencyParsing:TheChatbotlooksfortheobjectsandsubjects-verbs,nounsandcommonphrasesinthe
user’stexttofinddependentandrelatedphrasesthatusersmightbetryingtoconvey.
SentimentAnalysis:Triestolearniftheuserishavingagoodexperienceorifaftersomepointthechatshouldbeforwardedtothehuman.
NaturalLanguageProcessing(NLP)…
LikemostoftheApplications,theChatbotisalsoconnectedtotheDatabase.Theknowledge
baseorthedatabaseofinformationisusedtofeedthechatbotwiththeinformationneededtogiveasuitableresponsetotheuser.Dataofuser’sactivitiesandwhetherornotyourchatbotwasabletomatchtheirquestions,iscapturedinthedatastore.NLPtranslateshumanlanguageintoinformationwithacombinationofpatternsandtextthatcanbemappedintherealtimetofindapplicableresponses.
ThereareNLPservicesandapplicationsprogramminginterfaces(APIs)thatareusedtobuildthechatbotsandmakeitpossibleforalltypeoforganisations,small,mediumandlargescaletousechatbotservices.ThemainpointhereisthatSmartBotshavethepotentialtohelpincreaseyourbasebyimprovingthesupportservicesandasaresultbooststheefficiencyaswellasprofitsfortheorganisation.
ChatbotwithRasaStackandPython
RASAstackisanopen-sourceAItoolandbeinganopensourceframework,itiseasytocustomize.Clientsusually
donotwanttosharetheirdataandmajorityofthetoolsavailablearecloud-basedandprovidesoftwareasaservice.WithRASA,Onecanbuild,deployorhostRasainternallyinyourserverorenvironmentwithcompletecontrolonit.
Rasacomesupwith2components—
RasaNLU—alibraryfornaturallanguageunderstanding(NLU)whichdoestheclassificationofintentandextracttheentityfromtheuserinputandhelpsbottounderstandwhattheuserissaying.
RasaCore—achatbotframeworkwithmachinelearning-baseddialoguemanagementwhichtakesthestructuredinputfromtheNLUandpredictsthenextbestactionusingaprobabilisticmodellikeLSTMneuralnetwork.
NLUandCoreareindependentandonecanuseNLUwithoutCore,andviceversa.ThoughRasarecommendsusingboth.
RasaNLU
RasaNaturalLanguageUnderstanding(NLU)comprisesofIntentClassification&EntityextractionandcreateastructuredoutputwhichcanbefedintoRasaCore.
Weneedtoteachourbottounderstandourmessagesfirst.Forthat,wehavetotraintheNLUmodelwithinputsinasimpletextformatandextractstructureddata.Wewillachievethisbydefiningtheintentsandprovidingafewwaysusersmightexpressthem.
Tomakethiswork,weneedtodefinesomefiles.Letsfirstunderstandthesefiles.
NLUtrainingfile:Itcontainssometrainingdataintermsofuserinputsalongwiththemappingofintentsandentitiespresentineachofthem.Themorevaryingexamplesyouprovide,betteryourbot’sNLUcapabilitiesbecome.
RasaNLU…
Storiesfile:Thisfilecontainssampleinteractionstheuserandbotwillhave.Rasa(Core)
createsaprobablemodelofinteractionfromeachstory.
Domainfile:Thisfilelistsalltheintents,entities,actions,templatesandsomemoreinformation.Thetemplatesarenothingbutthesamplebotreplywhichcanbeusedasactions.
Objective
LetusbuildtheBot(sayNewsie)whichwillhelptheusertogetthenewsfromaroundtheworldandalsointhespecificcategorieslikesports,business,entertainment,technologiesandmore.
Installation&Setup
Letsfirstdotheenvironmentsetupandforthat,weneedtoinstallpython.WecanusesayConda(miniconda)tosetupthevirtualenvironmentforPython.
First,installtheConda(miniconda)aspertheOS.AfterinstallationchecktheCondaversion.
$conda--versionorconda–V
Incasethereisaneedtoupgrade,runbthefollowingcommand
$condaupdateconda
OnceCondaisinstalledweshouldcreatethevirtualenvironmentandproceedwithfurther
installationofRasaNLUpackages
.
Oncethepackageinstallationisdone,Let'screatetheprojectstructure.Wehavenamedthe
projectasnewsieandsothisisourbaseprojectdirectory.Fortraining/datafiles,wecreateadatadirectoryundernewsieandcreatethetrainingfilenlu.mdinthat.
$mkdirnewsie
$cdnewsie
$mkdirdata
$cddata
$touchnlu.md
ThetrainingdataforRasaNLUisstructuredintodifferentparts:commonexamples,synonyms,regexfeaturesandlookuptables.Whilecommonexamplesistheonlypartthatismandatory,includingotherswillhelptheNLUmodellearnthedomainwithfewerexamplesandalsohelpitbemoreconfidentofitspredictions.
##intent:greet
hey
hello
Hi
##intent:fine_ask
Iamgood,howareyoudoing?
I'mfine,howareyou?
I'mgood,howareyou?##intent:fine_normal
Iamdoinggreat
I'mfine
I'mgood
##regex:zipcode[0-9]{5}
RasaNLU…
##intent:news
RasaNLU…
Sharesomelatestnewsaroundthe[world](category)?
Sharesomelatestnewsin[sports](category)?
Whatisgoingonin[technology](category)?##lookup:currencies<!--lookuptablelist-->
USD
Euro
##intent:thanks
Thanks
Thankyousomuch
##intent:bye
No,Iamgoodasofnow.Bye
Bye,haveagoodday
RasaNLU…
‘##intent:’definesthenameoftheintentand[word](entity)definestheentitye.g.—
[word](category).WehavedefinedmultipleexamplesforBottounderstanditbetter.
Nowwehavetodefinethepipelinethroughwhichthisdatawillflowandintentclassification&entityextractioncanbedoneforthebot.‘spacy_sklearn’isusedaspipeline,createdafilenlu_config.ymlinthebaseprojectdirectory(i.e.newsie)andaddedthepipelineinthat.language:"en"
pipeline:"spacy_sklearn“
NowasourNLUdataandpipelineareready,itistimetotrainthebot.Wecandosoeitherby
runningthescriptinterminalorwecancreateapythonfileandrunit.
Ifusingthescriptinterminal(don’tforgettoactivatethevirtualenvwhereallthepackageswere
installed.Inourcaseitisbotenv)
$condaactivatebotenv
$cdnewsie
$python3-mrasa_nlu.train-cnlu_config.yml--datadata/nlu.md-omodels--fixed_model_namenlu--projectcurrent–verbose
ThiswilltraintheNLUmodelandsaveitat‘models/current/nlu’.ThesamepathispassedtoNLUInterpretertoparsesomesampleintentsfromtheusertoseeifNLUisabletoclassifytheintentandextracttheentitiescorrectly.
RasaCore
Finally,thetrainingofBotisconsideredsuccessfulwhereitisabletounderstandthenatural
languagebutwestillneedtobuildthedialoguessothatbotcanrespondtothemessages.
Aftertrainingthebotweneedtobuildadialoguesmanagementforbottorespondtothemessages.
RasaCoreHighLevelArchitecture
RasaCore—DialogManagement
Earlierwetalkedaboutthestorieswhicharenothingbutthesampleinteractionbetweentheuserandthechatbot.Theuser’sinputsareexpressedasintentswithcorrespondingentities,andchatbotresponsesareexpressedasactions.
Fordialogtraining,Rasahas4maincomponents—
Domain(domain.yml)
Thedomainconsistsoffivekeypartsconsistingofintents,entities,slots,actions,and
templates.Wealreadydiscussedthefirsttwointhepreviouspart,let’sunderstandtherest.
slots:slotsarebasicallybot’smemory.Theyactasakey-valuestorewhichcanbeusedtostoreinformationtheuserprovided(e.gtheirhomecity)aswellasinformationgatheredabouttheoutsideworld(e.g.theresultofadatabasequery).
RasaCore—DialogManagement…
actions:arenothingbutbotsresponsetouserinput.Thereare3kindsofactionsinRasa
Core:defaultactions,utteractions&customactions
templates:templatesaremessagesthebotwillsendbacktotheuser.
Stories(stories.md)
Oncewehavedefinedourdomainnowlet’screateasampleuser-botinteractionasbelow.
Letsputitdowninafile.Let’snamethisfileasstories.mdandkeptitinthedatadirectorywherewekeptthenlu.mdearlier.
$cdnewsie
$cddata
$touchstories.md
Policies(policy.yml)
Therasacorepoliciesdecidewhichactiontotakeateverystepintheconversation.Therearedifferentpoliciestochoosefrom,andonecanincludemultiplepoliciesinasinglerasacoreAgentbutateveryturn,thepolicywhichpredictsthenextactionwiththehighestconfidencewillbeused.
Wehaveconfiguredabasicpolicy(policy.yml)forourbotasshownbelowwhichhasFallbackPolicyaswell.Thefallbackpolicycomesintopicturewhen‘nlu_threshold’&‘core_threshold’meetsthelevelsdefinedinthepolicywhichmeansthatbotisnotabletounderstandtheusermessageanditrespondswith‘utter_default’.
RasaCore—DialogManagement…
CustomActions(actions.py)
Wehavealreadydefinedtheutteractionbyaddinganutterancetemplatetothedomainfilebutifw
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年展板印刷制作合同
- 2026年窗帘布艺安装与维护合同协议
- 2026年外卖平台加盟合同
- 家政服务合同范本2026
- 家政行业培训内容
- 2026届新高考英语冲刺复习 语篇及图形组织器助力高中英语记叙文读写教学
- 家政清洁工具培训课件
- 家庭消防安全课件
- 建筑施工企业项目经理年终总结
- 培训方案安全提示内容课件
- 2025年中职食品雕刻(食品雕刻技术)试题及答案
- 2026青海西宁市湟源县水务发展(集团)有限责任公司招聘8人考试参考试题及答案解析
- 2025年大学(运动康复)运动康复治疗技术测试试题及答案
- 1256《数据库应用技术》国家开放大学期末考试题库
- 配电红外测温课件
- 美容院店长年度总结课件
- 江苏省2025年普通高中学业水平合格性考试历史试卷(含答案详解)
- 小学阶段人工智能在激发学生学习动机中的应用研究教学研究课题报告
- 2025年山西大地环境投资控股有限公司社会招聘116人备考题库及完整答案详解一套
- (2025年)昆山杜克大学ai面试真题附答案
- 2025医美行业白皮书-罗兰贝格x美团医美-202508
评论
0/150
提交评论