【问题标题】:how to create Intents with Context from the DialogFlow API如何从 DialogFlow API 创建带有上下文的意图
【发布时间】:2020-09-07 06:18:58
【问题描述】:

我正在尝试批量创建一堆意图,我想分配一个Input context

据我所知,这不需要会话,因为它只是一个字符串上下文名称。 在 GUI 中像这样:

我进行的 API 调用创建了意图, 不会抛出错误, 但我无法显示上下文。

contexts 参数有什么奇怪的格式吗? 我已经导出了 zip 并查看了 JSON 文件,它们只是一个字符串数组。

我见过其他似乎需要用户对话 sessionId 来创建上下文的代码。但意图是全球性的——不是针对单一对话。而且我认为这些只是用于跟踪单个对话会话(或谷歌宇航员工程)中的上下文

我发布的数据如下所示

这里有一个不涉及上下文的谷歌示例 https://cloud.google.com/dialogflow/es/docs/how/manage-intents#create_intent

我尝试了各种格式的上下文,但没有成功


  // this is the part that doesn't work
  // const contexts = [{
  //   // name: `${sessionPath}/contexts/${name}`,
  //   // name: 'test context name'
  // }]

  const contexts = [
    'theater-critics'
  ]
createIntentRequest {
  "parent": "projects/XXXXXXXX-XXXXXXXX/agent",
  "intent": {
    "displayName": "test 4",
    "trainingPhrases": [
      {
        "type": "EXAMPLE",
        "parts": [
          {
            "text": "this is a test phrase"
          }
        ]
      },
      {
        "type": "EXAMPLE",
        "parts": [
          {
            "text": "this is a another test phrase"
          }
        ]
      }
    ],
    "messages": [
      {
        "text": {
          "text": [
            "this is a test response"
          ]
        }
      }
    ],
    "contexts": [
      "theater-critics"
    ]
  }
}
Intent projects/asylum-287516/agent/intents/XXXXXXXX-e852-4c09-bda6-e524b8329db8 created

下面的完整 JS (TS) 代码供其他人使用


import { DfConfig } from './DfConfig'
const dialogflow = require('@google-cloud/dialogflow');

const testData = {
  displayName: 'test 4',
  trainingPhrasesParts: [
    "this is a test phrase",
    "this is a another test phrase"
  ],
  messageTexts: [
    'this is a test response'
  ]
}
// const messageTexts = 'Message texts for the agent's response when the intent is detected, e.g. 'Your reservation has been confirmed';

const intentsClient = new dialogflow.IntentsClient();

export const DfCreateIntent = async () => {

  const agentPath = intentsClient.agentPath(DfConfig.projectId);

  const trainingPhrases = [];

  testData.trainingPhrasesParts.forEach(trainingPhrasesPart => {
    const part = {
      text: trainingPhrasesPart,
    };

    // Here we create a new training phrase for each provided part.
    const trainingPhrase = {
      type: 'EXAMPLE',
      parts: [part],
    };

    // @ts-ignore
    trainingPhrases.push(trainingPhrase);
  });

  const messageText = {
    text: testData.messageTexts,
  };

  const message = {
    text: messageText,
  };

  // this is the part that doesn't work
  // const contexts = [{
  //   // name: `${sessionPath}/contexts/${name}`,
  //   // name: 'test context name'
  // }]

  const contexts = [
    'theater-critics'
  ]

  const intent = {
    displayName: testData.displayName,
    trainingPhrases: trainingPhrases,
    messages: [message],
    contexts
  };

  const createIntentRequest = {
    parent: agentPath,
    intent: intent,
  };

  console.log('createIntentRequest', JSON.stringify(createIntentRequest, null, 2))

  // Create the intent
  const [response] = await intentsClient.createIntent(createIntentRequest);
  console.log(`Intent ${response.name} created`);
}

// createIntent();

【问题讨论】:

    标签: google-cloud-platform nlp dialogflow-es


    【解决方案1】:

    更新基于此 https://cloud.google.com/dialogflow/es/docs/reference/rest/v2/projects.agent.intents#Intent

    
      const contextId = 'runner'
      const contextName = `projects/${DfConfig.projectId}/agent/sessions/-/contexts/${contextId}`
      const inputContextNames = [
        contextName
      ]
    
      const intent = {
        displayName: testData.displayName,
        trainingPhrases: trainingPhrases,
        messages: [message],
        inputContextNames
      };
    
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多