Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#4568] scoreThreshold no longer works in 4.17.0 and later #4575

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading