我在我的Lex机器人中有4个意图,这些意图的逻辑非常相似,只是业务规则略有变化。
实现一个lambda函数,然后根据不同的意图调用不同的函数,这是一种好的实践吗?
这种方法是否会带来任何潜在的瓶颈或性能影响?
发布于 2018-02-01 14:28:44
对于不同的意图,使用单个Lambda函数是没有问题的。您可以只调用所有意图中的单个lambda函数,检查该lambda中的意图,并在同一lambda中调用相关的函数/方法。
正如你所说的,意图是非常相似的,所以你可能也可以使用通用函数来为这些意图做类似的事情。
def common_function():
# some processing
return cm
def intent2(intent_request):
cm = common_function()
# rest processing
return output
def intent1(intent_request):
cm = common_function()
# rest processing
return output
def dispatch(intent_request):
logger.debug('dispatch userId={}, intentName={}'.format(intent_request['userId'], intent_request['currentIntent']['name']))
intent_name = intent_request['currentIntent']['name']
if intent_name == 'intent1':
return intent1(intent_request)
if intent_name == 'intent2':
return intent2(intent_request)
if intent_name == 'intent3':
return intent3(intent_request)
if intent_name == 'intent4':
return intent4(intent_request)
raise Exception('Intent with name ' + intent_name + ' not supported')
def lambda_handler(event, context):
logger.debug(event)
logger.debug('event.bot.name={}'.format(event['bot']['name']))
return dispatch(event)https://stackoverflow.com/questions/48555866
复制相似问题