首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用一个Lambda函数处理多个意图

使用一个Lambda函数处理多个意图
EN

Stack Overflow用户
提问于 2018-02-01 13:23:19
回答 1查看 824关注 0票数 0

我在我的Lex机器人中有4个意图,这些意图的逻辑非常相似,只是业务规则略有变化。

实现一个lambda函数,然后根据不同的意图调用不同的函数,这是一种好的实践吗?

这种方法是否会带来任何潜在的瓶颈或性能影响?

EN

回答 1

Stack Overflow用户

发布于 2018-02-01 14:28:44

对于不同的意图,使用单个Lambda函数是没有问题的。您可以只调用所有意图中的单个lambda函数,检查该lambda中的意图,并在同一lambda中调用相关的函数/方法。

正如你所说的,意图是非常相似的,所以你可能也可以使用通用函数来为这些意图做类似的事情。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48555866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档