小皮博客 | Xiaopi's Blog

110-【RASA系列教程-4】-RASA做天气问答机器人

天气问答是NLU及DM领域的经典案例。

依赖准备

准备好对应的虚拟环境(略)
将对应的依赖下载下来

配置文件

vim config.yml

language: zh
pipeline: 
- name: "nlp_mitie"
  model: "data/total_word_feature_extractor_zh.dat"
- name: "tokenizer_jieba"
- name: "ner_mitie"
- name: "ner_synonyms"
- name: "intent_featurizer_mitie"
- name: "intent_classifier_sklearn"

数据准备

nlu.json

{
  "rasa_nlu_data": {
    "common_examples": [
      {
        "intent": "weather_address_date-time",
        "entities": [
          {
            "start": 2,
            "end": 4,
            "value": "上海",
            "entity": "address"
          },
          {
            "start": 4,
            "end": 6,
            "value": "明天",
            "entity": "date-time"
          }
        ],
        "text": "我要上海明天的天气"
      },
   ...

stories.md

## simple path with greet
* greet
  - utter_greet
* weather_address_date-time{"address": "上海", "date-time": "明天"}
  - utter_working_on_it
  - action_report_weather
  - utter_report_weather

## simple path
* weather_address_date-time{"address": "上海", "date-time": "明天"}
  - utter_working_on_it
  - action_report_weather
  - utter_report_weather
 ...

domain.yml

intents:
  - greet
  - goodbye
  - weather
  - weather_address_date-time
  - weather_address
  - weather_date-time

slots:
  address:
    type: text
  date-time:
    type: text
  matches:
    type: unfeaturized

entities:
  - address
  - date-time

actions:
  - utter_greet
  - utter_ask_address
  - utter_ask_date-time
  - utter_working_on_it
  - action_report_weather
  - utter_report_weather
  - utter_goodbye

templates:
  utter_greet:
    - text: "你好,请说出需要提供天气预测服务的地点和时间"

  utter_working_on_it:
    - text: "正在查询中,请稍后 ..."

  utter_goodbye:
    - text: "再见!"

  utter_ask_address:
    - text: "哪里呢?"

  utter_ask_date-time:
    - text: "什么时候?"

  utter_report_weather:
    - text: "{matches}"

  utter_default:
    - text: "系统不明白您说的话"

配置endpoints.yml

action_endpoint:
  url: "http://localhost:5055/webhook"

运行

生成模型

rasa train

运行action服务

git clone https://github.com/howl-anderson/WeatherBot_Action
pip3 install -r requirements.txt
python3 -m rasa_core_sdk.endpoint –actions bot


最终效果

使用restapi访问

import json
import secrets
import requests


def post(url, data=None):
    data = json.dumps(data, ensure_ascii=False)
    data = data.encode(encoding="utf-8")
    r = requests.post(url=url, data=data)
    r = json.loads(r.text)
    return r


sender = secrets.token_urlsafe(16)
url = "http://localhost:5005/webhooks/rest/webhook"
while True:
    message = input("Your input ->  ")
    data = {
        "sender": sender,
        "message": message
    }
    result = post(url, data)
    for i in result:
        print(i)

版权声明

本文标题:110-【RASA系列教程-4】-RASA做天气问答机器人

文章作者:盛领

发布时间:2020年09月26日 - 22:37:58

原始链接:http://blog.xiaoyuyu.net/post/b053a315.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

如您有任何商业合作或者授权方面的协商,请给我留言:sunsetxiao@126.com

盛领 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!