我正在尝试使用 Node.js 在 Dialogflow 上创建一个实体。是否有可能?如果是,我应该如何执行它?谢谢。
答案 0 :(得分:1)
是的,这是可能的。如需参考,请参阅 EntityTypesClient() 以了解您可以使用的有关实体的其他方法。
在执行代码之前,请确保您已按照 Dialogflow nodejs quickstart 中所述完成以下操作。
下面的代码示例创建一个实体 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 实体:
答案 1 :(得分:1)
是的,可以在 Dialogflow CX 和 ES 上创建实体类型。
根据您使用的 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)
结果:
方法参考: 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()