Try   HackMD

AI LINE Bot練功坊-L16 Azure AI Language 交談語言理解機器人

Azure AI Language

Azure AI Language是一個雲端式服務,可提供自然語言處理 (NLP) 功能,用於了解和分析文字,包括的項目有:

  • 具名實體辨識 (NER)
  • 語言偵測
  • 情感分析與意見挖掘
  • 摘要
  • 關鍵字擷取
  • 交談語言理解
  • 問題解答

Reference:https://learn.microsoft.com/zh-tw/azure/ai-services/language-service/overview

Conversational Language Understanding (CLU) 交談語言理解

當我們輸入一句話給機器人並且想要它知道我們的用意是甚麼就可以透過CLU,CLU有兩個重要的名詞分別是 intent 和 entity

intent意圖

就是判斷一句話的用意是甚麼,舉裡來說:

今天下午我想要跟張三去看鬼滅之刃

這句話的意圖就會是【看電影】

entity實體

就是句子中重要的資訊是甚麼,以剛剛的句子來說可能會有幾個重要資訊

  • 時間:今天下午
  • 跟誰去:張三
  • 電影名稱:鬼滅之刃

當然你想要的重要資訊不一定是這些,你有可能想要把時間再細分成"日期"和"時間",也就是把 "今天下午" 拆成 "今天" 和 "下午"

訓練語句

定義好 intent 和 entity 後,需要提供不同類型的句子作為訓練資料,讓模型學習如何辨識和理解不同的意圖和實體。舉例來說,針對剛才提到的「看電影」的意圖,我們可以再準備以下訓練語句:

明天晚上我要和李四去看名偵探柯南

intent: 看電影
entity:
時間:明天晚上
跟誰去:李四
電影名稱:名偵探柯南


7/5小華一起去看腦筋急轉彎

intent: 看電影
entity:
時間:7/5
跟誰去:小華
電影名稱:腦筋急轉彎


我跟小明七月十五要一起看排球少年

intent: 看電影
entity:
時間:七月十五
跟誰去:小明
電影名稱:排球少年


Azure的entity有四種類型

  • Learned

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • List

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • Prebuild

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • Regex

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

Reference:https://learn.microsoft.com/en-us/azure/ai-services/luis/concepts/entities
Reference:https://learn.microsoft.com/zh-tw/azure/ai-services/language-service/conversational-language-understanding/overview

安裝開發環境

https://hackmd.io/@ntuebigdata/setup-linebot-development-environment#建立Python虛擬環境

註冊LINE官方帳號

https://hackmd.io/@ntuebigdata/create-a-line-official-account#1-建立Line-Developers帳號

註冊教育版的Azure帳號

https://hackmd.io/@ntuebigdata/azure-basic-introduction#註冊教育版的Azure帳號


建立語言服務

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

要注意不是所有地區都有F0以及Conversation Language Understanding可以使用

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
下方連結的區域要Authoring有打勾的才有支援CLU

Reference:https://learn.microsoft.com/en-us/azure/ai-services/language-service/concepts/regional-support#conversational-language-understanding-and-orchestration-workflow

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

這裡的名稱在後面建立LineBot會用到
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

這裡的名稱也要記住,後面建立LineBot會用到
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

撰寫程式

程式準備

新增一個專案資料夾,並在專案資料夾內新增.envrequirements.txtapp.py檔案,並貼上以下內容

image

requirements.txt

line-bot-sdk
flask
azure-ai-translation-text

app.py

from flask import Flask, request, abort
from linebot.v3 import (
    WebhookHandler
)
from linebot.v3.exceptions import (
    InvalidSignatureError
)
from linebot.v3.webhooks import (
    MessageEvent,
    LocationMessageContent
)
from linebot.v3.messaging import (
    Configuration,
    ApiClient,
    MessagingApi,
    ReplyMessageRequest,
    TextMessage
)
import os
#Azure CLU
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.conversations import ConversationAnalysisClient

app = Flask(__name__)

