嗨,是否可以使用 Node.js 在 Dialogflow 中创建实体?

时间:2021-07-12 01:42:54

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

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

2 个答案:

答案 0 :(得分: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);

对话流输出:

enter image description here

test_sizing 实体:

enter image description here

答案 1 :(得分:1)

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

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

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

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

Dialogflow 客户体验:

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)

结果:

enter image description here

方法参考: 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)

结果:

enter image description here

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

相关问题