Reputation: 29
I'm trying to create an entity on Dialogflow using Node.js. Is it possible? If yes, how should I execute it? Thank you.
Upvotes: 0
Views: 825
Reputation: 413
Yes, it is possible to create entity types on Dialogflow CX and ES.
Depending on the Dialogflow Edition you are using, you can use one of the following Node.js client libraries:
Both client libraries have an EntityTypeClient
class which you can use to manage entity types. For your use case, you can use the EntityTypeClient.createEntityType()
class method to create entity types.
Below are sample codes for creating entity types for each Dialogflow editions using the Node.js client libraries:
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)
Result:
Method reference: 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)
Result:
Method reference: google.cloud.dialogflow.v2.EntityTypeClient.createEntityType()
Upvotes: 1
Reputation: 7287
Yes it is possible. For reference see EntityTypesClient() for other methods you can use regarding entities.
Prior to executing the code, make sure you have done the following as mentioned in Dialogflow nodejs quickstart.
The code example below creates an entity test_sizing with a value that have corresponding synonyms. You can also print values of response
if you need information from it.
'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 output:
test_sizing entity:
Upvotes: 1