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

ATO 678 missing slot set event on a custom action filled slot #12314

Merged
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
22 changes: 22 additions & 0 deletions changelog/12314.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
`SlotSet` events will be emitted when the value set by the custom action is the same as the existing value of the slot. This was fixed for `AugmentedMemoizationPolicy` to work properly with truncated trackers.

To restore the previous behaviour, the custom action can return a SlotSet only if the slot value has changed. For example,

```
class CustomAction(Action):
def name(self) -> Text:
return "custom_action"

def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
# current value of the slot
slot_value = tracker.get_slot('my_slot')

# value of the entity
# this is parsed from the user utterance
entity_value = next(tracker.get_latest_entity_values("entity_name"), None)

if slot_value != entity_value:
return[SlotSet("my_slot", entity_value)]
```
22 changes: 22 additions & 0 deletions data/test_action_extract_slots_12314/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
recipe: default.v1
language: en
pipeline:
- name: WhitespaceTokenizer
- name: RegexFeaturizer
- name: LexicalSyntacticFeaturizer
- name: CountVectorsFeaturizer
- name: CountVectorsFeaturizer
analyzer: char_wb
min_ngram: 1
max_ngram: 4
- name: DIETClassifier
epochs: 100
- name: EntitySynonymMapper
policies:
- name: MemoizationPolicy
max_history: 10
- name: TEDPolicy
max_history: 10
epochs: 100
- name: RulePolicy
assistant_id: 20230502-134237-congruent-inch
35 changes: 35 additions & 0 deletions data/test_action_extract_slots_12314/data/nlu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: "3.0"
nlu:
- intent: greet
examples: |
- hey
- hello
- hi
- hello there
- good morning
- good evening
- moin
- hey there
- let's go
- hey dude
- goodmorning
- goodevening
- good afternoon
- intent: install_app
examples: |
- help me install a [hard rsa token]{"entity": "rsa_token", "value": "hard"}
- help me install a [soft rsa token]{"entity": "rsa_token", "value": "soft"}
- help me install a [rsa token]{"entity": "rsa_token", "value": "unknown"}
- i need help installing my [rsa token]{"entity": "rsa_token", "value": "unknown"}
- i need help installing my [hard rsa token]{"entity": "rsa_token", "value": "hard"}
- i need help installing my [soft rsa token]{"entity": "rsa_token", "value": "soft"}
- help me to install [rsa token]{"entity": "rsa_token", "value": "unknown"}
- help me to install [hard rsa token]{"entity": "rsa_token", "value": "hard"}
- help me to install [soft rsa token]{"entity": "rsa_token", "value": "soft"}
- need a new [rsa token]{"entity": "rsa_token", "value": "unknown"}
- need a new [hard rsa token]{"entity": "rsa_token", "value": "hard"}
- need a new [soft rsa token]{"entity": "rsa_token", "value": "soft"}
- get me [rsa token]{"entity": "rsa_token", "value": "unknown"}
- get me [hard rsa token]{"entity": "rsa_token", "value": "hard"}
- get me [soft rsa token]{"entity": "rsa_token", "value": "soft"}
- get me [rsatoken]{"entity": "rsa_token", "value": "unknown"}
29 changes: 29 additions & 0 deletions data/test_action_extract_slots_12314/data/stories.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: "3.1"

stories:
- story: rsa token - not given on first intent
steps:
- intent: install_app
- slot_was_set:
- rsa_token: unknown
- action: utter_ask_rsa_token_type
- or:
- slot_was_set:
- rsa_token: hard
- slot_was_set:
- rsa_token: soft
- slot_was_set:
- rsa_token: unknown
- action: utter_token_type

- story: rsa token - type given in intent
steps:
- intent: install_app
- or:
- slot_was_set:
- rsa_token: hard
- slot_was_set:
- rsa_token: soft
- slot_was_set:
- rsa_token: unknown
- action: utter_token_type
34 changes: 34 additions & 0 deletions data/test_action_extract_slots_12314/domain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3.1'
session_config:
session_expiration_time: 60
carry_over_slots_to_new_session: true

intents:
- greet
- install_app

slots:
rsa_token:
type: categorical
values:
- hard
- soft
- unknown
influence_conversation: true
mappings:
- type: custom
action: action_rsa_token

