Use process of Rasa chat robot framework

Detailed chat process (Rasa robot)

sketch:

Rasa is a framework that can be used to build a robot dialogue system. Building a robot dialogue system based on rasa framework can be used in various industrial voice intelligent service scenarios, such as telemedicine consultation, intelligent customer service, insurance product sales, financial collection services, manual intelligent assistant and other fields. Support the construction of dialogue system based on rules, slot filling and machine learning. The main modules include: NLU (intention recognition and entity extraction) and Core (reply based on model and rules). It provides a scaffold for building a dialogue system.

Common commands are:

    rasa init   # Create project

    rasa train   # Training NLU and Core models

    rasa run actions  # Start the action service (mainly the actions written by yourself, the default actions of the system, and do not need to be started separately)

    rasa shell  # Start the dialogue robot (the default action service will be started automatically)
    
    rasa x   # Start rasa x visualization service

1. New construction

First, execute the following command to create a new project:

rasa init

or

rasa init --no-prompt

rasa init command will create all the files required by rasa project and train a simple chat robot on the initialization sample data. If the command does not carry the – no prompt flag, you will encounter some questions about the project settings.

Use rasa init to create the mybot project. The directory results are as follows:

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-dgtikq2l-1645008096429) (/ users / liuzepei / library / Application Support / typera user images / image-20220216181439694. PNG)]

The following files will be created:

init.pyhelp python Find an empty file for your feature
actions.pyIn this file, you can customize the code to perform actions
config.yml '*'Rasa NLU and Rasa Core model profiles
credentials.ymlDetailed configuration of connecting to other services
data/nlu.yml '*'Rasa NLU training data
data/stories.yml '*'Story writing document
domain.yml '*'Intelligent assistant function definition domain file
endpoints.ymlDetailed configuration of connecting to channels like fb messenger
models/.tar.gInitialization model

The '*' marks are very important documents, which are explained in detail in this tutorial.

2. View and build NLU training data

The first part of Rasa intelligent assistant is the NLU model. NLU is the abbreviation of Natural Language Understanding, which means to convert various user information into structured data. To use rasa to complete this function, you need to provide training corpus, through which rasa can learn how to understand user information. And rasa can use these corpora to train the model.

In the new project directory of rasa init command, run the following command to view the training configuration file:

cat data/nlu.yml 


Sample data:

nlu.yml is used to train the training data of NLU model

version: "2.0"
nlu:
- intent: greet
  examples: |
    - Hello
    - Good morning
    - Good noon
    - hi
- intent: goodbye
  examples: |
    - bye
    - see you around
    - good night
- intent: affirm
  examples: |
    - yes
    - have
    - of course
    - Sounds good
- intent: deny
  examples: |
    - no
    - No
    - No,
    - No,
    - I don't like it
- intent: mood_great
  examples: |
    - Great
    - I feel good
    - very nice
- intent: mood_unhappy
  examples: |
    - unhappy
    - Feel depressed
    - unhappy
    - just so so
- intent: bot_challenge
  examples: |
    - Are you a robot?
    - Are you human?
    - Am I talking to a robot?
    - Am I talking to humans?
- intent: request_names
  examples: |
    - I want to tell you your name
    - Do you know my name?
- lookup: names
  examples: |
    - Zhu Bajie
    - Sun WuKong
    - Sha Wujing
    - Tang Sanzang

rules.yml rules, according to the user's intention, carry out specific actions (including query, slot filling, reply, etc.)

version: "2.0"
 
rules:
 
- rule: Say goodbye anytime the user says goodbye
  steps:
  - intent: goodbye
  - action: utter_goodbye
 
- rule: Say 'I am a bot' anytime the user challenges
  steps:
  - intent: bot_challenge
  - action: utter_iamabot
 
- rule: Activate form
  steps:
  - intent: request_names
  - action: name_form
  - active_loop: name_form
 
- rule: Submit form
  condition:
  - active_loop: name_form
  steps:
  - action: name_form
  - active_loop: null
  - slot_was_set:
    - requested_slot: null
  - action: utter_submit
  - action: utter_slots_values

3. Define model configuration

Define the components of Rasa NLU and Rasa Core required by your model through the configuration file. In this example, the NLU model will use supervised_embeddings pipe. You can use the page here Understand the different NLU pipes.

View the model configuration file through the following command:

cat config.yml  # That is, the configuration of NLU and Core models
cat domain.yml  # Domain configuration
credentials.yml # Certificate configuration, which is used to call the interface of voice channel
endpoints.yml # Endpoint configuration, such as the model, action and storage service to be used by the robot

