Skip to content

Commit

Permalink
fix: [#4568] scoreThreshold no longer works in 4.17.0 and later (#4575)
Browse files Browse the repository at this point in the history
* Restore score threshold filter in QnAMaker results

* Add missing dot in comment.
  • Loading branch information
ceciliaavila authored Nov 30, 2023
1 parent 59b52cd commit 363e0c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
13 changes: 10 additions & 3 deletions libraries/botbuilder-ai/src/qnaMaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export class QnAMaker implements QnAMakerClient, QnAMakerTelemetryClient {
): Promise<QnAMakerResults> {
const question: string = this.getTrimmedMessageText(context);
const queryOptions: QnAMakerOptions = { ...this._options, ...options } as QnAMakerOptions;
const queryResult: QnAMakerResult[] = [] as QnAMakerResult[];

this.generateAnswerUtils.validateOptions(queryOptions);

Expand All @@ -279,19 +280,25 @@ export class QnAMaker implements QnAMakerClient, QnAMakerTelemetryClient {
result = await this.generateAnswerUtils.queryQnaServiceRaw(this.endpoint, question, queryOptions);
}

const sortedQnaAnswers: QnAMakerResult[] = GenerateAnswerUtils.sortAnswersWithinThreshold(
result?.answers,
queryOptions
);
queryResult.push(...sortedQnaAnswers);

if (!result) {
return result;
}

await Promise.all([
// Log telemetry
this.onQnaResults(result?.answers, context, telemetryProperties, telemetryMetrics),
this.generateAnswerUtils.emitTraceInfo(context, result?.answers, queryOptions),
this.onQnaResults(queryResult, context, telemetryProperties, telemetryMetrics),
this.generateAnswerUtils.emitTraceInfo(context, queryResult, queryOptions),
]);

const qnaResponse: QnAMakerResults = {
activeLearningEnabled: result.activeLearningEnabled,
answers: result?.answers,
answers: queryResult,
};

return qnaResponse;
Expand Down
24 changes: 24 additions & 0 deletions libraries/botbuilder-ai/src/qnamaker-utils/generateAnswerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ export class GenerateAnswerUtils {
}
}

/**
* Sorts all QnAMakerResult from highest-to-lowest scoring.
* Filters QnAMakerResults within threshold specified (default threshold: .001).
*
* @param {QnAMakerResult[]} answers Answers returned by QnA Maker.
* @param {QnAMakerOptions} queryOptions (Optional) The options for the QnA Maker knowledge base. If null, constructor option is used for this instance.
* @returns {QnAMakerResult[]} the sorted and filtered results.
*/
static sortAnswersWithinThreshold(
answers: QnAMakerResult[] = [] as QnAMakerResult[],
queryOptions: QnAMakerOptions
): QnAMakerResult[] {
const minScore: number = typeof queryOptions.scoreThreshold === 'number' ? queryOptions.scoreThreshold : 0.001;

if (answers.length === 1 && answers[0].id === -1) {
// if the answer is the default answer, don't filter it by score.
return answers;
}

return answers
.filter((ans: QnAMakerResult) => ans.score >= minScore)
.sort((a: QnAMakerResult, b: QnAMakerResult) => b.score - a.score);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private formatQnaResult(qnaResult: QnAMakerResults | any): QnAMakerResults {
qnaResult.answers = qnaResult.answers.map((answer: QnAMakerResult & { qnaId?: number }) => {
Expand Down

0 comments on commit 363e0c9

Please sign in to comment.