Bot Framework Composer中从CLU提取实体的更优方法探讨
Bot Framework Composer 从 LUIS 迁移到 CLU 后的实体提取简化方案
当前实体提取的现状
从 LUIS 迁移到 CLU 后,LUIS 中简洁的 @myEntity 实体提取语法无法直接复用,目前可行的提取逻辑分为两类:
- 普通实体:
=first(turn.recognized.entities.myEntity).text - 短语列表实体:
=first(first(turn.recognized.entities.myEntity).extraInformation).key
简化方法推荐
目前官方未提供完全等价于 @myEntity 的快捷语法,但可通过以下两种方式简化操作:
1. 自定义全局辅助函数
在 Composer 的高级设置 > 自定义函数中添加全局函数,封装实体提取逻辑:
- 普通实体提取函数:
function getEntityText(entityName) { const entity = turn.recognized.entities[entityName]; return entity?.length > 0 ? entity[0].text : null; }
- 短语列表实体提取函数:
function getPhraseListKey(entityName) { const entity = turn.recognized.entities[entityName]; return entity?.length > 0 && entity[0].extraInformation ? entity[0].extraInformation[0].key : null; }
后续设置属性时,只需调用 =getEntityText('myEntity') 或 =getPhraseListKey('myEntity') 即可,大幅缩短语句长度。
2. 预定义对话变量
预先在对话流程中定义变量存储实体值:
- 在对话的初始步骤或触发实体检测的步骤中,设置变量:
- 普通实体:
user.myEntity = first(turn.recognized.entities.myEntity).text - 短语列表实体:
user.myEntityKey = first(first(turn.recognized.entities.myEntity).extraInformation).key
- 普通实体:
- 后续流程中直接引用
user.myEntity或user.myEntityKey即可,无需重复书写冗长的表达式。
关键说明
CLU 返回的实体为数组结构(支持多实体匹配),而 LUIS 直接返回单个实体值,这是两者语法差异的核心原因,因此 @myEntity 无法直接兼容 CLU。使用上述方法时,建议加入空值判断,避免未检测到实体时引发错误。
内容的提问来源于stack exchange,提问作者DZW
相关产品推荐
相关产品推荐

