QnA Maker Dialog for Bot Framework

Update: The QnA Maker Dialog v3 is now available.  It adds support for v3 of the Microsoft QnA Maker API, including returning multiple answers and use of metadata to filter / boost answers that are returned.  You can read more about this and a new QnA Maker Sync library that is now also available on the announcement blog here.  Also, I have previously released an update to the QnAMakerDialog which supports adding rich media attachments to your Q&A responses.

The QnA Maker service from Microsoft, part of the Cognitive Services suite, allows you to quickly build, train and publish a question and answer bot service based on FAQ URLs or structured lists of questions and answers. Once published you can call a QnA Maker service using simple HTTP calls and integrate it with applications, including bots built on the Bot Framework.

Right now, out of the box, you will need to roll your own code / dialog within your bot to call the QnA Maker service. The new QnAMakerDialog which is now available via NuGet aims to make this integration even easier, by allowing you to integrate with the service in just a couple of minutes with virtually no code.

The QnAMakerDialog allows you to take the incoming message text from the bot, send it to your published QnA Maker service and send the answer sent back from the service to the bot user as a reply. You can add the new QnAMakerDialog to your project using the NuGet package manager console with the following command, or by searching for it using the NuGet Manager in Visual Studio.

Below is an example of a class inheriting from QnAMakerDialog and the minimal implementation.

When no matching answer is returned from the QnA service a default message, “Sorry, I cannot find an answer to your question.” is sent to the user. You can override the NoMatchHandler method to send a customised response.

For many people the default implementation will be enough, but you can also provide more granular responses for when the QnA Maker returns an answer, but is not confident in the answer (indicated using the score returned in the response between 0 and 100 with the higher the score indicating higher confidence). To do this you define a custom hanlder in your dialog and decorate it with a QnAMakerResponseHandler attribute, specifying the maximum score that the handler should respond to.

Below is an example with a customised method for when a match is not found and also a hanlder for when the QnA Maker service indicates a lower confidence in the match (using the score sent back in the QnA Maker service response). In this case the custom handler will respond to answers where the confidence score is below 50, with any obove 50 being hanlded in the default way. You can add as many custom handlers as you want and get as granular as you need.

Hopefully you will find the new QnAMakerDialog useful when building your bots and I would love to hear your feedback. The dialog is open source and available in my GitHub repo, along side the other additional dialog I have created for the Bot Framework, BestMatchDialog (also available on NuGet).

I will be publishing a walk through of creating a service with the QnA Maker in a separate post in the near future, but if you are having trouble with that, or indeed the QnAMakerDialog, in the mean time then please feel free to reach out.

