javascriptnode.jsdialogflow-esdialogflow-es-fulfillment

Creating entites in Dialogflow using Node.js


I'm trying to create entites in Dialogflow using node.js. The entity values will come from a JSON file. I'm currently testing it using postman. However, the entity values are not being separated and being combined in only one line. How do I fix this? Thank you

This is my sample JSON file that is being sent.

     {
"Entity_Values": [
    {
    "Value": "One",
    "Synonym":["Solo","1"]},
    {
    "Value": "Two",
    "Synonym":["Double","2"]}
    ],
"Entity_Name": "Sample"}

This is what I have so far:

function createEntity (inputEntityobj, EntityName) {
 const promises = [];
 let entity_values = {value:[], synonyms:[]};
 let inputEntity_values = [inputEntityobj];

 for (i = 0; i < inputEntityobj.length; ++i) {
  let inputEntity_values = [inputEntityobj[i].Value];
  let inputEntity_synonym = [inputEntityobj[i].Synonym];
  entity_values.value.push(inputEntity_values);
  entity_values.synonyms.push(inputEntity_synonym);
 }

 const sizeRequest = {
 parent: agentPath,
 entityType: {
  displayName: (EntityName),
  kind: 'KIND_MAP',
  autoExpansionMode: 'AUTO_EXPANSION_MODE_UNSPECIFIED',
  enableFuzzyExtraction: true,
  entities: [entity_values],
 },
 };

This code outputs

value: [ [ 'One' ], [ 'Two' ] ], synonyms: [ [ [Array] ], [ [Array] ] ]

And in Dialogflow, these are all in one entity entry instead of being in two separate entries.


Solution

  • Your JSON input is almost identical to the required object format for entities (reference) and will only need a little bit of tweaking to make it work. Using your JSON, I renamed the Value and Synonym to value and synonyms. Loop through Entity_values and push the key value pair to list entityVal and use it for the request.

    'use strict';
    
    const dialogflow = require('@google-cloud/dialogflow').v2;
    
    var inputEntityObj =      {
    "Entity_Values": [
        {
        "Value": "One",
        "Synonym":["Solo","1"]},
        {
        "Value": "Two",
        "Synonym":["Double","2"]}
        ],
    "Entity_Name": "Sample"};
    
    var obj = inputEntityObj['Entity_Values'];
    
    // rename keys
    var res = obj.map(item => {
      item.value= item.Value;
      item.synonyms= item.Synonym;
      delete item.Value;
      delete item.Synonym;
      return item;
    });
    
    
    var entityVal =[];
    for (const entity in res){
            entityVal.push(res[entity]);
    }
    
    var projectId = 'your-project-id';
    var entityType = { displayName: 'test', //hard coded value for testing purposes
            kind: 'KIND_MAP',
            autoExpansionMode: 'AUTO_EXPANSION_MODE_UNSPECIFIED',
            enableFuzzyExtraction: true,
            entities: entityVal,
    
    };
    
    var parent = `projects/${projectId}/agent`;
    var request = { parent: parent,
            entityType: entityType
    };
    
    console.log(JSON.stringify(request, null, 2));
    
    const client = new dialogflow.EntityTypesClient();
    client.createEntityType(request);
    

    See testing done below:

    request structure:

    {
      "parent": "projects/your-project-id/agent",
      "entityType": {
        "displayName": "test",
        "kind": "KIND_MAP",
        "autoExpansionMode": "AUTO_EXPANSION_MODE_UNSPECIFIED",
        "enableFuzzyExtraction": true,
        "entities": [
          {
            "value": "One",
            "synonyms": [
              "Solo",
              "1"
            ]
          },
          {
            "value": "Two",
            "synonyms": [
              "Double",
              "2"
            ]
          }
        ]
      }
    }
    

    Created entity in dialogflow:

    enter image description here