Skip to content

Commit aa2723a

Browse files
authored
[QnA Maker] Support for IsTest and RankerType in GetAnswerAsync call in QnAMaker (#1478)
1 parent fe85099 commit aa2723a

File tree

6 files changed

+112
-4
lines changed

6 files changed

+112
-4
lines changed

libraries/botbuilder-ai/src/qnamaker-interfaces/qnamakerOptions.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@ export interface QnAMakerOptions {
5454
* Id of the current question asked.
5555
*/
5656
qnaId?: number;
57+
58+
/**
59+
* A value indicating whether to call test or prod environment of knowledgebase.
60+
*/
61+
isTest?: boolean;
62+
63+
/**
64+
* Ranker types.
65+
*/
66+
rankerType?: string;
5767
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @module botbuilder-ai
3+
*/
4+
/**
5+
* Copyright (c) Microsoft Corporation. All rights reserved.
6+
* Licensed under the MIT License.
7+
*/
8+
9+
/**
10+
* Enumeration of types of ranking.
11+
*/
12+
export class RankerTypes {
13+
14+
/**
15+
* Default Ranker Behaviour. i.e. Ranking based on Questions and Answer.
16+
*/
17+
public static readonly default: string = 'Default';
18+
19+
/**
20+
* Ranker based on question Only.
21+
*/
22+
public static readonly questionOnly: string = 'QuestionOnly';
23+
24+
/**
25+
* Ranker based on Autosuggest for question field Only.
26+
*/
27+
public static readonly autoSuggestQuestion: string = 'AutoSuggestQuestion';
28+
}

libraries/botbuilder-ai/src/qnamaker-utils/generateAnswerUtils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { QnARequestContext } from '../qnamaker-interfaces/qnaRequestContext';
1717
import { HttpRequestUtils } from './httpRequestUtils';
1818

1919
import { QNAMAKER_TRACE_TYPE, QNAMAKER_TRACE_LABEL, QNAMAKER_TRACE_NAME } from '..';
20+
import { RankerTypes } from '../qnamaker-interfaces/rankerTypes';
2021

2122
/**
2223
* Generate Answer api utils class.
@@ -58,8 +59,9 @@ export class GenerateAnswerUtils {
5859
*/
5960
public async queryQnaServiceRaw(endpoint: QnAMakerEndpoint, question: string, options?: QnAMakerOptions): Promise<QnAMakerResults> {
6061
const url: string = `${ endpoint.host }/knowledgebases/${ endpoint.knowledgeBaseId }/generateanswer`;
61-
const queryOptions: QnAMakerOptions = { ...this._options, ...options } as QnAMakerOptions;
62-
62+
var queryOptions: QnAMakerOptions = { ...this._options, ...options } as QnAMakerOptions;
63+
64+
queryOptions.rankerType = !queryOptions.rankerType ? RankerTypes.default : queryOptions.rankerType;
6365
this.validateOptions(queryOptions);
6466

6567
var payloadBody = JSON.stringify({
@@ -109,8 +111,8 @@ export class GenerateAnswerUtils {
109111
*
110112
* @param options (Optional) The options for the QnA Maker knowledge base. If null, constructor option is used for this instance.
111113
*/
112-
public validateOptions(options: QnAMakerOptions): void {
113-
const { scoreThreshold, top } = options;
114+
public validateOptions(options: QnAMakerOptions) {
115+
const { scoreThreshold, top, rankerType } = options;
114116

115117
if (scoreThreshold) {
116118
this.validateScoreThreshold(scoreThreshold);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"activeLearningEnabled": true,
3+
"answers": [
4+
{
5+
"questions": [],
6+
"answer": "No good match found in KB.",
7+
"score": 0,
8+
"id": -1,
9+
"source": null,
10+
"metadata": []
11+
}
12+
]
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"activeLearningEnabled": false,
3+
"answers": [
4+
{
5+
"questions": [
6+
"Q1"
7+
],
8+
"answer": "A1",
9+
"score": 80,
10+
"id": 15,
11+
"source": "Editorial",
12+
"metadata": [
13+
{
14+
"name": "topic",
15+
"value": "value"
16+
}
17+
]
18+
},
19+
{
20+
"questions": [
21+
"Q2"
22+
],
23+
"answer": "A2",
24+
"score": 78,
25+
"id": 16,
26+
"source": "Editorial",
27+
"metadata": [
28+
{
29+
"name": "topic",
30+
"value": "value"
31+
}
32+
]
33+
}
34+
]
35+
}

libraries/botbuilder-ai/tests/qnaMaker.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,26 @@ describe('QnAMaker', function () {
252252
assert.strictEqual(qnaResults[0].score < 1, true, 'score should be low');
253253
});
254254

255+
it('should call qnamaker with isTest true', async function() {
256+
const qna = new QnAMaker(endpoint);
257+
const turnContext = new TestContext({ text: "Q11" });
258+
const options = { top: 1, context: null, isTest: true };
259+
260+
const qnaResults = await qna.getAnswers(turnContext, options);
261+
262+
assert.strictEqual(qnaResults.length, 0, 'no answers should be returned');
263+
});
264+
265+
it('should call qnamaker with rankerType questionOnly', async function() {
266+
const qna = new QnAMaker(endpoint);
267+
const turnContext = new TestContext({ text: "Q11" });
268+
const options = { top: 1, context: null, rankerType: "questionOnly" };
269+
270+
const qnaResults = await qna.getAnswers(turnContext, options);
271+
272+
assert.strictEqual(qnaResults.length, 2, 'no answers should be returned');
273+
});
274+
255275
it('should return answer with timeout option specified', async function() {
256276
const timeoutOption = { timeout: 500000 };
257277
const qna = new QnAMaker(endpoint, timeoutOption);

0 commit comments

Comments
 (0)