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: correct QnAMaker response types and expose error from server #3279

Merged
merged 5 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 14 additions & 13 deletions libraries/botbuilder-ai/src/qnamaker-utils/generateAnswerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,18 @@ export class GenerateAnswerUtils {
...queryOptions,
});

const qnaResultJson = await this.httpRequestUtils.executeHttpRequest(
const qnaResults = await this.httpRequestUtils.executeHttpRequest(
url,
payloadBody,
this.endpoint,
queryOptions.timeout
);

return this.formatQnaResult(qnaResultJson);
if (qnaResults?.answers && Array.isArray(qnaResults.answers)) {
chon219 marked this conversation as resolved.
Show resolved Hide resolved
return this.formatQnaResult(qnaResults);
}

throw new Error(`Failed to generate answers: ${qnaResults}`);
}

/**
Expand Down Expand Up @@ -166,22 +170,19 @@ export class GenerateAnswerUtils {
.sort((a: QnAMakerResult, b: QnAMakerResult) => b.score - a.score);
}

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

if (ans.qnaId) {
ans.id = ans.qnaId;
delete ans.qnaId;
if (answer.qnaId) {
answer.id = answer.qnaId;
delete answer.qnaId;
}

return ans as QnAMakerResult;
return answer;
});

qnaResult.activeLearningEnabled =
qnaResult.activeLearningEnabled != null ? qnaResult.activeLearningEnabled : true;
qnaResult.activeLearningEnabled ??= true;

return qnaResult;
}
Expand Down
26 changes: 4 additions & 22 deletions libraries/botbuilder-ai/src/qnamaker-utils/httpRequestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import * as os from 'os';

import { QnAMakerEndpoint } from '../qnamaker-interfaces/qnamakerEndpoint';
import { QnAMakerResult } from '../qnamaker-interfaces/qnamakerResult';
import { QnAMakerResults } from '../qnamaker-interfaces/qnamakerResults';

import { getFetch } from '../globals';
const fetch = getFetch();
Expand All @@ -31,14 +31,14 @@ export class HttpRequestUtils {
* @param {string} payloadBody Http request body.
* @param {QnAMakerEndpoint} endpoint QnA Maker endpoint details.
* @param {number} timeout (Optional)Timeout for http call
* @returns {QnAMakerResult} a promise that resolves to the QnAMakerResult
* @returns {QnAMakerResults} a promise that resolves to the QnAMakerResults
*/
public async executeHttpRequest(
requestUrl: string,
payloadBody: string,
endpoint: QnAMakerEndpoint,
timeout?: number
): Promise<QnAMakerResult> {
): Promise<QnAMakerResults> {
chon219 marked this conversation as resolved.
Show resolved Hide resolved
if (!requestUrl) {
throw new TypeError('Request url cannot be null.');
}
Expand All @@ -60,7 +60,7 @@ export class HttpRequestUtils {
body: payloadBody,
});

return qnaResult.status == 204 ? this.getSuccessful204Result() : await qnaResult.json();
return qnaResult.status !== 204 ? await qnaResult.json() : undefined;
}

/**
Expand Down Expand Up @@ -92,22 +92,4 @@ export class HttpRequestUtils {

return `${packageUserAgent} ${platformUserAgent}`;
}

/**
* Creates a QnAMakerResult for successful responses from QnA Maker service that return status code 204 No-Content.
* The [Train API](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/qnamakerruntime/runtime/train)
* is an example of one of QnA Maker's APIs that return a 204 status code.
*
* @private
*/
private getSuccessful204Result(): QnAMakerResult {
return {
questions: [],
answer: '204 No-Content',
score: 100,
id: -1,
source: null,
metadata: [],
};
}
}
11 changes: 1 addition & 10 deletions libraries/botbuilder-ai/tests/qnaMaker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -947,15 +947,6 @@ describe('QnAMaker', function () {
const trainApi = `/knowledgebases/${testKbId}/train`;
const feedbackRecords = [{ userId: 'user1', userQuestion: 'wi-fi', qnaId: '17' }];

const successfullyTrainedResult = {
questions: [],
answer: '204 No-Content',
score: 100,
id: -1,
source: null,
metadata: [],
};

it('executeHttpRequest() should return JSON result from Train API response of 204 No-Content', async function () {
nock(endpoint.host).post(trainApi).reply(204);

Expand All @@ -967,7 +958,7 @@ describe('QnAMaker', function () {

const qnaResult = await httpUtils.executeHttpRequest(endpoint.host + trainApi, payloadBody, endpoint);

assert.deepStrictEqual(qnaResult, successfullyTrainedResult);
assert.strictEqual(qnaResult, undefined);
});

it('executeHttpRequest() should throw when payload to QnA Service is malformed', async function () {
Expand Down