async def train_core_async( domain: Union[Domain, Text], config: Text, stories: Text, output: Text, train_path: Optional[Text] = None, fixed_model_name: Optional[Text] = None, kwargs: Optional[Dict] = None, ) -> Optional[Text]: """Trains a Core model. Args: domain: Path to the domain file. config: Path to the config file for Core. stories: Path to the Core training data. output: Output path. train_path: If `None` the model will be trained in a temporary directory, otherwise in the provided directory. fixed_model_name: Name of model to be stored. uncompress: If `True` the model will not be compressed. kwargs: Additional training parameters. Returns: If `train_path` is given it returns the path to the model archive, otherwise the path to the directory with the trained model files. """ skill_imports = SkillSelector.load(config, stories) if isinstance(domain, str): try: domain = Domain.load(domain, skill_imports) domain.check_missing_templates() except InvalidDomain as e: print_error( "Could not load domain due to: '{}'. To specify a valid domain path " "use the '--domain' argument.".format(e) ) return None train_context = TempDirectoryPath(data.get_core_directory(stories, skill_imports)) with train_context as story_directory: if not os.listdir(story_directory): print_error( "No stories given. Please provide stories in order to " "train a Rasa Core model using the '--stories' argument." ) return return await _train_core_with_validated_data( domain=domain, config=config, story_directory=story_directory, output=output, train_path=train_path, fixed_model_name=fixed_model_name, kwargs=kwargs, )
def shell(args: argparse.Namespace): from rasa.cli.utils import get_validated_path from rasa.constants import DEFAULT_MODELS_PATH from rasa.model import get_model, get_model_subdirectories args.connector = "cmdline" model = get_validated_path(args.model, "model", DEFAULT_MODELS_PATH) model_path = get_model(model) if not model_path: print_error( "No model found. Train a model before running the " "server using `rasa train`." ) return core_model, nlu_model = get_model_subdirectories(model_path) if not os.path.exists(core_model): import rasa.nlu.run rasa.nlu.run.run_cmdline(nlu_model) else: import rasa.cli.run rasa.cli.run.run(args)
def _overwrite_endpoints_for_local_x(endpoints: AvailableEndpoints, rasa_x_token: Text, rasa_x_url: Text): from rasa.utils.endpoints import EndpointConfig import questionary endpoints.model = EndpointConfig( f"{rasa_x_url}/projects/default/models/tags/production", token=rasa_x_token, wait_time_between_pulls=2, ) if endpoints.event_broker and not _is_correct_event_broker( endpoints.event_broker): cli_utils.print_error( "Rasa X currently only supports a SQLite event broker with path '{}' " "when running locally. You can deploy Rasa X with Docker " "(https://rasa.com/docs/rasa-x/deploy/) if you want to use " "other event broker configurations.".format(DEFAULT_EVENTS_DB)) overwrite_existing_event_broker = questionary.confirm( "Do you want to continue with the default SQLite event broker?" ).ask() if not overwrite_existing_event_broker: exit(0) endpoints.event_broker = EndpointConfig(type="sql", db=DEFAULT_EVENTS_DB, dialect="sqlite")
def test_parse(): # model = get_validated_path(args.model, "model", DEFAULT_MODELS_PATH) model = get_validated_path(None, "model", DEFAULT_MODELS_PATH) model_path = get_model(model) if not model_path: print_error("No model found. Train a model before running the " "server using `rasa train nlu`.") exit(1) _, nlu_model = get_model_subdirectories(model_path) if not os.path.exists(nlu_model): print_error("No NLU model found. Train a model before running the " "server using `rasa train nlu`.") exit(1) # input shell # rasa.nlu.run.run_cmdline(nlu_model) print("model_path is {},nlu_model is {}".format(model_path, nlu_model)) print("please input your text to parse") # message = input().strip() # message = "这款衣服有货吗" message = "身高170体重140" interpreter = Interpreter.load(nlu_model, component_builder) result = interpreter.parse(message) print(json.dumps(result, indent=2))
def _overwrite_endpoints_for_local_x(endpoints: "AvailableEndpoints", rasa_x_token: Text, rasa_x_url: Text): from rasa.utils.endpoints import EndpointConfig import questionary endpoints.model = EndpointConfig( "{}/projects/default/models/tags/production".format(rasa_x_url), token=rasa_x_token, wait_time_between_pulls=2, ) overwrite_existing_tracker_store = False if endpoints.tracker_store and not _is_correct_tracker_store( endpoints.tracker_store): print_error( "Rasa X currently only supports a SQLite tracker store with path '{}' " "when running locally. You can deploy Rasa X with Docker " "(https://rasa.com/docs/rasa-x/deploy/) if you want to use " "other tracker store configurations.".format(DEFAULT_TRACKER_DB)) overwrite_existing_tracker_store = questionary.confirm( "Do you want to continue with the default SQLite tracker store?" ).ask() if not overwrite_existing_tracker_store: exit(0) if not endpoints.tracker_store or overwrite_existing_tracker_store: endpoints.tracker_store = EndpointConfig(type="sql", db=DEFAULT_TRACKER_DB)
def shell_nlu(args: argparse.Namespace): from rasa.cli.utils import get_validated_path from rasa.constants import DEFAULT_MODELS_PATH from rasa.model import get_model, get_model_subdirectories import rasa.nlu.run args.connector = "cmdline" model = get_validated_path(args.model, "model", DEFAULT_MODELS_PATH) try: model_path = get_model(model) except ModelNotFound: print_error( "No model found. Train a model before running the " "server using `rasa train nlu`." ) return _, nlu_model = get_model_subdirectories(model_path) if not nlu_model: print_error( "No NLU model found. Train a model before running the " "server using `rasa train nlu`." ) return rasa.nlu.run.run_cmdline(nlu_model)
def shell(args: argparse.Namespace): from rasa.cli.utils import get_validated_path from rasa.constants import DEFAULT_MODELS_PATH from rasa.model import get_model, get_model_subdirectories args.connector = "cmdline" print("cmdline") model = get_validated_path(args.model, "model", DEFAULT_MODELS_PATH) try: model_path = get_model(model) except ModelNotFound: print_error( "No model found. Train a model before running the " "server using `rasa train`." ) return core_model, nlu_model = get_model_subdirectories(model_path) print("core_model", core_model) print("nlu_model", nlu_model) print("args", args) if not core_model: import rasa.nlu.run rasa.nlu.run.run_cmdline(nlu_model) else: import rasa.cli.run rasa.cli.run.run(args)
def _get_valid_config( config: Optional[Text], mandatory_keys: List[Text], default_config: Text = DEFAULT_CONFIG_PATH, ) -> Text: """Get a config from a config file and check if it is valid. Exit if the config isn't valid. Args: config: Path to the config file. mandatory_keys: The keys that have to be specified in the config file. default_config: default config to use if the file at `config` doesn't exist. Returns: The path to the config file if the config is valid. """ config = get_validated_path(config, "config", default_config) if not os.path.exists(config): print_error( "The config file '{}' does not exist. Use '--config' to specify a " "valid config file." "".format(config)) exit(1) missing_keys = missing_config_keys(config, mandatory_keys) if missing_keys: print_error( "The config file '{}' is missing mandatory parameters: " "'{}'. Add missing parameters to config file and try again." "".format(config, "', '".join(missing_keys))) exit(1) return config # pytype: disable=bad-return-type
def _get_event_broker_endpoint( event_broker_endpoint: Optional[EndpointConfig], ) -> EndpointConfig: import questionary default_event_broker_endpoint = EndpointConfig(type="sql", dialect="sqlite", db=DEFAULT_EVENTS_DB) if not event_broker_endpoint: return default_event_broker_endpoint elif not _is_correct_event_broker(event_broker_endpoint): cli_utils.print_error( "Rasa X currently only supports a SQLite event broker with path '{}' " "when running locally. You can deploy Rasa X with Docker " "(https://rasa.com/docs/rasa-x/deploy/) if you want to use " "other event broker configurations.".format(DEFAULT_EVENTS_DB)) continue_with_default_event_broker = questionary.confirm( "Do you want to continue with the default SQLite event broker?" ).ask() if not continue_with_default_event_broker: exit(0) return default_event_broker_endpoint else: return event_broker_endpoint
def rasa_x(args: argparse.Namespace): from rasa.cli.utils import print_success, print_error, signal_handler from rasa.core.utils import AvailableEndpoints signal.signal(signal.SIGINT, signal_handler) _configure_logging(args) if args.production: print_success("Starting Rasa X in production mode... 🚀") args.endpoints = get_validated_path( args.endpoints, "endpoints", DEFAULT_ENDPOINTS_PATH, True ) endpoints = AvailableEndpoints.read_endpoints(args.endpoints) _rasa_service(args, endpoints) else: if not is_rasa_x_installed(): print_error( "Rasa X is not installed. The `rasa x` " "command requires an installation of Rasa X." ) sys.exit(1) # noinspection PyUnresolvedReferences from rasax.community import local rasa_x_token = generate_rasa_x_token() start_rasa_for_local_rasa_x(args, rasa_x_token=rasa_x_token) local.main(args, ".", args.data, token=rasa_x_token)
def _get_event_broker_endpoint( event_broker_endpoint: Optional[EndpointConfig], ) -> EndpointConfig: import questionary default_event_broker_endpoint = EndpointConfig(type="sql", dialect="sqlite", db=DEFAULT_EVENTS_DB) if not event_broker_endpoint: return default_event_broker_endpoint elif not _is_correct_event_broker(event_broker_endpoint): cli_utils.print_error( f"Rasa X currently only supports a SQLite event broker with path " f"'{DEFAULT_EVENTS_DB}' when running locally. You can deploy Rasa X " f"with Docker ({DOCS_BASE_URL_RASA_X}/installation-and-setup/" f"docker-compose-quick-install/) if you want to use other event broker " f"configurations.") continue_with_default_event_broker = questionary.confirm( "Do you want to continue with the default SQLite event broker?" ).ask() if not continue_with_default_event_broker: exit(0) return default_event_broker_endpoint else: return event_broker_endpoint
async def test_core(model: Text, stories: Text, endpoints: Optional[Text] = None, output: Text = DEFAULT_RESULTS_PATH): _endpoints = core_utils.AvailableEndpoints.read_endpoints(endpoints) model = cli_utils.get_validated_path(model, "model", DEFAULT_MODELS_PATH) try: unpacked_model = get_model(model) except ModelNotFound: print_error( "Unable to test: could not find a model. Use 'rasa train' to train a " "Rasa model and provide it via the '--model' argument.") return core_path, nlu_path = get_model_subdirectories(unpacked_model) if not core_path: print_error( "Unable to test: could not find a Core model. Use 'rasa train' to train a " "Rasa model and provide it via the '--model' argument.") _interpreter = RegexInterpreter() _agent = Agent.load(unpacked_model, interpreter=_interpreter) return await rasa.core.test(stories, _agent, out_directory=output)
def run_build(cwd, output): if config.development_mode: build_cmd = "build-frontend-enterprise" else: build_cmd = "build-frontend" print_info("Building frontend (development mode)...") # this will always use the frontend enterprise build, as in this case we # have the source anyways (won't happen in packaged build) if subprocess.call(["make", "install-frontend"], cwd=cwd): print_error( "Failed to install frontend dependencies. Check logs for details." ) _write_index_html( frontend_directory, "Frontend install failed! Check the logs for details.", ) elif subprocess.call(["make", build_cmd], cwd=cwd): print_error("Failed to build frontend code. Check logs for details.") _write_index_html( frontend_directory, "Frontend build failed! Check the logs for details." ) else: print_success( "Finished building frontend, serving from {}." "".format(os.path.abspath(output)) )
def _get_valid_config( config: Optional[Text], mandatory_keys: List[Text], default_config: Text = DEFAULT_CONFIG_PATH, ) -> Text: config = get_validated_path(config, "config", default_config) if not os.path.exists(config): print_error( "The config file '{}' does not exist. Use '--config' to specify a " "valid config file." "".format(config) ) exit(1) missing_keys = missing_config_keys(config, mandatory_keys) if missing_keys: print_error( "The config file '{}' is missing mandatory parameters: " "'{}'. Add missing parameters to config file and try again." "".format(config, "', '".join(missing_keys)) ) exit(1) return config # pytype: disable=bad-return-type
def run(args: argparse.Namespace): import rasa.run args.model = _validate_model_path(args.model, "model", DEFAULT_MODELS_PATH) if not args.enable_api: # if the API is enabled you can start without a model as you can train a # model via the API once the server is up and running from rasa.model import get_model try: get_model(args.model) except ModelNotFound: print_error( "No model found. Train a model before running the " "server using `rasa train` and use '--model' to provide " "the model path.") return args.endpoints = get_validated_path(args.endpoints, "endpoints", DEFAULT_ENDPOINTS_PATH, True) args.credentials = get_validated_path(args.credentials, "credentials", DEFAULT_CREDENTIALS_PATH, True) rasa.run(**vars(args))
def run(args: argparse.Namespace): import rasa.run # botfront:start from rasa.utils.botfront import set_endpoints_credentials_args_from_remote set_endpoints_credentials_args_from_remote(args) # botfront:end args.endpoints = get_validated_path(args.endpoints, "endpoints", DEFAULT_ENDPOINTS_PATH, True) args.credentials = get_validated_path(args.credentials, "credentials", DEFAULT_CREDENTIALS_PATH, True) if args.enable_api: if not args.remote_storage: args.model = _validate_model_path(args.model, "model", DEFAULT_MODELS_PATH) rasa.run(**vars(args)) return # if the API is not enable you cannot start without a model # make sure either a model server, a remote storage, or a local model is # configured from rasa.model import get_model from rasa.core.utils import AvailableEndpoints # start server if remote storage is configured if args.remote_storage is not None: rasa.run(**vars(args)) return # start server if model server is configured endpoints = AvailableEndpoints.read_endpoints(args.endpoints) model_server = endpoints.model if endpoints and endpoints.model else None if model_server is not None: rasa.run(**vars(args)) return # start server if local model found args.model = _validate_model_path(args.model, "model", DEFAULT_MODELS_PATH) local_model_set = True try: get_model(args.model) except ModelNotFound: local_model_set = False if local_model_set: rasa.run(**vars(args)) return print_error( "No model found. You have three options to provide a model:\n" "1. Configure a model server in the endpoint configuration and provide " "the configuration via '--endpoints'.\n" "2. Specify a remote storage via '--remote-storage' to load the model " "from.\n" "3. Train a model before running the server using `rasa train` and " "use '--model' to provide the model path.\n" "For more information check {}.".format( DOCS_BASE_URL + "/user-guide/running-the-server/"))
def test_nlu( model: Optional[Text], nlu_data: Optional[Text], output_directory: Text = DEFAULT_RESULTS_PATH, kwargs: Optional[Dict] = None, ): from rasa.nlu.test import run_evaluation from rasa.model import get_model try: unpacked_model = get_model(model) except ModelNotFound: print_error( "Could not find any model. Use 'rasa train nlu' to train a " "Rasa model and provide it via the '--model' argument.") return io_utils.create_directory(output_directory) nlu_model = os.path.join(unpacked_model, "nlu") if os.path.exists(nlu_model): kwargs = utils.minimal_kwargs(kwargs, run_evaluation, ["data_path", "model"]) run_evaluation(nlu_data, nlu_model, output_directory=output_directory, **kwargs) else: print_error( "Could not find any model. Use 'rasa train nlu' to train a " "Rasa model and provide it via the '--model' argument.")
async def _train_nlu_async( config: Text, nlu_data: Text, output: Text, train_path: Optional[Text] = None, fixed_model_name: Optional[Text] = None, persist_nlu_training_data: bool = False, ): # training NLU only hence the training files still have to be selected file_importer = TrainingDataImporter.load_nlu_importer_from_config( config, training_data_paths=[nlu_data]) training_datas = await file_importer.get_nlu_data() if training_datas.is_empty(): print_error( "No NLU data given. Please provide NLU data in order to train " "a Rasa NLU model using the '--nlu' argument.") return return await _train_nlu_with_validated_data( file_importer, output=output, train_path=train_path, fixed_model_name=fixed_model_name, persist_nlu_training_data=persist_nlu_training_data, )
def run_locally(args: argparse.Namespace): # noinspection PyUnresolvedReferences from rasax.community import local # pytype: disable=import-error args.rasa_x_port = args.rasa_x_port or DEFAULT_RASA_X_PORT args.port = args.port or DEFAULT_RASA_PORT project_path = "." _validate_rasa_x_start(args, project_path) local.check_license_and_metrics(args) rasa_x_token = generate_rasa_x_token() process = start_rasa_for_local_rasa_x(args, rasa_x_token=rasa_x_token) config_path = _get_config_path(args) try: local.main(args, project_path, args.data, token=rasa_x_token, config_path=config_path) except Exception: print(traceback.format_exc()) cli_utils.print_error( "Sorry, something went wrong (see error above). Make sure to start " "Rasa X with valid data and valid domain and config files. Please, " "also check any warnings that popped up.\nIf you need help fixing " "the issue visit our forum: https://forum.rasa.com/.") finally: process.terminate()
async def _train_nlu_async( config: Text, nlu_data: Text, output: Text, train_path: Optional[Text] = None, fixed_model_name: Optional[Text] = None, persist_nlu_training_data: bool = False, additional_arguments: Optional[Dict] = None, ): if not nlu_data: print_error( "No NLU data given. Please provide NLU data in order to train " "a Rasa NLU model using the '--nlu' argument.") return # training NLU only hence the training files still have to be selected file_importer = TrainingDataImporter.load_nlu_importer_from_config( config, training_data_paths=[nlu_data]) training_datas = await file_importer.get_nlu_data() if training_datas.is_empty(): print_error(f"Path '{nlu_data}' doesn't contain valid NLU data in it. " "Please verify the data format. " "The NLU model training will be skipped now.") return return await _train_nlu_with_validated_data( file_importer, output=output, train_path=train_path, fixed_model_name=fixed_model_name, persist_nlu_training_data=persist_nlu_training_data, additional_arguments=additional_arguments, )
def _validate_domain(domain_path: Text): from rasa.core.domain import Domain, InvalidDomain try: Domain.load(domain_path) except InvalidDomain as e: print_error("The provided domain file could not be loaded. Error: {}".format(e)) sys.exit(1)
def _dump_config( config: Dict[Text, Any], config_file_path: Text, missing_keys: Set[Text], auto_configured_keys: Set[Text], training_type: Optional[TrainingType] = TrainingType.BOTH, ) -> None: """Dump the automatically configured keys into the config file. The configuration provided in the file is kept as it is (preserving the order of keys and comments). For keys that were automatically configured, an explanatory comment is added and the automatically chosen configuration is added commented-out. If there are already blocks with comments from a previous auto configuration run, they are replaced with the new auto configuration. Args: config: The configuration including the automatically configured keys. config_file_path: The file into which the configuration should be dumped. missing_keys: Keys that need to be added to the config file. auto_configured_keys: Keys for which a commented out auto configuration section needs to be added to the config file. training_type: NLU, CORE or BOTH depending on which is trained. """ config_as_expected = _is_config_file_as_expected(config_file_path, missing_keys, auto_configured_keys, training_type) if not config_as_expected: cli_utils.print_error( f"The configuration file at '{config_file_path}' has been removed or " f"modified while the automatic configuration was running. The current " f"configuration will therefore not be dumped to the file. If you want to " f"your model to use the configuration provided in '{config_file_path}', " f"you need to re-run training.") return _add_missing_config_keys_to_file(config_file_path, missing_keys) autoconfig_lines = _get_commented_out_autoconfig_lines( config, auto_configured_keys) with open(config_file_path, "r+", encoding=rasa.shared.utils.io.DEFAULT_ENCODING) as f: lines = f.readlines() updated_lines = _get_lines_including_autoconfig( lines, autoconfig_lines) f.seek(0) for line in updated_lines: f.write(line) auto_configured_keys = common_utils.transform_collection_to_sentence( auto_configured_keys) cli_utils.print_info( f"The configuration for {auto_configured_keys} was chosen automatically. It " f"was written into the config file at '{config_file_path}'.")
def test_core( model: Optional[Text] = None, stories: Optional[Text] = None, endpoints: Optional[Text] = None, output: Text = DEFAULT_RESULTS_PATH, kwargs: Optional[Dict] = None, ): import rasa.core.test import rasa.core.utils as core_utils from rasa.nlu import utils as nlu_utils from rasa.model import get_model from rasa.core.interpreter import NaturalLanguageInterpreter from rasa.core.agent import Agent _endpoints = core_utils.AvailableEndpoints.read_endpoints(endpoints) if kwargs is None: kwargs = {} if output: nlu_utils.create_dir(output) unpacked_model = get_model(model) if unpacked_model is None: print_error( "Unable to test: could not find a model. Use 'rasa train' to train a " "Rasa model." ) return core_path, nlu_path = get_model_subdirectories(unpacked_model) if not os.path.exists(core_path): print_error( "Unable to test: could not find a Core model. Use 'rasa train' to " "train a model." ) use_e2e = kwargs["e2e"] if "e2e" in kwargs else False _interpreter = RegexInterpreter() if use_e2e: if os.path.exists(nlu_path): _interpreter = NaturalLanguageInterpreter.create(nlu_path, _endpoints.nlu) else: print_warning( "No NLU model found. Using default 'RegexInterpreter' for end-to-end " "evaluation." ) _agent = Agent.load(unpacked_model, interpreter=_interpreter) kwargs = minimal_kwargs(kwargs, rasa.core.test, ["stories", "agent"]) loop = asyncio.get_event_loop() loop.run_until_complete( rasa.core.test(stories, _agent, out_directory=output, **kwargs) )
async def train_core_async( domain: Union[Domain, Text], config: Text, stories: Text, output: Text, train_path: Optional[Text] = None, fixed_model_name: Optional[Text] = None, uncompress: bool = False, kwargs: Optional[Dict] = None, ) -> Optional[Text]: """Trains a Core model. Args: domain: Path to the domain file. config: Path to the config file for Core. stories: Path to the Core training data. output: Output path. train_path: If `None` the model will be trained in a temporary directory, otherwise in the provided directory. fixed_model_name: Name of model to be stored. uncompress: If `True` the model will not be compressed. kwargs: Additional training parameters. Returns: If `train_path` is given it returns the path to the model archive, otherwise the path to the directory with the trained model files. """ config = _get_valid_config(config, CONFIG_MANDATORY_KEYS_CORE) skill_imports = SkillSelector.load(config) if isinstance(domain, str): try: domain = Domain.load(domain, skill_imports) except InvalidDomain as e: print_error(e) return None story_directory = data.get_core_directory(stories, skill_imports) if not os.listdir(story_directory): print_error( "No dialogue data given. Please provide dialogue data in order to " "train a Rasa Core model." ) return return await _train_core_with_validated_data( domain=domain, config=config, story_directory=story_directory, output=output, train_path=train_path, fixed_model_name=fixed_model_name, uncompress=uncompress, kwargs=kwargs, )
def test_core( model: Optional[Text] = None, stories: Optional[Text] = None, endpoints: Optional[Text] = None, output: Text = DEFAULT_RESULTS_PATH, kwargs: Optional[Dict] = None, ): import rasa.core.test import rasa.core.utils as core_utils import rasa.model from rasa.core.interpreter import RegexInterpreter, NaturalLanguageInterpreter from rasa.core.agent import Agent _endpoints = core_utils.AvailableEndpoints.read_endpoints(endpoints) if kwargs is None: kwargs = {} if output: io_utils.create_directory(output) try: unpacked_model = rasa.model.get_model(model) except ModelNotFound: print_error( "Unable to test: could not find a model. Use 'rasa train' to train a " "Rasa model and provide it via the '--model' argument." ) return core_path, nlu_path = rasa.model.get_model_subdirectories(unpacked_model) if not core_path: print_error( "Unable to test: could not find a Core model. Use 'rasa train' to train a " "Rasa model and provide it via the '--model' argument." ) use_e2e = kwargs["e2e"] if "e2e" in kwargs else False _interpreter = RegexInterpreter() if use_e2e: if nlu_path: _interpreter = NaturalLanguageInterpreter.create(_endpoints.nlu or nlu_path) else: print_warning( "No NLU model found. Using default 'RegexInterpreter' for end-to-end " "evaluation." ) _agent = Agent.load(unpacked_model, interpreter=_interpreter) kwargs = utils.minimal_kwargs(kwargs, rasa.core.test, ["stories", "agent"]) loop = asyncio.get_event_loop() loop.run_until_complete( rasa.core.test(stories, _agent, out_directory=output, **kwargs) )
async def start_training_process( self, team: Text = config.team_name, project_id: Text = config.project_name) -> Any: url = "/model/train" nlu_training_data = self.data_service.get_nlu_training_data_object( should_include_lookup_table_entries=True, ) responses: Optional[Dict] = None if self.data_service.training_data_contains_retrieval_intents( nlu_training_data): try: from rasa.utils import io as io_utils responses = io_utils.read_yaml(self._get_responses_file_name()) except ValueError as e: rasa_cli_utils.print_error( "Could not complete training request as your training data contains " "retrieval intents of the form 'intent/response_key' but there is no " "responses file found.") raise ValueError( f"Unable to train on data containing retrieval intents. " f"Details:\n{e}") nlu_training_data = nlu_training_data.filter_training_examples( lambda ex: ex.get(RESPONSE_KEY_ATTRIBUTE) is None) md_formatted_data = nlu_training_data.nlu_as_markdown().strip() stories = self.story_services.fetch_stories(None) combined_stories = self.story_services.get_stories_markdown(stories) domain = self.domain_service.get_or_create_domain() domain_yaml = dump_obj_as_yaml_to_string(domain) _config = self.settings_service.get_config(team, project_id) config_yaml = dump_obj_as_yaml_to_string(_config) payload = dict( domain=domain_yaml, config=config_yaml, nlu=md_formatted_data, stories=combined_stories, responses=dump_obj_as_yaml_to_string(responses), force=False, save_to_default_model_directory=False, ) async with self._session() as session: response = await session.post( self._request_url(url), params=self._query_parameters(), json=payload, timeout=24 * 60 * 60, # 24 hours ) return await response.read()
def train_nlu(config: Text, nlu_data: Text, output: Text, train_path: Optional[Text]) -> Optional[Text]: """Trains a NLU model. Args: config: Path to the config file for NLU. nlu_data: Path to the NLU training data. output: Output path. train_path: If `None` the model will be trained in a temporary directory, otherwise in the provided directory. Returns: If `train_path` is given it returns the path to the model archive, otherwise the path to the directory with the trained model files. """ import rasa.nlu.train config = get_valid_config(config, CONFIG_MANDATORY_KEYS_NLU) if not train_path: # training NLU only hence the training files still have to be selected skill_imports = SkillSelector.load(config) nlu_data_directory = data.get_nlu_directory(nlu_data, skill_imports) else: nlu_data_directory = nlu_data if not os.listdir(nlu_data_directory): print_error( "No NLU data given. Please provide NLU data in order to train " "a Rasa NLU model.") return _train_path = train_path or tempfile.mkdtemp() print_color("Start training NLU model ...", color=bcolors.OKBLUE) _, nlu_model, _ = rasa.nlu.train(config, nlu_data_directory, _train_path, fixed_model_name="nlu") print_color("Done.", color=bcolors.OKBLUE) if not train_path: output_path = create_output_path(output, prefix="nlu-") new_fingerprint = model.model_fingerprint(config, nlu_data=nlu_data_directory) model.create_package_rasa(_train_path, output_path, new_fingerprint) print_success( "Your Rasa NLU model is trained and saved at '{}'.".format( output_path)) return output_path return _train_path
def check_training_data(args): training_files = [ get_validated_path(f, "data", DEFAULT_DATA_PATH, none_is_valid=True) for f in args.data ] story_files, nlu_files = data.get_core_nlu_files(training_files) if not story_files or not nlu_files: print_error( "Cannot train initial Rasa model. Please provide NLU and Core data " "using the '--data' argument.") exit(1)
async def train_async( domain: Union[Domain, Text], config: Text, training_files: Optional[Union[Text, List[Text]]], output_path: Text = DEFAULT_MODELS_PATH, force_training: bool = False, fixed_model_name: Optional[Text] = None, kwargs: Optional[Dict] = None, ) -> Optional[Text]: """Trains a Rasa model (Core and NLU). Args: domain: Path to the domain file. config: Path to the config for Core and NLU. training_files: Paths to the training data for Core and NLU. output_path: Output path. force_training: If `True` retrain model even if data has not changed. fixed_model_name: Name of model to be stored. kwargs: Additional training parameters. Returns: Path of the trained model archive. """ skill_imports = SkillSelector.load(config, training_files) try: domain = Domain.load(domain, skill_imports) domain.check_missing_templates() except InvalidDomain as e: print_error( "Could not load domain due to error: {} \nTo specify a valid domain " "path, use the '--domain' argument.".format(e) ) return None story_directory, nlu_data_directory = data.get_core_nlu_directories( training_files, skill_imports ) with ExitStack() as stack: train_path = stack.enter_context(TempDirectoryPath(tempfile.mkdtemp())) nlu_data = stack.enter_context(TempDirectoryPath(nlu_data_directory)) story = stack.enter_context(TempDirectoryPath(story_directory)) return await _train_async_internal( domain, config, train_path, nlu_data, story, output_path, force_training, fixed_model_name, kwargs, )
async def train_core_async( domain: Union[Domain, Text], config: Text, stories: Text, output: Text, train_path: Optional[Text] = None, fixed_model_name: Optional[Text] = None, additional_arguments: Optional[Dict] = None, ) -> Optional[Text]: """Trains a Core model. Args: domain: Path to the domain file. config: Path to the config file for Core. stories: Path to the Core training data. output: Output path. train_path: If `None` the model will be trained in a temporary directory, otherwise in the provided directory. fixed_model_name: Name of model to be stored. uncompress: If `True` the model will not be compressed. additional_arguments: Additional training parameters. Returns: If `train_path` is given it returns the path to the model archive, otherwise the path to the directory with the trained model files. """ file_importer = TrainingDataImporter.load_core_importer_from_config( config, domain, [stories] ) domain = await file_importer.get_domain() if domain.is_empty(): print_error( "Core training was skipped because no valid domain file was found. " "Please specify a valid domain using '--domain' argument or check if the provided domain file exists." ) return None if not await file_importer.get_stories(): print_error( "No stories given. Please provide stories in order to " "train a Rasa Core model using the '--stories' argument." ) return return await _train_core_with_validated_data( file_importer, output=output, train_path=train_path, fixed_model_name=fixed_model_name, additional_arguments=additional_arguments, )