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

Latest commit

 

History

History
113 lines (76 loc) · 5.84 KB

File metadata and controls

113 lines (76 loc) · 5.84 KB

QnA 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 response 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. These info are needed when access 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 C#. The bot is fairly simple, but if you're new to Microsoft bot builder, several of the concepts might be foreign. For a quick ramp up check out aka.ms/botcourse, specifically the sections about setting up a C# project, using cards and using dialogs.

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

    See QnAService/Client.cs

    client.BaseAddress = new Uri(baseUrl);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //ACCEPT header
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

    ...

    var requestUri = $"knowledgebases/{knowledgebaseId}/generateAnswer";
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri);
    var body = $"{{\"question\": \"{question}\", \"top\": \"{top}\"}}";
If request succeed 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 get back in request body.

If request succeed, but no answers found in the knowledge base, it could be:
- Just there is no answer for that question
- The question doesn't really have a match in knowledge base
  1. Propose question and answer If no answers found to the question, we propose 3 most frequently ask questions, together with the answers in a carousel list:

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

Run locally

Set up the environment for your bot as described here. Install Visual Studio 2017 for Windows and update all extensions. Download the Bot Application, Bot Controller, and Bot Dialog templates and install it as instructed.

Update Config

  • In Web.config file, replace values from your QnA service you created above.
<add key="QnABaseUrl" value="$value$"/>
<add key="QnAKnowledgeBaseId" value="$value$"/>
<add key="QnASubscriptionKey" value="$value$"/>

Usage with bot emulator

Generate answers: Generate answer

Propose question/answer: Propose question and answer

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 Web.config file, replace $MicrosoftAppId$ and $MicrosoftAppPassword$ with values obtained during the bot registration from the previous step. Also, replace $BotId$ with what you want your bot Id to be. Conventionally, Bot Id is set to "28:$MicrosoftAppId$".
<appSettings>
    <!-- update these with your BotId, Microsoft App Id and your Microsoft App Password-->
    <add key="BotId" value="$BotId$" />
    <add key="MicrosoftAppId" value="$MicrosoftAppId$" />
    <add key="MicrosoftAppPassword" value="$MicrosoftAppPassword$" />
</appSettings>

Deploy to Cloud

Follow the instructions here to deploy the bot to Azure.

More Information

To get more information about the Microsoft Bot Framework and the Microsoft QnA Maker, please review the following resources: