I want to pass the role information and value information of the entity at the same time (because the mapping of slots requires role information) but the official website only gives an example of the payload passing value information: '/my_intent{"my_entity": "my_value"}'
I’m trying to add the entity’s role information like this '/my_intent{"my_entity#entity_role": "my_value"}', but it doesn’t work.
Also tried '/my_intent{"my_entity": {"value": "my_value", "role": "entity_role"}}' doesn’t work either.
I see someone asking a similar question in 2021, but not getting a valid answer.
If anybody can shed some lights on the above, that would be greatly appreciated!
I modified the _entities_from_regex_match in yaml_story_reader.py to support '/my_intent {"my_entity#entity_role": "my_value"}' payload.
def _entities_from_regex_match(
match: Match, domain: Domain, extractor_name: Optional[Text]
) -> List[Dict[Text, Any]]:
"""Extracts the optional entity information from the given pattern match.
If no entities are specified or if the extraction fails, then an empty list
is returned.
Args:
match: a match produced by `self.pattern`
domain: the domain
extractor_name: A extractor name which should be added for the entities
Returns:
some list of entities
"""
entities_str = match.group(ENTITIES)
if entities_str is None:
return []
try:
parsed_entities = json.loads(entities_str)
if not isinstance(parsed_entities, dict):
raise ValueError(
f"Parsed value isn't a json object "
f"(instead parser found '{type(parsed_entities)}')"
)
except (JSONDecodeError, ValueError) as e:
rasa.shared.utils.io.raise_warning(
f"Failed to parse arguments in line '{match.string}'. "
f"Failed to decode parameters as a json object (dict). "
f"Make sure the intent is followed by a proper json object (dict). "
f"Continuing without entities. "
f"Error: {e}",
docs=DOCS_URL_STORIES,
)
parsed_entities = dict()
# validate the given entity types
if domain:
entity_types = {key.split('#')[0] for key in parsed_entities.keys()}
# entity_types = set(parsed_entities.keys())
unknown_entity_types = entity_types.difference(domain.entities)
if unknown_entity_types:
rasa.shared.utils.io.raise_warning(
f"Failed to parse arguments in line '{match.string}'. "
f"Expected entities from {domain.entities} "
f"but found {unknown_entity_types}. "
f"Continuing without unknown entity types. ",
docs=DOCS_URL_STORIES,
)
parsed_entities = {
key: value
for key, value in parsed_entities.items()
if key not in unknown_entity_types
}
logger.info(f'parsed_entities:{parsed_entities}')
# convert them into the list of dictionaries that we expect
entities: List[Dict[Text, Any]] = []
default_properties = {}
if extractor_name:
default_properties = {EXTRACTOR: extractor_name}
for entity_type, entity_values in parsed_entities.items():
if not isinstance(entity_values, list):
entity_values = [entity_values]
parts = entity_type.split("#")
entity_type_temp = parts[0]
entity_role = parts[1] if len(parts) > 1 else None
for entity_value in entity_values:
entity_data = {
ENTITY_ATTRIBUTE_TYPE: entity_type_temp,
ENTITY_ATTRIBUTE_VALUE: entity_value,
ENTITY_ATTRIBUTE_START: match.start(ENTITIES),
ENTITY_ATTRIBUTE_END: match.end(ENTITIES),
**default_properties,
}
if entity_role:
# logger.debug('an entity with role')
entity_data[ENTITY_ATTRIBUTE_ROLE] = entity_role
entities.append(entity_data)
return entities
It has not been fully tested and there may be some problems.
Thanks again!