Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit a9f9b96

Browse files
authored
Fix a regression on multiturn extraction functionality of qna url or file import api (#1042) (#1043)
* fix regression on file and url import api * add unit tests for multiturn extraction"
1 parent 6f4722f commit a9f9b96

File tree

2 files changed

+184
-9
lines changed

2 files changed

+184
-9
lines changed

packages/lu/src/parser/qnabuild/builder.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ export class Builder {
301301
url: string,
302302
subscriptionkey: string,
303303
endpoint: string,
304-
kbName: string) {
304+
kbName: string,
305+
enableHierarchicalExtraction: boolean = false,
306+
defaultAnswerUsedForExtraction: string = 'More Answers') {
305307
const qnaBuildCore = new QnaBuildCore(subscriptionkey, endpoint)
306308
const kbs = (await qnaBuildCore.getKBList()).knowledgebases
307309

@@ -320,7 +322,7 @@ export class Builder {
320322
}
321323

322324
// create a new kb
323-
kbId = await this.createUrlKB(qnaBuildCore, url, kbName)
325+
kbId = await this.createUrlKB(qnaBuildCore, url, kbName, enableHierarchicalExtraction, defaultAnswerUsedForExtraction)
324326

325327
const kbJson = await qnaBuildCore.exportKB(kbId, 'Test')
326328
const kb = new KB(kbJson)
@@ -335,7 +337,9 @@ export class Builder {
335337
fileUri: string,
336338
subscriptionkey: string,
337339
endpoint: string,
338-
kbName: string) {
340+
kbName: string,
341+
enableHierarchicalExtraction: boolean = false,
342+
defaultAnswerUsedForExtraction: string = 'More Answers') {
339343
const qnaBuildCore = new QnaBuildCore(subscriptionkey, endpoint)
340344
const kbs = (await qnaBuildCore.getKBList()).knowledgebases
341345

@@ -354,7 +358,7 @@ export class Builder {
354358
}
355359

356360
// create a new kb
357-
kbId = await this.createFileKB(qnaBuildCore, fileName, fileUri, kbName)
361+
kbId = await this.createFileKB(qnaBuildCore, fileName, fileUri, kbName, enableHierarchicalExtraction, defaultAnswerUsedForExtraction)
358362

359363
const kbJson = await qnaBuildCore.exportKB(kbId, 'Test')
360364
const kb = new KB(kbJson)
@@ -465,12 +469,17 @@ export class Builder {
465469
return kbId
466470
}
467471

468-
async createUrlKB(qnaBuildCore: QnaBuildCore, url: string, kbName: string) {
469-
const kbJson = {
472+
async createUrlKB(qnaBuildCore: QnaBuildCore, url: string, kbName: string, enableHierarchicalExtraction: boolean, defaultAnswerUsedForExtraction: string) {
473+
let kbJson: any = {
470474
name: kbName,
471475
qnaList: [],
472476
urls: [url],
473-
files: []
477+
files: [],
478+
}
479+
480+
if (enableHierarchicalExtraction) {
481+
kbJson.enableHierarchicalExtraction = true
482+
kbJson.defaultAnswerUsedForExtraction = defaultAnswerUsedForExtraction
474483
}
475484

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

484-
async createFileKB(qnaBuildCore: QnaBuildCore, fileName: string, fileUri: string, kbName: string) {
485-
let kbJson = {
493+
async createFileKB(qnaBuildCore: QnaBuildCore, fileName: string, fileUri: string, kbName: string, enableHierarchicalExtraction: boolean, defaultAnswerUsedForExtraction: string) {
494+
let kbJson: any = {
486495
name: kbName,
487496
qnaList: [],
488497
urls: [],
@@ -492,6 +501,11 @@ export class Builder {
492501
}]
493502
}
494503

504+
if (enableHierarchicalExtraction) {
505+
kbJson.enableHierarchicalExtraction = true
506+
kbJson.defaultAnswerUsedForExtraction = defaultAnswerUsedForExtraction
507+
}
508+
495509
let response = await qnaBuildCore.importKB(kbJson)
496510
let operationId = response.operationId
497511
const opResult = await this.getKBOperationStatus(qnaBuildCore, operationId, 1000)

packages/lu/test/parser/qnabuild/qnabuild.test.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,87 @@ describe('builder: importUrlOrFileReference function return lu content from file
134134
})
135135
})
136136

137+
describe('builder: importUrlOrFileReference function return lu content from file sucessfully with multiturn extraction enabled', () => {
138+
before(function () {
139+
nock('https://westus.api.cognitive.microsoft.com')
140+
.get(uri => uri.includes('qnamaker'))
141+
.reply(200, {
142+
knowledgebases:
143+
[{
144+
name: 'test.en-us.qna',
145+
id: 'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
146+
hostName: 'https://myqnamakerbot.azurewebsites.net'
147+
}]
148+
})
149+
150+
nock('https://westus.api.cognitive.microsoft.com')
151+
.post(uri => uri.includes('createasync'))
152+
.reply(202, {
153+
operationId: 'f8c64e2a-aaaa-3a09-8f78-39d7adc76ec5'
154+
})
155+
156+
nock('https://westus.api.cognitive.microsoft.com')
157+
.get(uri => uri.includes('operations'))
158+
.reply(200, {
159+
operationState: 'Succeeded',
160+
resourceLocation: 'a/b/f8c64e2a-2222-3a09-8f78-39d7adc76ec5'
161+
})
162+
163+
nock('https://westus.api.cognitive.microsoft.com')
164+
.get(uri => uri.includes('knowledgebases'))
165+
.reply(200, {
166+
qnaDocuments: [{
167+
id: 1,
168+
source: 'SurfaceManual.pdf',
169+
questions: ['User Guide'],
170+
answer: 'More Answers',
171+
metadata: [],
172+
prompts: [{
173+
displayOrder: 0,
174+
displayText: 'With Windows 10',
175+
qnaId: 2
176+
}]
177+
},
178+
{
179+
id: 2,
180+
source: 'SurfaceManual.pdf',
181+
questions: ['With Windows 10'],
182+
answer: '**With Windows 10**',
183+
metadata: [],
184+
prompts: []
185+
}]
186+
})
187+
})
188+
189+
nock('https://westus.api.cognitive.microsoft.com')
190+
.delete(uri => uri.includes('knowledgebases'))
191+
.reply(200)
192+
193+
it('should return lu content from file successfully', async () => {
194+
const builder = new Builder()
195+
const luContent = await builder.importFileReference(
196+
'SurfaceManual.pdf',
197+
'https://download.microsoft.com/download/2/9/B/29B20383-302C-4517-A006-B0186F04BE28/surface-pro-4-user-guide-EN.pdf',
198+
uuidv1(),
199+
'https://westus.api.cognitive.microsoft.com/qnamaker/v4.0',
200+
'mytest.en-us.qna',
201+
true)
202+
203+
assert.equal(luContent,
204+
`> # QnA pairs${NEWLINE}${NEWLINE}` +
205+
`> !# @qna.pair.source = SurfaceManual.pdf${NEWLINE}${NEWLINE}` +
206+
`<a id = "1"></a>${NEWLINE}${NEWLINE}` +
207+
`# ? User Guide${NEWLINE}${NEWLINE}` +
208+
`\`\`\`markdown${NEWLINE}` +
209+
`More Answers${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}` +
210+
`> !# @qna.pair.source = SurfaceManual.pdf${NEWLINE}${NEWLINE}` +
211+
`<a id = "2"></a>${NEWLINE}${NEWLINE}` +
212+
`# ? With Windows 10${NEWLINE}${NEWLINE}` +
213+
`\`\`\`markdown${NEWLINE}` +
214+
`**With Windows 10**${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}`)
215+
})
216+
})
217+
137218
describe('builder: importUrlOrFileReference function return lu content from url sucessfully', () => {
138219
before(function () {
139220
nock('https://westus.api.cognitive.microsoft.com')
@@ -193,6 +274,86 @@ describe('builder: importUrlOrFileReference function return lu content from url
193274
})
194275
})
195276

277+
describe('builder: importUrlOrFileReference function return lu content from url sucessfully with multiturn extraction enabled', () => {
278+
before(function () {
279+
nock('https://westus.api.cognitive.microsoft.com')
280+
.get(uri => uri.includes('qnamaker'))
281+
.reply(200, {
282+
knowledgebases:
283+
[{
284+
name: 'test.en-us.qna',
285+
id: 'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
286+
hostName: 'https://myqnamakerbot.azurewebsites.net'
287+
}]
288+
})
289+
290+
nock('https://westus.api.cognitive.microsoft.com')
291+
.post(uri => uri.includes('createasync'))
292+
.reply(202, {
293+
operationId: 'f8c64e2a-aaaa-3a09-8f78-39d7adc76ec5'
294+
})
295+
296+
nock('https://westus.api.cognitive.microsoft.com')
297+
.get(uri => uri.includes('operations'))
298+
.reply(200, {
299+
operationState: 'Succeeded',
300+
resourceLocation: 'a/b/f8c64e2a-2222-3a09-8f78-39d7adc76ec5'
301+
})
302+
303+
nock('https://westus.api.cognitive.microsoft.com')
304+
.get(uri => uri.includes('knowledgebases'))
305+
.reply(200, {
306+
qnaDocuments: [{
307+
id: 1,
308+
source: 'SurfaceManual.pdf',
309+
questions: ['User Guide'],
310+
answer: 'More Answers',
311+
metadata: [],
312+
prompts: [{
313+
displayOrder: 0,
314+
displayText: 'With Windows 10',
315+
qnaId: 2
316+
}]
317+
},
318+
{
319+
id: 2,
320+
source: 'SurfaceManual.pdf',
321+
questions: ['With Windows 10'],
322+
answer: '**With Windows 10**',
323+
metadata: [],
324+
prompts: []
325+
}]
326+
})
327+
328+
nock('https://westus.api.cognitive.microsoft.com')
329+
.delete(uri => uri.includes('knowledgebases'))
330+
.reply(200)
331+
})
332+
333+
it('should return lu content from url successfully', async () => {
334+
const builder = new Builder()
335+
const luContent = await builder.importUrlReference(
336+
'https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs',
337+
uuidv1(),
338+
'https://westus.api.cognitive.microsoft.com/qnamaker/v4.0',
339+
'mytest.en-us.qna',
340+
true)
341+
342+
assert.equal(luContent,
343+
`> # QnA pairs${NEWLINE}${NEWLINE}` +
344+
`> !# @qna.pair.source = SurfaceManual.pdf${NEWLINE}${NEWLINE}` +
345+
`<a id = "1"></a>${NEWLINE}${NEWLINE}` +
346+
`# ? User Guide${NEWLINE}${NEWLINE}` +
347+
`\`\`\`markdown${NEWLINE}` +
348+
`More Answers${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}` +
349+
`> !# @qna.pair.source = SurfaceManual.pdf${NEWLINE}${NEWLINE}` +
350+
`<a id = "2"></a>${NEWLINE}${NEWLINE}` +
351+
`# ? With Windows 10${NEWLINE}${NEWLINE}` +
352+
`\`\`\`markdown${NEWLINE}` +
353+
`**With Windows 10**${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}`)
354+
})
355+
})
356+
196357
describe('builder: importUrlOrFileReference function return lu content from url sucessfully when updating kb', () => {
197358
before(function () {
198359
nock('https://westus.api.cognitive.microsoft.com')

0 commit comments

Comments
 (0)