




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 临时供应合同范本
- 企业修路合同范本
- 2025年衡水驾驶员货运从业资格证模拟考试题
- 中介交易服务合同范本
- 会展项目服务合同范例
- 2025年昆明道路货运从业资格证模拟考试官方题下载
- 修车配件合同范本
- 出租合同范本版
- 农村水源地租赁合同范本
- 与演员合作合同范本
- 收费站稽查管理制度
- 老年心房颤动诊治中国专家共识(2024)解读
- NB-T31056-2014风力发电机组接地技术规范
- 部编版八年级上册历史期中复习重点总结
- DL5190.5-2019电力建设施工技术规范第5部分:管道及系统
- 农信银支付系统文档
- 华为认证HCIA-Security安全H12-711考试题库及答案
- 建筑工地春节前安全教育
- (正式版)YST 1682-2024 镁冶炼行业绿色工厂评价要求
- DL-T 5148-2021水工建筑物水泥灌浆施工技术条件-PDF解密
- JGJ6-2011 高层建筑筏形与箱形基础技术规范
评论
0/150
提交评论