Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

QnA Bot Sample

Deploy to Azure

Description

QnA is a bot that helps you get answers based on FAQs. The bot uses Azure Cognitive knowledge base QnAMaker service. You can ask Azure Support questions and either get the answers to the question, or get proposed answer/question if the asked question doesn't have answers from the knowledge base.

Bot Demo

To add the QnA demo bot to your Skype account, click here.

Background

Microsoft QnA Maker is a free, easy-to-use, REST API and web-based service that trains AI to respond to a user's questions in a more natural, conversational way (read more).

Create your own QnA Maker service

  1. Create a knowledge base

Click here to login into QnA Maker service website. Click "Create new service" to open the page for creating knowledge base. Fill in "service name" and "faq urls", then click "Create". (More details can be found here.)

Create a knowledge base

  1. Publish the knowledge base

Click "publish" button to publish your knowledge base. (More details can be found here.)

Publish the knowledge base

  1. Get knowledge base ID and Ocp-Apim-Subscription-Key From "My services" -> "View code", you can find the knowledge base ID and Ocp-Apim-Subscription-Key. This information is needed when accessing your knowledge base.

Get knowledge base ID and Ocp-Apim-Subscription-Key

Build your bot: Call QnA Maker service from Microsoft bot framework project

  1. The bot I will demonstrate is built in Node.js. The bot is fairly simple, but if you're new to Microsoft bot builder, several concepts might be foreign. For a quick ramp-up check out aka.ms/botcourse, specifically the sections about setting up a node project, using cards and using dialogs.

  2. Generate answers for question: Build a POST requst based on QnA Maker service generate-answer API.

    const headers = {
        'content-type': 'application/json',
        'Ocp-Apim-Subscription-Key': process.env.OCP_APIM_SUBSCRIPTION_KEY
    };

    function query(question, counts) {
        return new Promise(
            (resolve, reject) => {
                let url = buildUrl() + '/generateAnswer';
                if (url && headers) {
                    const requestData = {
                        url: url,
                        headers: headers,
                        body: JSON.stringify({
                            question: question,
                            top: counts
                        })
                    };

                    request.post(requestData, (error, response, body) => {
                        if (error) {
                            reject(error);
                        } else if (response.statusCode != 200) {
                            reject(body);
                        } else {
                            resolve(body);
                        }
                    });
                } else {
                    reject('The request url or headers is not valid.');
                }
            }
        );
If the request succeeds and answers are found in the knowledge base, then return results will be JSON of: answer, score of match. You can specify how many answers to return in request body.
  1. Propose question and answer If no answers were found to the question, we propose 3 most frequently ask questions, together with the answers in a carousel:

    Build a GET http request based on QnA Maker service download API.

    function download() {
        return new Promise(
            (resolve, reject) => {
                let url = buildUrl();
                if (url && headers) {
                    const requestData = {
                        url: url,
                        headers: headers
                    };

                    request.get(requestData, (error, response, body) => {
                        if (error) {
                            reject(error);
                        } else if (response.statusCode != 200) {
                            reject(body);
                        } else {
                            resolve(body);
                        }
                    });
                } else {
                    reject('The request url or headers is not valid.');
                }
            }
        );
    }

    function buildUrl() {
        const url = process.env.QNA_SERVICE_API_URL;
        return url + process.env.KNOWLEDGE_BASE;
    }

Run locally

Set up the environment for your bot as described here. Install Node.js and npm if not already installed, and install the Bot Builder SDK for Node.js and restify as instructed.

Update Configurations

In the .env file, add QnA Maker service API Variables including: OCP_APIM_SUBSCRIPTION_KEY, KNOWLEDGE_BASE and QNA_SERVICE_API_URL.

# QnA Maker service API Variables
OCP_APIM_SUBSCRIPTION_KEY='value'
KNOWLEDGE_BASE='value'
QNA_SERVICE_API_URL='https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/'

Deploy the Bot Sample

Register the Sample Bot

Register the sample bot following this link, and make a note of the Microsoft App ID and Password to update the configurations of your bot.

Update Config

  • In the .env file, add values for MICROSOFT_APP_ID, and MICROSOFT_APP_PASSWORD with values obtained during the bot registration process.
MICROSOFT_APP_ID='value'
MICROSOFT_APP_PASSWORD='value'

Test your bot

Either deploy the bot to an Azure web app and fill in the process.env variables in the portal's app Settings, or test it locally using bot Emulator.

Generate answers: Generate answer

propose question/answer: Propose question and answer