[echo]Amazon Alexa 開發快速入門

in #echo7 years ago (edited)

甚麼是 Alexa

Alexa SDK 是搭配 Amazon Echo 的語音開發 SDK. 可以讓開發者開發 Echo 的 Skill. Skill 比較難理解。我是這樣看的 - Echo 是身體, Alexa 是靈魂。 當你有一個 Alexa 時,她已經上過 Amazon 智能小學了。 所以,會一些簡單的自然語言會話。 也會做一些基本的工作,比如播放你 iphone 的音樂,或是幫你播放 amazon 音樂庫的音樂。 但是 alexa 只要上過某些教材就可以做更多的事,比如告訴你某家旅行社的旅遊資訊, 操做IOT的裝置。 Alexa SDK 就是用來設計教導 Alexa 新技能的教材工具. 開發者是利用alexa sdk 開發新技能給 Alexa 學習。

Echo 的雲架構

當使用者語音被 echo 接收後,會傳送到 Amazon 的 alexa service cloud 上,alexa service cloud 接著會將訊息傳送到 AWS Lambda 或是你指定的HTTP Endpoint. 接著, 由 Lambda 與你自己的 cloud 或是你想要互動的 cloud 做溝通。 Lambda 完成工作後再將想要 echo 回復使用者的訊息傳送到 alexa service與, 最後由 echo 說出語音回復使用者。 因此,基本的組成是 Echo, 連上 alexa cloud , 然後傳語音內容訊息到 aws lambda, 由 lambda 來處你要做的事情。 回復也一樣, lambda 到 alexa cloud 到 echo device.

Alexa Skill 的開發流程

  • 首先要先有帳號
  • 在 Amazon Developer Console 裡設定 Alexa 開發語音部分
  • 在 AWS Lambda 裡開發要做的事情的與回覆的語音內容
  • 上架準備與審核

30 分鐘的 Hello World Project

很簡單的一個 skill , 使用者說 "Alexa, ask greeter to say hello to mary", 然後 Alexa 會回 "Hello, mary",Mary 可以是 John, Sarah 等等使用者指定名子。

Amazon Alexa 語音設定

  • Login 到 amazon developer site

  • 選擇 Alexa

  • 到下一頁後,右上角點選新增一個 skill

  • 進入下列畫面就開始了 Alexa skill 的設定,左邊Menu 是產生 skill 所需要的步驟,右邊是相對應步驟需要輸入的資訊。

Step1 : skill Information

  • Skill Type: 這邊有4種,一般通用是第一種customer skill。 第二種是要開發 smart home device 的。第三種是要開發新聞簡報類的. 第 4 種是開發 Video Skill 是給最新的 Echo show 用的。
  • language : 語言目前有英文德文
  • Name : 這個 skill 要叫什麼名字
  • Invocation Name: 使用這個 skill 時的關鍵字 , 比如這邊見就 Alexa, ask "greeter" to say hello. 當 Alexa, 聽到 greeter 時就會知道要使用這個 skill
  • Global Fields: 是特殊種類的 skill 設定,目前先不選
  • Save and Next

Step2 : Interaction Model

Interaction model 可以說是 Voice Interface 設計的地方, Intent Schema 讓你設計語意, Custom Slot Types 讓你設計自己定義的字組,Sample Utterances 是告訴 Alexa, 什樣的句型代表什麼語意。

  • Intent Schema :
    語意的設計在這邊,使用 json format 的描述來告訴 alexa 會有多少種的語意以及每個語意帶著的參數。
 {
  "intents": [
      {
      "intent": "HelloIntent",
      "slots": [
        {
          "name": "ANAME",
          "type":"AMAZON.US_FIRST_NAME"
        }
      ]
    },
    {
      "intent": "AMAZON.StopIntent"
    },
    {
      "intent": "AMAZON.HelpIntent"
    }
  ]
}

  • JSON format: 這邊可以看到語意是以JSON 格式來定義。 "intents"(語意) 是 array 型態,就是可以有很多的 intent 物件。可以看到 intent 物件有幾個組成:

    • "Intent" : 語意的名子.
    • "Slots" : slots 也是個 array 也就是可以有很多 slot 物件。 slot 物件由 name 跟 type 組成。
  • Slot : 這邊可以理解成程式裡的變數。 比如我說 alexa, ask greeter say hello to Rachael. alexa 會回我 hello Rachael. 若我要把 Rachael 改成任何的名子的話,那麼只要把Rachael 的地方變成一個變數(這邊叫 slot) 就好. 這個變數要有個代號, 這就是 slots 裡的 name attribute. 這邊,變數的值要有型別,這就是type attribute. 比如 Rachel, Richard, Marry or John 都是美國的 first name, 所以給AMAZON.FIRST_NAME 這樣的型別。 amazon 內建型別可以在slot type reference找到

  • HelpIntent 與 stopIntent: 這是 alexa skill 強制要做的語意. helpIntent 功用在於說明這個 skill 怎麼使用, amazon 的要求是,使用者只要聽 help 就可以懂怎麼用你的 skill. stopIntent 是要告訴alexa 當使用者想要終止對話時,該怎麼處理

  • Custom Slot Type: 如果 AMAZON 官方 slot type 沒有你要的, 那麼就自己建立一個. 這邊不會用到, 需要時可參考 Custom Interaction Model Reference裡的 custom slot type

  • Sample Utterances : 就是把可能會用到的句型列下來, 前面是 Intent 的名子, 後面是句子會對應到的 Intent。 {}用來表示 slot, {ANAME} 裡的 ANAME 是 slot 的名子。

    • 以下兩個例句是說, say hello to mary 或是 greeting mary 都是同一個意思就是 HelloIntent.
    • HelpIntent 與 StopIntent 因為是內建得所以不用在這列舉.
    HelloIntent say hello to {ANAME}
    HelloIntent greeting {ANAME}
  • Save N Next

Step 3 Configuration :

有了語意以後,要設定相對該語意的動作。 這邊就不屬於 Alexa Cloud 的範圍了,是屬於開發者自己要負責的。這邊提供兩個方式,1. AWS lambda Service 2. 提供一個 Endopint 給 alexa cloud.

  • EndPoint 設定: 這邊使用 AWS Lambda service. Http 可能很多東西要自己寫與設. 定. 這邊需要離開 Amazon Developer Consol 到 AWS 去設定 Lambda 服務, 取得 lambda function ARN 後回來填寫。

  • 進入 AWS account, 進入 Lambda Service

    • 選擇要使用的lambda區域, 可以自由選擇,不過並不是所有區域支援 alexa trigger lambda, 這邊我選 Virgina.

    • 選 Blank Project

    • Trigger 選 Alexa Skill Kit

    • Next

    • 設定名子與lambda要用的語言. 名子是lambda 程式的名子. 隨喜取一個. 語言是寫lambda的語言,此處我選 Node.js

    • 設定 role

    • Next and Create Lambda

    • 記下右上角的 Lambda ARN, 暫時完成設定,等下再回來寫 code

    • 回到 Amazon Alexa 的 Configuration 頁, 把 lambda 的 arn 填入 Endpoint

    • Account Linking: 第三方授權的設定. 目前不需要。 這個要講很久,有機會再另篇.

    • Permissions : 目前不需要

    • Save N Next

在 Lambda 開發互動內容

寫 code的部分是在 Lambda 裡. code 裡面除了架構外, 主要是在處理先前定義的 Intents.

  • HelloIntent: 在 hello intent 下面的 message 裡修改字
  • HelpIntent : 這個skill 的說明, 在 hello intent 下面的 message 裡修改字
  • StopIntent : 終止命令, 在 hello intent 下面的 message 裡修改字
  • 如何 Retrieve slot :
var name = event.request.intent.slots.ANAME.value;

Step 4 Test

  • Service Simulator: step 1 and step 2

  • 整體測試: 使用 echo or echosim.io

Step 5 Publishing Information: 上架前的準備

Step 6 Privacy & Compliance : 法律文件

如何取得 skill certificate 和上架

簡單的 skill 把主功能, Launch, Help , stop/cancel Intents Implement 好, 把Publication 裡的文件寫好.大概就可以過了. 但是複雜的 skill, 要把 functional test 非常仔細測過一遍, 因為有一些fuctional test是沒想到的, 搞不好還要修改流程, 比如 security 型的 skill, 需要 pincode 的互動. 仔細的看才有機會通過歐!

Reference

If you think this is helpful ...

  • Let me know and give me suggestions
  • 如果你覺得有幫助,請加入 steemit.com 然後幫我按個 votes (讚) 吧! 加入 steemit.com 發文有虛擬幣獎賞阿 ~ 快來加入!

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63490.29
ETH 2598.32
USDT 1.00
SBD 2.78