iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 9
1
Microsoft Azure

利用Python開發一個以Azure服務為基底的Chat Bot系列 第 9

【Day09】Language Understanding (2/2)

大家好,今天要來跟大家一起操作 Language Understanding (LUIS),話不多說,我們直接開始吧!

用 Python 程式碼簡單實作一個範例

前置步驟

0.1 準備好一個 Azure Account

0.2 建立好 Python 環境

0.3 打開 terminal (Powershell or CMD),輸入以下指令

pip install requests

在 Azure Portal 上建立 LUIS

1.1 前往 Azure Portal,並搜尋 LUIS
https://ithelp.ithome.com.tw/upload/images/20200924/20129689NLkTR8wl5P.jpg

1.2 自己完成以下的設定

Create options: Authoring

Authoring location: West US
https://ithelp.ithome.com.tw/upload/images/20200924/20129689o5madu5SF2.jpg

在 LUIS 官網創建第一個模型

2.1 前往 LUIS 官網

2.2 確認已經登入 Azure Account

2.3 建立一個新的 LUIS APP
https://ithelp.ithome.com.tw/upload/images/20200924/20129689UE85aRTVMJ.jpg

2.4 設定名稱及語言
https://ithelp.ithome.com.tw/upload/images/20200924/20129689niLBajVRQA.jpg

2.5 建立兩個 Entity

品項

數量
https://ithelp.ithome.com.tw/upload/images/20200924/20129689nfZkii7MYK.jpg

2.6 建立一個 Intent

點餐
https://ithelp.ithome.com.tw/upload/images/20200924/20129689Zsdi7eWIGR.jpg

2.7 新增訓練句子

我想要外帶2杯咖啡

我想外帶一份燒餅

老闆,3杯豆漿謝謝

兩份蛋餅內用

我要2片吐司和一杯米漿

https://ithelp.ithome.com.tw/upload/images/20200924/20129689LlCPLRHSyB.jpg

2.8 新增完成後,手動標註 Entity 的位置
https://ithelp.ithome.com.tw/upload/images/20200924/2012968978ao6QqHS4.jpg

2.9 訓練 LUIS APP 模型
https://ithelp.ithome.com.tw/upload/images/20200924/20129689vsZi0AboQ3.jpg

2.10 Publish 這個 demo LUIS APP
https://ithelp.ithome.com.tw/upload/images/20200924/20129689xKDCadZlqr.jpg

2.11 複製 LUIS App ID
https://ithelp.ithome.com.tw/upload/images/20200924/20129689JoYVRFX29z.jpg

2.12 複製 Key + Endpoint URL
https://ithelp.ithome.com.tw/upload/images/20200924/201296895LNYxTfkrc.jpg

寫一個簡單的小程式

3.1 打開 Visual Studio Code (VS Code)

3.2 新增一個 Python 程式檔,命名為test-luis.py,並將以下程式碼複製貼上

import requests
import json
  
subscription_key = '<key>'     ## 貼上你的key

def predict_sentence_test(prediction_url, body):
    headers = {
        # Request headers
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': subscription_key ,
    }
    url = prediction_url
    response = requests.request("POST", url, data = body, headers=headers)
    response_text = json.loads(response.text)      
    
    # print(response_text)                                                           #print出所有json的資料    
    print("Intent:{}".format(response_text["topScoringIntent"]['intent']))           #單獨print出這個句子的intent
    for i in range(len(response_text["entities"])) :                                 
        print("Entity:{}".format(response_text["entities"][i]["entity"]))            #將所有讀到的entity列印出來

#測試 從訓練好的luis app問取得一句話的intent及entity等資料
prediction_url = '<endpoint url>/luis/v2.0/apps/<appid>'   # 貼上的你的endpoint url + luis app id
prediction_sentence = "我想要外帶2杯咖啡"
prediction_sentence_json = json.dumps(prediction_sentence)
predict_sentence_test(prediction_url, prediction_sentence_json)

3.3 打開terminal,確認位置為程式檔案的位置後,輸入以下指令

python test-luis.py

以上是我今天想要分享的內容/images/emoticon/emoticon29.gif


上一篇
【Day08】Language Understanding (1/2)
下一篇
【Day10】在 Cognitive Service 畫上一個逗號
系列文
利用Python開發一個以Azure服務為基底的Chat Bot30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言