Skip to content

Commit

Permalink
[QnA Maker bot samples] Configurable default answer - nodejs, csharp (#…
Browse files Browse the repository at this point in the history
…2934)

* Nodejs - configurable DefaultAnswer

* csharp - configurable default answer

* Update read me files

Corresponding changes to be done in docs here after merging these changes.

* Added null/empty checks

tested okay

* null empty check in javascript

tested ok

* simplified as per PR comments

* DefaultAnswer optional - make explicit in readme

* conforming to usage pattern

* removed extra space

* var to const and let as applicable

* ActivityFactory and MessageFactory both from botbuilder instead of botbuilder-core
  • Loading branch information
sahithikkss-zz authored Dec 10, 2020
1 parent 3eb7bf6 commit a5e2386
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ namespace Microsoft.BotBuilderSamples.Dialog
public class QnAMakerBaseDialog : QnAMakerDialog
{
// Dialog Options parameters
public const string DefaultNoAnswer = "No QnAMaker answers found.";
public const string DefaultCardTitle = "Did you mean:";
public const string DefaultCardNoMatchText = "None of the above.";
public const string DefaultCardNoMatchResponse = "Thanks for the feedback.";

private readonly IBotServices _services;
private readonly IConfiguration _configuration;
private readonly string DefaultAnswer = "No QnAMaker answers found.";

/// <summary>
/// Initializes a new instance of the <see cref="QnAMakerBaseDialog"/> class.
Expand All @@ -33,7 +32,10 @@ public class QnAMakerBaseDialog : QnAMakerDialog
public QnAMakerBaseDialog(IBotServices services, IConfiguration configuration) : base()
{
this._services = services;
this._configuration = configuration;
if (!string.IsNullOrWhiteSpace(configuration["DefaultAnswer"]))
{
this.DefaultAnswer = configuration["DefaultAnswer"];
}
}

protected async override Task<IQnAMakerClient> GetQnAMakerClientAsync(DialogContext dc)
Expand All @@ -55,16 +57,15 @@ protected override Task<QnAMakerOptions> GetQnAMakerOptionsAsync(DialogContext d

protected async override Task<QnADialogResponseOptions> GetQnAResponseOptionsAsync(DialogContext dc)
{
var noAnswer = (Activity)Activity.CreateMessageActivity();
noAnswer.Text = this._configuration["DefaultAnswer"] ?? DefaultNoAnswer;
var defaultAnswerActivity = MessageFactory.Text(this.DefaultAnswer);

var cardNoMatchResponse = (Activity)MessageFactory.Text(DefaultCardNoMatchResponse);

var responseOptions = new QnADialogResponseOptions
{
ActiveLearningCardTitle = DefaultCardTitle,
CardNoMatchText = DefaultCardNoMatchText,
NoAnswer = noAnswer,
NoAnswer = defaultAnswerActivity,
CardNoMatchResponse = cardNoMatchResponse,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ In this sample, we demonstrate
- Follow instructions [here](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/set-up-qnamaker-service-azure) to create a QnA Maker service.
- Follow instructions [here](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/multiturn-conversation) to create multiturn experience.
- Follow instructions [here](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/quickstarts/create-publish-knowledge-base) to import and publish your newly created QnA Maker service.
- Update [appsettings.json](appsettings.json) with your kbid (KnowledgeBase Id), endpointKey and endpointHost. QnA knowledge base setup and application configuration steps can be found [here](https://aka.ms/qna-instructions).
- Update [appsettings.json](appsettings.json) with your kbid (KnowledgeBase Id), endpointKey and endpointHost. You may also change the default answer by updating `DefaultAnswer` (optional) field. QnA knowledge base setup and application configuration steps can be found [here](https://aka.ms/qna-instructions).
- (Optional) Follow instructions [here](https://github.com/Microsoft/botbuilder-tools/tree/master/packages/QnAMaker) to set up the
QnA Maker CLI to deploy the model.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"MicrosoftAppPassword": "",
"QnAKnowledgebaseId": "",
"QnAEndpointKey": "",
"QnAEndpointHostName": ""
"QnAEndpointHostName": "",
"DefaultAnswer": ""
}
3 changes: 2 additions & 1 deletion samples/javascript_nodejs/49.qnamaker-all-features/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ MicrosoftAppPassword=

QnAKnowledgebaseId=
QnAEndpointKey=
QnAEndpointHostName=
QnAEndpointHostName=
DefaultAnswer=
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ In this sample, we demonstrate
`- Follow instructions` [here](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/set-up-qnamaker-service-azure) to create a QnA Maker service.
- Follow instructions [here](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/multiturn-conversation) to create multiturn experience.
- Follow instructions [here](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/quickstarts/create-publish-knowledge-base) to import and publish your newly created QnA Maker service.
- Update [appsettings.json](appsettings.json) with your kbid (KnowledgeBase Id), endpointKey and endpointHost. QnA knowledge base setup and application configuration steps can be found [here](https://aka.ms/qna-instructions).
- Update [.env](.env) with your kbid (KnowledgeBase Id), endpointKey and endpointHost. You may also change the default answer by updating `DefaultAnswer` (optional) field. QnA knowledge base setup and application configuration steps can be found [here](https://aka.ms/qna-instructions).
- (Optional) Follow instructions [here](https://github.com/Microsoft/botbuilder-tools/tree/master/packages/QnAMaker) to set up the
QnA Maker CLI to deploy the model.
### Create a QnAMaker Application to enable QnA Knowledge Bases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const {
} = require('botbuilder-ai');

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

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

// Card parameters
const DefaultCardTitle = 'Did you mean:';
Expand All @@ -27,10 +28,10 @@ class QnAMakerBaseDialog extends QnAMakerDialog {
* Core logic of QnA Maker dialog.
* @param {QnAMaker} qnaService A QnAMaker service object.
*/
constructor(knowledgebaseId, authkey, host) {
var noAnswer = ActivityFactory.fromObject(DefaultNoAnswer);
var filters = [];
super(knowledgebaseId, authkey, host, noAnswer, DefaultThreshold, DefaultCardTitle, DefaultCardNoMatchText,
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);
this.id = QNAMAKER_BASE_DIALOG;
}
Expand All @@ -40,7 +41,7 @@ module.exports.QnAMakerBaseDialog = QnAMakerBaseDialog;
module.exports.QNAMAKER_BASE_DIALOG = QNAMAKER_BASE_DIALOG;
module.exports.DefaultThreshold = DefaultThreshold;
module.exports.DefaultTopN = DefaultTopN;
module.exports.DefaultNoAnswer = DefaultNoAnswer;
module.exports.DefaultAnswer = DefaultAnswer;
module.exports.DefaultCardTitle = DefaultCardTitle;
module.exports.DefaultCardNoMatchText = DefaultCardNoMatchText;
module.exports.DefaultCardNoMatchResponse = DefaultCardNoMatchResponse;
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class RootDialog extends ComponentDialog {
* Root dialog for this bot.
* @param {QnAMaker} qnaService A QnAMaker service object.
*/
constructor(knowledgebaseId, authkey, host) {
constructor(knowledgebaseId, authkey, host, defaultAnswer) {
super(ROOT_DIALOG);
// Initial waterfall dialog.
this.addDialog(new WaterfallDialog(INITIAL_DIALOG, [
this.startInitialDialog.bind(this)
]));
this.addDialog(new QnAMakerBaseDialog(knowledgebaseId, authkey, host));
this.addDialog(new QnAMakerBaseDialog(knowledgebaseId, authkey, host, defaultAnswer));
this.initialDialogId = INITIAL_DIALOG;
}

Expand Down
4 changes: 2 additions & 2 deletions samples/javascript_nodejs/49.qnamaker-all-features/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const adapter = new BotFrameworkAdapter({
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);

Expand Down Expand Up @@ -80,7 +80,7 @@ if (!endpointKey || 0 === endpointKey.length)
}

// Create the main dialog.
const dialog = new RootDialog(process.env.QnAKnowledgebaseId, endpointKey, endpointHostName);
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);
Expand Down

0 comments on commit a5e2386

Please sign in to comment.