使用 Node.js 在 Dialogflow 中创建实体

Creating entites in Dialogflow using Node.js

我正在尝试使用 node.js 在 Dialogflow 中创建实体。实体值将来自 JSON 文件。我目前正在使用邮递员对其进行测试。但是,实体值并没有被分开,而是只组合在一行中。我该如何解决?谢谢

这是我正在发送的示例 JSON 文件。

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

这是我目前拥有的:

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],
 },
 };

这段代码输出

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

而在 Dialogflow 中,这些都在一个实体条目中,而不是在两个单独的条目中。

您的 JSON 输入几乎与 entities (reference) 所需的对象格式相同,只需稍作调整即可使其正常工作。使用你的 JSON,我将 ValueSynonym 重命名为 valuesynonyms。遍历 Entity_values 并将键值对推送到列表 entityVal 并将其用于请求。

'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);

查看下面完成的测试:

request结构:

{
  "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"
        ]
      }
    ]
  }
}

在对话流中创建的实体: