def _fill_template( self, template: Dict[Text, Any], filled_slots: Optional[Dict[Text, Any]] = None, **kwargs: Any, ) -> Dict[Text, Any]: """"Combine slot values and key word arguments to fill templates.""" # Getting the slot values in the template variables template_vars = self._template_variables(filled_slots, kwargs) keys_to_interpolate = [ "text", "image", "custom", "buttons", "attachment", "quick_replies", ] if template_vars: for key in keys_to_interpolate: if key in template: template[key] = interpolator.interpolate( template[key], template_vars) return template
def _fill_template(self, template: Dict[Text, Any], filled_slots: Optional[Dict[Text, Any]] = None, **kwargs: Any) -> Dict[Text, Any]: """"Combine slot values and key word arguments to fill templates.""" # Getting the slot values in the template variables template_vars = self._template_variables(filled_slots, kwargs) # Filling the template variables in the template text if template_vars: if "text" in template: template["text"] = interpolate(template["text"], template_vars) elif "custom" in template: template["custom"] = interpolate(template["custom"], template_vars) return template
async def generate( self, template_name: Text, tracker: DialogueStateTracker, output_channel: Text, **kwargs: Any, ) -> List[Dict[Text, Any]]: fallback_language_slot = tracker.slots.get("fallback_language") fallback_language = (fallback_language_slot.initial_value if fallback_language_slot else None) language = tracker.latest_message.metadata.get( "language") or fallback_language body = nlg_request_format( template_name, tracker, output_channel, **kwargs, language=language, projectId=os.environ.get("BF_PROJECT_ID"), ) logger.debug("Requesting NLG for {} from {}." "".format(template_name, self.nlg_endpoint.url)) try: if "graphql" in self.nlg_endpoint.url: from sgqlc.endpoint.http import HTTPEndpoint logging.getLogger("sgqlc.endpoint.http").setLevel( logging.WARNING) api_key = os.environ.get("API_KEY") headers = [{"Authorization": api_key}] if api_key else [] response = HTTPEndpoint(self.nlg_endpoint.url, *headers)(NLG_QUERY, body) if response.get("errors"): raise urllib.error.URLError(", ".join( [e.get("message") for e in response.get("errors")])) response = response.get("data", {}).get("getResponse", {}) rewrite_url(response, self.url_substitution_patterns) if "customText" in response: response["text"] = response.pop("customText") if "customImage" in response: response["image"] = response.pop("customImage") if "customQuickReplies" in response: response["quick_replies"] = response.pop( "customQuickReplies") if "customButtons" in response: response["buttons"] = response.pop("customButtons") if "customElements" in response: response["elements"] = response.pop("customElements") if "customAttachment" in response: response["attachment"] = response.pop("customAttachment") metadata = response.pop("metadata", {}) or {} for key in metadata: response[key] = metadata[key] keys_to_interpolate = [ "text", "image", "custom", "buttons", "attachment", "quick_replies", ] for key in keys_to_interpolate: if key in response: response[key] = interpolate( response[key], tracker.current_slot_values()) else: response = await self.nlg_endpoint.request( method="post", json=body, timeout=DEFAULT_REQUEST_TIMEOUT) response = response[ 0] # legacy route, use first message in seq except urllib.error.URLError as e: message = e.reason logger.error( f"NLG web endpoint at {self.nlg_endpoint.url} returned errors: {message}" ) return {"text": template_name} if self.validate_response(response): return response else: logger.error( f"NLG web endpoint at {self.nlg_endpoint.url} returned an invalid response." ) return {"text": template_name}