-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
optional slot mappings #7171
Merged
Merged
optional slot mappings #7171
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
49acec2
fix typo
wochinge bd7e116
add missing types
wochinge cea6483
make forms overridable
wochinge d278dea
describe form overriding
wochinge 432771f
polish
wochinge 2830354
drop retrieval intent parameter
wochinge bfd4a20
explain how to migrate forms iteratively
wochinge 7a69d60
Apply suggestions from code review
wochinge 9f029a3
adapt Domain.empty to new format
wochinge 7373ae2
Merge branch 'master' into optional-mappings
rasabot 2cc9220
initialize form with Dict by default
wochinge f17978e
Merge branch 'master' into optional-mappings
rasabot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Slot mappings for [Forms](forms.mdx) in the domain are now optional. If you do not | ||
provide any slot mappings as part of the domain, you need to provide | ||
[custom slot mappings](forms.mdx#custom-slot-mappings) through a custom action. | ||
A form without slot mappings is specified as follows: | ||
|
||
```rasa-yaml | ||
forms: | ||
my_form: | ||
# no mappings | ||
``` | ||
|
||
The action for [forms](forms.mdx) can now be overridden by defining a custom action | ||
with the same name as the form. This can be used to keep using the deprecated | ||
Rasa Open Source `FormAction` which is implemented within the Rasa SDK. Note that it is | ||
**not** recommended to override the form action for anything else than using the | ||
deprecated Rasa SDK `FormAction`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -104,7 +104,7 @@ class Domain: | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||||||||||
def empty(cls) -> "Domain": | ||||||||||||||||||||||||||||||
return cls([], [], [], {}, [], []) | ||||||||||||||||||||||||||||||
return cls([], [], [], {}, [], {}) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||||||||||
def load(cls, paths: Union[List[Union[Path, Text]], Text, Path]) -> "Domain": | ||||||||||||||||||||||||||||||
|
@@ -507,13 +507,14 @@ def __init__( | |||||||||||||||||||||||||||||
self.intent_properties = self.collect_intent_properties( | ||||||||||||||||||||||||||||||
intents, self.entities, self.roles, self.groups | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
self.overriden_default_intents = self._collect_overridden_default_intents( | ||||||||||||||||||||||||||||||
self.overridden_default_intents = self._collect_overridden_default_intents( | ||||||||||||||||||||||||||||||
intents | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
self.forms: Dict[Text, Any] = {} | ||||||||||||||||||||||||||||||
self.form_names: List[Text] = [] | ||||||||||||||||||||||||||||||
self._initialize_forms(forms) | ||||||||||||||||||||||||||||||
self.form_names, self.forms, overridden_form_actions = self._initialize_forms( | ||||||||||||||||||||||||||||||
forms | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
action_names += overridden_form_actions | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
self.slots = slots | ||||||||||||||||||||||||||||||
self.templates = templates | ||||||||||||||||||||||||||||||
|
@@ -528,7 +529,11 @@ def __init__( | |||||||||||||||||||||||||||||
# includes all actions (custom, utterance, default actions and forms) | ||||||||||||||||||||||||||||||
self.action_names = ( | ||||||||||||||||||||||||||||||
self._combine_user_with_default_actions(self.user_actions) | ||||||||||||||||||||||||||||||
+ self.form_names | ||||||||||||||||||||||||||||||
+ [ | ||||||||||||||||||||||||||||||
form_name | ||||||||||||||||||||||||||||||
for form_name in self.form_names | ||||||||||||||||||||||||||||||
if form_name not in self._custom_actions | ||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||
+ self.action_texts | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
@@ -569,34 +574,55 @@ def _collect_overridden_default_intents( | |||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
return sorted(intent_names & set(rasa.shared.core.constants.DEFAULT_INTENTS)) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def _initialize_forms(self, forms: Union[Dict[Text, Any], List[Text]]) -> None: | ||||||||||||||||||||||||||||||
"""Initialize the domain's `self.form` and `self.form_names` attributes. | ||||||||||||||||||||||||||||||
@staticmethod | ||||||||||||||||||||||||||||||
def _initialize_forms( | ||||||||||||||||||||||||||||||
forms: Union[Dict[Text, Any], List[Text]] | ||||||||||||||||||||||||||||||
) -> Tuple[List[Text], Dict[Text, Any], List[Text]]: | ||||||||||||||||||||||||||||||
"""Retrieves the initial values for the Domain's form fields. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Args: | ||||||||||||||||||||||||||||||
forms: Form names (if forms are a list) or a form dictionary. Forms | ||||||||||||||||||||||||||||||
provided in dictionary format have the form names as keys, and either | ||||||||||||||||||||||||||||||
empty dictionaries as values, or objects containing | ||||||||||||||||||||||||||||||
`SlotMapping`s. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Returns: | ||||||||||||||||||||||||||||||
The form names, a mapping of form names and slot mappings, and custom | ||||||||||||||||||||||||||||||
actions. | ||||||||||||||||||||||||||||||
Returning custom actions for each forms means that Rasa Open Source should | ||||||||||||||||||||||||||||||
not use the default `FormAction` for the forms, but rather a custom action | ||||||||||||||||||||||||||||||
for it. This can e.g. be used to run the deprecated Rasa Open Source 1 | ||||||||||||||||||||||||||||||
`FormAction` which is implemented in the Rasa SDK. | ||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||
if not forms: | ||||||||||||||||||||||||||||||
# empty dict or empty list | ||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||
elif isinstance(forms, dict): | ||||||||||||||||||||||||||||||
if isinstance(forms, dict): | ||||||||||||||||||||||||||||||
# dict with slot mappings | ||||||||||||||||||||||||||||||
self.forms = forms | ||||||||||||||||||||||||||||||
self.form_names = list(forms.keys()) | ||||||||||||||||||||||||||||||
elif isinstance(forms, list) and isinstance(forms[0], str): | ||||||||||||||||||||||||||||||
# list of form names | ||||||||||||||||||||||||||||||
self.forms = {form_name: {} for form_name in forms} | ||||||||||||||||||||||||||||||
self.form_names = forms | ||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||
return list(forms.keys()), forms, [] | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if isinstance(forms, list) and (not forms or isinstance(forms[0], str)): | ||||||||||||||||||||||||||||||
# list of form names (Rasa Open Source 1 format) | ||||||||||||||||||||||||||||||
rasa.shared.utils.io.raise_warning( | ||||||||||||||||||||||||||||||
f"The `forms` section in the domain needs to contain a dictionary. " | ||||||||||||||||||||||||||||||
f"Instead found an object of type '{type(forms)}'.", | ||||||||||||||||||||||||||||||
"The `forms` section in the domain used the old Rasa Open Source 1 " | ||||||||||||||||||||||||||||||
"list format to define forms. Rasa Open Source will be configured to " | ||||||||||||||||||||||||||||||
"use the deprecated `FormAction` within the Rasa SDK. If you want to " | ||||||||||||||||||||||||||||||
"use the new Rasa Open Source 2 `FormAction` adapt your `forms` " | ||||||||||||||||||||||||||||||
"section as described in the documentation. Support for the " | ||||||||||||||||||||||||||||||
"deprecated `FormAction` in the Rasa SDK will be removed in Rasa Open " | ||||||||||||||||||||||||||||||
"Source 3.0.", | ||||||||||||||||||||||||||||||
Comment on lines
+604
to
+610
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need the |
||||||||||||||||||||||||||||||
docs=rasa.shared.constants.DOCS_URL_FORMS, | ||||||||||||||||||||||||||||||
category=FutureWarning, | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
return forms, {form_name: {} for form_name in forms}, forms | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
rasa.shared.utils.io.raise_warning( | ||||||||||||||||||||||||||||||
f"The `forms` section in the domain needs to contain a dictionary. " | ||||||||||||||||||||||||||||||
f"Instead found an object of type '{type(forms)}'.", | ||||||||||||||||||||||||||||||
docs=rasa.shared.constants.DOCS_URL_FORMS, | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return [], {}, [] | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def __hash__(self) -> int: | ||||||||||||||||||||||||||||||
"""Returns a unique hash for the domain.""" | ||||||||||||||||||||||||||||||
self_as_dict = self.as_dict() | ||||||||||||||||||||||||||||||
self_as_dict[ | ||||||||||||||||||||||||||||||
KEY_INTENTS | ||||||||||||||||||||||||||||||
|
@@ -1032,7 +1058,7 @@ def _transform_intents_for_file(self) -> List[Union[Text, Dict[Text, Any]]]: | |||||||||||||||||||||||||||||
for intent_name, intent_props in intent_properties.items(): | ||||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||||
intent_name in rasa.shared.core.constants.DEFAULT_INTENTS | ||||||||||||||||||||||||||||||
and intent_name not in self.overriden_default_intents | ||||||||||||||||||||||||||||||
and intent_name not in self.overridden_default_intents | ||||||||||||||||||||||||||||||
): | ||||||||||||||||||||||||||||||
# Default intents should be not dumped with the domain | ||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is equivalent to (
X and not (X and Y)
):which in turn is equivalent to (
X and not Y
):So then maybe we could just write:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assigned names to the single checks to make their meaning more explicit.
Developers might wonder what
name not in domain.user_actions
means whileuser_overrode_form_action
conveys way more information about this. What do you think?