language and pipeline mainly determine how to build Rasa NLU model. Policies mainly defines the policies used by the Rasa Core model policies.

The configuration files involved are:

config.yml is the configuration of NLU and Core models

# Configuration for Rasa NLU.
# https://rasa.com/docs/rasa/nlu/components/
language: zh
 
pipeline:
# # No configuration for the NLU pipeline was provided. The following default pipeline was used to train your model.
# # If you'd like to customize it, uncomment and adjust the pipeline.
# # See https://rasa.com/docs/rasa/tuning-your-model for more information.
#   - name: WhitespaceTokenizer
   - name: JiebaTokenizer #Support Chinese
   - name: RegexFeaturizer
   - name: LexicalSyntacticFeaturizer
   - name: CountVectorsFeaturizer
   - name: CountVectorsFeaturizer
     analyzer: char_wb
     min_ngram: 1
     max_ngram: 4
   - name: DIETClassifier
     epochs: 100
     constrain_similarities: true
   - name: EntitySynonymMapper
   - name: ResponseSelector
     epochs: 100
     constrain_similarities: true
   - name: FallbackClassifier
     threshold: 0.3
     ambiguity_threshold: 0.1
 
# Configuration for Rasa Core.
# https://rasa.com/docs/rasa/core/policies/
policies:
# # No configuration for policies was provided. The following default policies were used to train your model.
# # If you'd like to customize them, uncomment and adjust the policies.
# # See https://rasa.com/docs/rasa/policies for more information.
   - name: MemoizationPolicy
   - name: TEDPolicy
     max_history: 5
     epochs: 100
     constrain_similarities: true
   - name: RulePolicy

domain.yml domain configuration

version: '2.0'
config:
  store_entities_as_slots: true
session_config:
  session_expiration_time: 60
  carry_over_slots_to_new_session: true
intents:
- greet:
    use_entities: true
- deny:
    use_entities: true
- request_names:
    use_entities: true
- goodbye:
    use_entities: true
- affirm:
    use_entities: true
- mood_great:
    use_entities: true
- mood_unhappy:
    use_entities: true
- bot_challenge:
    use_entities: true
entities: []
slots:
  first_name:
    type: rasa.shared.core.slots.TextSlot
    initial_value: null
    auto_fill: true
    influence_conversation: true
  last_name:
    type: rasa.shared.core.slots.TextSlot
    initial_value: null
    auto_fill: true
    influence_conversation: true
  name_spelled_correctly:
    type: rasa.shared.core.slots.BooleanSlot
    initial_value: null
    auto_fill: true
    influence_conversation: true
  requested_slot:
    type: rasa.shared.core.slots.UnfeaturizedSlot
    initial_value: null
    auto_fill: true
    influence_conversation: false
responses:
  utter_greet:
  - text: Hi, how are you?
  utter_cheer_up:
  - image: https://i.imgur.com/nGF1K8f.jpg
    text: 'It's something that lifts you up:'
  utter_did_that_help:
  - text: Does this help you?
  utter_happy:
  - text: Great, go on!
  utter_goodbye:
  - text: bye
  utter_iamabot:
  - text: I'm a robot, by Rasa Framework support.
  utter_ask_first_name:
  - text: What's your family name??
  utter_ask_last_name:
  - text: Your Name?
  utter_ask_name_spelled_correctly:
  - buttons:
    - payload: /affirm
      title: yes
    - payload: /deny
      title: no
    text:  surname {first_name} Is the spelling right?
  utter_submit:
  - text: That's fine. Thank you!
  utter_slots_values:
  - text: I remember you, {first_name} {last_name}!
actions:
- utter_greet
- utter_slots_values
- utter_submit
- validate_name_form
forms:
  name_form:
    first_name:
    - type: from_text
    last_name:
    - type: from_text
e2e_actions: []

credentials.yml certificate configuration is used to call the interface of voice channel

# This file contains the credentials for the voice & chat platforms
# which your bot is using.
# https://rasa.com/docs/rasa/messaging-and-voice-channels
 
rest:
#  # you don't need to provide anything here - this channel doesn't
#  # require any credentials
 
 
#facebook:
#  verify: "<verify>"
#  secret: "<your secret>"
#  page-access-token: "<your page access token>"
 
#slack:
#  slack_token: "<your slack token>"
#  slack_channel: "<the slack channel>"
#  slack_signing_secret: "<your slack signing secret>"
 
