如何解决嗨,是否可以使用 Node.js 在 Dialogflow 中创建实体?
我正在尝试使用 Node.js 在 Dialogflow 上创建一个实体。是否有可能?如果是,我应该如何执行它?谢谢。
解决方法
是的,这是可能的。如需参考,请参阅 EntityTypesClient() 以了解您可以使用的有关实体的其他方法。
在执行代码之前,请确保您已按照 Dialogflow nodejs quickstart 中所述完成以下操作。
- 选择或创建一个 Cloud Platform 项目。
- 为您的项目启用结算功能。
- 启用 Dialogflow API API。
- 使用服务帐户设置身份验证,以便您可以从本地工作站访问 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 实体:
,是的,可以在 Dialogflow CX 和 ES 上创建实体类型。
根据您使用的 Dialogflow 版本,您可以使用以下 Node.js 客户端库之一:
- Dialogflow CX - @google-cloud/dialogflow-cx
- Dialogflow ES/试用 - @google-cloud/dialogflow
两个客户端库都有一个 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,agentId)
let entityType = {
displayName,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,kind)
结果:
方法参考: google.cloud.dialogflow.cx.v3.EntityTypeClient.createEntityType()
Dialogflow ES/试用版:
const {
EntityTypesClient
} = require('@google-cloud/dialogflow');
const client = new EntityTypesClient()
async function createEntityType(projectId,kind) {
parent = client.projectAgentPath(projectId)
let entityType = {
displayName,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",{
value: "Medium",synonyms: [
"Medium",kind)
结果:
方法参考: google.cloud.dialogflow.v2.EntityTypeClient.createEntityType()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。