async def flag_message(request: Request, conversation_id: Text, message_timestamp: float) -> HTTPResponse: _event_service(request).add_flagged_message(conversation_id, message_timestamp) telemetry.track(telemetry.MESSAGE_FLAGGED_EVENT, {"set_flag": True}) return response.text("", HTTPStatus.CREATED)
async def delete_flag_from_conversation( request: Request, conversation_id: Text, message_timestamp: float) -> HTTPResponse: _event_service(request).delete_flagged_message(conversation_id, message_timestamp) telemetry.track(telemetry.MESSAGE_FLAGGED_EVENT, {"set_flag": False}) return response.text("", HTTPStatus.OK)
async def tag_model(request, project_id, model, tag): try: await _model_service(request).tag_model(project_id, model, tag) if tag == RASA_PRODUCTION_ENVIRONMENT: telemetry.track(telemetry.MODEL_PROMOTED_EVENT) return response.text("", http.HTTPStatus.NO_CONTENT) except ValueError as e: return rasa_x_utils.error( http.HTTPStatus.NOT_FOUND, "ModelTagError", f"Failed to tag model '{model}'.", details=e, )
async def telemetry_event(request: Request) -> HTTPResponse: """Attempts to track a telemetry event. The event will only be tracked if telemetry is enabled. Args: request: Received HTTP request. Returns: HTTP 204 response. """ rj = request.json telemetry.track(rj["event_name"], rj.get("properties"), rj.get("context")) return response.text("", 204)
async def train_model(request, project_id): stack_services = SettingsService( request[REQUEST_DB_SESSION_KEY]).stack_services(project_id) environment = utils.deployment_environment_from_request( request, "worker") stack_service = stack_services[environment] try: training_start = time.time() content = await stack_service.start_training_process() telemetry.track(telemetry.MODEL_TRAINED_EVENT) model_name = await _model_service(request).save_trained_model( project_id, content) nlg_service = NlgService(request[REQUEST_DB_SESSION_KEY]) nlg_service.mark_responses_as_used(training_start) return response.json({ "info": "New model trained.", "model": model_name }) except FileExistsError as e: logger.error(e) return response.json( { "info": "Model already exists.", "path": str(e) }, http.HTTPStatus.CREATED, ) except ValueError as e: logger.error(e) return rasa_x_utils.error( http.HTTPStatus.BAD_REQUEST, "StackTrainingFailed", "Unable to train on data containing retrieval intents.", details=e, ) except ClientError as e: logger.error(e) return rasa_x_utils.error( http.HTTPStatus.INTERNAL_SERVER_ERROR, "StackTrainingFailed", "Failed to train a Rasa model.", details=e, )
def main( args: argparse.Namespace, project_path: Text, data_path: Text, token: Optional[Text] = None, config_path: Optional[Text] = config.default_config_path, ) -> None: """Start Rasa X in local mode.""" config.LOCAL_MODE = True print_success("Starting Rasa X in local mode... 🚀") config.self_port = args.rasa_x_port _configure_for_local_server(data_path, config_path, token) rasax.community.jwt.initialise_jwt_keys() app = rasa_x_server.configure_app() with session_scope() as session: auth_endpoints = _enable_development_mode_and_get_additional_auth_endpoints( app) initialize_app(app, class_views=auth_endpoints) sql_migrations.run_migrations(session) initialise.create_community_user(session, app) _initialize_with_local_data(project_path, data_path, session, args.port, config_path) telemetry.track(telemetry.LOCAL_START_EVENT) telemetry.track_project_status(session) # this needs to run after initial database structures are created # otherwise projects assigned to events won't be present _start_event_service() scheduler.start_background_scheduler() app.run( host="0.0.0.0", port=config.self_port, auto_reload=os.environ.get("SANIC_AUTO_RELOAD"), access_log=False, )
async def upload_model(request, project_id): model_service = _model_service(request) try: tpath = model_service.save_model_to_disk(request) except (FileNotFoundError, ValueError, TypeError) as e: return rasa_x_utils.error( http.HTTPStatus.BAD_REQUEST, "ModelSaveError", f"Could not save model.\n{e}", ) minimum_version = await model_service.minimum_compatible_version() if not model_service.is_model_compatible(minimum_version, fpath=tpath): return rasa_x_utils.error( http.HTTPStatus.BAD_REQUEST, "ModelVersionError", f"Model version unsupported.\n" f"The minimum compatible version is {minimum_version}.", ) try: filename: Text = os.path.basename(tpath) model_name = filename.split(".tar.gz")[0] saved = await model_service.save_uploaded_model( project_id, model_name, tpath) if saved: telemetry.track(telemetry.MODEL_UPLOADED_EVENT) return response.json(saved, http.HTTPStatus.CREATED) return rasa_x_utils.error( http.HTTPStatus.BAD_REQUEST, "ModelSaveError", "Could not save model.\nModel name '{}'." "File path '{}'.".format(model_name, tpath), ) except FileExistsError: return rasa_x_utils.error( http.HTTPStatus.CONFLICT, "ModelExistsError", "A model with that name already exists.", )