如何使用 Flask-Ask、Amazon Alexa 和 Python 3 后端创建对话技巧

How to create conversational skills using Flask-Ask, Amazon Alexa and Python 3 backend

我正在攻读软件开发学士学位的期末项目。该项目要求学生选择一个与软件开发相关的主题,并根据他们的发现写一篇论文。

问题定义

以下是我需要帮助的内容:

我的主题是使用 Python 为 Amazon Alexa 开发技能 3. 当前的重点是创建 自定义技能

我的自定义技能会计算物体的体积。

就此问题而言,对象是长方体、立方体、圆柱体或球体。我无法获得盒子的体积。我需要帮助将值从用户获取到我的 Python 3 后端。

我希望对话是这样的:

Alexa: "Welcome to Volume Calculator. Would you like to calculate the volume of an object?"

用户:"Yes"

Alexa: "Which object would you like me to calculate the volume of?"

用户:"A Box"

Alexa: "What is the length of the box?"

用户:“5”

Alexa: "What is the width of the box?"

用户:“5”

Alexa: "What is the height of the box?"

用户:“5”

Alexa "The volume of the box is one-hundred and twenty-five cubic meters."

Alexa 的当前响应是 "There was a problem with the requested skill's response."

Python 3 后端

@ask.intent("BoxLengthIntent", convert={"length": int})
def box_length():
box_length_prompt = "What is the length of the box?"
return question(box_length_prompt)


@ask.intent("BoxWidthIntent", convert={"width": int})
def box_width():
box_length_prompt = "What is the width of the box?"
return question(box_length_prompt)


@ask.intent("BoxHeightIntent", convert={"height": int})
def box_height():
box_height_prompt = "What is the height of the box?"
return question(box_height_prompt)


@ask.intent("BoxVolumeIntent", convert={"length": int, "width": int, 
"height": int})
def calculate_box_volume():
length = box_length()
# session.attributes["length"] = length

width = box_width()
# session.attributes["width"] = width

height = box_height()
# session.attributes["height"] = height

# Question does not define mul. Program crashes here.
volume_of_box = length * width * height 

msg = "The volume of the box is {} cubic meters"\
    .format(volume_of_box)
return statement(msg).simple_card(title="VolumeCalculator", content=msg)

意图架构

{
  "intents": [
    {
      "intent": "YesIntent"
    },
    {
      "intent": "NoIntent"
    },
    {
      "intent": "CubeIntent",
      "slots": [
        {
          "name": "length",
          "type": "AMAZON.NUMBER"
        }
      ]
    },
    {
      "intent": "CubeVolumeIntent",
      "slots": [
        {
          "name": "length",
          "type": "AMAZON.NUMBER"
        }
      ]
    },
    {
      "intent": "BoxVolumeIntent",
      "slots": [
        {
          "name": "length",
          "type": "AMAZON.NUMBER"
        },
        {
          "name": "width",
          "type": "AMAZON.NUMBER"
        },
        {
          "name": "height",
          "type": "AMAZON.NUMBER"
        }
      ]
    }
  ]
}

示例话语

BoxVolumeIntent 框

BoxVolumeIntent 给我一个盒子的体积

BoxVolumeIntent 给我一个长 {length} 高的盒子的体积

{高度}和宽度{宽度}

BoxVolumeIntent 告诉我盒子的体积

BoxVolumeIntent 盒子的体积是多少

我找到了问题的解决方案。

步骤

  1. 首先,我需要使用 Amazon Skill Builder 创建一个对话模型。在 Skill Builder 页面上有很多关于如何使用它的教程。
  2. 然后我需要弄清楚 flask -ask 是如何处理多轮对话的。解决方案是我在浏览存储库问题时发现的以下代码块。

    dialog_state = get_dialog_state()
        if dialog_state != "COMPLETED":
            return delegate(speech=None)
    

    其中标签dialog_state设置为函数get_dialog_state()的值。如果我们进入 if 语句,我们 return 委托对象委托回 Alexa 语音服务,需要另一个提示来实现意图。

get_dialog_state() 函数如下所示

def get_dialog_state:
    return session['dialogState']

将第 2 步中的代码块放在您想要进行多轮对话的任何位置。在满足所有提示后,处理您将对该数据执行的操作。还包括所需的任何错误检查。

除了@bradleyGamiMarques 的回答中概述的步骤外,我发现我需要包括 delegate class 以及其他 Flask-Ask classes使用.

from flask_ask import Ask, statement, question, session, context, delegate

我在这里遇到的挂断是,在这种情况下,我的 IDE (PyCharm) 将 delegate 标记为 "unresolved reference." 我忽略了这一点,不过,在手动确认后它的存在和一切正常。

除了从 flask-ask 导入委托之外还有一个变化

而不是

if dialog_state != "COMPLETED":
        return delegate(speech=None)

使用

if dialog_state != "COMPLETED":
        return delegate()