responses:
utter_ask_rsa_token_type:
- text: What type of rsa token do you need?
utter_token_type:
- text: User has requested rsa token type of {rsa_token}
utter_greet:
- text: Hey! How are you?

actions:
- utter_greet

entities:
- rsa_token
13 changes: 5 additions & 8 deletions rasa/core/actions/action.py
Original file line number Diff line number Diff line change
@@ -153,7 +153,6 @@ def is_retrieval_action(action_name: Text, retrieval_intents: List[Text]) -> boo
`True` if the resolved intent name is present in the list of retrieval
intents, `False` otherwise.
"""

return (
ActionRetrieveResponse.intent_name_from_action(action_name) in retrieval_intents
)
@@ -210,7 +209,6 @@ def action_for_name_or_text(

def create_bot_utterance(message: Dict[Text, Any]) -> BotUttered:
"""Create BotUttered event from message."""

bot_message = BotUttered(
text=message.pop("text", None),
data={
@@ -236,7 +234,6 @@ class Action:

def name(self) -> Text:
"""Unique identifier of this simple action."""

raise NotImplementedError

async def run(
@@ -508,7 +505,8 @@ class ActionListen(Action):
"""The first action in any turn - bot waits for a user message.

The bot should stop taking further actions and wait for the user to say
something."""
something.
"""

def name(self) -> Text:
return ACTION_LISTEN_NAME
@@ -568,7 +566,6 @@ def _slot_set_events_from_tracker(
tracker: "DialogueStateTracker",
) -> List["SlotSet"]:
"""Fetch SlotSet events from tracker and carry over key, value and metadata."""

return [
SlotSet(key=event.key, value=event.value, metadata=event.metadata)
for event in tracker.applied_events()
@@ -830,7 +827,8 @@ def name(self) -> Text:

class ActionExecutionRejection(RasaException):
"""Raising this exception will allow other policies
to predict a different action"""
to predict a different action.
"""

def __init__(self, action_name: Text, message: Optional[Text] = None) -> None:
self.action_name = action_name
@@ -1098,8 +1096,7 @@ async def _run_custom_action(
)
for event in custom_events:
if isinstance(event, SlotSet):
if tracker.get_slot(event.key) != event.value:
slot_events.append(event)
slot_events.append(event)
elif isinstance(event, BotUttered):
slot_events.append(event)
else:
60 changes: 59 additions & 1 deletion tests/core/test_actions.py
Original file line number Diff line number Diff line change
@@ -2509,7 +2509,7 @@ async def test_action_extract_slots_with_none_value_custom_mapping():
tracker,
domain,
)
assert events == []
assert events == [SlotSet("custom_slot", None)]


async def test_action_extract_slots_returns_bot_uttered():
@@ -2825,3 +2825,61 @@ async def test_action_extract_slots_priority_of_slot_mappings():
)
tracker.update_with_events(events, domain=domain)
assert tracker.get_slot("location_slot") == entity_value


async def test_action_extract_slots_allows_slotset_for_same_value(
caplog: LogCaptureFixture,
):
domain_yaml = textwrap.dedent(
f"""
version: "{LATEST_TRAINING_DATA_FORMAT_VERSION}"

slots:
custom_slot_a:
type: text
influence_conversation: false
mappings:
- type: custom
action: custom_extract_action

actions:
- custom_extract_action
- action_validate_slot_mappings
"""
)
domain = Domain.from_yaml(domain_yaml)
event = UserUttered("Hi")
tracker = DialogueStateTracker.from_events(
sender_id="test_id", evts=[event], slots=domain.slots
)

# Set the value of the slot in the tracker manually
tracker.update(SlotSet("custom_slot_a", "test_A"))
action_server_url = "https://my-action-server:5055/webhook"

with aioresponses() as mocked:
mocked.post(
action_server_url,
payload={
"events": [
{"event": "slot", "name": "custom_slot_a", "value": "test_A"}
]
},
)

action_server = EndpointConfig(action_server_url)
action_extract_slots = ActionExtractSlots(action_server)

with caplog.at_level(logging.INFO):
events = await action_extract_slots.run(
CollectingOutputChannel(),
TemplatedNaturalLanguageGenerator(domain.responses),
tracker,
domain,
)

caplog_info_records = list(
filter(lambda x: x[1] == logging.INFO, caplog.record_tuples)
)
assert len(caplog_info_records) == 0
assert events == [SlotSet("custom_slot_a", "test_A")]
Loading