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

Hi is it possible to create an entity in Dialogflow using Node.js?

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

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

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

  1. Select 或创建云平台项目。
  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);

Dialogflow 输出:

test_sizing实体:

是的,可以在 Dialogflow 上创建实体类型 CX and ES

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

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

下面是使用 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/Trial:

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()