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

Commit 854190a

Browse files
resolve qnamaker build issues (#812)
* write out endpointKeys to console for qnamaker build * qnamaker:build do not write .dialog if no --out is specified * fix typo Co-authored-by: Emilio Munoz <[email protected]>
1 parent c0e8483 commit 854190a

File tree

5 files changed

+115
-55
lines changed

5 files changed

+115
-55
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ export class Builder {
218218
return dialogContents
219219
}
220220

221+
async getEndpointKeys(subscriptionkey: string, endpoint: string) {
222+
const qnaBuildCore = new QnaBuildCore(subscriptionkey, endpoint)
223+
const endPointKeys = await qnaBuildCore.getEndpointKeys()
224+
225+
return endPointKeys
226+
}
227+
221228
async importUrlReference(
222229
url: string,
223230
subscriptionkey: string,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ export class QnaBuildCore {
109109
}
110110
}
111111

112+
public async getEndpointKeys() {
113+
const response = await this.service.createRequest('/endpointkeys', 'GET')
114+
const text = await response.text()
115+
try {
116+
return JSON.parse(text)
117+
} catch {
118+
return text
119+
}
120+
}
121+
112122
public generateDeclarativeAssets(recognizers: Array<Recognizer>, multiRecognizer: MultiLanguageRecognizer, settings: Settings)
113123
: Array<any> {
114124
let contents = new Array<any>()

packages/qnamaker/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,22 @@ USAGE
121121
122122
OPTIONS
123123
-b, --botName=botName (required) Bot name
124-
-f, --force If --dialog flag is provided, overwirtes relevant dialog file
124+
-f, --force If --out flag is provided, overwirtes relevant dialog file
125125
-h, --help show CLI help
126126
-i, --in=in Source .qna file or folder
127127
128-
-o, --out=out Output file or folder name. If not specified, current directory will be used as
129-
output
128+
-o, --out=out Output folder name to write out .dialog files. If not specified, knowledge base
129+
ids will be output to console
130130
131131
-s, --subscriptionKey=subscriptionKey (required) QnA maker subscription key
132132
133133
--defaultCulture=defaultCulture Culture code for the content. Infer from .qna if available. Defaults to en-us
134134
if not set
135135
136-
--dialog=dialog [default: multiLanguage] Write out .dialog files whose recognizer type
137-
[multiLanguage|crosstrained] is specified by --dialog
136+
--dialog=dialog [default: multiLanguage] Dialog recognizer type [multiLanguage|crosstrained]
138137
139138
--fallbackLocale=fallbackLocale Locale to be used at the fallback if no locale specific recognizer is found.
140-
Only valid if --dialog is set
139+
Only valid if --out is set
141140
142141
--log write out log messages to console
143142

packages/qnamaker/src/commands/qnamaker/build.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ export default class QnamakerBuild extends Command {
3030
subscriptionKey: flags.string({char: 's', description: 'QnA maker subscription key', required: true}),
3131
botName: flags.string({char: 'b', description: 'Bot name', required: true}),
3232
region: flags.string({description: 'Overrides public endpoint https://<region>.api.cognitive.microsoft.com/qnamaker/v4.0/', default: 'westus'}),
33-
out: flags.string({char: 'o', description: 'Output file or folder name. If not specified, current directory will be used as output'}),
33+
out: flags.string({char: 'o', description: 'Output folder name to write out .dialog files. If not specified, knowledge base ids will be output to console'}),
3434
defaultCulture: flags.string({description: 'Culture code for the content. Infer from .qna if available. Defaults to en-us if not set'}),
35-
fallbackLocale: flags.string({description: 'Locale to be used at the fallback if no locale specific recognizer is found. Only valid if --dialog is set'}),
35+
fallbackLocale: flags.string({description: 'Locale to be used at the fallback if no locale specific recognizer is found. Only valid if --out is set'}),
3636
suffix: flags.string({description: 'Environment name as a suffix identifier to include in qnamaker kb name. Defaults to current logged in user alias'}),
37-
dialog: flags.string({description: 'Write out .dialog files whose recognizer type [multiLanguage|crosstrained] is specified by --dialog', default: 'multiLanguage'}),
38-
force: flags.boolean({char: 'f', description: 'If --dialog flag is provided, overwrites relevant dialog file', default: false}),
37+
dialog: flags.string({description: 'Dialog recognizer type [multiLanguage|crosstrained]', default: 'multiLanguage'}),
38+
force: flags.boolean({char: 'f', description: 'If --out flag is provided, overwrites relevant dialog file', default: false}),
3939
log: flags.boolean({description: 'write out log messages to console', default: false}),
4040
}
4141

@@ -108,18 +108,30 @@ export default class QnamakerBuild extends Command {
108108
if (flags.log) this.log('Handling qnamaker knowledge bases...')
109109
const dialogContents = await builder.build(qnaContents, recognizers, flags.subscriptionKey, endpoint, flags.botName, flags.suffix, flags.fallbackLocale, multiRecognizer, settings)
110110

111+
// get endpointKeys
112+
const endpointKeysInfo = await builder.getEndpointKeys(flags.subscriptionKey, endpoint)
113+
const endpointKeys: any = {
114+
"primaryEndpointKey": endpointKeysInfo.primaryEndpointKey,
115+
"secondaryEndpointKey": endpointKeysInfo.secondaryEndpointKey
116+
}
117+
111118
// write dialog assets based on config
112-
if (flags.dialog) {
113-
const outputFolder = flags.out ? path.resolve(flags.out) : dialogFilePath
119+
if (flags.out) {
120+
const outputFolder = path.resolve(flags.out)
114121
const writeDone = await builder.writeDialogAssets(dialogContents, flags.force, outputFolder, flags.dialog, files)
115122
if (writeDone) {
116123
this.log(`Successfully wrote .dialog files to ${outputFolder}\n`)
124+
this.log('QnA knowledge base endpointKeys:')
125+
this.log(endpointKeys)
117126
} else {
118127
this.log(`No changes to .dialog files in ${outputFolder}\n`)
119128
}
120129
} else {
121130
this.log('The published knowledge base setting:')
122131
this.log(JSON.parse(dialogContents[dialogContents.length - 1].content).qna)
132+
this.log('\n')
133+
this.log('QnA knowledge base endpointKeys:')
134+
this.log(endpointKeys)
123135
}
124136
} catch (error) {
125137
if (error instanceof exception) {

packages/qnamaker/test/commands/qnamaker/build.test.ts

Lines changed: 75 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ describe('qnamaker:build cli parameters test', () => {
5555
})
5656

5757
describe('qnamaker:build create a new knowledge base successfully', () => {
58-
before(async function () {
59-
await fs.ensureDir(path.join(__dirname, './../../../results/'))
60-
58+
before(function () {
6159
nock('https://westus.api.cognitive.microsoft.com')
6260
.get(uri => uri.includes('qnamaker'))
6361
.reply(200, {
@@ -100,29 +98,32 @@ describe('qnamaker:build create a new knowledge base successfully', () => {
10098
nock('https://westus.api.cognitive.microsoft.com')
10199
.put(uri => uri.includes('alterations'))
102100
.reply(204)
103-
})
104101

105-
after(async function () {
106-
await fs.remove(path.join(__dirname, './../../../results/'))
102+
nock('https://westus.api.cognitive.microsoft.com')
103+
.get(uri => uri.includes('endpointkeys'))
104+
.reply(200, {
105+
primaryEndpointKey: 'xxxx',
106+
secondaryEndpointKey: 'yyyy'
107+
})
107108
})
108109

109110
test
110111
.stdout()
111-
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/sandwich/qnafiles/sandwich.en-us.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development', '--out', './results'])
112+
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/sandwich/qnafiles/sandwich.en-us.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development'])
112113
.it('should create a new knowledge base successfully', ctx => {
113114
expect(ctx.stdout).to.contain('Handling qnamaker knowledge bases...')
114115
expect(ctx.stdout).to.contain('Creating qnamaker KB: test(development).en-us.qna...')
115116
expect(ctx.stdout).to.contain('Creating finished')
116117
expect(ctx.stdout).to.contain('Publishing kb')
117118
expect(ctx.stdout).to.contain('Publishing finished')
118119
expect(ctx.stdout).to.contain('Replacing alterations...')
120+
expect(ctx.stdout).to.contain('xxxx')
121+
expect(ctx.stdout).to.contain('yyyy')
119122
})
120123
})
121124

122125
describe('qnamaker:build update knowledge base succeed when qa changed', () => {
123-
before(async function () {
124-
await fs.ensureDir(path.join(__dirname, './../../../results/'))
125-
126+
before(function () {
126127
nock('https://westus.api.cognitive.microsoft.com')
127128
.get(uri => uri.includes('qnamaker'))
128129
.reply(200, {
@@ -153,15 +154,18 @@ describe('qnamaker:build update knowledge base succeed when qa changed', () => {
153154
nock('https://westus.api.cognitive.microsoft.com')
154155
.post(uri => uri.includes('knowledgebases'))
155156
.reply(204)
156-
})
157-
158-
after(async function () {
159-
await fs.remove(path.join(__dirname, './../../../results/'))
157+
158+
nock('https://westus.api.cognitive.microsoft.com')
159+
.get(uri => uri.includes('endpointkeys'))
160+
.reply(200, {
161+
primaryEndpointKey: 'xxxx',
162+
secondaryEndpointKey: 'yyyy'
163+
})
160164
})
161165

162166
test
163167
.stdout()
164-
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/sandwich/qnafiles/sandwich2.en-us.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development', '--out', './results'])
168+
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/sandwich/qnafiles/sandwich2.en-us.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development'])
165169
.it('should update a knowledge base when qa list changed', ctx => {
166170
expect(ctx.stdout).to.contain('Handling qnamaker knowledge bases...')
167171
expect(ctx.stdout).to.contain('Updating to new version for kb test(development).en-us.qna')
@@ -172,8 +176,6 @@ describe('qnamaker:build update knowledge base succeed when qa changed', () => {
172176

173177
describe('qnamaker:build not update knowledge if no changes', () => {
174178
before(async function () {
175-
await fs.ensureDir(path.join(__dirname, './../../../results/'))
176-
177179
nock('https://westus.api.cognitive.microsoft.com')
178180
.get(uri => uri.includes('qnamaker'))
179181
.reply(200, {
@@ -196,15 +198,18 @@ describe('qnamaker:build not update knowledge if no changes', () => {
196198
metadata: []
197199
}]
198200
})
199-
})
200201

201-
after(async function () {
202-
await fs.remove(path.join(__dirname, './../../../results/'))
202+
nock('https://westus.api.cognitive.microsoft.com')
203+
.get(uri => uri.includes('endpointkeys'))
204+
.reply(200, {
205+
primaryEndpointKey: 'xxxx',
206+
secondaryEndpointKey: 'yyyy'
207+
})
203208
})
204209

205210
test
206211
.stdout()
207-
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/sandwich/qnafiles/sandwich2.en-us.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development', '--out', './results'])
212+
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/sandwich/qnafiles/sandwich2.en-us.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development'])
208213
.it('should not update a knowledge base when no changes', ctx => {
209214
expect(ctx.stdout).to.contain('Handling qnamaker knowledge bases...')
210215
expect(ctx.stdout).to.contain('no changes')
@@ -237,6 +242,13 @@ describe('qnamaker:build write dialog assets successfully if --dialog set to mul
237242
metadata: []
238243
}]
239244
})
245+
246+
nock('https://westus.api.cognitive.microsoft.com')
247+
.get(uri => uri.includes('endpointkeys'))
248+
.reply(200, {
249+
primaryEndpointKey: 'xxxx',
250+
secondaryEndpointKey: 'yyyy'
251+
})
240252
})
241253

242254
after(async function () {
@@ -245,8 +257,11 @@ describe('qnamaker:build write dialog assets successfully if --dialog set to mul
245257

246258
test
247259
.stdout()
248-
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/sandwich/qnafiles/sandwich2.en-us.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--dialog', 'multiLanguage', '--out', './results', '--log', '--suffix', 'development'])
260+
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/sandwich/qnafiles/sandwich2.en-us.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--out', './results', '--log', '--suffix', 'development'])
249261
.it('should write dialog assets successfully when --dialog set to multiLanguage', async ctx => {
262+
expect(ctx.stdout).to.contain('xxxx')
263+
expect(ctx.stdout).to.contain('yyyy')
264+
250265
expect(await compareFiles('./../../../results/qnamaker.settings.development.westus.json', './../../fixtures/testcases/qnabuild/sandwich/config/qnamaker.settings.development.westus.json')).to.be.true
251266
expect(await compareFiles('./../../../results/test.en-us.qna.dialog', './../../fixtures/testcases/qnabuild/sandwich/dialogs/test.en-us.qna.dialog')).to.be.true
252267
expect(await compareFiles('./../../../results/test.qna.dialog', './../../fixtures/testcases/qnabuild/sandwich/dialogs/test.qna.dialog')).to.be.true
@@ -279,6 +294,13 @@ describe('qnamaker:build write dialog assets successfully if --dialog set to cro
279294
metadata: []
280295
}]
281296
})
297+
298+
nock('https://westus.api.cognitive.microsoft.com')
299+
.get(uri => uri.includes('endpointkeys'))
300+
.reply(200, {
301+
primaryEndpointKey: 'xxxx',
302+
secondaryEndpointKey: 'yyyy'
303+
})
282304
})
283305

284306
after(async function () {
@@ -345,6 +367,13 @@ describe('qnamaker:build write dialog assets successfully with multi locales', (
345367
metadata: []
346368
}]
347369
})
370+
371+
nock('https://westus.api.cognitive.microsoft.com')
372+
.get(uri => uri.includes('endpointkeys'))
373+
.reply(200, {
374+
primaryEndpointKey: 'xxxx',
375+
secondaryEndpointKey: 'yyyy'
376+
})
348377
})
349378

350379
after(async function () {
@@ -363,9 +392,7 @@ describe('qnamaker:build write dialog assets successfully with multi locales', (
363392
})
364393

365394
describe('qnamaker:build not update knowledge base if only cases are changed', () => {
366-
before(async function () {
367-
await fs.ensureDir(path.join(__dirname, './../../../results/'))
368-
395+
before(function () {
369396
nock('https://westus.api.cognitive.microsoft.com')
370397
.get(uri => uri.includes('qnamaker'))
371398
.reply(200, {
@@ -388,25 +415,26 @@ describe('qnamaker:build not update knowledge base if only cases are changed', (
388415
metadata: []
389416
}]
390417
})
391-
})
392418

393-
after(async function () {
394-
await fs.remove(path.join(__dirname, './../../../results/'))
419+
nock('https://westus.api.cognitive.microsoft.com')
420+
.get(uri => uri.includes('endpointkeys'))
421+
.reply(200, {
422+
primaryEndpointKey: 'xxxx',
423+
secondaryEndpointKey: 'yyyy'
424+
})
395425
})
396426

397427
test
398428
.stdout()
399-
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/sandwich/qnafiles/sandwich2.en-us.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development', '--out', './results'])
429+
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/sandwich/qnafiles/sandwich2.en-us.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development'])
400430
.it('should not update a knowledge base when only cases changed', ctx => {
401431
expect(ctx.stdout).to.contain('Handling qnamaker knowledge bases...')
402432
expect(ctx.stdout).to.contain('no changes')
403433
})
404434
})
405435

406436
describe('qnamaker:build create a new knowledge base with multiturn qna successfully', () => {
407-
before(async function () {
408-
await fs.ensureDir(path.join(__dirname, './../../../results/'))
409-
437+
before(function () {
410438
nock('https://westus.api.cognitive.microsoft.com')
411439
.get(uri => uri.includes('qnamaker'))
412440
.reply(200, {
@@ -445,15 +473,18 @@ describe('qnamaker:build create a new knowledge base with multiturn qna successf
445473
id: 'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
446474
hostName: 'https://myqnamakerbot.azurewebsites.net'
447475
})
448-
})
449476

450-
after(async function () {
451-
await fs.remove(path.join(__dirname, './../../../results/'))
477+
nock('https://westus.api.cognitive.microsoft.com')
478+
.get(uri => uri.includes('endpointkeys'))
479+
.reply(200, {
480+
primaryEndpointKey: 'xxxx',
481+
secondaryEndpointKey: 'yyyy'
482+
})
452483
})
453484

454485
test
455486
.stdout()
456-
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/multiturn/multiturn.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development', '--out', './results'])
487+
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/multiturn/multiturn.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development'])
457488
.it('should create a new knowledge base with multiturn qna successfully', ctx => {
458489
expect(ctx.stdout).to.contain('Handling qnamaker knowledge bases...')
459490
expect(ctx.stdout).to.contain('Creating qnamaker KB: test(development).en-us.qna...')
@@ -464,9 +495,7 @@ describe('qnamaker:build create a new knowledge base with multiturn qna successf
464495
})
465496

466497
describe('qnamaker:build update knowledge base with multiturn successfully when qa changed', () => {
467-
before(async function () {
468-
await fs.ensureDir(path.join(__dirname, './../../../results/'))
469-
498+
before(function () {
470499
nock('https://westus.api.cognitive.microsoft.com')
471500
.get(uri => uri.includes('qnamaker'))
472501
.reply(200, {
@@ -497,15 +526,18 @@ describe('qnamaker:build update knowledge base with multiturn successfully when
497526
nock('https://westus.api.cognitive.microsoft.com')
498527
.post(uri => uri.includes('knowledgebases'))
499528
.reply(204)
500-
})
501529

502-
after(async function () {
503-
await fs.remove(path.join(__dirname, './../../../results/'))
530+
nock('https://westus.api.cognitive.microsoft.com')
531+
.get(uri => uri.includes('endpointkeys'))
532+
.reply(200, {
533+
primaryEndpointKey: 'xxxx',
534+
secondaryEndpointKey: 'yyyy'
535+
})
504536
})
505537

506538
test
507539
.stdout()
508-
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/multiturn/multiturn.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development', '--out', './results'])
540+
.command(['qnamaker:build', '--in', './test/fixtures/testcases/qnabuild/multiturn/multiturn.qna', '--subscriptionKey', uuidv1(), '--botName', 'test', '--log', '--suffix', 'development'])
509541
.it('should update a knowledge base with multiturn when qa list changed', ctx => {
510542
expect(ctx.stdout).to.contain('Handling qnamaker knowledge bases...')
511543
expect(ctx.stdout).to.contain('Updating to new version')

0 commit comments

Comments
 (0)