#socketio:
#  user_message_evt: <event name for user message>
#  bot_message_evt: <event name for bot messages>
#  session_persistence: <true/false>
 
#mattermost:
#  url: "https://<mattermost instance>/api/v4"
#  token: "<bot token>"
#  webhook_url: "<callback URL>"
 
# This entry is needed if you are using Rasa X. The entry represents credentials
# for the Rasa X "channel", i.e. Talk to your bot and Share with guest testers.
rasa:
  url: "http://localhost:5002/api"

endpoints.yml endpoint configuration, such as the model, action and storage service to be used by the robot

# This file contains the different endpoints your bot can use.
 
# Server where the models are pulled from.
# https://rasa.com/docs/rasa/model-storage#fetching-models-from-a-server
 
#models:
#  url: http://my-server.com/models/default_core@latest
#  wait_time_between_pulls:  10   # [optional](default: 100)
 
# Server which runs your custom actions.
# https://rasa.com/docs/rasa/custom-actions
 
#action_endpoint:
#  url: "http://localhost:5055/webhook"
action_endpoint:
    url: "http://localhost:5055/webhook"
 
# Tracker store which is used to store the conversations.
# By default the conversations are stored in memory.
# https://rasa.com/docs/rasa/tracker-stores
 
#tracker_store:
#    type: redis
#    url: <host of the redis instance, e.g. localhost>
#    port: <port of your redis instance, usually 6379>
#    db: <number of your database within redis, e.g. 0>
#    password: <password used for authentication>
#    use_ssl: <whether or not the communication is encrypted, default false>
 
#tracker_store:
#    type: mongod
#    url: <url to your mongo instance, e.g. mongodb://localhost:27017>
#    db: <name of the db within your mongo instance, e.g. rasa>
#    username: <username used for authentication>
#    password: <password used for authentication>
tracker_store:
  type: SQL
  dialect: sqlite
  db: trackers.db
 
# Event broker which all conversation events should be streamed to.
# https://rasa.com/docs/rasa/event-brokers
 
#event_broker:
#  url: localhost
#  username: username
#  password: password
#  queue: queue
event_broker:
  type: SQL
  dialect: sqlite
  db: events.db

names.txt list of names used in this case

Sun WuKong
 Zhu Bajie
 Tang Sanzang
 Sha Wujing
 Qing-Yun Zhuge 

4. Write your first story

In this section, you will teach your assistant how to respond to your message. This is called conversation management and is handled by your Rasa Core model. The Rasa Core model learns from real session data by training "stories". A story represents the real conversation process between users and assistants.

The contents and entities lines reflect the user's input, and the action names show how the assistant responds to the user's input.

An example of dialogue. The user said hello and the assistant replied hello. That's why it looks like a story. You can Stories View details in.

The line starting with - is the assistant's action. In this session, all actions return the user's information as it is, like utter_greet. However, generally, action can do anything, including calling api, interacting with external and so on.

Run the following command to view data / stories Example of story in MD file:

cat data/stories.md

stories.yml storyline, describing the process of dialogue

version: "2.0"
 
stories:
 
- story: happy path
  steps:
  - intent: greet
  - action: utter_greet
  - intent: mood_great
  - action: utter_happy
 
- story: sad path 1
  steps:
  - intent: greet
  - action: utter_greet
  - intent: mood_unhappy
  - action: utter_cheer_up
  - action: utter_did_that_help
  - intent: affirm
  - action: utter_happy
 
- story: sad path 2
  steps:
  - intent: greet
  - action: utter_greet
  - intent: mood_unhappy
  - action: utter_cheer_up
  - action: utter_did_that_help
  - intent: deny
  - action: utter_goodbye
 
- story: interactive_story_1
  steps:
  - intent: greet
  - action: utter_greet
  - intent: request_names
  - action: name_form
  - active_loop: name_form
  - slot_was_set:
    - requested_slot: first_name
  - slot_was_set:
    - name_spelled_correctly: None
  - slot_was_set:
    - first_name: None
  - slot_was_set:
    - requested_slot: last_name
  - slot_was_set:
    - name_spelled_correctly: None
  - slot_was_set:
    - last_name: None
  - slot_was_set:
    - requested_slot: null
  - active_loop: null
  - action: utter_submit
  - action: utter_slots_values

‚Äč

5. Defining areas

Next, we will define a domain. This field defines the boundaries that your assistant can handle: what information you want the user to input, what actions you can predict, how to respond to the user, and what information you store. The realm of the assistant is stored in the file domain. XML In YML:

