forked from microsoft/BotFramework-Composer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluisAndQnA.ts
261 lines (227 loc) · 8.79 KB
/
luisAndQnA.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import * as path from 'path';
import * as fs from 'fs-extra';
import { isUsingAdaptiveRuntime } from '@bfc/shared';
import { ILuisConfig, FileInfo, IBotProject, RuntimeTemplate, DialogSetting } from '@botframework-composer/types';
import axios, { AxiosRequestConfig } from 'axios';
import { AzurePublishErrors } from './utils/errorHandler';
import { BotProjectDeployLoggerType } from './types';
const botPath = (projPath: string, runtime?: DialogSetting['runtime']) => projPath;
type QnaConfigType = {
subscriptionKey: string;
qnaRegion: string | 'westus';
};
type Resource = { id: string; isEmpty: boolean };
type Resources = {
luResources: Resource[];
qnaResources: Resource[];
};
interface BuildSettingType {
luis: ILuisConfig;
qna: QnaConfigType;
luResources: Resource[];
qnaResources: Resource[];
runtime?: DialogSetting['runtime'];
}
function getAccount(accounts: any, filter: string) {
for (const account of accounts) {
if (account.AccountName === filter) {
return account;
}
}
}
/**
* return an array of all the files in a given directory
* @param dir
*/
async function getFiles(dir: string): Promise<string[]> {
const files = [];
// eslint-disable-next-line security/detect-non-literal-fs-filename
const dirents = await fs.readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const res = path.resolve(dir, dirent.name);
if (dirent.isDirectory()) {
files.push(...(await getFiles(res)));
} else {
files.push(res);
}
}
return files;
}
const extractLuisErrorData = (err) => {
let errorMsg = '';
if (err.response?.data) {
errorMsg = `Response Status: ${err.response.status || 'Unknown'}; `;
if (err.response.data.error) {
errorMsg =
errorMsg +
`Error Code: ${err.response.data.error.code || 'Unknown'}; Error Message: ${
err.response.data.error.message || 'Unknown'
} `;
}
}
return errorMsg;
};
export async function publishLuisToPrediction(
name: string,
environment: string,
accessToken: string,
luisSettings: ILuisConfig,
luisResource: string,
path: string,
logger,
runtime?: RuntimeTemplate
) {
let {
// eslint-disable-next-line prefer-const
authoringKey: luisAuthoringKey,
authoringEndpoint: authoringEndpoint,
authoringRegion: luisAuthoringRegion,
} = luisSettings;
if (!luisAuthoringRegion) {
luisAuthoringRegion = luisSettings.region || 'westus';
}
if (!authoringEndpoint) {
authoringEndpoint = `https://${luisAuthoringRegion}.api.cognitive.microsoft.com`;
}
// Find any files that contain the name 'luis.settings' in them
// These are generated by the LuBuild process and placed in the generated folder
// These contain dialog-to-luis app id mapping
const luisConfigFiles = (await getFiles(botPath(path, runtime))).filter((filename) =>
filename.includes('luis.settings')
);
const luisAppIds: any = {};
// Read in all the luis app id mappings
for (const luisConfigFile of luisConfigFiles) {
const luisSettings = await fs.readJson(luisConfigFile);
Object.assign(luisAppIds, luisSettings.luis);
}
if (!Object.keys(luisAppIds).length) return luisAppIds;
logger({
status: BotProjectDeployLoggerType.DEPLOY_INFO,
message: 'start publish luis',
});
// In order for the bot to use the LUIS models, we need to assign a LUIS key to the endpoint of each app
// First step is to get a list of all the accounts available based on the given luisAuthoringKey.
let accountList;
// Retry twice here
let retryCount = 0;
while (retryCount < 2) {
try {
// Make a call to the azureaccounts api
// DOCS HERE: https://westus.dev.cognitive.microsoft.com/docs/services/5890b47c39e2bb17b84a55ff/operations/5be313cec181ae720aa2b26c
// This returns a list of azure account information objects with AzureSubscriptionID, ResourceGroup, AccountName for each.
const getAccountUri = `${authoringEndpoint}/luis/api/v2.0/azureaccounts`;
const options: AxiosRequestConfig = {
headers: { Authorization: `Bearer ${accessToken}`, 'Ocp-Apim-Subscription-Key': luisAuthoringKey },
};
const response = await axios.get(getAccountUri, options);
// this should include an array of account info objects
accountList = response?.data ?? [];
break;
} catch (err) {
if (retryCount < 1) {
logger({
status: AzurePublishErrors.LUIS_PUBLISH_ERROR,
message: JSON.stringify(err, Object.getOwnPropertyNames(err)),
});
retryCount++;
} else {
// handle the token invalid
const error = JSON.parse(err.error);
if (error?.error?.message && error?.error?.message.indexOf('access token expiry') > 0) {
throw new Error(
`Type: ${error?.error?.code}, Message: ${error?.error?.message}, run az account get-access-token, then replace the accessToken in your configuration`
);
} else {
throw err;
}
}
}
}
// Extract the account object that matches the expected resource name.
// This is the name that would appear in the azure portal associated with the luis endpoint key.
const account = getAccount(accountList, luisResource ? luisResource : `${name}-${environment}-luis`);
// Assign the appropriate account to each of the applicable LUIS apps for this bot.
// DOCS HERE: https://westus.dev.cognitive.microsoft.com/docs/services/5890b47c39e2bb17b84a55ff/operations/5be32228e8473de116325515
for (const dialogKey in luisAppIds) {
const luisAppId = luisAppIds[dialogKey].appId;
logger({
status: BotProjectDeployLoggerType.DEPLOY_INFO,
message: `Assigning to luis app id: ${luisAppId}`,
});
// Retry at most twice for each api call
let retryCount = 0;
while (retryCount < 2) {
try {
const luisAssignEndpoint = `${authoringEndpoint}/luis/api/v2.0/apps/${luisAppId}/azureaccounts`;
const options: AxiosRequestConfig = {
headers: { Authorization: `Bearer ${accessToken}`, 'Ocp-Apim-Subscription-Key': luisAuthoringKey },
};
await axios.post(luisAssignEndpoint, account, options);
break;
} catch (err) {
if (retryCount < 1) {
logger({
status: AzurePublishErrors.LUIS_PUBLISH_ERROR,
message: JSON.stringify(err, Object.getOwnPropertyNames(err), 2),
});
retryCount++;
} else {
// handle the token invalid
// handle the token invalid
if (typeof err.error === 'string') {
const error = JSON.parse(err.error);
if (error?.error?.message && error?.error?.message.indexOf('access token expiry') > 0) {
throw new Error(
`Type: ${error?.error?.code}, Message: ${error?.error?.message}, run az account get-access-token, then replace the accessToken in your configuration`
);
}
}
const luisError = extractLuisErrorData(err);
const luisDebugInfo = `Luis App ID: ${luisAppId}; Luis Authoring Key: ${luisAuthoringKey}; AzureSubscriptionID: ${account.AzureSubscriptionId} ResourceGroup: ${account.ResourceGroup}; AccountName: ${account.AccountName}; Location: ${account.Location}`;
throw Error(
`Failed to bind luis prediction resource to luis applications. Please check if your luisResource is set to luis prediction service name in your publish profile.
${luisDebugInfo}.
${luisError}`
);
}
}
}
}
// The process has now completed.
logger({
status: BotProjectDeployLoggerType.DEPLOY_INFO,
message: 'Luis Publish Success! ...',
});
// return the new settings that need to be added to the main settings file.
return luisAppIds;
}
export async function build(project: IBotProject, path: string, settings: BuildSettingType) {
const { luResources, qnaResources, luis: luisConfig, qna: qnaConfig } = settings;
const { builder, files } = project;
const luFiles: FileInfo[] = [];
const emptyFiles = {};
luResources.forEach(({ id, isEmpty }) => {
const fileName = `${id}.lu`;
const f = files.get(fileName);
if (isEmpty) emptyFiles[fileName] = true;
if (f) {
luFiles.push(f);
}
});
const qnaFiles: FileInfo[] = [];
qnaResources.forEach(({ id, isEmpty }) => {
const fileName = `${id}.qna`;
const f = files.get(fileName);
if (isEmpty) emptyFiles[fileName] = true;
if (f) {
qnaFiles.push(f);
}
});
builder.rootDir = botPath(path, settings?.runtime);
builder.setBuildConfig({ ...luisConfig, ...qnaConfig }, project.settings.downsampling, project.settings.crossTrain);
await builder.build(luFiles, qnaFiles, Array.from(files.values()) as FileInfo[], emptyFiles);
await builder.copyModelPathToBot();
}