Post

RSS and Multi-FAQ Chatbot: FAQ Implementation Learnings (Part 4)

This post is part of a series of blog posts about a chat bot RazType™ that I implemented recently. Given that chat bots are advertised as a modern application that’s super easy to do, I decided to implement a prototype to learn through hands-on experience. There are 5 parts to this series:

  1. High Level Design (Part 1)
  2. Choices and Decisions (Part 2)
  3. RSS Implementation Learnings (Part 3)
  4. FAQ Implementation Learnings (Part 4) — this post
  5. Adding the Facebook Channel (Part 5)

Parts 4 and 5 are code heavy. For the readers who prefer getting the full source rather than going through snippets, you may find the source code here.

Basic Building Block: QnAMaker

A lot of my base code and reference links that I used were already covered in Part 3. So I will dive straight into the topic of this post.

Building an FAQ chat bot is fairly straight forward with Azure Bot Services. This is because we just need to use 1 main cognitive service: QnA Maker.

QnA Maker makes it easy to create an FAQ knowledge base because it can scrape your FAQ content from your public FAQ page, spreadsheet, PDF and/or other sources.

20181218-chatbot-qnamaker.png

Challenge: Multiple QnA Makers

Now this sounded like a super easy task at first. The plan was simply to use the sample code in Microsoft’s botbuilder-samples and it should work. But the challenge I had was that there was no sample (at least at the time when I was building this proof-of-concept) that shows how I can separately query multiple knowledge bases in 1 code.

As in the design below, I initially planned to have at least 6 separate FAQ knowledge bases. But I want my code to be able to handle more government agencies than what is designed.

20181010-chatbot-dialog3

Solution: REST API Call

After attempts of different methods and after scourging the internet, the best solution I found was recommended here. The best and simplest way to do this was not to use any QnA maker related SDK but instead do a basic REST API call.

With this advise, I wrote the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var get_restQnA = function (hostname, knowledgeId, authkey, question, callback){

    qnaurl=`${hostname}/knowledgebases/${knowledgeId}/generateAnswer`;
    authorizationKey = `EndpointKey ${authkey}`;

    fetch(qnaurl, {
        method: 'POST',
        headers: {
            "Content-Type": "application/json",
            "Authorization": authorizationKey
        },
        body: JSON.stringify({
            "question": question,
            "top": 1
        })
    }).then(response => {
        return response.json();
    }).then(data => {
        if (data && data.answers && data.answers.length != 0)
        {
            var answer = data.answers[0];
            callback(`${answer.answer} (Confidence Score: ${answer.score})`);
        }
    }).catch(err => {console.log(err);});
}

Alternative Solution: Dispatch

As mentioned at the beginning of this blog series, this proof-of-concept was built using Bot Service SDK v3. Now with v4, a cleaner/nicer Bot Service Dispatch was introduced. This service allows you to more logically have multiple LUIS and QnA models in a single bot.

20181218-chatbot-dispatch.png

Now I haven’t actually tried this to share my learnings, but feel free to try it out! The how-to is documented here.

This post is licensed under CC BY 4.0 by the author.