python rasa聊天机器人教程五:经典事例查询天气

从用户输入的消息中提取城市信息然后查询天气信息

rasa版本3.6.4

1.培训数据

data/nlu.yml 文件中,添加意图和实体的例子:

定义用户的意图和提取实体

nlu:
- intent: ask_weather
  examples: |
    - [北京](location)[明天](weather_time)的天气怎么样?
    - [上海](location)[下周一](weather_time)会下雨吗?
    - 告诉我[深圳](location)[今天](weather_time)的天气。

 2.定义 rule

根据用户的意图和槽,定义对话流程。

data/rule.yml 文件中,定义一个简单的 rule:

- rule: query weather data
  steps:
  - intent: ask_weather
  - action: action_query_weather

 3.定义 actions:

根据用户的意图和槽,执行特定的动作。

from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.types import DomainDict

# 这里仅为模拟查询,实际上需要调用第三方API来获取真实的天气数据
def mock_query_weather(location: str, time: str) -> str:
    if location == "北京" and time == "明天":
        return "晴朗"
    elif location == "上海" and time == "下周一":
        return "多云"
    else:
        return "未知"

class ActionQueryWeather(Action):
    def name(self) -> str:
        return "action_query_weather"

    def run(
            self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: DomainDict
    ) -> list:
        location = tracker.get_slot("location")
        time = tracker.get_slot("weather_time")

        weather_condition = mock_query_weather(location, time)

        dispatcher.utter_message(response="utter_weather_data", location=location, time=time,
                                 weather_condition=weather_condition)

        return []

 4.定义 slots 和 responses:

domain.yml 文件中定义槽、实体、意图和响应。

intents:
  - ask_weather

entities:
  - location
  - weather_time

slots:
  location:
    type: text
    influence_conversation: false
    mappings:
      - type: from_entity
        entity: location
  weather_time:
    type: text
    influence_conversation: false
    mappings:
      - type: from_entity
        entity: weather_time

actions:
  - action_query_weather

responses:
  utter_weather_data:
    - text: "{location}在{weather_time}的天气是{weather_condition}。"

 5.启动 actions server:

使用以下命令培训模型:

rasa train

使用以下命令启动 action server:

rasa run actions

 6.启动服务

启动 Rasa 服务:

rasa shell

 7.测试

Your input ->  [上海](location)[下周一](weather_time)会下雨吗?
上海在下周一的天气是多云。
Your input ->  [上海](location)[今天](weather_time)会下雨吗?
上海在下周一的天气是多云。
Your input ->  告诉我[深圳](location)[今天](weather_time)的天气。
深圳在下周一的天气是未知。
Your input ->  告诉我[深圳](location)[今天](weather_time)的天气。
深圳在下周一的天气是未知。
Your input ->  [北京](location)[明天](weather_time)的天气怎么样?
北京在明天的天气是晴朗。
Your input ->  告诉我[深圳](location)[今天](weather_time)的天气。
深圳在明天的天气是未知。

 现在有个问题就是时间weather_time的提取有错误。

8.其他

8.1 查看 Rasa NLU 提取的实体

运行 Rasa 在 shell 模式下,并观察其如何从句子中提取实体。这可以通过以下命令完成:

rasa shell nlu

 在该模式下,输入句子,并注意Rasa如何提取实体。这将为您提供关于哪些句子或词组可能导致问题的直观了解。

8.2 Interactive Learning

通过 Rasa 的交互式学习模式进行对话,它允许您在对话中更正任何实体或意图识别的错误。这不仅可以帮助您识别问题的根源,而且还可以创建更多的训练数据来改善模型的性能。可以使用以下命令启动交互式学习:

rasa interactive

 8.3 config.yml

针对中文文本,我们需要使用一个可以处理中文分词的组件。以下是一个针对中文配置的 config.yml 文件:

version: "3.0"

language: zh

pipeline:
- name: "JiebaTokenizer"
- name: "LexicalSyntacticFeaturizer"
- name: "CountVectorsFeaturizer"
- name: "CountVectorsFeaturizer"
  analyzer: "char_wb"
  min_ngram: 1
  max_ngram: 4
- name: "DIETClassifier"
  epochs: 100
- name: "EntitySynonymMapper"
- name: "ResponseSelector"
  epochs: 100

policies:
- name: "MemoizationPolicy"
- name: "TEDPolicy"
  max_history: 5
  epochs: 100
- name: "RulePolicy"

注意事项:

  • 使用了 JiebaTokenizer 进行中文分词。在使用之前,你可能需要安装 jieba:pip install jieba

  • 为了捕获中文字符的模式,CountVectorsFeaturizer 有两次定义:一次是基于词,另一次是基于字符的 n-gram。

  • 策略(policies)定义了机器人的对话管理行为。这里的配置使用了 MemoizationPolicy(记忆策略)、TEDPolicy(Transformer Embedding Dialogue策略)和 RulePolicy(规则策略)。

确保你已经安装了Rasa和其他必要的依赖,并确保其与你的Rasa版本兼容。

 

posted @ 2023-08-17 22:19  繁华博客  阅读(376)  评论(0编辑  收藏  举报