Error: Resource name 'foo' does not match 'projects/*/agent'

Error: Resource name 'foo' does not match 'projects/*/agent'

我正在尝试在 DialogFlow 上创建一个新的 Entity

const dialogflow = require('dialogflow');

/**
 * trains the NLP to recognize language constructs
 */
export function intTraining() {

  const ENTITY_DEFINITION_BOOLEAN = {
    parent: 'foo',
    entityType: {
      displayName: 'myBoolean',
      kind: 'KIND_MAP',
      autoExpansionMode: 'AUTO_EXPANSION_MODE_UNSPECIFIED',
      entities: [
        {value: 'true',  synonyms: [ 'yes', 'yeah', 'sure', 'okay' ]},
        {value: 'false', synonyms: [ 'no', 'no thanks', 'never' ]}
      ],
    },
  };

  // allocate
  const entityTypesClient = new dialogflow.EntityTypesClient();
  // declare promises
  const promises = [];
  // allocate entities
  prepareEntity(entityTypesClient, promises, ENTITY_DEFINITION_BOOLEAN);
  // execute state initialization
  Promise.all(promises);
}

/** Buffers an Entity onto the Promise Queue. */
function prepareEntity(entityTypesClient, promises, definition) {
  // boolean entity
  promises.push(entityTypesClient
    .createEntityType(definition)
    .then(responses => { })
    .catch(err => { console.error('', err) })
  );
}

然而,当我执行这段代码时,我收到以下错误:

Error: Resource name 'foo' does not match 'projects/*/agent'.

我已经使用 gcloud auth application-default login 在我的机器上创建了 API 访问凭据,foo 配置为当前选择的项目,但这没有帮助。

我做错了什么?

您需要提供项目代理的准确路径。您应该使用:

,而不是在 ENTITY_DEFINITION_BOOLEAN 的分配中使用 parent : foo

parent: 'projects/foo/agent'

这与 DialogFlow 预期的路径结构相匹配。

您需要在创建实体请求中包含 Dialogflow 代理的完整路径,因为您的帐户可能有权访问多个代理。 Dialogflow v2 Node.js 库(您似乎正在使用)有一个辅助方法,可以使用代理的项目 ID 为您构建代理路径(可以在 your Dialogflow agent's settings). Here is an modified excerpt of code from Dialogflow's v2 Node.js samples 中找到,它显示了如何构建和发出创建实体类型请求:

// Imports the Dialogflow library
const dialogflow = require('dialogflow');

// Instantiates clients
const entityTypesClient = new dialogflow.EntityTypesClient();
const intentsClient = new dialogflow.IntentsClient();

// The path to the agent the created entity type belongs to.
const agentPath = intentsClient.projectAgentPath(projectId);

// Create an entity type named "size", with possible values of small, medium
// and large and some synonyms.
const sizeRequest = {
  parent: agentPath,
  entityType: {
    displayName: 'size',
    entities: [
      {value: 'small', synonyms: ['small', 'petit']},
      {value: 'medium', synonyms: ['medium']},
      {value: 'large', synonyms: ['large', 'big']},
    ],
  },
};

entityTypesClient.createEntityType(sizeRequest)
  .then(responses => {
    console.log('Created size entity type:');
    logEntityType(responses[0]);
  })
  .catch(err => {
    console.error('Failed to create size entity type:', err);
  })
);