Skip to content

Commit

Permalink
Merge pull request RasaHQ#8239 from RasaHQ/bug_micro-8223
Browse files Browse the repository at this point in the history
2.4.x `_utter_responses` should handle `template` and `response`
  • Loading branch information
wochinge authored Mar 23, 2021
2 parents f56c8b8 + 76f30e7 commit ef7cc66
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 6 deletions.
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.
2 changes: 1 addition & 1 deletion docs/docs/connectors/custom-connectors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ the host and port with the appropriate values from your running Rasa X or Rasa O
## The `blueprint` method

The `blueprint` method
needs to create a [sanic blueprint](https://sanic.readthedocs.io/en/stable/sanic/blueprints.html#blueprints)
needs to create a [sanic blueprint](https://sanicframework.org/en/guide/best-practices/blueprints.html#overview)
that can be attached to a sanic server.
Your blueprint should have at least the two routes: `health` on the route `/`,
and `receive` on the route `/webhook` (see example custom channel below).
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/http-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ By default, the HTTP server runs as a single process. You can change the number
of worker processes using the `SANIC_WORKERS` environment variable. It is
recommended that you set the number of workers to the number of available CPU cores
(check out the
[Sanic docs](https://sanic.readthedocs.io/en/stable/sanic/deploying.html#workers)
[Sanic docs](https://sanicframework.org/en/guide/deployment/running.html#workers)
for more details). This will only work in combination with the
`RedisLockStore` (see [Lock Stores](./lock-stores.mdx).

Expand Down Expand Up @@ -118,3 +118,4 @@ The following is an example payload for a JWT token:
```

To create and encode the token, you can use tools such as the [JWT Debugger](https://jwt.io/), or a Python module such as [PyJWT](https://pyjwt.readthedocs.io/en/latest/).

2 changes: 1 addition & 1 deletion docs/docs/migration-guide.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ model before trying to use it with this improved version.
* The `--num_threads` parameter was removed from the `run` command. The
server will always run single-threaded, but will now run asynchronously. If you want to
make use of multiple processes, feel free to check out the [Sanic server
documentation](https://sanic.readthedocs.io/en/stable/sanic/deploying.html#running-via-gunicorn).
documentation](https://sanicframework.org/en/guide/deployment/running.html#gunicorn).

* To avoid conflicts in script parameter names, connectors in the `run` command now need to be specified with
`--connector`, as `-c` is no longer supported. The maximum history in the `rasa visualize` command needs to be
Expand Down
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 @@ -626,6 +626,16 @@ async def _utter_responses(
bot_messages = []
for response in responses:
generated_response = response.pop("response", None)
generated_template = response.pop("template", None)
if generated_template and not generated_response:
generated_response = generated_template
rasa.shared.utils.io.raise_deprecation_warning(
"The terminology 'template' is deprecated and replaced by "
"'response', use the `response` parameter instead of "
"`template` in `dispatcher.utter_message`. You can do that "
"by upgrading 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 @@ -288,6 +288,55 @@ async def test_remote_action_utterances_with_none_values(
]


async def test_remote_action_with_template_param(
default_channel, default_tracker, default_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, default_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, default_domain
):
Expand Down

0 comments on commit ef7cc66

Please sign in to comment.