Skip to content

Commit

Permalink
cleanup default params in sample 49
Browse files Browse the repository at this point in the history
* for javascript_nodejs
  • Loading branch information
stevengum committed Dec 14, 2020
1 parent a5e2386 commit 5c5f453
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 112 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,47 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

const {
QnAMakerDialog
} = require('botbuilder-ai');

const {
ActivityFactory,
MessageFactory
} = require('botbuilder');

// Default parameters
const DefaultThreshold = 0.3;
const DefaultTopN = 3;
const DefaultAnswer = 'No QnAMaker answers found.';

// Card parameters
const DefaultCardTitle = 'Did you mean:';
const DefaultCardNoMatchText = 'None of the above.';
const DefaultCardNoMatchResponse = 'Thanks for the feedback.';
const { QnAMakerDialog } = require('botbuilder-ai');
const { MessageFactory } = require('botbuilder');

/// QnA Maker dialog.
const QNAMAKER_BASE_DIALOG = 'qnamaker-base-dailog';
const QNAMAKER_BASE_DIALOG = 'qnamaker-base-dialog';

class QnAMakerBaseDialog extends QnAMakerDialog {
/**
* Core logic of QnA Maker dialog.
* @param {QnAMaker} qnaService A QnAMaker service object.
* @param {string} knowledgeBaseId A QnAMaker service object.
* @param {string} endpointKey A QnAMaker service object.
* @param {string} endpointHostName A QnAMaker service object.
* @param {string} defaultAnswer A QnAMaker service object.
*/
constructor(knowledgebaseId, authkey, host, defaultAnswer) {
const defaultAnswerActivity = MessageFactory.text(!!defaultAnswer.trim() ? defaultAnswer : DefaultAnswer);
let filters = [];
super(knowledgebaseId, authkey, host, defaultAnswerActivity, DefaultThreshold, DefaultCardTitle, DefaultCardNoMatchText,
DefaultTopN, ActivityFactory.cardNoMatchResponse, filters, QNAMAKER_BASE_DIALOG);
constructor(
knowledgeBaseId,
endpointKey,
endpointHostName,
defaultAnswer,
) {
let noAnswer;
if (typeof defaultAnswer === 'string') {
noAnswer = MessageFactory.text(defaultAnswer);
}

super(knowledgeBaseId, endpointKey, endpointHostName, noAnswer);
this.id = QNAMAKER_BASE_DIALOG;
}
}

module.exports.QnAMakerBaseDialog = QnAMakerBaseDialog;
module.exports.QNAMAKER_BASE_DIALOG = QNAMAKER_BASE_DIALOG;
module.exports.DefaultThreshold = DefaultThreshold;
module.exports.DefaultTopN = DefaultTopN;
module.exports.DefaultAnswer = DefaultAnswer;
module.exports.DefaultCardTitle = DefaultCardTitle;
module.exports.DefaultCardNoMatchText = DefaultCardNoMatchText;
module.exports.DefaultCardNoMatchResponse = DefaultCardNoMatchResponse;
36 changes: 17 additions & 19 deletions samples/javascript_nodejs/49.qnamaker-all-features/index.js
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.

// index.js is used to setup and configure your bot
Expand All @@ -12,20 +12,13 @@ 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 5c5f453

Please sign in to comment.