【问题标题】:Hi is it possible to create an entity in Dialogflow using Node.js?嗨,是否可以使用 Node.js 在 Dialogflow 中创建实体?
【发布时间】:2021-07-12 01:42:54
【问题描述】:

我正在尝试使用 Node.js 在 Dialogflow 上创建一个实体。是否可以?如果是,我应该如何执行它?谢谢。

【问题讨论】:

    标签: node.js dialogflow-es dialogflow-es-fulfillment dialogflow-cx


    【解决方案1】:

    是的,这是可能的。如需参考,请参阅EntityTypesClient(),了解您可以使用的有关实体的其他方法。

    在执行代码之前,请确保您已按照Dialogflow nodejs quickstart 中的说明完成以下操作。

    1. 选择或创建一个 Cloud Platform 项目。
    2. 为您的项目启用结算功能。
    3. 启用 Dialogflow API API。
    4. 使用服务帐户设置身份验证,以便您可以从本地工作站访问 API。

    下面的代码示例创建了一个实体 test_sizing,其值具有对应的同义词。如果需要从中获取信息,还可以打印 response 的值。

    'use strict';
    
    const dialogflow = require('@google-cloud/dialogflow');
    
    const entityClient = new dialogflow.EntityTypesClient();
    const agentPath = entityClient.projectAgentPath('your-project-id-here');
    const entityType = {
            displayName: 'test_sizing',
            kind: 'KIND_MAP',
            entities: [
            {value: 'small', synonyms: ['small', 'petit']},
            {value: 'medium', synonyms: ['medium']},
            {value: 'large', synonyms: ['large', 'big']},
          ],
    };
    const request = { parent: agentPath, entityType: entityType };
    const response = entityClient.createEntityType(request);
    

    对话流输出:

    test_sizing 实体:

    【讨论】:

      【解决方案2】:

      是的,可以在 Dialogflow CXES 上创建实体类型。

      根据您使用的 Dialogflow 版本,您可以使用以下 Node.js 客户端库之一:

      两个客户端库都有一个EntityTypeClient 类,您可以使用它来管理实体类型。对于您的用例,您可以使用EntityTypeClient.createEntityType() 类方法来创建实体类型。

      以下是使用 Node.js 客户端库为每个 Dialogflow 版本创建实体类型的示例代码:

      Dialogflow CX:

      const {EntityTypesClient} = require('@google-cloud/dialogflow-cx');
      
      const client = new EntityTypesClient()
      
      async function createEntityType(projectId, location, agentId, language, displayName, entities, kind) {
      
          parent = client.agentPath(projectId, location, agentId)
      
          let entityType = {
              displayName,
              entities,
              kind
          }
      
          let request = {
              parent,
              entityType,
              language
          }
      
          const [response] = await client.createEntityType(request);
          console.log(response)
      }
      
      projectId = "<PROJECT_ID>"
      location = "<LOCATION>"
      agentId = "<AGENT_ID>" 
      language = "<LANGUAGE_CODE>" 
      displayName = "size" 
      kind = "KIND_MAP" 
      entities = [                  
          {
              value:"Small",
              synonyms:[
                  "Small",
                  "S"
              ]
          },
          {
              value:"Medium",
              synonyms:[
                  "Medium",
                  "M"
              ]
          }
      ]
      
      createEntityType(projectId, location, agentId, language, displayName, entities, kind)
      

      结果:

      方法参考:google.cloud.dialogflow.cx.v3.EntityTypeClient.createEntityType()

      Dialogflow ES/试用版:

      const {
          EntityTypesClient
      } = require('@google-cloud/dialogflow');
      
      const client = new EntityTypesClient()
      
      async function createEntityType(projectId, language, displayName, entities, kind) {
      
          parent = client.projectAgentPath(projectId)
      
          let entityType = {
              displayName,
              entities,
              kind
          }
      
          let request = {
              parent,
              entityType,
              language
          }
      
          const [response] = await client.createEntityType(request);
          console.log(response)
      }
      
      projectId = "<PROJECT_ID>"
      language = "<LANGUAGE_CODE>"
      
      displayName = "size"
      kind = "KIND_MAP"
      entities = [{
              value: "Small",
              synonyms: [
                  "Small",
                  "S"
              ]
          },
          {
              value: "Medium",
              synonyms: [
                  "Medium",
                  "M"
              ]
          }
      ]
      
      createEntityType(projectId, language, displayName, entities, kind)
      

      结果:

      方法参考:google.cloud.dialogflow.v2.EntityTypeClient.createEntityType()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-29
        • 1970-01-01
        • 2020-10-16
        • 1970-01-01
        • 1970-01-01
        • 2018-08-17
        • 2016-02-14
        • 1970-01-01
        相关资源
        最近更新 更多