Skip to content

Commit

Permalink
Cleanup javascript_nodejs/49.qnamaker-all-features sample (#2986)
Browse files Browse the repository at this point in the history
* cleanup default params in sample 49
* for javascript_nodejs

* directly use SDK's QnAMakerDialog
* unify sample headers
* cleanup additional unnecessary code
  • Loading branch information
stevengum authored Dec 15, 2020
1 parent 82870e2 commit 7dbebc2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 140 deletions.
7 changes: 4 additions & 3 deletions samples/javascript_nodejs/49.qnamaker-all-features/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# QnA Maker

Bot Framework v4 QnA Maker bot sample. This sample shows how to integrate Multiturn and Active learning in a QnA Maker bot with ASP.Net Core-2. Click [here][72] to know more about using follow-up prompts to create multiturn conversation. To know more about how to enable and use active learning, click [here][71].
Bot Framework v4 QnA Maker bot sample. This sample shows how to integrate Multiturn and Active learning in a QnA Maker bot with Node.js. Click [here][72] to know more about using follow-up prompts to create multiturn conversation. To know more about how to enable and use active learning, click [here][71].

This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a bot that uses the [QnA Maker Cognitive AI](https://www.qnamaker.ai) service.

Expand Down Expand Up @@ -117,13 +117,14 @@ This bot uses [QnA Maker Service](https://www.qnamaker.ai), an AI based cognitiv

[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel.

- Install the Bot Framework Emulator version 4.9.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)

- Install the latest Bot Framework Emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)

### Connect to the bot using Bot Framework Emulator

- Launch Bot Framework Emulator
- File -> Open Bot
- Enter a Bot URL of `http://localhost:3999/api/messages`
- Enter a Bot URL of `http://localhost:3978/api/messages`

# QnA Maker service
QnA Maker enables you to power a question and answer service from your semi-structured content.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

const { ActivityHandler } = require('botbuilder');
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

const { QnAMakerDialog } = require('botbuilder-ai');
const {
ComponentDialog,
DialogSet,
DialogTurnStatus,
WaterfallDialog
WaterfallDialog,
} = require('botbuilder-dialogs');

const {
QnAMakerBaseDialog
} = require('./qnamakerBaseDialog');
const { MessageFactory } = require('botbuilder');

const INITIAL_DIALOG = 'initial-dialog';
const ROOT_DIALOG = 'root-dialog';
const QNAMAKER_BASE_DIALOG = 'qnamaker-base-dailog';
const QNAMAKER_BASE_DIALOG = 'qnamaker-base-dialog';

/**
* Creates QnAMakerDialog instance with provided configuraton values.
*/
const createQnAMakerDialog = (knowledgeBaseId, endpointKey, endpointHostName, defaultAnswer) => {
let noAnswerActivity;
if (typeof defaultAnswer === 'string') {
noAnswerActivity = MessageFactory.text(defaultAnswer);
}

const qnaMakerDialog = new QnAMakerDialog(knowledgeBaseId, endpointKey, endpointHostName, noAnswerActivity);
qnaMakerDialog.id = QNAMAKER_BASE_DIALOG;

return qnaMakerDialog;
}

class RootDialog extends ComponentDialog {
/**
* Root dialog for this bot.
* @param {QnAMaker} qnaService A QnAMaker service object.
* Root dialog for this bot. Creates a QnAMakerDialog.
* @param {string} knowledgeBaseId Knowledge Base ID of the QnA Maker instance.
* @param {string} endpointKey Endpoint key needed to query QnA Maker.
* @param {string} endpointHostName Host name of the QnA Maker instance.
* @param {string} defaultAnswer (optional) Text used to create a fallback response when QnA Maker doesn't have an answer for a question.
*/
constructor(knowledgebaseId, authkey, host, defaultAnswer) {
constructor(knowledgeBaseId, endpointKey, endpointHostName, defaultAnswer) {
super(ROOT_DIALOG);
// Initial waterfall dialog.
this.addDialog(new WaterfallDialog(INITIAL_DIALOG, [
this.startInitialDialog.bind(this)
]));
this.addDialog(new QnAMakerBaseDialog(knowledgebaseId, authkey, host, defaultAnswer));

this.addDialog(createQnAMakerDialog(knowledgeBaseId, endpointKey, endpointHostName, defaultAnswer));
this.initialDialogId = INITIAL_DIALOG;
}

Expand Down
38 changes: 18 additions & 20 deletions samples/javascript_nodejs/49.qnamaker-all-features/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// index.js is used to setup and configure your bot

// Import required packages
const path = require('path');

// Note: Ensure you have a .env file and include QnAMakerKnowledgeBaseId, QnAMakerEndpointKey and QnAMakerHost.
// Note: Ensure you have a .env file and include QnAKnowledgebaseId, QnAEndpointKey and QnAEndpointHostName.
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });

const restify = require('restify');

// Import required bot services. See https://aka.ms/bot-services to learn more about the different parts of a bot.
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter, ConversationState, MemoryStorage, UserState } = require('botbuilder');

const { QnABot } = require('./bots/QnABot');
const { RootDialog } = require('./dialogs/rootDialog');

// Create HTTP server.
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }.`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
console.log('\nTo talk to your bot, open the emulator select "Open Bot"');
});

// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
Expand Down Expand Up @@ -56,9 +49,11 @@ adapter.onTurnError = async (context, error) => {

// Define the state store for your bot. See https://aka.ms/about-bot-state to learn more about using MemoryStorage.
// A bot requires a state storage system to persist the dialog and user state between messages.
const memoryStorage = new MemoryStorage();

// Create conversation and user state with in-memory storage provider.
// For local development, in-memory storage is used.
// CAUTION: The Memory Storage used here is for local bot debugging only. When the bot
// is restarted, anything stored in memory will be gone.
const memoryStorage = new MemoryStorage();
const conversationState = new ConversationState(memoryStorage);
const userState = new UserState(memoryStorage);

Expand All @@ -71,20 +66,23 @@ if (!endpointHostName.includes('/v5.0') && !endpointHostName.endsWith('/qnamaker
endpointHostName = endpointHostName + '/qnamaker';
}

var endpointKey = process.env.QnAEndpointKey;

if (!endpointKey || 0 === endpointKey.length)
{
// To support backward compatibility for Key Names
endpointKey = process.env.QnAAuthKey;
}
// To support backward compatibility for Key Names, fallback to process.env.QnAAuthKey.
const endpointKey = process.env.QnAEndpointKey || process.env.QnAAuthKey;

// Create the main dialog.
const dialog = new RootDialog(process.env.QnAKnowledgebaseId, endpointKey, endpointHostName, process.env.DefaultAnswer);

// Create the bot's main handler.
const bot = new QnABot(conversationState, userState, dialog);

// Create HTTP server.
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }.`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
console.log('\nTo talk to your bot, open the emulator select "Open Bot"');
});

// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
Expand Down

This file was deleted.

0 comments on commit 7dbebc2

Please sign in to comment.