Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic 2 stage fallback working with intent retrieval #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ models/
*.db-wal
gactions
credentials.yml
results.md
results.md
creds.data
63 changes: 39 additions & 24 deletions action.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
{
"actions": [
{
"description": "Default Welcome Intent",
"name": "MAIN",
"fulfillment": {
"conversationName": "<INSERT YOUR CONVERSATION NAME HERE>"
},
"intent": {
"name": "actions.intent.MAIN",
"trigger": {
"queryPatterns": [
"talk to Instill Distilling"
]
}
}
}
],
"conversations": {
"<INSERT YOUR CONVERSATION NAME HERE>": {
"name": "<INSERT YOUR CONVERSATION NAME HERE>",
"url": "<INSERT YOUR FULLFILLMENT URL HERE>"
}
},
"locale": "en"
}
{
"description": "Default Welcome Intent",
"name": "MAIN",
"fulfillment": {
"conversationName": "welcome"
},
"intent": {
"name": "actions.intent.MAIN",
"trigger": {
"queryPatterns":["talk to Instill Distilling"]
}
}
},
{
"description": "Rasa Intent",
"name": "TEXT",
"fulfillment": {
"conversationName": "rasa_intent"
},
"intent": {
"name": "actions.intent.TEXT",
"trigger": {
"queryPatterns":[]
}
}
}],
"conversations": {
"welcome": {
"name": "welcome",
"url": "https://221215ef.ngrok.io/webhooks/google_assistant/webhook",
"fulfillmentApiVersion": 2
},
"rasa_intent": {
"name": "rasa_intent",
"url": "https://221215ef.ngrok.io/webhooks/google_assistant/webhook",
"fulfillmentApiVersion": 2
}
}
}
145 changes: 144 additions & 1 deletion actions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import logging

from typing import Dict, Text, Any, List, Union

from rasa_sdk import Tracker, Action
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction
from rasa_sdk.events import EventType
from rasa_sdk.events import (
UserUtteranceReverted,
ConversationPaused,
EventType,
)
import json

logger = logging.getLogger(__name__)


class ActionPause(Action):
"""Pause the conversation"""

def name(self) -> Text:
return "action_pause"

def run(self, dispatcher, tracker, domain) -> List[EventType]:
return [ConversationPaused()]


with open("drinks.json", "r") as f:
drink_recipes_dict = json.load(f)

Expand Down Expand Up @@ -108,3 +127,127 @@ def submit(
f"and garnish with {drink_garnish}"
)
return []


class ActionDefaultAskAffirmation(Action):
"""Asks for an affirmation of the intent if NLU threshold is not met."""

def name(self) -> Text:
return "action_default_ask_affirmation"

def __init__(self) -> None:
import pandas as pd

self.intent_mappings = pd.read_csv("intent_description_mappings.csv")
self.intent_mappings.fillna("", inplace=True)
self.intent_mappings.entities = self.intent_mappings.entities.map(
lambda entities: {e.strip() for e in entities.split(",")}
)

