s ta te fu l b o ts...dat acamp b ui l di ng chat bot s i n p yt hon w hat do we mean by st at ef ul...

20
DataCamp Building Chatbots in Python Stateful bots BUILDING CHATBOTS IN PYTHON Alan Nichol Co-founder and CTO, Rasa

Upload: others

Post on 19-Jan-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Stateful bots

BUILDING CHATBOTS IN PYTHON

Alan NicholCo-founder and CTO, Rasa

Page 2: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

What do we mean by stateful?

"I love stateless systems!"

"don't what have drawbacks?"

"don't they have drawbacks?"

Page 3: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

State machines

Browsing

Providing address, billing info

Order complete

Page 4: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Implementing a state machine

Example rules:

INIT = 0

CHOOSE_COFFEE = 1

ORDERED = 2

policy_rules = {

(INIT, "order"): (CHOOSE_COFFEE, "ok, Columbian or Kenyan?"),

(CHOOSE_COFFEE, "specify_coffee"):

(ORDERED, "perfect, the beans are on their way!"),

}

Page 5: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Using the state machineIn [1]: state = INIT

In [2]: def respond(state, message):

...: (new_state, response) = policy_rules[(state, interpret(message))]

...: return new_state, response

In [3]: def send_message(state, message):

...: new_state, response = respond(state, message)

...: return new_state

In [4]: state = send_message(state, message)

Page 6: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Let's practice!

BUILDING CHATBOTS IN PYTHON

Page 7: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Asking questions &queuing answers

BUILDING CHATBOTS IN PYTHON

Alan NicholCo-founder and CTO, Rasa

Page 8: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Reusable patterns

"I'd like some Kenyan beans"

"I'm sorry, we're out of those. Shall Iorder some Brazilian ones for you?"

"Yes please"

"Can I get a box of 200 brown filters"

"I'm sorry, we're out of those, but I canget your some white ones. Should Iorder those for you?"

"Yes please"

Page 9: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Pending actions

Policy returns two values: Selected action and pending_action

pending_action is saved in the outer scope

If we get a "yes" intent and there is a pending action, we execute it

If we get a "no" intent, we wipe any pending actions

Page 10: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Pending state transitions

"I'd like to order some coffee"

Sounds good! I'd love to help you but you'll have to log in first, what's your phone

number?

"555-12345"

Perfect! welcome back :)

state = INIT

action = "request_auth"

pending_state = AUTHED

state = AUTHED

action = "acknowledge_auth"

pending_state = None

Page 11: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Let's practice!

BUILDING CHATBOTS IN PYTHON

Page 12: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Frontiers of dialoguetechnology

BUILDING CHATBOTS IN PYTHON

Alan NicholCo-founder and CTO, Rasa

Page 13: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

A neural conversational model

"What do you think of Cleopatra?" "Oh, she's very regal"

"What do you think of Messi?" "He's a great player"

Page 14: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Seq2seq

Machine translation

Completely data driven, no hand-crafting

Requires large amount of data

No guarantee that output is coherent

Difficult to integrate DB / API calls & other logic

Page 15: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Grounded dialogue systems

Systems you've built in this course: hand-crafted

Seq2seq: Data driven

ML based dialogue systems:

NLU

Dialogue state manager

API logic

Natural language response generator

Human pretend to be a bot: "Wizard of Oz" technique

Reinforcement learning

Receives a reward for a successful conversation

Page 16: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Language generation

Not recommended if building a bot

Pre-trained neural network which can generate text

Scripts of every episode of The Simpsons

Page 17: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Generating sample textgenerated = sample_text(

saved_params,

temperature,

num_letters=num_letters,

init_text=text

)

Page 18: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Let's practice!

BUILDING CHATBOTS IN PYTHON

Page 19: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Congratulations!

BUILDING CHATBOTS IN PYTHON

Alan NicholCo-founder and CTO, Rasa

Page 20: S ta te fu l b o ts...Dat aCamp B ui l di ng Chat bot s i n P yt hon W hat do we mean by st at ef ul ? "I love stateless systems!" "don't what have drawbacks?" "don't they ... DataCamp

DataCamp Building Chatbots in Python

Let's practice!

BUILDING CHATBOTS IN PYTHON