RasaCore : 0.14.1 — RasaNLU : 0.15.0 – Python 3.6.7
Before this step, you will need a NLU model (you can follow the first step of this video : https://www.youtube.com/watch?v=xu6D_vLP5vY (in my exemple you will need to train 3 intents (greet,goodbye,ask_find))
Step 1) Then you will need to create 5 files :
-endpoints.yml | -domain.yml | -stories.md | -actions.py | -train_online.py
endpoints.yml
action_endpoint:
url: "http://localhost:5055/webhook/"
domain.yml
slots:
recherche:
type: text
intents:
- greet
- goodbye
- ask_find
entities:
- recherche
templates:
utter_greet:
- text: "Hey !"
utter_goodbye:
- text: "Thk body"
utter_ask_recherche:
- text: "Do you need something ?"
utter_slots_values:
- text: "What you asked for --> {recherche}"
actions:
- utter_slots_values
- utter_greet
- utter_goodbye
- utter_ask_recherche
- action_wiki
stories.md
## Story 1
* greet
- utter_greet
* ask_find
- utter_ask_recherche
- action_listen
- action_wiki
- utter_slots_values
* goodbye
- utter_goodbye
actions.py
# -*- coding: utf-8 -*-
from typing import Dict, Text, Any, List, Union, Optional
from rasa_core_sdk import ActionExecutionRejection
from rasa_core_sdk import Tracker
from rasa_core_sdk.events import SlotSet
from rasa_core_sdk.executor import CollectingDispatcher
from rasa_core_sdk.forms import FormAction, REQUESTED_SLOT
from rasa_core_sdk import Action
class Action_wiki(Action):
"""Example of a custom action"""
def name(self):
# type: () -> Text
"""Unique identifier of the action"""
return "action_wiki"
def run(self, dispatcher, tracker, domain):
message = tracker.latest_message.get('text')
return [SlotSet('recherche', message)]
train_online.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import logging
from rasa_core.agent import Agent
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.training import interactive
from rasa_core.utils import EndpointConfig
from rasa_core.policies.form_policy import FormPolicy
logger = logging.getLogger(__name__)
def run_online(interpreter,
domain_file="domain.yml",
training_data_file='stories.md'):
action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
agent = Agent(domain_file,
policies=[MemoizationPolicy(max_history=2), KerasPolicy(max_history=3, epochs=3, batch_size=50), FormPolicy()],
interpreter=interpreter,
action_endpoint=action_endpoint)
data = agent.load_data(training_data_file)
agent.train(data)
interactive.run_interactive_learning(agent, training_data_file)
return agent
if __name__ == '__main__':
logging.basicConfig(level="INFO")
nlu_interpreter = RasaNLUInterpreter('./Model/default/model_20190502-164014')
run_online(nlu_interpreter)
Step 2) Open a cmd Terminal where your 5 files are and put this command line :
If you have only python3 →
python -m rasa_core_sdk.endpoint --actions actions
- or if you have python2 & python3 → python3 -m rasa_core_sdk.endpoint --actions actions
Step 3) Run in an other terminal your “train_online.py” file
END
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
For information (in actions.py) the run function take the last user message (listened by the “action_listen” function) and fill the slot named “recherche” (means ‘search’ in french):
def run(self, dispatcher, tracker, domain):
message = tracker.latest_message.get('text')
return [SlotSet('recherche', message)]
PS : There is an other method to get free text from user with Forms.
Hope i helped you