1 of 35

Desarrollo de Chatbots con Rasa

Rafa Haro Ramos

@rafa_haro

rafa_haro

2 of 35

Agenda

  • Chatbots Overview
  • What is Rasa?
  • Demo
  • Rasa Architecture
  • Starting a Project with Rasa
  • Rasa NLU
  • Rasa Core
  • Interactive Learning
  • Channels

3 of 35

What’s a Chatbot?

4 of 35

Hype, Hype Everywhere

5 of 35

Chatbots Landscape

6 of 35

Disclaimer: Manage The Expectations

7 of 35

Rasa Framework

  • Rasa is an open source machine learning framework for automated text and voice-based conversations. Understand messages, hold conversations, and connect to messaging channels and APIs.
  • Backed by a company of the same name�
  • Mature Community:
    • 500K+ Downloads
    • 3500K+ Forum Members
    • 300+ Contributors�
  • Similar to vendors systems like DialogFlow (Google), Amazon Lex or Wit.ai (Facebook)

8 of 35

Why Rasa?

  • Open Source
    • Customizable
    • Community
    • Tune models for your use case�
  • Own your data�
  • Avoid Vendor Lock-In�
  • Work locally

9 of 35

Demo

10 of 35

Rasa Architecture

11 of 35

Starting a Project with Rasa

  • rasa init

12 of 35

Rasa NLU

  • NLU stands for Natural Language Understanding�
  • Understand User Request (Intent)�
  • Converts user input text into a Data Structure, including extracted features�
  • Entities: most common features, but can be more�
  • NLU process can be rule based or ML based. Rasa is ML based

13 of 35

Rasa NLU: Select a Pipeline

  • Rasa provides two “macros”

pipeline: "pretrained_embeddings_spacy"

pipeline: "supervised_embeddings"

14 of 35

Rasa NLU: Select a Pipeline

15 of 35

Rasa NLU: Intents

## intent:saludo

- hola

- buenas

- hola, como estas?

- buenos días

- buenas tardes

- buenas noches

- que pasa

- saludos

## intent:a_que_hora

- a qué hora es la charla de [Rafa Haro](speaker)

- cuando es la charla de [Antonio David Perez](speaker)

- a qué hora presenta [María Marcos](speaker)

- cuándo es la charla de [Guillem Duran](speaker)

- cuando habla [Alberto](speaker)

- a que hora es la charla de [antonio](speaker)

- a qué hora es la presentación de [Orange](speaker)

- cuando presenta [Ravenpack](speaker)

- a que hora es la charla de [irene](speaker)

- cuando es la charla de [david garcia](speaker)

## intent:que_hay_a_las

- que charlas hay a las [11:30](time)?

- que hay a las [12](time)?

- que charlas hay [hoy](day) a las [17:00](time)

- que hay [mañana](day) a las [14:30](time)

- que charlas hay el [sabado](day) a las [11:00](time)

- charlas el [domingo](day) a la [13:00](time)?

- quién presenta el [sabado](day) a las [17](time)?

- que charlas hay el [sabado](day)

- charlas del [domingo](day)

- [hoy](day)

- [sabado](day)

- el [domingo](day)

- [12:10](time)

- [17](time)

- [1](time)

## regex:time

- ([01]?[0-9]|2[0-3])(:[0-5][0-9])?

## lookup:day

- hoy

- mañana

- el sabado

- el domingo

- el sábado

- sábado

- domingo

## lookup:speaker

data/speakers.txt

## lookup:talk

data/talks.txt

16 of 35

Rasa NLU: Input Processing

17 of 35

Rasa NLU: Entities

  • Entity Extraction depends on the extractor configured in the Pipeline
  • By default it trains a CRF NER
  • Training data support lookup tables, regex and synonyms�

18 of 35

Rasa NLU: Entities

  • You can build your own Entity Extractor tailored to your use case
  • For example: talks title extraction:
    • Dummy Frequency based extraction
    • Remove Stopwords
    • Normalize
    • Tokenize the input text
    • Select the candidates with the higher number of tokens in common

{

‘Python para calentar tu casa’: [‘python’, ‘calentar’, ‘casa’],

‘MI CASA: venturas y desventuras de una radioastrónoma en Python’: [‘casa’, ‘venturas’, ‘desventuras’, ‘radioastronoma’, ‘python’]

}

charla sobre calentar mi casa

2

1

19 of 35

Rasa NLU Demo

20 of 35

Rasa Core: Stories

  • Conversations Training data for the chatbot
  • Rasa will decide the next action depending of this training data. LSTM Models �
  • Stories are made by a combination of Intents, Actions, Templates, Slots and Forms
  • Try to reflect as much conversations’ paths as possible (Interactive Learning can help)�

21 of 35

Rasa Core: Stories

## say hello

* saludo

- action_hello

## say goodbye

* despedida

- utter_despedida

- action_restart

## estoy bien path

* como_estas

- utter_estoy_bien

- utter_como_estas

* estoy_bien

