Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Chatbots bauen mit dem Microsoft Bot Framework

82 views

Published on

Slides for a talk presented at dotnet Cologne 2019

Published in: Software
  • Be the first to comment

  • Be the first to like this

Chatbots bauen mit dem Microsoft Bot Framework

  1. 1. Microsoft Bot Framework .NET Edition für Microsoft Bot Framework Version 4 Jens Siebert (@jens_siebert) dotnet Cologne, 10. Mai 2019 https://www.slideshare.net/JensSiebert1
  2. 2. Über mich • Senior Software Engineer bei B.Braun Melsungen AG • Fullstack IoT Developer (Infusionspumpen und Cloud-Services) • Speaker bei den .NET User Groups Paderborn und Kassel und beim WebMontag Kassel
  3. 3. Chatbots A chatbot is an application, often available via messaging platforms and using some form of intelligence, that interacts with a user via a conversational user interface (CUI). (Joe Mayo, Programming the Microsoft Bot Framework)
  4. 4. Messaging Platform Chatbots Chatbot Service Service Service AI Service (z.B. NLP) Conversational UI Backend Business Logic User Interface
  5. 5. Messaging Plattformen
  6. 6. Conversational User Interface Turn
  7. 7. Conversational User Interface Waterfall
  8. 8. Conversational User Interface VoiceUserInterface TextUserInterface Speech Speech Recognition Text Text Natural Language Processing Intent Intent Intent Handling Request Response Intent Handling Text Text Speech Synthesis Speech
  9. 9. Beispiele
  10. 10. Warum sollte ich mich damit beschäftigen? […] as messaging apps have grown to dominate both phones and workplaces, we see conversations with other humans being supplemented by intelligent chatbots. As these platforms improve, they will learn to understand the context and intent of conversations, making interactions more lifelike and therefore more compelling. The explosion of interest in the marketplace and mainstream media leads to a corresponding rise in developer interest […] (ThoughtWorks Technology Radar, Volume 16)
  11. 11. Warum sollte ich mich damit beschäftigen? http://www.businessinsider.de/the-messaging-app-report-2015-11
  12. 12. Warum sollte ich mich damit beschäftigen? https://www.twilio.com/learn/commerce-communications/how-consumers-use-messaging
  13. 13. Warum sollte ich mich damit beschäftigen? https://www.twilio.com/learn/commerce-communications/how-consumers-use-messaging
  14. 14. Warum sollte ich mich damit beschäftigen? https://www.gartner.com/smarterwithgartner/top-trends-in-the-gartner-hype-cycle-for-emerging-technologies-2017/
  15. 15. Anwendungsfälle für Chatbots • Virtual Assistant • Customer Care • Enterprise Productivity • IoT
  16. 16. Vorteile und Nachteile • Konversation: CUIs bieten, durch Nutzung geschriebener oder gesprochener Sprache, einen natürlicheren Zugang zu Informationen. • Kontext: Es finden keine Kontextwechsel (z.B. unterschiedliche Bedienparadigmen bei mobilen Apps) statt. • Bereitstellung: Die Bereitstellung eines Chatbots ist für den Anwender transparent. Keine Installation, keine Updates, immer aktuell. • Geräte-unabhängig: Die Interaktion mit dem Chatbot kann mit allen Geräten erfolgen, die von einer Messaging-Plattform unterstützt werden. • Plattform-unabhängig: Die Interaktion mit dem Chatbot kann mit allen Plattformen erfolgen, die von einer Messaging-Plattform unterstützt werden. • Notwendigkeit: Es gibt bereits eine erfolgreiche mobile App für einen Service. Welche Vorteile bringt ein zusätzlicher Chatbot? • Angemessenheit: Ist ein CUI die angemessene Benutzerschnittstelle für einen Service? • Kritikalität: Bietet ein Chatbot die richtige Form der Interaktion für einen Service?
  17. 17. Das Microsoft Bot Framework Bot Connector Channels Azure Bot Service Chatbot (ASP.NET/Node.js) (Python/Java) Backend Services AI Services (LUIS) Bot Builder SDK Bot Builder .NET Bot Builder Node.js Bot Builder Python Bot Builder Java
  18. 18. Turn Bot Connector Channel Chatbot Backend Service AI Service Activity Route Message Query Query Response Response Response Route Response
  19. 19. Waterfall WaterfallDialog <Step> Enter first name <Step> Enter last name <Step> Enter e-mail address <Step> Enter company name input input input
  20. 20. Das Microsoft Bot Framework • Bot Connector Client • Activities • Dialog-Management • State-Management • GUI-Elemente • Anbindung an AI-Services (z.B. Cognitive Services)
  21. 21. Quickstart https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-sdk-quickstart
  22. 22. Quickstart
  23. 23. Quickstart
  24. 24. Quickstart (.NET Core CLI) dotnet new -i Microsoft.Bot.Framework.CSharp.EmptyBot dotnet new emptybot -n MyEmptyBot
  25. 25. Bot Framework Emulator https://docs.microsoft.com/en-us/azure/bot-service/bot-service-debug-emulator
  26. 26. Bot Framework Emulator
  27. 27. Bot-Klasse public class DNUGPBBot : ActivityHandler { public DNUGPBBot(ConversationState conversationState, UserState userState) { _conversationState = conversationState; _userState = userState; _dialogSet = new DialogSet(_conversationState.CreateProperty<DialogState>(nameof(DialogState))); _dialogSet.Add(new SearchDialogBasic(nameof(SearchDialogBasic)); } protected override async Task OnMessageActivityAsync( ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) { var dialogContext = await _dialogSet.CreateContextAsync(turnContext, cancellationToken); var results = await dialogContext.ContinueDialogAsync(cancellationToken); if (results.Status == DialogTurnStatus.Empty) { await dialogContext.BeginDialogAsync( nameof(SearchDialogBasic), null, cancellationToken); } } […] } ActivityTypes.Message ActivityTypes.ConversationUpdate ActivityTypes.ContactRelationUpdate ActivityTypes.Typing ActivityTypes.Ping ActivityTypes.EndOfConversation ActivityTypes.Trigger ActivityTypes.Event ActivityTypes.Invoke ActivityTypes.DeleteUserData ActivityTypes.InstallationUpdate ActivityTypes.MessageReaction
  28. 28. Einfache Dialog-Klasse public class SearchDialogBasic : ComponentDialog { public SearchDialogBasic(string dialogId) : base(dialogId) { var waterfallSteps = new WaterfallStep[] { EventFilterStep, EventSelectionStep, EventDetailsStep }; AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps)); AddDialog(new ChoicePrompt(nameof(ChoicePrompt))); InitialDialogId = nameof(WaterfallDialog); } private static async Task<DialogTurnResult> EventFilterStep(WaterfallStepContext stepContext, […]) { } private static async Task<DialogTurnResult> EventSelectionStep(WaterfallStepContext stepContext, […]) { } private static async Task<DialogTurnResult> EventDetailsStep(WaterfallStepContext stepContext, […]) { } }
  29. 29. Eingabeaufforderungen private static async Task<DialogTurnResult> EventFilterStep( WaterfallStepContext stepContext, CancellationToken cancellationToken) { var options = new[] { "All Events", "Upcoming Events", "Past Events" }; return await stepContext.PromptAsync(nameof(ChoicePrompt), new PromptOptions { Prompt = MessageFactory.Text("Which Events would you like too see?"), Choices = ChoiceFactory.ToChoices(options), RetryPrompt = MessageFactory.Text("Please select a valid option!") }, cancellationToken); } } private static async Task<DialogTurnResult> EventSelectionStep( WaterfallStepContext stepContext, CancellationToken cancellationToken) { var selection = ((FoundChoice)stepContext.Result).Value; […] } Microsoft.Bot.Builder.Dialogs.AttachmentPrompt Microsoft.Bot.Builder.Dialogs.ChoicePrompt Microsoft.Bot.Builder.Dialogs.ConfirmPrompt Microsoft.Bot.Builder.Dialogs.DateTimePrompt Microsoft.Bot.Builder.Dialogs.NumberPrompt<T> Microsoft.Bot.Builder.Dialogs.TextPrompt
  30. 30. Dialog-basierte Interaktion private static async Task<DialogTurnResult> EventFilterStep( WaterfallStepContext stepContext, CancellationToken cancellationToken) { return await stepContext.PromptAsync(nameof(ChoicePrompt), […] } private static async Task<DialogTurnResult> EventSelectionStep( WaterfallStepContext stepContext, CancellationToken cancellationToken) { var selection = ((FoundChoice)stepContext.Result).Value; […] } Suspended Resumed StartedBeginDialogAsync() context.PromptAsync() context.PromptAsync() OnTurnAsync() EndDialogAsync()
  31. 31. SearchDialog Interaktion mit mehreren Dialogen RootDialog <Step> EventFilter <Step> EventSelection <Step> EventDetails EventRegistrationDialog ProfileCreationDialog […] […] search register create
  32. 32. Der Dialog-Stack DialogStack RootDialog BeginDialogAsync( RootDialog) DialogStack RootDialog SearchDialog BeginDialogAsync( SearchDialog) DialogStack RootDialog SearchDialog EndDialogAsync(result)
  33. 33. Der Dialog-Stack DialogStack RootDialog SearchDialog BeginDialogAsync( SearchDialog) DialogStack RootDialog SearchDialog DialogStack RootDialog SearchDialog EndDialogAsync( result) RegDialog BeginDialogAsync( RegDialog, eventId) RegDialog EndDialogAsync( result) DialogContext.BeginDialogAsync DialogContext.CancelAllDialogsAsync DialogContext.ContinueDialogAsync DialogContext.EndDialogAsync DialogContext.PromptAsync DialogContext.ReplaceDialogAsync DialogContext.RepromptDialogAsync
  34. 34. Dialoge erweitern mit UI-Elementen
  35. 35. Message Attachments & Rich Cards • Animation Card • Audio Card • Video Card • Hero Card • Thumbnail Card • Receipt Card • SignIn Card • SuggestedAction • CardCarousel • Adaptive Card Activity Attachment Rich Card Media Entity Speech
  36. 36. Adaptive Cards var adaptiveCard = new AdaptiveCard("1.0"); adaptiveCard.Body.Add(new AdaptiveTextBlock() { Text = $"### {eventDetails.Name}", }); adaptiveCard.Body.Add(new AdaptiveTextBlock() { Text = converter.Convert(eventDetails.ShortDescription), Wrap = true }); adaptiveCard.Body.Add(new AdaptiveImage() { Url = new Uri(string.Format($"https://dev.virtualearth.net/REST/v1/Imagery/Map/[...])), Size = AdaptiveImageSize.Stretch }); adaptiveCard.Body.Add(new AdaptiveTextBlock() { Text = $"{eventLocation.Name}, {eventLocation.Street}, {eventLocation.PostCode} {eventLocation.City}" }); var attachment = new Attachment() { ContentType = AdaptiveCard.ContentType, Content = adaptiveCard }; reply.Attachments = new List<Attachment>() { attachment };
  37. 37. Natural Language Processing (NLP) private static async Task<DialogTurnResult> OptionDisplayStep(WaterfallStepContext stepContext, CancellationToken cancellationToken) { var options = new[] { "Search for Events", "Register for Event", "Create Profile" }; return await stepContext.PromptAsync(nameof(ChoicePrompt), […] } private static async Task<DialogTurnResult> OptionSelectionStep(WaterfallStepContext stepContext, CancellationToken cancellationToken) { var selection = ((FoundChoice)stepContext.Result).Value; if (selection.ToLower().StartsWith("search")) { […] } if (selection.ToLower().StartsWith("create")) { […] } […] } - „Kommando-artig“ - Keine natürliche Sprache
  38. 38. Language Understanding Intelligent Service https://www.luis.ai Utterances „Show me a list of upcoming events“ Intents „Search“ Entities SearchOptions
  39. 39. Modellierung
  40. 40. Training & Publishing
  41. 41. LUIS Modell im Code einbinden private static async Task<DialogTurnResult> EventSelectionStep(WaterfallStepContext stepContext, CancellationToken cancellationToken) { var luisApplication = new LuisApplication(ApiKeys.LuisAppId, ApiKeys.LuisApiKey, "https://" + ApiKeys.LuisApiHostName); var luisRecognizer = new LuisRecognizer(luisApplication); var result = await luisRecognizer.RecognizeAsync(stepContext.Context, cancellationToken); var (intent, score) = result.GetTopScoringIntent(); if (intent.Equals("None")) { await stepContext.Context.SendActivityAsync( "Sorry, I didn't get that. […]", cancellationToken: cancellationToken); } if (intent.Equals("Search")) { if (result.Entities.HasValues) { var entity = result.Entities["SearchOption"].First().First().ToString(); […] } } }
  42. 42. Azure Bot Service erstellen https://portal.azure.com
  43. 43. Veröffentlichen aus Visual Studio
  44. 44. Testen in Web Chat
  45. 45. Kanäle konfigurieren
  46. 46. Beispiel: DNUGPBBot in Skype
  47. 47. Weitere Themen • State Data Management • Localization • Hand-off to Human • Cortana Skills • QnA Maker
  48. 48. //build 2019 Update Adaptive Dialogs Language Generation Common Expression Language New Botframework Channels Bot Framework Skills Virtual Assistant TemplateBot Inspector Enhanced Language Understanding QnA Maker Enhancements
  49. 49. Vielen Dank! https://dev.botframework.com https://docs.microsoft.com/bot-framework Slides: https://www.slideshare.net/JensSiebert1 Code: https://github.com/jsiebert/DNUGPBBot Demo-Bot (WebChat): http://dnugpbbot.azurewebsites.net Twitter: @jens_siebert

×