使用 Rasa 构建支持中文的聊天机器人
本指南将向您介绍如何使用 Rasa 构建支持中文的聊天机器人,并将其应用于“Big Camera”这样的 IT 小工具商店,以支持相机销售管理。
第 1 步:安装 Rasa
确保已安装 Python,然后使用 pip 安装 Rasa:
pip install rasa
第 2 步:初始化新 Rasa 项目
创建一个新的 Rasa 项目:
rasa init --no-prompt
此命令将生成一个默认的项目结构。
第 3 步:添加中文支持
修改 config.yml
文件以支持中文。使用能够有效处理中文的分词器和流水线。例如:
language: zh
pipeline:
- name: "JiebaTokenizer"
- name: "RegexFeaturizer"
- name: "LexicalSyntacticFeaturizer"
- name: "CountVectorsFeaturizer"
analyzer: "word"
- name: "CountVectorsFeaturizer"
analyzer: "char_wb"
min_ngram: 1
max_ngram: 4
- name: "DIETClassifier"
epochs: 100
- name: "EntitySynonymMapper"
- name: "ResponseSelector"
epochs: 100
- name: "FallbackClassifier"
threshold: 0.3
第 4 步:定义意图和响应
在 domain.yml
文件中定义与相机销售相关的意图和响应。例如:
intents:
- greet
- ask_discount
- ask_camera_features
- check_stock
- thank_you
responses:
utter_greet:
- text: 您好!欢迎来到 Big Camera 的聊天机器人!
utter_ask_discount:
- text: 这款相机目前有特价!您对哪一款感兴趣呢?
utter_ask_camera_features:
- text: 请告诉我们您需要的相机功能:高清?长焦?我们将为您推荐合适的型号!
utter_check_stock:
- text: 请告诉我们您需要的型号,我们将为您查询库存。
utter_thank_you:
- text: 谢谢!如果您有其他问题,请随时联系。
第 5 步:创建训练数据
在 data/nlu.yml
文件中添加中文训练示例:
version: "3.0"
nlu:
- intent: greet
examples: |
- 你好
- 您好
- intent: ask_discount
examples: |
- 有什么优惠吗?
- 这款相机有折扣吗?
- intent: ask_camera_features
examples: |
- 这款相机有什么特点?
- 我想要一款高清的相机
- intent: check_stock
examples: |
- 这款有货吗?
- 请帮我查一下库存
- intent: thank_you
examples: |
- 谢谢
- 非常感谢
第 6 步:创建自定义动作
在 actions/actions.py
文件中定义一个查询库存的自定义动作:
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
class ActionCheckStock(Action):
def name(self) -> Text:
return "action_check_stock"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
# 示例库存数据
stock_data = {
"camera_x": "有库存",
"camera_y": "无库存",
}
model = tracker.get_slot("camera_model")
if model in stock_data:
stock_status = stock_data[model]
response = f"型号 {model} 的库存状态:{stock_status}"
else:
response = "未找到您指定的型号。"
dispatcher.utter_message(text=response)
return []
更新 domain.yml
文件以包括自定义动作和相机型号的槽位:
actions:
- action_check_stock
slots:
camera_model:
type: text
更新 utter_check_stock
响应,以在未提供型号时提示用户:
responses:
utter_check_stock:
- text: 请告诉我们您需要查询的相机型号。
第 7 步:更新故事
在 data/stories.yml
中添加查询库存的故事:
version: "3.0"
stories:
- story: Check Stock
steps:
- intent: check_stock
- action: utter_check_stock
- slot_was_set:
- camera_model: "camera_x"
- action: action_check_stock
第 8 步:测试聊天机器人
使用以下命令运行聊天机器人:
rasa train
rasa shell
通过输入中文示例测试聊天机器人的功能。
第 9 步:部署聊天机器人
考虑使用 Rasa X 或集成到 Web 界面进行部署。确保用户可以通过网站或社交媒体访问聊天机器人。
第 10 步:持续改进
分析聊天机器人的性能,优化训练数据,并改进模型以更好地处理用户的多样化查询。
Rasa 工作流程
以下是 Rasa 工作流程的 MermaidJS 图示:
graph TD
User[用户输入] -->|发送消息| NLU[NLU 流水线]
NLU -->|识别意图和实体| Core[Rasa Core]
Core -->|根据策略执行| Action[动作服务器]
Action -->|执行自定义动作或响应| Bot[机器人响应]
Bot -->|回复用户| User
subgraph Rasa 系统
NLU
Core
Action
end
通过这些步骤,您可以创建一个支持中文的 Rasa 聊天机器人,用于管理和推广 Big Camera 或其他 IT 小工具商店的相机销售。
Related Posts
- Building an OCPP 1.6 Central System with Flask async, WebSockets, and MongoDB
- How AI Supercharges Accounting and Inventory in Odoo (with Dev Insights)
- Building a Fullstack E-commerce System with JavaScript
- Building Agentic AI with Python, Langchain, and Ollama for eCommerce & Factory Automation
- Diagnosing the Root Cause of P0420 with Python, OBD-II, and Live Sensor Data
- How to Apply The Mom Test to Validate Your Startup Idea the Right Way
- When to Choose Rasa vs Langchain for Building Chatbots
- Introducing OCR Document Manager: Extract Text from Documents with Ease
- Testing an AI Tool That Finds Winning Products Before They Trend — Interested?
- Your Website Is Losing Leads After Hours — Here’s the Fix
- How Agentic AI is Revolutionizing Smart Farming — And Why Your Farm Needs It Now
- How to Apply RAG Chatbot with LangChain + Ollama
- Automating EXFO Instruments with SCPI: A Practical Guide
- Design Patterns That Help Tame Legacy Code (With Python Examples)
- How to Safely Add New Features to Legacy Code — A Developer’s Guide
- Modernizing Legacy Software — Without Breaking Everything
- How OpenSearch Works — Architecture, Internals & Real-Time Search Explained
- Choosing the Right Strategy for Basic vs Premium Features in Django
- Transform Your Custom Furniture Business with a Modern eCommerce Platform
- Introducing simpliPOS: The Smart POS Built on ERPNext
Our Products
Related Posts
- Building an OCPP 1.6 Central System with Flask async, WebSockets, and MongoDB
- How AI Supercharges Accounting and Inventory in Odoo (with Dev Insights)
- Building a Fullstack E-commerce System with JavaScript
- Building Agentic AI with Python, Langchain, and Ollama for eCommerce & Factory Automation
- Diagnosing the Root Cause of P0420 with Python, OBD-II, and Live Sensor Data
- How to Apply The Mom Test to Validate Your Startup Idea the Right Way
- When to Choose Rasa vs Langchain for Building Chatbots
- Introducing OCR Document Manager: Extract Text from Documents with Ease
- Testing an AI Tool That Finds Winning Products Before They Trend — Interested?
- Your Website Is Losing Leads After Hours — Here’s the Fix
- How Agentic AI is Revolutionizing Smart Farming — And Why Your Farm Needs It Now
- How to Apply RAG Chatbot with LangChain + Ollama
- Automating EXFO Instruments with SCPI: A Practical Guide
- Design Patterns That Help Tame Legacy Code (With Python Examples)
- How to Safely Add New Features to Legacy Code — A Developer’s Guide
- Modernizing Legacy Software — Without Breaking Everything
- How OpenSearch Works — Architecture, Internals & Real-Time Search Explained
- Choosing the Right Strategy for Basic vs Premium Features in Django
- Transform Your Custom Furniture Business with a Modern eCommerce Platform
- Introducing simpliPOS: The Smart POS Built on ERPNext