网络研讨会开发聊天机器人Python04_第1页
网络研讨会开发聊天机器人Python04_第2页
网络研讨会开发聊天机器人Python04_第3页
网络研讨会开发聊天机器人Python04_第4页
网络研讨会开发聊天机器人Python04_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论