39 thoughts to “QnA Maker Dialog for Bot Framework”

  1. Hi

    I have problem with unicode char when use on facebook messenger. The text display is like this: ” thông báo và tác”.

    It worked well on emulator, how can i change format text in QnAMakerDialog?

    Thank you.

  2. Dear Gary,
    Thank you very much for your helpful information. would you please answer the following questions?
    Is the LUIS support Persian language?
    Is it possible to generate CODE by LUIS and use it in an application without inserting URL of LUIS?
    Thank you in advance for taking time to reply me.
    I look forward to hearing from you.
    With best regards
    Ebrahimi

    1. Hi. Right now LUIS only supports English, Spanish, French, Italian and Chinese. Also you must use this as a service and you cannot generate code from LUIS right now. You can export your model if you wish, but this wouldn’t include all of the training already carried out on the model, so this probably isn’t very useful for your I’m afraid.

  3. Hey Gary, thanks so much for this! And the BestMatchDialog – both so useful

    Wanted to ask – is there an easy way for me to change the subscription key and knowledgebase key at runtime? I want to have different users using different QnA Services but from the same bot, so need a method of redirecting.

    Thanks!

    1. Interesting use case. I will look to get this update with a constructor maybe when you instantiate the dialog so that you can pass in the pair you want at runtime. How does that sound?

        1. Hi again. I have the changes made and in my repo. You will now be able to set the sub id and knowledge base id at runtime via public properties on the QnAMakerDialog and this will override the attribute on the class (which actually isn’t needed anymore if you handle things as below, but you could still have one in place to fall back to as a default). e.g.

          return Chain.From(() => new Dialogs.QnADialog() { SubscriptionKey = “YOUR_ID”, KnowledgeBaseId = “YOUR_ID” });

          I will be updating NuGet later on tonight hopefully so this change should be available tomorrow (note, another version (1.1) went to NuGet earlier but this is not included – this will be 1.2).

          Does that help?

          1. Hi. Just to let you know I needed to get a bug fix into NuGet, so this change has now made it in to v1.1.1. Should be available in a few hours. Let me know how you get on.

          2. Great news! Let me know if you need anything else. Also check out the new attachment feature too 🙂

          3. Brilliant! I was actually going to implement something similar to allow for buttons on the responses.

            What attachment types are you currently supporting?

          4. Right now I support any attachment type that bot framework supports. Buttons I don’t do at the moment, but that’s an interesting scenario. I’ll have a think today about how I could implement it and try and get something in the dialog for you.

          5. Sorry, just to be clear, right now it is Media Attachments that I support. Not actions (e.g. buttons or cards).

          6. Oh sorry I missed the one above! Obviously that would be fantastic! Personally I was just going to add the json of an Activity.Attachment/hero card object, and actually include the text within that. Bit messy I guess.

            Also was a bit worried that would impact the training of the NLP? Or is that unlikely?

          7. You are right. It could do, so it is definitely something to take into account when you are training.

          8. In which case I was thinking to just include the post back value of the button. As this would likely be a related section name or topic, it could actually help the training as this would by virtue be a relevant keyword(s).

            In response to that postback I pull out the section from my FAQ database.

            I feel this gives me the ability to provide both the answer to what QnA service thinks they are asking and immediate links to related areas of my FAQ tree.

            Does that sound sensible?

    1. Sorry, I think I now do understand your question. If you mean can you update the questions / answers without redeploying the bot, then yes you can. You simply log into the dashboard and update the service, re-train it and publish it. Hope that helps. If not then let me know.

  4. Hey,
    I had a couple of questions
    1) Can we integrate LUIS and qna maker together in a bot is that possible?
    2) In qna maker we can only feed in question and answers so what is the use of your plugin?

    1. Hi. Thanks for reaching out.

      1. Integrating LUIS and QnA Maker -> Yes, you can fallback to QnA Maker from LUIS or vice versa (or in fact from any dialog to any dialog). See the section in the readme here about using the QnA Maker dialog as a child dialog to see an example implementation https://github.com/garypretty/botframework/tree/master/QnAMakerDialog

      2. You are right that QnA Maker only supports text out of the box. The initial point of my dialog was to allow you to integrate without needing to write the calls to the service yourself. However, I have extended it so that you can add your own markup to send attachments down with your answers also (card support coming soon). You can see the readme with examples at https://github.com/garypretty/botframework/tree/master/QnAMakerDialog and also see my block post about attachments here -> http://www.garypretty.co.uk/2017/02/23/adding-rich-attachments-to-your-qnamaker-bot-responses/

  5. While downloading the latest version i.e v1.1.1 NuGet package in Visual Studio, I am getting the following error message:

    Attempting to gather dependencies information for package ‘QnAMakerDialog.1.0.0’ with respect to project ‘Samples\QnAMaker\QnAMakerSampleBot’, targeting ‘.NETFramework,Version=v4.6’
    An error occurred while retrieving package metadata for ‘Microsoft.Rest.ClientRuntime.2.3.2’ from source ‘D:\17March\BotBuilder-CognitiveServices-master\CSharp\packages’.
    ========== Finished ==========
    Can you please help me to resolve this issue.

  6. Hey gary,
    Your post was so helpful.
    Actually the bot that I am trying to create is, if i upload a file to the bot, it should understand the context and it must be able to reply with an answer to the question asked. The file format will not contain in the question answer format rather It will contain the whole bunch of paragraphs. can u help me by providing some ideas in this case.

    Else if i upload a document with Q and A format, how can i integrate the qna maker with the bot that im developing in node.js.
    Thanks in advance.

  7. Hi Gary,

    I am using your QNA Maker Dialog successfully, except that I get no response when the question includes one or several quotation marks (“”). Could this be a problem?

      1. I also have them in answers, there is no problem. But for me it’s not working in questions. Thanks for looking into it

  8. Hi Gary,

    I’m wondering – if during a QnA conversation I said a keyword like ‘Sandwich’ how I might pass control to a FormFlow dialog. Would you do that in one of the overrides somehow ?

    1. You could use a scorable dialog to interrupt the conversation flow and then return at the appropriate point. Scorables allow you to monitor incoming messages and take over the conversation if appropriate. I have a blog post on it if you check out my recent posts.

  9. Thank you so much for all of the help Gary! Between you and James E Manns youtubes I was able to build my bot in Visual Studio and it is running great. I am new to coding and using a lot of languages I dont fully understand so forgive me if this sounds like a stupid question.. Following your and James instruction I have built my QnABot in VS and it is running great in the emulator. How do I now embed this bot into a webpage? I would like to create a basic facebook style pop up chat with the QnA maker that is on every page of my website but I cannot find any instruction on how to actually embed and link up the bot and get it working on my page and not just the emulator. Any help would GREATLY appreciated!

    Thank You Thank You

Leave a Reply to ARITRO MUKHERJEE Cancel reply

Your email address will not be published. Required fields are marked *