在Dialogflow CX中创建SessionEntityType时遭遇NotFound错误求助
Dialogflow CX会话实体创建报错及跨会话持久化解决方案
问题描述
使用Node.js客户端在Google Dialogflow CX中实现会话实体,意图在当前会话存储自定义值,但调用createSessionEntityType时触发错误:
Error: 5 NOT_FOUND: com.google.apps.framework.request.NotFoundException: No entity is found for this agent id '844d799-4bc6-826b-bfc38de06' and entity id 'userState'
目标为跨会话持久化与userState关联的值,原实现代码如下:
async createContextSessionEntities(conversationId: string, entityTypeDisplayName: string, contextParameters: string[], environment?: string, userId?: string, entityMode?: string) { // console.log(conversationId,entityTypeDisplayName, contextParameters, entityMode); try { const sessionEntityTypesClient = new dialogflow.SessionEntityTypesClient( { credentials: this.config as any, apiEndpoint: this.location !== 'global' ? `${this.location}-dialogflow.googleapis.com` : 'dialogflow.googleapis.com' }); const sessionEntityTypesClientPath = sessionEntityTypesClient.projectLocationAgentSessionPath( this.dialogFlowProjectId, this.location, this.agentid, conversationId ) const sessionParamter = { name: `${sessionEntityTypesClientPath}/entityTypes/${entityTypeDisplayName}`, entityOverrideMode: entityMode || "ENTITY_OVERRIDE_MODE_SUPPLEMENT" as any, entities: [{ name: Object.keys(contextParameters) as any, value: Object.keys(contextParameters) as any, }]} const sessionEntityTypeRequest = { parent: sessionEntityTypesClientPath, sessionEntityType: sessionParamter, }; const [response] = await sessionEntityTypesClient.createSessionEntityType( sessionEntityTypeRequest ); LogManager.EventInfoLog('SessionEntityType created:'); return response; } catch (error) { LogManager.EventErrorLog('df error creating session entity', error.stack) throw new Error(error.stack); } }
注:代码中entityTypeDisplayName对应值为userState。
问题原因及解决办法
1. 基础实体未创建
Dialogflow CX的会话实体必须依赖Agent中已存在的基础实体类型。报错说明你的Agent内没有ID为userState的基础实体,需先在Dialogflow CX控制台创建该实体。
2. 实体名称/ID不匹配
代码中使用entityTypeDisplayName作为实体ID传入,但Dialogflow API要求使用实体的ID(而非控制台显示名称)。若控制台中实体的ID与userState不一致,会导致找不到实体。需确认实体ID并替换代码中的entityTypeDisplayName参数。
3. 会话实体无法跨会话持久化
会话实体仅绑定当前会话,会话结束后自动失效,无法实现跨会话持久化。若要保存userState跨会话使用,需采用以下方案:
- 使用Dialogflow CX的全局上下文(设置较长生命周期)存储状态值
- 将状态值存入外部数据库(如Firebase、MySQL),通过用户ID关联,在新会话启动时加载并同步到上下文或实体中
修正后的代码示例
async createContextSessionEntities(conversationId: string, entityTypeId: string, contextParameters: Record<string, string>, environment?: string, userId?: string, entityMode?: string) { try { const sessionEntityTypesClient = new dialogflow.SessionEntityTypesClient({ credentials: this.config as any, apiEndpoint: this.location !== 'global' ? `${this.location}-dialogflow.googleapis.com` : 'dialogflow.googleapis.com' }); const sessionPath = sessionEntityTypesClient.projectLocationAgentSessionPath( this.dialogFlowProjectId, this.location, this.agentid, conversationId ); // 构造符合要求的实体列表:每个实体包含value和同义词数组 const entities = Object.entries(contextParameters).map(([synonym, value]) => ({ value: value, synonyms: [synonym] })); const sessionEntityType = { name: `${sessionPath}/entityTypes/${entityTypeId}`, entityOverrideMode: entityMode || "ENTITY_OVERRIDE_MODE_SUPPLEMENT", entities: entities }; const request = { parent: sessionPath, sessionEntityType: sessionEntityType, }; const [response] = await sessionEntityTypesClient.createSessionEntityType(request); LogManager.EventInfoLog('SessionEntityType created:', response); return response; } catch (error) { LogManager.EventErrorLog('df error creating session entity', error.stack); throw error; } }
内容的提问来源于stack exchange,提问作者subhajit saha
相关产品推荐
相关产品推荐

