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
Show file tree
Hide file tree
Changes from all 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)]
```
13 changes: 5 additions & 8 deletions rasa/core/actions/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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={
Expand All @@ -236,7 +234,6 @@ class Action:

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

raise NotImplementedError

async def run(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
ancalita marked this conversation as resolved.
Show resolved Hide resolved
slot_events.append(event)
slot_events.append(event)
elif isinstance(event, BotUttered):
slot_events.append(event)
else:
Expand Down
60 changes: 59 additions & 1 deletion tests/core/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
ancalita marked this conversation as resolved.
Show resolved Hide resolved
)

# 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")]