Skip to content

Commit

Permalink
Fix a regression on multiturn extraction functionality of qna url or …
Browse files Browse the repository at this point in the history
…file import api (#1042) (#1043)

* fix regression on file and url import api

* add unit tests for multiturn extraction"
  • Loading branch information
feich-ms authored Oct 30, 2020
1 parent 6f4722f commit a9f9b96
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 9 deletions.
32 changes: 23 additions & 9 deletions packages/lu/src/parser/qnabuild/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ export class Builder {
url: string,
subscriptionkey: string,
endpoint: string,
kbName: string) {
kbName: string,
enableHierarchicalExtraction: boolean = false,
defaultAnswerUsedForExtraction: string = 'More Answers') {
const qnaBuildCore = new QnaBuildCore(subscriptionkey, endpoint)
const kbs = (await qnaBuildCore.getKBList()).knowledgebases

Expand All @@ -320,7 +322,7 @@ export class Builder {
}

// create a new kb
kbId = await this.createUrlKB(qnaBuildCore, url, kbName)
kbId = await this.createUrlKB(qnaBuildCore, url, kbName, enableHierarchicalExtraction, defaultAnswerUsedForExtraction)

const kbJson = await qnaBuildCore.exportKB(kbId, 'Test')
const kb = new KB(kbJson)
Expand All @@ -335,7 +337,9 @@ export class Builder {
fileUri: string,
subscriptionkey: string,
endpoint: string,
kbName: string) {
kbName: string,
enableHierarchicalExtraction: boolean = false,
defaultAnswerUsedForExtraction: string = 'More Answers') {
const qnaBuildCore = new QnaBuildCore(subscriptionkey, endpoint)
const kbs = (await qnaBuildCore.getKBList()).knowledgebases

Expand All @@ -354,7 +358,7 @@ export class Builder {
}

// create a new kb
kbId = await this.createFileKB(qnaBuildCore, fileName, fileUri, kbName)
kbId = await this.createFileKB(qnaBuildCore, fileName, fileUri, kbName, enableHierarchicalExtraction, defaultAnswerUsedForExtraction)

const kbJson = await qnaBuildCore.exportKB(kbId, 'Test')
const kb = new KB(kbJson)
Expand Down Expand Up @@ -465,12 +469,17 @@ export class Builder {
return kbId
}

async createUrlKB(qnaBuildCore: QnaBuildCore, url: string, kbName: string) {
const kbJson = {
async createUrlKB(qnaBuildCore: QnaBuildCore, url: string, kbName: string, enableHierarchicalExtraction: boolean, defaultAnswerUsedForExtraction: string) {
let kbJson: any = {
name: kbName,
qnaList: [],
urls: [url],
files: []
files: [],
}

if (enableHierarchicalExtraction) {
kbJson.enableHierarchicalExtraction = true
kbJson.defaultAnswerUsedForExtraction = defaultAnswerUsedForExtraction
}

let response = await qnaBuildCore.importKB(kbJson)
Expand All @@ -481,8 +490,8 @@ export class Builder {
return kbId
}

async createFileKB(qnaBuildCore: QnaBuildCore, fileName: string, fileUri: string, kbName: string) {
let kbJson = {
async createFileKB(qnaBuildCore: QnaBuildCore, fileName: string, fileUri: string, kbName: string, enableHierarchicalExtraction: boolean, defaultAnswerUsedForExtraction: string) {
let kbJson: any = {
name: kbName,
qnaList: [],
urls: [],
Expand All @@ -492,6 +501,11 @@ export class Builder {
}]
}

if (enableHierarchicalExtraction) {
kbJson.enableHierarchicalExtraction = true
kbJson.defaultAnswerUsedForExtraction = defaultAnswerUsedForExtraction
}

let response = await qnaBuildCore.importKB(kbJson)
let operationId = response.operationId
const opResult = await this.getKBOperationStatus(qnaBuildCore, operationId, 1000)
Expand Down
161 changes: 161 additions & 0 deletions packages/lu/test/parser/qnabuild/qnabuild.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,87 @@ describe('builder: importUrlOrFileReference function return lu content from file
})
})

describe('builder: importUrlOrFileReference function return lu content from file sucessfully with multiturn extraction enabled', () => {
before(function () {
nock('https://westus.api.cognitive.microsoft.com')
.get(uri => uri.includes('qnamaker'))
.reply(200, {
knowledgebases:
[{
name: 'test.en-us.qna',
id: 'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
hostName: 'https://myqnamakerbot.azurewebsites.net'
}]
})

nock('https://westus.api.cognitive.microsoft.com')
.post(uri => uri.includes('createasync'))
.reply(202, {
operationId: 'f8c64e2a-aaaa-3a09-8f78-39d7adc76ec5'
})

nock('https://westus.api.cognitive.microsoft.com')
.get(uri => uri.includes('operations'))
.reply(200, {
operationState: 'Succeeded',
resourceLocation: 'a/b/f8c64e2a-2222-3a09-8f78-39d7adc76ec5'
})

nock('https://westus.api.cognitive.microsoft.com')
.get(uri => uri.includes('knowledgebases'))
.reply(200, {
qnaDocuments: [{
id: 1,
source: 'SurfaceManual.pdf',
questions: ['User Guide'],
answer: 'More Answers',
metadata: [],
prompts: [{
displayOrder: 0,
displayText: 'With Windows 10',
qnaId: 2
}]
},
{
id: 2,
source: 'SurfaceManual.pdf',
questions: ['With Windows 10'],
answer: '**With Windows 10**',
metadata: [],
prompts: []
}]
})
})

nock('https://westus.api.cognitive.microsoft.com')
.delete(uri => uri.includes('knowledgebases'))
.reply(200)

it('should return lu content from file successfully', async () => {
const builder = new Builder()
const luContent = await builder.importFileReference(
'SurfaceManual.pdf',
'https://download.microsoft.com/download/2/9/B/29B20383-302C-4517-A006-B0186F04BE28/surface-pro-4-user-guide-EN.pdf',
uuidv1(),
'https://westus.api.cognitive.microsoft.com/qnamaker/v4.0',
'mytest.en-us.qna',
true)

assert.equal(luContent,
`> # QnA pairs${NEWLINE}${NEWLINE}` +
`> !# @qna.pair.source = SurfaceManual.pdf${NEWLINE}${NEWLINE}` +
`<a id = "1"></a>${NEWLINE}${NEWLINE}` +
`# ? User Guide${NEWLINE}${NEWLINE}` +
`\`\`\`markdown${NEWLINE}` +
`More Answers${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}` +
`> !# @qna.pair.source = SurfaceManual.pdf${NEWLINE}${NEWLINE}` +
`<a id = "2"></a>${NEWLINE}${NEWLINE}` +
`# ? With Windows 10${NEWLINE}${NEWLINE}` +
`\`\`\`markdown${NEWLINE}` +
`**With Windows 10**${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}`)
})
})

describe('builder: importUrlOrFileReference function return lu content from url sucessfully', () => {
before(function () {
nock('https://westus.api.cognitive.microsoft.com')
Expand Down Expand Up @@ -193,6 +274,86 @@ describe('builder: importUrlOrFileReference function return lu content from url
})
})

describe('builder: importUrlOrFileReference function return lu content from url sucessfully with multiturn extraction enabled', () => {
before(function () {
nock('https://westus.api.cognitive.microsoft.com')
.get(uri => uri.includes('qnamaker'))
.reply(200, {
knowledgebases:
[{
name: 'test.en-us.qna',
id: 'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
hostName: 'https://myqnamakerbot.azurewebsites.net'
}]
})

nock('https://westus.api.cognitive.microsoft.com')
.post(uri => uri.includes('createasync'))
.reply(202, {
operationId: 'f8c64e2a-aaaa-3a09-8f78-39d7adc76ec5'
})

nock('https://westus.api.cognitive.microsoft.com')
.get(uri => uri.includes('operations'))
.reply(200, {
operationState: 'Succeeded',
resourceLocation: 'a/b/f8c64e2a-2222-3a09-8f78-39d7adc76ec5'
})

nock('https://westus.api.cognitive.microsoft.com')
.get(uri => uri.includes('knowledgebases'))
.reply(200, {
qnaDocuments: [{
id: 1,
source: 'SurfaceManual.pdf',
questions: ['User Guide'],
answer: 'More Answers',
metadata: [],
prompts: [{
displayOrder: 0,
displayText: 'With Windows 10',
qnaId: 2
}]
},
{
id: 2,
source: 'SurfaceManual.pdf',
questions: ['With Windows 10'],
answer: '**With Windows 10**',
metadata: [],
prompts: []
}]
})

nock('https://westus.api.cognitive.microsoft.com')
.delete(uri => uri.includes('knowledgebases'))
.reply(200)
})

it('should return lu content from url successfully', async () => {
const builder = new Builder()
const luContent = await builder.importUrlReference(
'https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs',
uuidv1(),
'https://westus.api.cognitive.microsoft.com/qnamaker/v4.0',
'mytest.en-us.qna',
true)

assert.equal(luContent,
`> # QnA pairs${NEWLINE}${NEWLINE}` +
`> !# @qna.pair.source = SurfaceManual.pdf${NEWLINE}${NEWLINE}` +
`<a id = "1"></a>${NEWLINE}${NEWLINE}` +
`# ? User Guide${NEWLINE}${NEWLINE}` +
`\`\`\`markdown${NEWLINE}` +
`More Answers${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}` +
`> !# @qna.pair.source = SurfaceManual.pdf${NEWLINE}${NEWLINE}` +
`<a id = "2"></a>${NEWLINE}${NEWLINE}` +
`# ? With Windows 10${NEWLINE}${NEWLINE}` +
`\`\`\`markdown${NEWLINE}` +
`**With Windows 10**${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}`)
})
})

describe('builder: importUrlOrFileReference function return lu content from url sucessfully when updating kb', () => {
before(function () {
nock('https://westus.api.cognitive.microsoft.com')
Expand Down

0 comments on commit a9f9b96

Please sign in to comment.