Skip to content

Commit 3d57940

Browse files
Template name in bot uttered event (RasaHQ#5836)
* Added template name when creating BotUttered inside ActionUtterTemplate Signed-off-by: Vinicius Lima <[email protected]> * Adapted tests to recieve template name inside BotUttered metadata Signed-off-by: Vinicius Lima <[email protected]> * Added template name to all actions that generate BotUttered event Signed-off-by: Vinicius Lima <[email protected]> * renamed remaining references of domain templates to responses * fixed tests * renamed key to template_name * fixed tests * Update 4745.feature.rst Co-authored-by: Vinicius Lima <[email protected]>
1 parent 8ad8b21 commit 3d57940

File tree

11 files changed

+104
-54
lines changed

11 files changed

+104
-54
lines changed

changelog/4745.feature.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Added template name to the metadata of bot utterance events.
2+
3+
``BotUttered`` event contains a ``template_name`` property in its metadata for any
4+
new bot message.

docs/core/domains.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ can be sent like so:
202202
Channel-Specific Responses
203203
--------------------------
204204

205-
For each response, you can have multiple **response templates** (see :ref:`variations`).
206-
If you have certain response templates that you would like sent only to specific
205+
For each response, you can have multiple **response variations** (see :ref:`variations`).
206+
If you have certain response variations that you would like sent only to specific
207207
channels, you can specify this with the ``channel:`` key. The value should match
208208
the name defined in the ``name()`` method of the channel's ``OutputChannel``
209209
class. Channel-specific responses are especially useful if creating custom
@@ -228,10 +228,10 @@ output payloads that will only work in certain channels.
228228
payload: '/inform{"game": "fortnite"}'
229229
230230
Each time your bot looks for responses, it will first check to see if there
231-
are any channel-specific response templates for the connected channel. If there are, it
232-
will choose **only** from these response templates. If no channel-specific response templates are
233-
found, it will choose from any response templates that do not have a defined ``channel``.
234-
Therefore, it is good practice to always have at least one response template for each
231+
are any channel-specific response variations for the connected channel. If there are, it
232+
will choose **only** from these response variations. If no channel-specific response variations are
233+
found, it will choose from any response variations that do not have a defined ``channel``.
234+
Therefore, it is good practice to always have at least one response variation for each
235235
response that has no ``channel`` specified so that your bot can respond in all
236236
environments, including in the shell and in interactive learning.
237237

@@ -284,7 +284,7 @@ Variations
284284
----------
285285

286286
If you want to randomly vary the response sent to the user, you can list
287-
multiple **response templates** and Rasa will randomly pick one of them, e.g.:
287+
multiple **response variations** and Rasa will randomly pick one of them, e.g.:
288288

289289
.. code-block:: yaml
290290

docs/core/retrieval-actions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Next, include response texts for all retrieval intents in a **separate** trainin
9797
The retrieval model is trained separately as part of the NLU training pipeline to select the correct response.
9898
One important thing to remember is that the retrieval model uses the text of the response messages
9999
to select the correct one. If you change the text of these responses, you have to retrain your retrieval model!
100-
This is a key difference to the response templates in your domain file.
100+
This is a key difference to the responses defined in your domain file.
101101

102102
.. note::
103103
The file containing response texts must exist as a separate file inside the training data directory passed

docs/dialogue-elements/dialogue-elements.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ issues and new features.
1616

1717
- highest level: user goals
1818
- middle level: dialogue elements
19-
- lowest level: intents, entities, actions, slots, and templates.
19+
- lowest level: intents, entities, actions, slots, and responses.
2020

2121

2222

docs/user-guide/rasa-tutorial.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,16 @@ So what do the different parts mean?
187187
+---------------+-------------------------------------------------------------+
188188
| ``actions`` | things your assistant can do and say |
189189
+---------------+-------------------------------------------------------------+
190-
| ``templates`` | template strings for the things your assistant can say |
190+
| ``responses`` | responses for the things your assistant can say |
191191
+---------------+-------------------------------------------------------------+
192192

193193

194194
**How does this fit together?**
195195
Rasa Core's job is to choose the right action to execute at each step
196196
of the conversation. In this case, our actions simply send a message to the user.
197197
These simple utterance actions are the ``actions`` in the domain that start
198-
with ``utter_``. The assistant will respond with a message based on a template
199-
from the ``templates`` section. See :ref:`custom-actions`
198+
with ``utter_``. The assistant will respond with a message based on a response
199+
from the ``responses`` section. See :ref:`custom-actions`
200200
to build actions that do more than just send a message.
201201

202202

rasa/core/actions/action.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,12 @@ async def run(
225225
return []
226226

227227
logger.debug(f"Picking response from selector of type {query_key}")
228+
selected = response_selector_properties[query_key]
228229
message = {
229-
"text": response_selector_properties[query_key][
230-
OPEN_UTTERANCE_PREDICTION_KEY
231-
]["name"]
230+
"text": selected[OPEN_UTTERANCE_PREDICTION_KEY]["name"],
231+
"template_name": selected["full_retrieval_intent"],
232232
}
233+
233234
return [create_bot_utterance(message)]
234235

235236
def name(self) -> Text:
@@ -262,10 +263,11 @@ async def run(
262263
if message is None:
263264
if not self.silent_fail:
264265
logger.error(
265-
"Couldn't create message for template '{}'."
266+
"Couldn't create message for response '{}'."
266267
"".format(self.template_name)
267268
)
268269
return []
270+
message["template_name"] = self.template_name
269271

270272
return [create_bot_utterance(message)]
271273

@@ -292,7 +294,7 @@ async def run(
292294
tracker: "DialogueStateTracker",
293295
domain: "Domain",
294296
) -> List[Event]:
295-
# only utter the template if it is available
297+
# only utter the response if it is available
296298
evts = await super().run(output_channel, nlg, tracker, domain)
297299

298300
return evts + [UserUtteranceReverted(), UserUtteranceReverted()]
@@ -320,7 +322,7 @@ async def run(
320322
class ActionRestart(ActionUtterTemplate):
321323
"""Resets the tracker to its initial state.
322324
323-
Utters the restart template if available."""
325+
Utters the restart response if available."""
324326

325327
def name(self) -> Text:
326328
return ACTION_RESTART_NAME
@@ -337,7 +339,7 @@ async def run(
337339
) -> List[Event]:
338340
from rasa.core.events import Restarted
339341

340-
# only utter the template if it is available
342+
# only utter the response if it is available
341343
evts = await super().run(output_channel, nlg, tracker, domain)
342344

343345
return evts + [Restarted()]
@@ -405,7 +407,7 @@ async def run(
405407
) -> List[Event]:
406408
from rasa.core.events import UserUtteranceReverted
407409

408-
# only utter the template if it is available
410+
# only utter the response if it is available
409411
evts = await super().run(output_channel, nlg, tracker, domain)
410412

411413
return evts + [UserUtteranceReverted()]
@@ -505,6 +507,7 @@ async def _utter_responses(
505507
)
506508
if not draft:
507509
continue
510+
draft["template_name"] = template
508511
else:
509512
draft = {}
510513

@@ -719,6 +722,7 @@ async def run(
719722
{"title": "Yes", "payload": f"/{intent_to_affirm}"},
720723
{"title": "No", "payload": f"/{USER_INTENT_OUT_OF_SCOPE}"},
721724
],
725+
"template_name": self.name(),
722726
}
723727

724728
return [create_bot_utterance(message)]

rasa/core/domain.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,29 +368,29 @@ def collect_templates(
368368
validated_variations = []
369369
if template_variations is None:
370370
raise InvalidDomain(
371-
"Utterance '{}' does not have any defined templates.".format(
371+
"Response '{}' does not have any defined variations.".format(
372372
template_key
373373
)
374374
)
375375

376376
for t in template_variations:
377377

378-
# templates should be a dict with options
378+
# responses should be a dict with options
379379
if isinstance(t, str):
380380
raise_warning(
381-
f"Templates should not be strings anymore. "
382-
f"Utterance template '{template_key}' should contain "
381+
f"Responses should not be strings anymore. "
382+
f"Response '{template_key}' should contain "
383383
f"either a '- text: ' or a '- custom: ' "
384-
f"attribute to be a proper template.",
384+
f"attribute to be a proper response.",
385385
FutureWarning,
386-
docs=DOCS_URL_DOMAINS + "#utterance-templates",
386+
docs=DOCS_URL_DOMAINS + "#responses",
387387
)
388388
validated_variations.append({"text": t})
389389
elif "text" not in t and "custom" not in t:
390390
raise InvalidDomain(
391-
f"Utter template '{template_key}' needs to contain either "
391+
f"Response '{template_key}' needs to contain either "
392392
f"'- text: ' or '- custom: ' attribute to be a proper "
393-
f"template."
393+
f"response."
394394
)
395395
else:
396396
validated_variations.append(t)
@@ -1062,11 +1062,11 @@ def check_missing_templates(self) -> None:
10621062
if missing_templates:
10631063
for template in missing_templates:
10641064
raise_warning(
1065-
f"Utterance '{template}' is listed as an "
1066-
f"action in the domain file, but there is "
1067-
f"no matching utterance template. Please "
1065+
f"Action '{template}' is listed as a "
1066+
f"response action in the domain file, but there is "
1067+
f"no matching response defined. Please "
10681068
f"check your domain.",
1069-
docs=DOCS_URL_DOMAINS + "#utterance-templates",
1069+
docs=DOCS_URL_DOMAINS + "#responses",
10701070
)
10711071

10721072
def is_empty(self) -> bool:

rasa/core/training/interactive.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ async def send_action(
200200
if action_name in NEW_TEMPLATES:
201201
warning_questions = questionary.confirm(
202202
f"WARNING: You have created a new action: '{action_name}', "
203-
f"with matching template: '{[*NEW_TEMPLATES[action_name]][0]}'. "
203+
f"with matching response: '{[*NEW_TEMPLATES[action_name]][0]}'. "
204204
f"This action will not return its message in this session, "
205-
f"but the new utterance will be saved to your domain file "
205+
f"but the new response will be saved to your domain file "
206206
f"when you exit and save this session. "
207207
f"You do not need to do anything further."
208208
)
@@ -358,10 +358,8 @@ async def _request_free_text_utterance(
358358
) -> Text:
359359

360360
question = questionary.text(
361-
message=(
362-
f"Please type the message for your new utterance template '{action}':"
363-
),
364-
validate=io_utils.not_empty_validator("Please enter a template message"),
361+
message=(f"Please type the message for your new bot response '{action}':"),
362+
validate=io_utils.not_empty_validator("Please enter a response"),
365363
)
366364
return await _ask_questions(question, conversation_id, endpoint)
367365

rasa/model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def __init__(
8888
Args:
8989
nlu: `True` if the NLU model should be retrained.
9090
core: `True` if the Core model should be retrained.
91-
nlg: `True` if the templates in the domain should be updated.
91+
nlg: `True` if the responses in the domain should be updated.
9292
force_training: `True` if a training of all parts is forced.
9393
"""
9494
self.nlu = nlu
@@ -107,7 +107,7 @@ def should_retrain_core(self) -> bool:
107107
return self.force_training or self.core
108108

109109
def should_retrain_nlg(self) -> bool:
110-
"""Check if the templates have to be updated."""
110+
"""Check if the responses have to be updated."""
111111

112112
return self.should_retrain_core() or self.nlg
113113

@@ -286,7 +286,7 @@ async def model_fingerprint(file_importer: "TrainingDataImporter") -> Fingerprin
286286
nlu_data = await file_importer.get_nlu_data()
287287

288288
domain_dict = domain.as_dict()
289-
templates = domain_dict.pop("responses")
289+
responses = domain_dict.pop("responses")
290290
domain_without_nlg = Domain.from_dict(domain_dict)
291291

292292
return {
@@ -300,7 +300,7 @@ async def model_fingerprint(file_importer: "TrainingDataImporter") -> Fingerprin
300300
config, include_keys=CONFIG_MANDATORY_KEYS_NLU
301301
),
302302
FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY: hash(domain_without_nlg),
303-
FINGERPRINT_NLG_KEY: get_dict_hash(templates),
303+
FINGERPRINT_NLG_KEY: get_dict_hash(responses),
304304
FINGERPRINT_NLU_DATA_KEY: hash(nlu_data),
305305
FINGERPRINT_STORIES_KEY: hash(stories),
306306
FINGERPRINT_TRAINED_AT_KEY: time.time(),

0 commit comments

Comments
 (0)