Skip to content
This repository was archived by the owner on Apr 28, 2021. It is now read-only.

Commit f6c9ca0

Browse files
author
Nathan Zylbersztejn
authored
Merge pull request #8 from botfront/feat/templates
Feat/templates
2 parents c8bec46 + e1dda5c commit f6c9ca0

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

rasa/core/actions/action.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ async def _utter_responses(
368368
responses += resolved
369369
else:
370370
responses += [resolved]
371+
else:
372+
responses += [response]
371373

372374
for response in responses:
373375
if "template" in response:

rasa/core/domain.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def from_yaml(cls, yaml: Text) -> "Domain":
110110

111111
@classmethod
112112
def from_dict(cls, data: Dict) -> "Domain":
113-
utter_templates = cls.collect_templates(data.get("templates", {}))
113+
utter_templates = cls.collect_bf_templates(data.get("templates", {}))
114114
slots = cls.collect_slots(data.get("slots", {}))
115115
additional_arguments = data.get("config", {})
116116
intents = data.get("intents", {})
@@ -238,6 +238,15 @@ def collect_intent_properties(
238238
intent_properties.update(intent)
239239
return intent_properties
240240

241+
@staticmethod
242+
def collect_bf_templates(
243+
yml_templates: Dict[Text, Dict[Text, List[List[Any]]]]
244+
) -> Dict[Text, Dict[Text, List[List[Dict[Text, Any]]]]]:
245+
"""template name -> lang -> sequence of templates
246+
Placeholder for actual validation
247+
"""
248+
return yml_templates
249+
241250
@staticmethod
242251
def collect_templates(
243252
yml_templates: Dict[Text, List[Any]]

rasa/core/nlg/template.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import re
44
from collections import defaultdict
5+
from rasa.cli.utils import bcolors
56

67
from rasa.core.trackers import DialogueStateTracker
78
from typing import Text, Any, Dict, Optional, List
@@ -10,14 +11,20 @@
1011

1112
logger = logging.getLogger(__name__)
1213

14+
class InvalidNLGRequest(Exception):
15+
def __init__(self, message):
16+
self.message = message
17+
18+
def __str__(self):
19+
return bcolors.FAIL + self.message + bcolors.ENDC
1320

1421
class TemplatedNaturalLanguageGenerator(NaturalLanguageGenerator):
1522
"""Natural language generator that generates messages based on templates.
1623
1724
The templates can use variables to customize the utterances based on the
1825
state of the dialogue."""
1926

20-
def __init__(self, templates: Dict[Text, List[Dict[Text, Any]]]) -> None:
27+
def __init__(self, templates: Dict[Text, Dict[Text, List[List[Dict[Text, Any]]]]]) -> None:
2128
self.templates = templates
2229

2330
def _templates_for_utter_action(self, utter_action, output_channel):
@@ -71,10 +78,28 @@ async def generate(
7178
"""Generate a response for the requested template."""
7279

7380
filled_slots = tracker.current_slot_values()
74-
return self.generate_from_slots(
81+
return self.generate_from_bf_template(
7582
template_name, filled_slots, output_channel, **kwargs
7683
)
7784

85+
def generate_from_bf_template(
86+
self,
87+
template_name: Text,
88+
filled_slots: Dict[Text, Any],
89+
output_channel: Text,
90+
**kwargs: Any
91+
) -> Optional[Dict[Text, Any]]:
92+
93+
language = kwargs.get("language", None)
94+
if not language:
95+
raise InvalidNLGRequest("Generator expected a language to return template")
96+
if template_name not in self.templates:
97+
return None
98+
99+
return [self._fill_template_text(
100+
copy.deepcopy(template), filled_slots, **kwargs
101+
) for template in self.templates[template_name][language]]
102+
78103
def generate_from_slots(
79104
self,
80105
template_name: Text,

0 commit comments

Comments
 (0)