python rasa聊天机器人教程五:经典事例查询天气
从用户输入的消息中提取城市信息然后查询天气信息
rasa版本3.6.4
1.培训数据
在 data/nlu.yml
文件中,添加意图和实体的例子:
定义用户的意图和提取实体
1 2 3 4 5 6 | nlu: - intent: ask_weather examples: | - [北京](location)[明天](weather_time)的天气怎么样? - [上海](location)[下周一](weather_time)会下雨吗? - 告诉我[深圳](location)[今天](weather_time)的天气。 |
2.定义 rule
根据用户的意图和槽,定义对话流程。
在 data/rule.yml
文件中,定义一个简单的 rule:
1 2 3 4 | - rule: query weather data steps: - intent: ask_weather - action: action_query_weather |
3.定义 actions:
根据用户的意图和槽,执行特定的动作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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
文件中定义槽、实体、意图和响应。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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:
使用以下命令培训模型:
1 | rasa train |
使用以下命令启动 action server:
1 | rasa run actions |
6.启动服务
启动 Rasa 服务:
1 | rasa shell |
7.测试
1 2 3 4 5 6 7 8 9 10 11 12 | 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 模式下,并观察其如何从句子中提取实体。这可以通过以下命令完成:
1 | rasa shell nlu |
在该模式下,输入句子,并注意Rasa如何提取实体。这将为您提供关于哪些句子或词组可能导致问题的直观了解。
8.2 Interactive Learning
通过 Rasa 的交互式学习模式进行对话,它允许您在对话中更正任何实体或意图识别的错误。这不仅可以帮助您识别问题的根源,而且还可以创建更多的训练数据来改善模型的性能。可以使用以下命令启动交互式学习:
1 | rasa interactive |
8.3 config.yml
针对中文文本,我们需要使用一个可以处理中文分词的组件。以下是一个针对中文配置的 config.yml
文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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版本兼容。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?