def run(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[EventType]:

intent_ranking = tracker.latest_message.get("intent_ranking", [])
if len(intent_ranking) > 1:
diff_intent_confidence = intent_ranking[0].get(
"confidence"
) - intent_ranking[1].get("confidence")
if diff_intent_confidence < 0.2:
intent_ranking = intent_ranking[:2]
else:
intent_ranking = intent_ranking[:1]
first_intent_names = [
intent.get("name", "")
for intent in intent_ranking
if intent.get("name", "") != "out_of_scope"
]

message_title = (
"Sorry, I'm not sure that I've understood "
"you correctly 🤔 Do you mean..." # noqa: E501
)

entities = tracker.latest_message.get("entities", [])
entities = {e["entity"]: e["value"] for e in entities}

entities_json = json.dumps(entities)

buttons = []
for intent in first_intent_names:
logger.debug(intent)
logger.debug(entities)
buttons.append(
{
"title": self.get_button_title(intent, entities),
"payload": "/{}{}".format(intent, entities_json),
}
)

# /out_of_scope is a retrieval intent
# you cannot send rasa the '/out_of_scope' intent
# instead, you can send one of the sentences that it will
# map onto the response
buttons.append(
{
"title": "Something else",
"payload": "I am asking you an out of scope question",
}
)

dispatcher.utter_button_message(message_title, buttons=buttons)

return []

def get_button_title(
self, intent: Text, entities: Dict[Text, Text]
) -> Text: # noqa: E501
default_utterance_query = self.intent_mappings.intent == intent
utterance_query = (
self.intent_mappings.entities == entities.keys()
) & ( # noqa: E501
default_utterance_query
)

utterances = self.intent_mappings[utterance_query].button.tolist()

if len(utterances) > 0:
button_title = utterances[0]
else:
utterances = self.intent_mappings[
default_utterance_query
].button.tolist() # noqa: E501
button_title = utterances[0] if len(utterances) > 0 else intent

return button_title.format(**entities)


class ActionDefaultFallback(Action):
def name(self) -> Text:
return "action_default_fallback"

def run(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[EventType]:

# Fallback caused by TwoStageFallbackPolicy
if (
len(tracker.events) >= 4
and tracker.events[-4].get("name")
== "action_default_ask_affirmation" # noqa: E501
):

dispatcher.utter_template("utter_restart_with_button", tracker)

return []

# Fallback caused by Core
else:
dispatcher.utter_template("utter_default", tracker)
return [UserUtteranceReverted()]
11 changes: 9 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ pipeline:
- name: CountVectorsFeaturizer
token_pattern: (?u)\b\w+\b
- name: EmbeddingIntentClassifier
epochs: 50
loss_type: margin
- name: ResponseSelector
retrieval_intent: out_of_scope
policies:
- name: FallbackPolicy
- name: AugmentedMemoizationPolicy
- max_history: 6
name: AugmentedMemoizationPolicy
- core_threshold: 0.3
name: TwoStageFallbackPolicy
nlu_threshold: 0.8
- name: FormPolicy
- name: MappingPolicy
8 changes: 8 additions & 0 deletions data/stories.md → data/core/stories.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## out of scope
* out_of_scope
- respond_out_of_scope

## happy path w/greet
* greet
- utter_greet
Expand Down Expand Up @@ -105,3 +109,7 @@
- utter_hours
* thanks
- utter_thanks

## location story
* location
- utter_location
5 changes: 0 additions & 5 deletions data/faq.md.old

This file was deleted.

24 changes: 23 additions & 1 deletion data/nlu.md → data/nlu/nlu.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

## intent:drink_list
- what recipes do you know
- what drinks can I make
- what drink recipes do you know
- can you tell me what recipes you know about
- I need help knowing what recipes I can make
- what are the drink options
Expand Down Expand Up @@ -74,6 +74,8 @@
- [Rum Punch](drink)
- [Hemingway](drink)
- [hemingway](drink)
- the [Painkiller](drink) please
- I want the [Joco Mule](drink)

## intent:thanks
- thanks
Expand Down Expand Up @@ -106,6 +108,10 @@
## intent:tours
- Do you do tours
- do you do tours
- do you offer tours?
- can I tour your facility?
- I want to go on a tour
- when is the best time to come for a tour.

## intent:hours
- What are your hours today
Expand All @@ -124,3 +130,19 @@

## intent:location
- where are you located
- how do I get to instill?
- where is instill located
- what is the address
- can you give me information on how to get to instill?

## intent:out_of_scope/other
- who is the president
- what is the square root of 5
- why are you stupid
- I am asking you an out of scope question

## intent:out_of_scope/bourbon
- do you make bourbon
- do you have bourbon there
- can I get bourbon at your place
- I'm a bourbon guy
7 changes: 7 additions & 0 deletions data/nlu/responses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## out of scope other
* out_of_scope/other
- I can't help you with that, I'm sorry.

## out of scope bourbon
* out_of_scope/bourbon
- Sorry I don't have any information about bourbon, we only make rum.
15 changes: 0 additions & 15 deletions data/responses.md.old

This file was deleted.

Loading