如何识别触发Lambda Function调用的具体Slot?
如何识别触发Lambda函数的Slot
当你的Lambda函数被对话系统(比如AWS Lex、Alexa Skills Kit)调用时,触发它的Slot信息会包含在Lambda接收的事件请求体里,你可以通过解析这个请求体来定位具体触发的Slot。以下是具体实现方式:
1. 解析Lambda的输入事件
Lambda每次被调用时,都会收到包含对话上下文的事件对象。以AWS Lex为例,事件里的currentIntent.slots字段会列出所有已填充的Slot,结合invocationSource或dialogAction.type可辅助判断当前处于Slot填充阶段还是其他流程阶段。
示例代码(Python)
def lambda_handler(event, context): # 获取当前意图与Slot集合 current_intent = event['currentIntent'] slots = current_intent['slots'] # 筛选出已填充的非空Slot filled_slots = [slot_name for slot_name, value in slots.items() if value is not None] # 结合会话属性判断最新触发的Slot recent_slot = None session_attrs = event.get('sessionAttributes', {}) last_recorded_slot = session_attrs.get('last_triggered_slot') for slot in filled_slots: if slot != last_recorded_slot: recent_slot = slot break # 根据不同Slot执行对应逻辑 if recent_slot == 'UserAge': # 处理年龄Slot触发的业务逻辑 pass elif recent_slot == 'UserLocation': # 处理地点Slot触发的业务逻辑 pass # 更新会话属性,记录当前触发的Slot session_attrs['last_triggered_slot'] = recent_slot return { 'sessionAttributes': session_attrs, 'dialogAction': { 'type': 'Delegate', 'slots': slots } }
2. 利用对话平台的上下文标记
不同对话平台会提供专属字段标识触发的Slot:
- Alexa Skills Kit:
request.intent.slots中的每个Slot对象包含value和confirmationStatus,刚填充的Slot通常confirmationStatus为NONE,结合对话历史可区分触发源。 - AWS Lex V2:事件结构调整为
interpretations[0].intent.slots存储Slot信息,sessionState.dialogAction.type可判断是否处于Slot填充流程。
3. 关键注意事项
- 如果是必填Slot自动填充触发的调用,此时只有刚填充的Slot有值,其余未填充Slot为
null,直接通过非空Slot列表即可判断触发源。 - 若存在Slot确认流程,确认动作触发的调用会在Slot的
confirmationStatus字段标记为CONFIRMED或DENIED,以此区分是填充触发还是确认触发。
内容的提问来源于stack exchange,提问作者Ajay Naik
相关产品推荐
相关产品推荐

