如何使用其API将新的训练短语添加到Dialogflow CX中的意图



我想知道是否可以通过API训练Dialogflow CX。通过在我的代码中放置新的训练短语(我使用的是NodeJS(,并自动更新该意图中的短语列表。需要补充的一点是,我想在意向列表中添加一个新短语,而不是更新现有短语。提前谢谢!

我在阅读Dialogflow CX的文档时发现,https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/update-intent.js.但是,此实现将更新特定短语,而不是将其添加到列表中。

使用您在问题中提供的示例代码,我对其进行了更新,以展示如何向列表中添加新短语。newTrainingPhrase将包含训练短语、将newTrainingPhrase附加到intent[0].trainingPhrases并将updateMask设置为"0";training_ phrass";指向您想要更新的意图部分。

参见以下代码:

'use strict';
async function main(projectId, agentId, intentId, location, displayName) {
const {IntentsClient} = require('@google-cloud/dialogflow-cx');
const intentClient = new IntentsClient({apiEndpoint: 'us-central1-dialogflow.googleapis.com'});
async function updateIntent() {
const projectId = 'your-project-id';
const agentId = 'your-agent-id';
const intentId = 'your-intent-id';
const location = 'us-central1'; // define your location
const displayName = 'store.hours'; // define display name
const agentPath = intentClient.projectPath(projectId);
const intentPath = `${agentPath}/locations/${location}/agents/${agentId}/intents/${intentId}`;
//define your training phrase
var newTrainingPhrase =  {
"parts": [
{
"text": "What time do you open?",
"parameterId": ""
}
],
"id": "",
"repeatCount": 1
};
const intent = await intentClient.getIntent({name: intentPath});
intent[0].trainingPhrases.push(newTrainingPhrase);
const updateMask = {
paths: ['training_phrases'],
};
const updateIntentRequest = {
intent: intent[0],
updateMask,
languageCode: 'en',
};
//Send the request for update the intent.
const result = await intentClient.updateIntent(updateIntentRequest);
console.log(result);
}
updateIntent();
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));

最新更新