要使用LUIS SDK來透過LUIS應用程式預測用戶的意圖,我們需要預測端點App ID, predictionResourceName, authoringKey 等重要參數。
在Day [15]時我們將Chatbot會使用到的重要設定參數交由config.ts程式管理,現在我們把LUIS的金耀等重要資訊也寫入config.ts,完成後目前的程式如下:
export const LINE = {
channelSecret: "<YOUR_CHANNEL_SECRET>",
channelAccessToken: "<YOUR_CHANNEL_ACCESS_TOKEN>"
};
export const REDIS = {
REDISCACHEHOSTNAME: '<YOUR_AZURE_REDIS_REDISCACHEHOSTNAME>',
REDISCACHEKEY: '<YOUR_AZURE_REDIS_REDISCACHEKEY>'
}
export const LUIS = {
appId: "<PEPLACE-WITH-YOUR-APPID>",
authoringKey: "<REPLACE-WITH-YOUR-ASSIGNED-AUTHORING-KEY>",
predictionResourceName: "<REPLACE-WITH-YOUR-PREDICTION-RESOURCE-NAME>"
}
appID可在LUIS入口網站點選Settings取得:
authoringKey&predictionResourceName可點選Azure Resources中Prediction Resources找到:
設定config.ts完成後,我們新增一隻LUISService.ts程式來負責Chatbot中的語意理解功能
const msRest = require("@azure/ms-rest-js");
const LUIS_Prediction = require("@azure/cognitiveservices-luis-runtime");
import { LUIS } from "../config"
const predictionEndpoint = `https://${LUIS.predictionResourceName}.cognitiveservices.azure.com/`;
const luisAuthoringCredentials = new msRest.ApiKeyCredentials({
inHeader: { "Ocp-Apim-Subscription-Key": LUIS.authoringKey }
});
const luisPredictionClient = new LUIS_Prediction.LUISRuntimeClient(
luisAuthoringCredentials,
predictionEndpoint
);
export const languageUnderStanding = async (query: string) => {
const request = { query: query };
const response = await luisPredictionClient.prediction.getSlotPrediction(LUIS.appId, "Production", request);
// console.log(JSON.stringify(response.prediction, null, 4));
return response
}
明天就可以使用今天的程式來完成整合Chatbot與LUIS。