cat domain.yml                    

So what do the different parts mean?

intentsWhat you want users to say
actionsWhat an assistant can do and say
templatesScript template of assistant

How are these combined? The job of Rasa Core is to select a correct action to execute at each step of the session. In this example, our actions simply send a message to the user. These simple words are executed in the domain with the word "utter"_ Actions at the beginning. The assistant selects a template response message from templates. reference resources Custom Actions Create a custom action.

The configuration files involved have been listed one by one above. Let's take a look at how the user-defined action is written. It mainly completes the process of input verification. The default action is based on utter_ First, the system defaults to payment

import yaml 
import pathlib 
from typing import Text, List, Any, Dict, Optional
 
from rasa_sdk import Tracker, FormValidationAction
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.types import DomainDict
 
names = pathlib.Path("names.txt").read_text().split("\n")
 
 
class ValidateNameForm(FormValidationAction):
    def name(self) -> Text:
        return "validate_name_form"
    
    async def required_slots(
        self,
        slots_mapped_in_domain: List[Text],
        dispatcher: "CollectingDispatcher",
        tracker: "Tracker",
        domain: "DomainDict",
    ) -> Optional[List[Text]]:
        first_name = tracker.slots.get("first_name")
        if first_name is not None:
            if first_name not in names:
                return ["name_spelled_correctly"] + slots_mapped_in_domain
        return slots_mapped_in_domain
    
    async def extract_name_spelled_correctly(
        self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
    ) -> Dict[Text, Any]:
        intent = tracker.get_intent_of_latest_message()
        return {"name_spelled_correctly": intent == "affirm"}
 
    def validate_name_spelled_correctly(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        """Validate `first_name` value."""
        if tracker.get_slot("name_spelled_correctly"):
            return {"first_name": tracker.get_slot("first_name"), "name_spelled_correctly": True}
        return {"first_name": None, "name_spelled_correctly": None}
        
    def validate_first_name(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        """Validate `first_name` value."""
 
        # If the name is super short, it might be wrong.
        print(f"surname = {slot_value} length = {len(slot_value)}")
        if len(slot_value) <=1:
            dispatcher.utter_message(text=f"The last name is too short. Are you sure you spelled it right?")
            return {"first_name": None}
        else:
            return {"first_name": slot_value}
 
    def validate_last_name(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        """Validate `last_name` value."""
 
        # If the name is super short, it might be wrong.
        print(f"name = {slot_value} length = {len(slot_value)}")
        if len(slot_value) <= 1:
            dispatcher.utter_message(text=f"The name is too short. Are you sure you spelled it right?")
            return {"last_name": None}
        else:
            return {"last_name": slot_value}

6. Training model

Once we add new NLU or Core data, or update the domain or configuration, we need to retrain the neural network on the sample story and NLU data. The neural network can be retrained by using the following command. This command will call Rasa's Core and NLU training functions and store the trained model in the models / folder. This command will automatically retrain Rasa's data or configuration change part of the model.

rasa train

The rasa train command will find the data of NLU and CORE and train the combined model.

Illustration:

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-qvfxkayp-1645008096430) (/ users / liuzepei / library / Application Support / typera user images / image-20220216182032542. PNG)]

7. Use your assistant

congratulations! You have just set up an assistant that is completely driven by machine learning.

The next step is to test it! If you are learning this tutorial locally, start starting and using your assistant with the following command:

rasa shell

Start the robot for dialogue

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-xhbgqhxw-1645008096430) (/ users / liuzepei / library / Application Support / typera user images / image-20220216182605475. PNG)]

8. Create rasa x

You can use Rasa X to collect more sessions to improve your assistant:

Start the rasa X command under the project directory as follows:

rasa x

Startup example:

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG alnrlgfv-1645008096431) (/ users / liuzepei / library / Application Support / typera user images / image-20220216182943485. PNG)]

If the service is started on the server, fill in the ip address of the server when logging in the IE browser, for example: 192.168.10.192:5002

Illustration:

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-aaekzhkk-1645008096431) (/ users / liuzepei / library / Application Support / typera user images / image-20220216183320305. PNG)]

Input password:

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-tc6g2spi-1645008096431) (/ users / liuzepei / library / Application Support / typera user images / image-20220216183545033. PNG)]

Post login Icon:

[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-xttir2a5-1645008096431) (/ users / liuzepei / library / Application Support / typera user images / image-20220216183650359. PNG)]

Keywords: NLP

Added by cheerio on Wed, 16 Feb 2022 13:02:43 +0200