CHANNEL_ACCESS_TOKEN = os.getenv("CHANNEL_ACCESS_TOKEN")
CHANNEL_SECRET = os.getenv("CHANNEL_SECRET")
clu_endpoint = os.getenv("ENDPOINT")
clu_key = os.getenv("API_KEY")
project_name = os.getenv("PROJECT_NAME")
deployment_name = os.getenv("DEPLOYMENT_NAME")    

line_handler = WebhookHandler(CHANNEL_SECRET)
configuration = Configuration(access_token=CHANNEL_ACCESS_TOKEN)

@app.route("/callback", methods=['POST'])
def callback():
    # get X-Line-Signature header value
    signature = request.headers['X-Line-Signature']
    # get request body as text
    body = request.get_data(as_text=True)
    app.logger.info("Request body: " + body)

    # parse webhook body
    try:
        line_handler.handle(body, signature)
    except InvalidSignatureError:
        abort(400)
    return 'OK'


@line_handler.add(event=MessageEvent, message=LocationMessageContent)
def handle_location_message(event):
    address = event.message.address
    result = analyze_address(address)
    entities = result['prediction']['entities']

    messages = []
    if len(entities) == 2 and entities[0]['category'] == 'city' and entities[1]['category'] == 'town':
        city = result['prediction']['entities'][0]['text']
        town = result['prediction']['entities'][1]['text']
        messages.append(TextMessage(text=f"你傳送的位址資訊的城市:{city}"))
        messages.append(TextMessage(text=f"你傳送的位址資訊的鄉鎮:{town}"))
    else:
        messages.append(TextMessage(text="無法辨識你傳送的位址資訊"))

    with ApiClient(configuration) as api_client:
        line_bot_api = MessagingApi(api_client)
        line_bot_api.reply_message_with_http_info(
            ReplyMessageRequest(
                reply_token=event.reply_token,
                messages=messages
            )
        )

def analyze_address(address):
    credential = AzureKeyCredential(clu_key)
    client = ConversationAnalysisClient(clu_endpoint, credential)
    with client:
        result = client.analyze_conversation(
            task={
                "kind": "Conversation",
                "analysisInput": {
                    "conversationItem": {
                        "participantId": "1",
                        "id": "1",
                        "modality": "text",
                        "language": "zh-hant",
                        "text": address
                    },
                    "isLoggingEnabled": False
                },
                "parameters": {
                    "projectName": project_name,
                    "deploymentName": deployment_name,
                    "verbose": True
                }
            }
        )
    return result['result']

if __name__ == "__main__":
    app.run()

.env

CHANNEL_ACCESS_TOKEN = "XXX" CHANNEL_SECRET = "XXX" API_KEY = "XXX" ENDPOINT = "XXX" PROJECT_NAME = "XXX" DEPLOYMENT_NAME = "XXX"

貼上之後記得修改幾個地方:

  • Line Developers的Channel Access Token
  • Line Developers的Channel Secret
  • 剛剛建立Azure資源的金鑰
  • 剛剛建立Azure資源的端點
  • 剛剛建立Azure語言的project的名稱
  • 剛剛建立Azure服務後部署的deplotment的名稱

測試效果

在聊天室點選定位功能,會回傳一個縣市的訊息以及一個鄉鎮的訊息

image
image

image

Reference:https://azure.microsoft.com/zh-tw/pricing/details/cognitive-services/language-service/

Youtube 課程影片

關於我們

image 國立臺北教育大學 教育大數據微學程

先修微課程

🤖 AI LineBot 練功坊系列課程
從入門到精通,學習如何開發並應用 LINE Bot,讓你輕鬆掌握最前沿的聊天機器人技術。

👨‍💻 Python 初學小教室
針對零基礎學員設計,循序漸進地教授 Python 基本語法及實作技巧,幫助你快速上手。

📊 統計學小教室
系統講解統計學理論及其應用,適合想要提升數據分析能力的學習者。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Facebook
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Instagram
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Threads
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
YouTube
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Line官方帳號

相關教材連結

◀◀◀ L15 Azure Translator 翻譯機器人 ◀◀◀
◀◀◀ L15 Azure Translator 翻譯機器人 ◀◀◀