示例#1
0
    def validate_and_store_domain_yaml(
        self,
        domain_yaml: Text,
        project_id: Text = config.project_name,
        path: Optional[Text] = None,
        store_responses: bool = False,
        username: Text = config.default_username,
        should_dump_domain: bool = True,
    ) -> Optional[Text]:
        """Store a domain from a yaml dump.

        Args:
            domain_yaml: The domain as yaml.
            project_id: The project_id the domain belongs to.
            path: File path of the domain.
            store_responses: Whether or not to store responses.
            username: Username performing this operation.
            should_dump_domain: Whether to dump domain to disk after storing it.
        """
        # create Rasa domain object (validation happens automatically)
        domain = RasaDomain.from_yaml(domain_yaml)
        cleaned = domain.cleaned_domain()
        self.store_domain(cleaned, project_id, path, store_responses, username)

        if should_dump_domain:
            background_dump_service.add_domain_change()

        return self.get_domain_yaml(project_id)
示例#2
0
 async def delete_response(request: Request,
                           response_id: int) -> HTTPResponse:
     deleted = _nlg_service(request).delete_response(response_id)
     if deleted:
         background_dump_service.add_domain_change()
         return response.text("", http.HTTPStatus.NO_CONTENT)
     return rasa_x_utils.error(
         http.HTTPStatus.NOT_FOUND,
         "ResponseNotFound",
         "Response could not be found.",
     )
示例#3
0
    def add_items_to_domain(
        self,
        actions: Optional[Iterable[Text]] = None,
        intents: Optional[Iterable[Text]] = None,
        entities: Optional[Iterable[Text]] = None,
        slots: Optional[Iterable[Text]] = None,
        project_id: Text = config.project_name,
        dump_data: bool = False,
        origin: Optional[Text] = None,
    ) -> Optional[Dict[Text, Any]]:
        """Add actions, intents, slots and entities to a project's domain. Create a new
        domain if none exists first.

        Args:
            actions: Set of action names to be added.
            intents: Set of intent names to be added.
            entities: Set of entity names to be added.
            slots: Set of the slot names to be added.
            project_id: The project id of the domain.
            dump_data: Whether to dump the domain.
            origin: origin of the domain changes to be printed as user info.

        Returns:
            Updated domain as dict.
        """
        domain = self._get_or_create_domain(project_id)

        if actions:
            self._add_actions_to_domain(domain, project_id, actions, origin)
        if slots:
            self._add_slots_to_domain(domain, project_id, slots, origin)
        if intents:
            self._add_intents_to_domain(domain, project_id, intents, origin)
        if entities:
            self._add_entities_to_domain(domain, project_id, entities, origin)

        if dump_data and any([actions, slots, entities, intents]):
            background_dump_service.add_domain_change()

        return self.get_domain(project_id)
示例#4
0
    async def add_response(request: Request, user: Dict[Text,
                                                        Any]) -> HTTPResponse:
        rjs = request.json
        domain_id = _domain_service(request).get_domain_id()

        try:
            saved_response = _nlg_service(request).save_response(
                rjs, user["username"], domain_id=domain_id)
            background_dump_service.add_domain_change()

            return response.json(saved_response, http.HTTPStatus.CREATED)

        except (AttributeError, ValueError) as e:
            status_code = (http.HTTPStatus.UNPROCESSABLE_ENTITY if isinstance(
                e, AttributeError) else http.HTTPStatus.BAD_REQUEST)

            return rasa_x_utils.error(
                status_code,
                "WrongResponse",
                "Could not add the specified response.",
                str(e),
            )
示例#5
0
    async def modify_response(request: Request, response_id: int,
                              user: Dict[Text, Any]) -> HTTPResponse:
        rjs = request.json
        try:
            updated_response = _nlg_service(request).update_response(
                response_id, rjs, user["username"])
            background_dump_service.add_domain_change()
            return response.json(updated_response.as_dict())

        except (KeyError, AttributeError, ValueError) as e:
            if isinstance(e, KeyError):
                status_code = http.HTTPStatus.NOT_FOUND
            elif isinstance(e, AttributeError):
                status_code = http.HTTPStatus.UNPROCESSABLE_ENTITY
            else:
                status_code = http.HTTPStatus.BAD_REQUEST

            return rasa_x_utils.error(
                status_code,
                "WrongResponse",
                "Could not modify the specified response.",
                str(e),
            )
示例#6
0
    async def update_responses(request: Request,
                               user: Dict[Text, Any]) -> HTTPResponse:
        """Delete old bot responses and replace them with the responses in the
        payload."""

        rjs = request.json
        domain_id = _domain_service(request).get_domain_id()
        try:
            inserted_count = _nlg_service(request).replace_responses(
                rjs, user["username"], domain_id=domain_id)
            background_dump_service.add_domain_change()

            return response.text(
                f"Successfully uploaded {inserted_count} responses.")

        except AttributeError as e:
            status_code = (http.HTTPStatus.UNPROCESSABLE_ENTITY if isinstance(
                e, AttributeError) else http.HTTPStatus.BAD_REQUEST)
            return rasa_x_utils.error(
                status_code,
                "WrongResponse",
                "Could not update the specified response.",
                str(e),
            )