- utter_me_alegro

- utter_te_puedo_ayudar

templates:

utter_saludo:

- text: "Qué pasa pisha?"

- text: "Qué pasa cabesa?"

- text: "Qué pasa quillo?"

utter_despedida:

- text: "venga nos vemos"

utter_estoy_bien:

- text: "yo estoy de arte"

- text: "aquí estamos chatboteando"

- text: "de categoría"

utter_como_estas:

- text: "¿y tu como andas?"

- text: "¿y tu que te cuentas?"

utter_me_alegro:

- text: "me alegro"

- text: "wena esa"

utter_te_puedo_ayudar:

- text: "¿qué te hace falta?"

- text: "¿qué necesitas?"

- text: "¿en qué te puedo ayudar?"

22 of 35

Rasa Core: Flow Diagram Example

Hola cómo estás?

RASA NLU

intent: como_estas, 95%

entities: []

RASA CORE

next_action: utter_estoy_bien, 100%

slots: []

Aquí estamos chatboteando

utter_estoy_bien:

- text: "yo estoy de arte"

- text: "aquí estamos chatboteando"

- text: "de categoría"

RASA CORE

## estoy bien path

* como_estas

- utter_estoy_bien

- utter_como_estas

* estoy_bien

- utter_me_alegro

- utter_te_puedo_ayudar

23 of 35

Rasa Core: Slots

  • Key - Value store for your bot. It’s the bot memory�
  • Can be used to store data gathered from the user (entities) or from the outside world (API, database…)�
  • Slots also influence next action decision�
  • Actions can fill slots and return Slot events

24 of 35

Rasa Core: Slots

## a que hora es la charla

* a_que_hora

- slot{"speaker":"Rafa Haro"}

- action_find_talk

- action_listen

## que hay a las sin hora

* que_hay_a_las

- action_find_talks_by_time{"day":"domingo"}

- form{"name": "talk_form"}

- action_find_talks_by_time{"time":"11:30","day":"domingo"}

Stories must reflect where the declared slots are filled

25 of 35

Rasa Core: Slots Example

26 of 35

Rasa Core: Actions

  • Actions are the things your bot runs in response to user input
  • Four kinds:
    • Utterance actions: start with utter_ and send a specific message to the user
    • Retrieval actions: start with respond_ and send a message selected by a retrieval model
    • Custom actions: run arbitrary code and send any number of messages (or none)
    • Default actions: e.g. action_listen, action_restart, action_default_fallback�
  • Custom Actions: backend query when the required slots are filled (Example: look for a talk when the bot knows time and day)�

27 of 35

Rasa Core: Actions

class ActionFindNextTalks(Action):

def name(self) -> Text:

return "action_find_next_talks"

def run(self, dispatcher: CollectingDispatcher,

tracker: Tracker,

domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

now = datetime.now()

search = Talk.search()

query = search.sort('start').query('range', start={'gt': now})

if query.count() == 0:

dispatcher.utter_message("Pues ahora mismo no encuentro ninguna")

else:

message = "Las próximas charlas que he encontrado son:\n"

message += range_query_to_message(query)

dispatcher.utter_message(message)

return []

28 of 35

Rasa Core: Events

  • Actions are executed in a separated server. As a result of an action execution, a set of events are returned�
  • Types:
    • SlotSet (Fill a set of slots)
    • Restarted (Restart a conversation)
    • AllSlotsReset
    • ReminderScheduled (Schedule another action….Ex: Are you there?)
    • ConversationPaused
    • FollowupAction (force next action, not predicted)
    • Utter Message
    • …..

29 of 35

Rasa Core: Forms

  • Interactive Slot Filling Strategy
  • Single action which contains the logic to loop over the required slots and ask the user for information�
  • The requested slots must be defined and the bot will keep going asking for them until all are filled. Unhappy paths can still be managed�
  • The message to ask for the next slot can be customized using utter_ask_{slot_name} templates

30 of 35

Rasa Core: Forms

31 of 35

Rasa Core: Forms

  • The way to fill the slot from user message can be customized:
    • By default, from entities with the same name
    • Directly from text (input text is the slot value)
    • From an intent (if an intent is detected, fill the slot with a particular predefined value). This is useful for affirmations and negations
    • From a particular entity with a particular name�
  • Forms are active until filling all slots or programmatically deactivated. BE CAREFUL!�
  • On one hand, it could give the impression of having “memory” but, on the other, it could enter into an annoying loop

32 of 35

Rasa Workflow

  • rasa init
  • rasa train
  • rasa shell (--debug)
  • rasa interactive�
  • rasa run actions
  • rasa run

33 of 35

Rasa Interactive Demo

34 of 35

Rasa Channels

  • Rasa can ‘easily’ be used from several bot channels like Telegram, Slack, etc...

HTTPS

setWebhook(https://your_url.com/..)

ProxyPass

35 of 35

GRACIAS!

@adperezmorales

@rafa_haro

adperezmorales

rafa_haro