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

_utter_responses should handle template and response #8226

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions changelog/8223.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In Rasa 2.4.0, support for using `template` in `utter_message` when handling a custom action was wrongly deprecated. Both `template` and `response` are now supported, though note that `template` will be deprecated at Rasa 3.0.0.
6 changes: 3 additions & 3 deletions examples/formbot/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def validate_cuisine(
# validation succeeded, set the value of the "cuisine" slot to value
return {"cuisine": value}
else:
dispatcher.utter_message(template="utter_wrong_cuisine")
dispatcher.utter_message(response="utter_wrong_cuisine")
# validation failed, set this slot to None, meaning the
# user will be asked for the slot again
return {"cuisine": None}
Expand All @@ -65,7 +65,7 @@ def validate_num_people(
if self.is_int(value) and int(value) > 0:
return {"num_people": value}
else:
dispatcher.utter_message(template="utter_wrong_num_people")
dispatcher.utter_message(response="utter_wrong_num_people")
# validation failed, set slot to None
return {"num_people": None}

Expand All @@ -86,7 +86,7 @@ def validate_outdoor_seating(
# convert "in..." to False
return {"outdoor_seating": False}
else:
dispatcher.utter_message(template="utter_wrong_outdoor_seating")
dispatcher.utter_message(response="utter_wrong_outdoor_seating")
# validation failed, set slot to None
return {"outdoor_seating": None}

Expand Down
10 changes: 10 additions & 0 deletions rasa/core/actions/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,17 @@ async def _utter_responses(
"""Use the responses generated by the action endpoint and utter them."""
bot_messages = []
for response in responses:
generated_response = None
yennycheung marked this conversation as resolved.
Show resolved Hide resolved
generated_response = response.pop("response", None)
if not generated_response:
generated_response = response.pop("template", None)
rasa.shared.utils.io.raise_deprecation_warning(
"The terminology 'template' is deprecated and replaced by "
"'response', use the `response` parameter instead of "
"`template` in the dispatcher. You can do that by upgrading "
yennycheung marked this conversation as resolved.
Show resolved Hide resolved
"to Rasa SDK 2.4.1 or adapting your custom SDK.",
docs=f"{rasa.shared.constants.DOCS_BASE_URL_ACTION_SERVER}/sdk-dispatcher",
)
if generated_response:
draft = await nlg.generate(
generated_response, tracker, output_channel.name(), **response
Expand Down
1 change: 1 addition & 0 deletions rasa/shared/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
DOCS_URL_TELEMETRY = DOCS_BASE_URL + "/telemetry/telemetry"
DOCS_BASE_URL_RASA_X = "https://rasa.com/docs/rasa-x"
DOCS_BASE_URL_ACTION_SERVER = "https://rasa.com/docs/action-server"

INTENT_MESSAGE_PREFIX = "/"

Expand Down
49 changes: 49 additions & 0 deletions tests/core/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,55 @@ async def test_remote_action_utterances_with_none_values(
]


async def test_remote_action_with_template_param(
default_channel, default_tracker, domain: Domain
):
endpoint = EndpointConfig("https://example.com/webhooks/actions")
remote_action = action.RemoteAction("my_action", endpoint)

response = {
"events": [
{"event": "form", "name": "restaurant_form", "timestamp": None},
{
"event": "slot",
"timestamp": None,
"name": "requested_slot",
"value": "cuisine",
},
],
"responses": [
{
"text": None,
"buttons": [],
"elements": [],
"custom": {},
"template": "utter_ask_cuisine",
"image": None,
"attachment": None,
}
],
}

nlg = TemplatedNaturalLanguageGenerator(
{"utter_ask_cuisine": [{"text": "what dou want to eat?"}]}
)
with aioresponses() as mocked:
mocked.post("https://example.com/webhooks/actions", payload=response)

with pytest.warns(FutureWarning):
events = await remote_action.run(
default_channel, nlg, default_tracker, domain
)

assert events == [
BotUttered(
"what dou want to eat?", metadata={"utter_action": "utter_ask_cuisine"}
),
ActiveLoop("restaurant_form"),
SlotSet("requested_slot", "cuisine"),
]


async def test_remote_action_without_endpoint(
default_channel, default_nlg, default_tracker, domain: Domain
):
Expand Down