async def test_training_data_is_reproducible(): training_data_file = "examples/moodbot/data/stories.yml" agent = Agent( "examples/moodbot/domain.yml", policies=[AugmentedMemoizationPolicy()] ) training_data = await agent.load_data(training_data_file) # make another copy of training data same_training_data = await agent.load_data(training_data_file) # test if both datasets are identical (including in the same order) for i, x in enumerate(training_data): assert str(x.as_dialogue()) == str(same_training_data[i].as_dialogue())
async def form_bot_agent(trained_async: Callable) -> Agent: endpoint = EndpointConfig("https://example.com/webhooks/actions") zipped_model = await trained_async( domain="examples/formbot/domain.yml", config="examples/formbot/config.yml", training_files=[ "examples/formbot/data/rules.yml", "examples/formbot/data/stories.yml", ], ) return Agent.load_local_model(zipped_model, action_endpoint=endpoint)
async def _evaluate_core_model(model: Text, stories_file: Text) -> int: from rasa.core.agent import Agent logger.info(f"Evaluating model '{model}'") agent = Agent.load(model) generator = await _create_data_generator(stories_file, agent) completed_trackers = generator.generate_story_trackers() story_eval_store, number_of_stories = await _collect_story_predictions( completed_trackers, agent ) failed_stories = story_eval_store.failed_stories return number_of_stories - len(failed_stories)
async def on_tryNow(self, sid, data): print("----------- Inside Try now --from SID {}--------------".format(sid)) result = await ExportProject.main(sid, data, 'SESSION') print(result) if result is not None: await sio.emit('chatResponse', {"status": "Error", "message": result}, namespace='/trynow', room=sid) return 1 import rasa.model as model from rasa.core.agent import Agent from rasa.core.tracker_store import MongoTrackerStore from rasa.core.domain import Domain from rasa.train import train_async from rasa.utils.endpoints import EndpointConfig base_path = CONFIG.get('api_gateway', 'SESSION_MODEL_PATH') config = "config.yml" training_files = "data/" domain = "domain.yml" output = "models/" endpoints = EndpointConfig(url="http://action_server:5055/webhook") base_path = base_path + sid + "/" config = base_path + config training_files = base_path + training_files domain = base_path + domain output = base_path + output start_time = time.time() try: model_path = await train_async(domain, config, [training_files], output, kwargs={"augmentation_factor": 10}) end_time = time.time() print("it took this long to run: {}".format(end_time - start_time)) unpacked = model.get_model(model_path) domain = Domain.load(domain) _tracker_store = MongoTrackerStore(domain=domain, host=CONFIG.get('api_gateway', 'MONGODB_URL'), db=CONFIG.get('api_gateway', 'MONGODB_NAME'), username=None, password=None, auth_source="admin", collection="conversations", event_broker=None) print("*************** Actions Endpoint as per data ********** {}".format(endpoints.url)) self.agent = Agent.load(unpacked, tracker_store=_tracker_store, action_endpoint=endpoints) await sio.emit('chatResponse', {"status": "Success", "message": "Ready to chat"}, namespace='/trynow', room=sid) except Exception as e: print("Exception while try Now --- "+str(e)) await sio.emit('chatResponse', {"status": "Error", "message": repr(e)}, namespace='/trynow', room=sid)
async def test_end_to_evaluation_trips_circuit_breaker( e2e_story_file_trips_circuit_breaker_path: Text, trained_async: Callable, tmp_path: Path, ): config = textwrap.dedent(f""" version: '{LATEST_TRAINING_DATA_FORMAT_VERSION}' policies: - name: MemoizationPolicy max_history: 11 pipeline: [] """) config_path = tmp_path / "config.yml" rasa.shared.utils.io.write_text_file(config, config_path) model_path = await trained_async( "data/test_domains/default.yml", str(config_path), e2e_story_file_trips_circuit_breaker_path, ) agent = Agent.load_local_model(model_path) generator = _create_data_generator( e2e_story_file_trips_circuit_breaker_path, agent, use_conversation_test_files=True, ) test_stories = generator.generate_story_trackers() story_evaluation, num_stories, _ = await _collect_story_predictions( test_stories, agent) circuit_trip_predicted = [ "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "circuit breaker tripped", "circuit breaker tripped", ] assert (story_evaluation.evaluation_store.action_predictions == circuit_trip_predicted) assert num_stories == 1
async def visualize( config_path: Text, domain_path: Text, stories_path: Text, nlu_data_path: Text, output_path: Text, max_history: int, ): from rasa.core.agent import Agent from rasa.core import config try: policies = config.load(config_path) except ValueError as e: print_error( "Could not load config due to: '{}'. To specify a valid config file use " "the '--config' argument.".format(e)) return try: agent = Agent(domain=domain_path, policies=policies) 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 # this is optional, only needed if the `/greet` type of # messages in the stories should be replaced with actual # messages (e.g. `hello`) if nlu_data_path is not None: import rasa.shared.nlu.training_data.loading nlu_training_data = rasa.shared.nlu.training_data.loading.load_data( nlu_data_path) else: nlu_training_data = None logger.info("Starting to visualize stories...") telemetry.track_visualization() await agent.visualize(stories_path, output_path, max_history, nlu_training_data=nlu_training_data) full_output_path = "file://{}".format(os.path.abspath(output_path)) logger.info(f"Finished graph creation. Saved into {full_output_path}") import webbrowser webbrowser.open(full_output_path)
async def test_random_seed(tmp_path: Path, monkeypatch: MonkeyPatch, domain_path: Text, stories_path: Text): policies_config = { "policies": [ { "name": "TEDPolicy", "random_seed": 42 }, { "name": "RulePolicy" }, ] } config_file = tmp_path / "config.yml" rasa.shared.utils.io.write_yaml(policies_config, config_file) model_file_1 = rasa.model_training.train_core( domain_path, config=str(config_file), stories=stories_path, output=str(tmp_path), additional_arguments={}, ) model_file_2 = rasa.model_training.train_core( domain_path, config=str(config_file), stories=stories_path, output=str(tmp_path), additional_arguments={}, ) processor_1 = Agent.load(model_file_1).processor processor_2 = Agent.load(model_file_2).processor probs_1 = await processor_1.predict_next_for_sender_id("1") probs_2 = await processor_2.predict_next_for_sender_id("2") assert probs_1["confidence"] == probs_2["confidence"]
def train_dialogue_transformer(domain_file="mobile_domain.yml", model_path="models/dialogue_transformer", training_data_file="data/mobile_edit_story.md"): # 通过加载yml配置文件方式配置policy policies = config.load('./policy/attention_policy.yml') agent = Agent(domain_file, policies=policies) training_data = agent.load_data(training_data_file) agent.train(training_data, validation_split=0.2) agent.persist(model_path) return agent
async def test_end_to_evaluation_trips_circuit_breaker( e2e_story_file_trips_circuit_breaker_path: Text, ): agent = Agent( domain="data/test_domains/default.yml", policies=[MemoizationPolicy(max_history=11)], ) training_data = await agent.load_data( e2e_story_file_trips_circuit_breaker_path) agent.train(training_data) generator = await _create_data_generator( e2e_story_file_trips_circuit_breaker_path, agent, use_conversation_test_files=True, ) test_stories = generator.generate_story_trackers() story_evaluation, num_stories, _ = await _collect_story_predictions( test_stories, agent, use_e2e=True) circuit_trip_predicted = [ "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "circuit breaker tripped", "circuit breaker tripped", ] assert (story_evaluation.evaluation_store.action_predictions == circuit_trip_predicted) assert num_stories == 1
def reload(bot: Text): try: endpoint = AgentProcessor.mongo_processor.get_endpoints( bot, raise_exception=False) action_endpoint = (EndpointConfig( url=endpoint["action_endpoint"]["url"]) if endpoint and endpoint.get("action_endpoint") else None) model_path = Utility.get_latest_file( os.path.join(DEFAULT_MODELS_PATH, bot)) agent = Agent.load(model_path, action_endpoint=action_endpoint) InMemoryAgentCache.set(bot, agent) except Exception as e: logging.info(e) raise AppException("Please train the bot first")
async def load_agent_on_start( model_path: Text, endpoints: Optional[AvailableEndpoints], remote_storage: Optional[Text], app: Sanic, loop: Text, ): """Load an agent. Used to be scheduled on server start (hence the `app` and `loop` arguments).""" from rasa.core import broker try: _, nlu_model = get_model_subdirectories(get_model(model_path)) _interpreter = NaturalLanguageInterpreter.create( nlu_model, endpoints.nlu) except Exception: logger.debug("Could not load interpreter from '{}'".format(model_path)) _interpreter = None _broker = broker.from_endpoint_config(endpoints.event_broker) _tracker_store = TrackerStore.find_tracker_store(None, endpoints.tracker_store, _broker) model_server = endpoints.model if endpoints and endpoints.model else None app.agent = await load_agent( model_path, model_server=model_server, remote_storage=remote_storage, interpreter=_interpreter, generator=endpoints.nlg, tracker_store=_tracker_store, action_endpoint=endpoints.action, ) if not app.agent: logger.error( "Agent could not be loaded with the provided configuration." "Load default agent without any model.") app.agent = Agent( interpreter=_interpreter, generator=endpoints.nlg, tracker_store=_tracker_store, action_endpoint=endpoints.action, ) return app.agent
async def test_training_script_with_max_history_set(tmpdir): await train(DEFAULT_DOMAIN_PATH, DEFAULT_STORIES_FILE, tmpdir.strpath, interpreter=RegexInterpreter(), policy_config='data/test_config/max_hist_config.yml', kwargs={}) agent = Agent.load(tmpdir.strpath) for policy in agent.policy_ensemble.policies: if hasattr(policy.featurizer, 'max_history'): if type(policy) == FormPolicy: assert policy.featurizer.max_history == 2 else: assert policy.featurizer.max_history == 5
def main(): from rasa.core.agent import Agent from rasa.core.interpreter import NaturalLanguageInterpreter from rasa.core.utils import (AvailableEndpoints, set_default_subparser) import rasa_nlu.utils as nlu_utils import rasa.core.cli from rasa.core import utils loop = asyncio.get_event_loop() # Running as standalone python application arg_parser = create_argument_parser() set_default_subparser(arg_parser, 'default') cmdline_arguments = arg_parser.parse_args() logging.basicConfig(level=cmdline_arguments.loglevel) _endpoints = AvailableEndpoints.read_endpoints(cmdline_arguments.endpoints) if cmdline_arguments.output: nlu_utils.create_dir(cmdline_arguments.output) if not cmdline_arguments.core: raise ValueError("you must provide a core model directory to evaluate " "using -d / --core") if cmdline_arguments.mode == 'default': _interpreter = NaturalLanguageInterpreter.create( cmdline_arguments.nlu, _endpoints.nlu) _agent = Agent.load(cmdline_arguments.core, interpreter=_interpreter) stories = loop.run_until_complete( rasa.core.cli.train.stories_from_cli_args(cmdline_arguments)) loop.run_until_complete( test(stories, _agent, cmdline_arguments.max_stories, cmdline_arguments.output, cmdline_arguments.fail_on_prediction_errors, cmdline_arguments.e2e)) elif cmdline_arguments.mode == 'compare': compare(cmdline_arguments.core, cmdline_arguments.stories, cmdline_arguments.output) story_n_path = os.path.join(cmdline_arguments.core, 'num_stories.json') number_of_stories = utils.read_json_file(story_n_path) plot_curve(cmdline_arguments.output, number_of_stories) logger.info("Finished evaluation")
def test_core(model: Text, stories: Text, endpoints: Text = None, output: Text = DEFAULT_RESULTS_PATH, model_path: Text = None, **kwargs: Dict): 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 output: nlu_utils.create_dir(output) if os.path.isfile(model): model_path = get_model(model) if model_path: # Single model: Normal evaluation loop = asyncio.get_event_loop() model_path = get_model(model) core_path, nlu_path = get_model_subdirectories(model_path) if os.path.exists(core_path) and os.path.exists(nlu_path): _interpreter = NaturalLanguageInterpreter.create( nlu_path, _endpoints.nlu) _agent = Agent.load(core_path, interpreter=_interpreter) kwargs = minimal_kwargs(kwargs, rasa.core.test) loop.run_until_complete( rasa.core.test(stories, _agent, out_directory=output, **kwargs)) else: logger.warning("Not able to test. Make sure both models, core and " "nlu, are available.") else: from rasa.core.test import compare, plot_curve compare(model, stories, output) story_n_path = os.path.join(model, "num_stories.json") number_of_stories = core_utils.read_json_file(story_n_path) plot_curve(output, number_of_stories)
def test_single_state_featurizer_with_interpreter_state_with_action_listen( unpacked_trained_moodbot_path: Text, ): from rasa.core.agent import Agent interpreter = Agent.load(unpacked_trained_moodbot_path).interpreter f = SingleStateFeaturizer() f._default_feature_states[INTENT] = {"a": 0, "b": 1} f._default_feature_states[ENTITIES] = {"c": 0} f._default_feature_states[ACTION_NAME] = { "e": 0, "d": 1, "action_listen": 2 } f._default_feature_states[SLOTS] = {"e_0": 0, "f_0": 1, "g_0": 2} f._default_feature_states[ACTIVE_LOOP] = {"h": 0, "i": 1, "j": 2, "k": 3} encoded = f.encode_state( { "user": { "text": "a ball", "intent": "b", "entities": ["c"] }, "prev_action": { "action_name": "action_listen", "action_text": "throw a ball", }, "active_loop": { "name": "k" }, "slots": { "e": (1.0, ) }, }, interpreter=interpreter, ) # check all the features are encoded and *_text features are encoded by a densefeaturizer assert sorted(list(encoded.keys())) == sorted( [TEXT, ENTITIES, ACTION_NAME, SLOTS, ACTIVE_LOOP, INTENT, ACTION_TEXT]) assert encoded[TEXT][0].features.shape[-1] == 300 assert encoded[ACTION_TEXT][0].features.shape[-1] == 300 assert (encoded[INTENT][0].features != scipy.sparse.coo_matrix( [[0, 1]])).nnz == 0 assert (encoded[ACTION_NAME][0].features != scipy.sparse.coo_matrix( [[0, 0, 1]])).nnz == 0 assert encoded[ENTITIES][0].features.shape[-1] == 1 assert (encoded[SLOTS][0].features != scipy.sparse.coo_matrix([[1, 0, 0] ])).nnz == 0 assert (encoded[ACTIVE_LOOP][0].features != scipy.sparse.coo_matrix( [[0, 0, 0, 1]])).nnz == 0
async def test_wait_time_between_pulls_without_interval( model_server: TestClient, monkeypatch: MonkeyPatch ): monkeypatch.setattr( "rasa.core.agent._schedule_model_pulling", lambda *args: 1 / 0 ) # will raise an exception model_endpoint_config = EndpointConfig.from_dict( {"url": model_server.make_url("/model"), "wait_time_between_pulls": None} ) agent = Agent() # should not call _schedule_model_pulling, if it does, this will raise await rasa.core.agent.load_from_server(agent, model_server=model_endpoint_config)
def train_dialog() -> None: training_data_file = './data/stories.md' model_path = './models' policies = policy_config.load("Config.yml") # featurizer = MaxHistoryTrackerFeaturizer(BinarySingleStateFeaturizer(), max_history=5) agent = Agent('domain.yml', policies=policies) data = asyncio.run( agent.load_data(training_data_file, augmentation_factor=50)) # data = agent.load_data(training_data_file) agent.train(data) agent.persist(model_path)
def test_register_channel_without_route(): """Check we properly connect the input channel blueprint if route is None""" from rasa.core.channels import RestInput import rasa.core # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = RestInput() app = Sanic(__name__) rasa.core.channels.channel.register([input_channel], app, route=None) routes_list = utils.list_routes(app) assert routes_list.get("custom_webhook_RestInput.receive").startswith( "/webhook")
async def train( domain_file: Union[Domain, Text], training_resource: Union[Text, "TrainingDataImporter"], output_path: Text, interpreter: Optional["NaturalLanguageInterpreter"] = None, endpoints: "AvailableEndpoints" = None, policy_config: Optional[Union[Text, Dict]] = None, exclusion_percentage: Optional[int] = None, additional_arguments: Optional[Dict] = None, model_to_finetune: Optional["Agent"] = None, ) -> "Agent": from rasa.core import config, utils from rasa.core.utils import AvailableEndpoints from rasa.core.agent import Agent if not endpoints: endpoints = AvailableEndpoints() if not additional_arguments: additional_arguments = {} policies = config.load(policy_config) agent = Agent( domain_file, generator=endpoints.nlg, action_endpoint=endpoints.action, interpreter=interpreter, policies=policies, ) data_load_args, additional_arguments = utils.extract_args( additional_arguments, { "use_story_concatenation", "unique_last_num_states", "augmentation_factor", "remove_duplicates", "debug_plots", }, ) training_data = await agent.load_data( training_resource, exclusion_percentage=exclusion_percentage, **data_load_args) if model_to_finetune: agent.policy_ensemble = model_to_finetune.policy_ensemble agent.train(training_data, **additional_arguments) agent.persist(output_path) return agent
async def load_agent_on_start(core_model, endpoints, nlu_model, app, loop): """Load an agent. Used to be scheduled on server start (hence the `app` and `loop` arguments).""" from rasa.core import broker from rasa.core.agent import Agent _interpreter = NaturalLanguageInterpreter.create(nlu_model, endpoints.nlu) _broker = broker.from_endpoint_config(endpoints.event_broker) _tracker_store = TrackerStore.find_tracker_store(None, endpoints.tracker_store, _broker) if endpoints and endpoints.model: from rasa.core import agent app.agent = Agent( interpreter=_interpreter, generator=endpoints.nlg, tracker_store=_tracker_store, action_endpoint=endpoints.action, ) await agent.load_from_server(app.agent, model_server=endpoints.model) else: app.agent = Agent.load( core_model, interpreter=_interpreter, generator=endpoints.nlg, tracker_store=_tracker_store, action_endpoint=endpoints.action, ) return app.agent
def run(serve_forever=True): # create rasa NLU interpreter # interpreter = RasaNLUInterpreter("models/nlu/current") agent = Agent.load("./models/20210127-153235.tar.gz") # input_channel = FacebookInput( # fb_verify="your_fb_verify_token", # you need tell facebook this token, to confirm your URL # fb_secret="your_app_secret", # your app secret # fb_tokens={"your_page_id": "your_page_token"}, # page ids + tokens you subscribed to # debug_mode=True # enable debug mode for underlying fb library # ) if serve_forever: agent.handle_channel(HttpInputChannel(5004, "/app")) return agent
async def test_multiple_conversation_ids(default_agent: Agent): text = INTENT_MESSAGE_PREFIX + 'greet{"name":"Rasa"}' conversation_ids = [f"conversation {i}" for i in range(2)] # ensure conversations are processed in order tasks = [ default_agent.handle_text(text, sender_id=_id) for _id in conversation_ids ] results = await asyncio.gather(*tasks) assert results processed_ids = [result[0]["recipient_id"] for result in results] assert processed_ids == conversation_ids
async def test_end_to_evaluation_trips_circuit_breaker(): agent = Agent( domain="data/test_domains/default.yml", policies=[MemoizationPolicy(max_history=11)], ) training_data = await agent.load_data(STORY_FILE_TRIPS_CIRCUIT_BREAKER) agent.train(training_data) generator = await _create_data_generator( E2E_STORY_FILE_TRIPS_CIRCUIT_BREAKER, agent, use_e2e=True ) test_stories = generator.generate_story_trackers() story_evaluation, num_stories = await _collect_story_predictions( test_stories, agent, use_e2e=True ) circuit_trip_predicted = [ "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "utter_greet", "circuit breaker tripped", "circuit breaker tripped", ] assert ( story_evaluation.evaluation_store.action_predictions == circuit_trip_predicted ) assert num_stories == 1
async def prepared_agent(tmpdir_factory) -> Agent: model_path = tmpdir_factory.mktemp("model").strpath agent = Agent("data/test_domains/default.yml", policies=[AugmentedMemoizationPolicy(max_history=3)]) training_data = await agent.load_data(DEFAULT_STORIES_FILE) agent.train(training_data) agent.persist(model_path) return agent
def test_single_state_featurizer_with_interpreter_state_not_with_action_listen( unpacked_trained_spacybot_path: Text, ): # check that user features are ignored when action_name is not action_listen from rasa.core.agent import Agent interpreter = Agent.load(unpacked_trained_spacybot_path).interpreter f = SingleStateFeaturizer() f._default_feature_states[INTENT] = {"a": 0, "b": 1} f._default_feature_states[ENTITIES] = {"c": 0} f._default_feature_states[ACTION_NAME] = { "e": 0, "d": 1, "action_listen": 2 } f._default_feature_states[SLOTS] = {"e_0": 0, "f_0": 1, "g_0": 2} f._default_feature_states[ACTIVE_LOOP] = {"h": 0, "i": 1, "j": 2, "k": 3} encoded = f.encode_state( { "user": { "text": "a ball", "intent": "b", "entities": ["c"] }, "prev_action": { "action_name": "d", "action_text": "throw a ball" }, "active_loop": { "name": "k" }, "slots": { "e": (1.0, ) }, }, interpreter=interpreter, ) # check user input is ignored when action is not action_listen assert list( encoded.keys()) == [ACTION_TEXT, ACTION_NAME, ACTIVE_LOOP, SLOTS] assert encoded[ACTION_TEXT][0].features.shape[-1] == 300 assert (encoded[ACTION_NAME][0].features != scipy.sparse.coo_matrix( [[0, 1, 0]])).nnz == 0 assert (encoded[SLOTS][0].features != scipy.sparse.coo_matrix([[1, 0, 0] ])).nnz == 0 assert (encoded[ACTIVE_LOOP][0].features != scipy.sparse.coo_matrix( [[0, 0, 0, 1]])).nnz == 0
async def test_training_script_with_max_history_set(tmpdir): await train( DEFAULT_DOMAIN_PATH_WITH_SLOTS, DEFAULT_STORIES_FILE, tmpdir.strpath, interpreter=RegexInterpreter(), policy_config="data/test_config/max_hist_config.yml", additional_arguments={}, ) agent = Agent.load(tmpdir.strpath) for policy in agent.policy_ensemble.policies: if hasattr(policy.featurizer, "max_history"): if type(policy) == FormPolicy: assert policy.featurizer.max_history == 2 else: assert policy.featurizer.max_history == 5
def test_core( model: Optional[Text] = None, stories: Optional[Text] = None, output: Text = DEFAULT_RESULTS_PATH, additional_arguments: Optional[Dict] = None, ) -> None: """Tests a trained Core model against a set of test stories.""" import rasa.model from rasa.shared.nlu.interpreter import RegexInterpreter from rasa.core.agent import Agent if additional_arguments is None: additional_arguments = {} if output: rasa.shared.utils.io.create_directory(output) try: unpacked_model = rasa.model.get_model(model) except ModelNotFound: rasa.shared.utils.cli.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 _agent = Agent.load(unpacked_model) if _agent.policy_ensemble is None: rasa.shared.utils.cli.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.") if isinstance(_agent.interpreter, RegexInterpreter): rasa.shared.utils.cli.print_warning( "No NLU model found. Using default 'RegexInterpreter' for end-to-end " "evaluation. If you added actual user messages to your test stories " "this will likely lead to the tests failing. In that case, you need " "to train a NLU model first, e.g. using `rasa train`.") from rasa.core.test import test kwargs = rasa.shared.utils.common.minimal_kwargs(additional_arguments, test, ["stories", "agent"]) rasa.utils.common.run_in_loop( test(stories, _agent, out_directory=output, **kwargs))
async def test_agent_train(trained_rasa_model: Text): domain = Domain.load("data/test_domains/default_with_slots.yml") loaded = Agent.load(trained_rasa_model) # test domain assert loaded.domain.action_names_or_texts == domain.action_names_or_texts assert loaded.domain.intents == domain.intents assert loaded.domain.entities == domain.entities assert loaded.domain.responses == domain.responses assert [s.name for s in loaded.domain.slots] == [s.name for s in domain.slots] # test policies assert isinstance(loaded.policy_ensemble, SimplePolicyEnsemble) assert [type(p) for p in loaded.policy_ensemble.policies] == [ MemoizationPolicy, RulePolicy, ]
def load_model(): """Loads rasa nlu models Args: model_dir: The model directory of the loaded model Returns: Returns the agent responsible for parsing text """ ROOT = "models/" MODELS = [ "{}{}".format(ROOT, file) for file in os.listdir(ROOT) if "nlu" in file ] MODEL_PATH = sorted(MODELS, key=os.path.getctime, reverse=True)[0] get_interpreter = get_model(MODEL_PATH) return Agent.load(get_interpreter)
async def test_parsing_with_tracker(): tracker = DialogueStateTracker.from_dict("1", [], [Slot("requested_language")]) # we'll expect this value 'en' to be part of the result from the interpreter tracker._set_slot("requested_language", "en") endpoint = EndpointConfig("https://interpreter.com") with aioresponses() as mocked: mocked.post("https://interpreter.com/parse", repeat=True, status=200) # mock the parse function with the one defined for this test with patch.object(RasaNLUHttpInterpreter, "parse", mocked_parse): interpreter = RasaNLUHttpInterpreter(endpoint_config=endpoint) agent = Agent(None, None, interpreter) result = await agent.parse_message_using_nlu_interpreter("lunch?", tracker) assert result["requested_language"] == "en"