簡體   English   中英

如何使用其 API 在 Dialogflow CX 中向意圖添加新的訓練短語?

[英]How can I add new training phrases to an intent in Dialogflow CX using its API?

我想知道是否可以通過 API 訓練 Dialogflow CX。 通過在我的代碼中放置新的訓練短語(我正在使用 NodeJS)並自動更新該意圖中的短語列表。 要添加的一件事,我想在意圖列表中添加一個新短語,而不是更新現有短語。 先感謝您!

我正在閱讀 Dialogflow CX 的文檔,發現了這個https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/update-intent.js 但是,此實現將更新特定短語而不是將其添加到列表中。

使用您在問題中提供的示例代碼,我對其進行了更新以顯示如何將新短語添加到列表中。 newTrainingPhrase將包含訓練短語 append newTrainingPhraseintent[0].trainingPhrases並將updateMask設置為“training_phrases”以指向您要更新的意圖部分。

請參見下面的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM