2

I want to know if it's possible to train Dialogflow CX through API. By placing the new training phrases in my code (I am using NodeJS) and automatically update the list of phrases in that intent. One thing to add, I want to add a new phrase to the intent list no update an existing phrase. Thank you in advance!

I was reading the documentation of Dialogflow CX and found this, https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/update-intent.js. But, this implementation will update a specific phrase instead of add it to the list.

1 Answers1

0

Using the sample code that you have provided in your question, I updated it to show how to add a new phrase to the list. newTrainingPhrase will contain the training phrase, append newTrainingPhrase to intent[0].trainingPhrases and set updateMask to "training_phrases" to point to the part of the intent you would like to update.

See code below:

'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));

Ricco D
  • 6,873
  • 1
  • 8
  • 18