def test_ignore_action_unlikely_intent( self, trained_policy: MemoizationPolicy, default_domain: Domain, tracker_events_with_action: List[Event], tracker_events_without_action: List[Event], ): tracker_with_action = DialogueStateTracker.from_events( "test 1", evts=tracker_events_with_action, slots=default_domain.slots) tracker_without_action = DialogueStateTracker.from_events( "test 2", evts=tracker_events_without_action, slots=default_domain.slots) prediction_with_action = trained_policy.predict_action_probabilities( tracker_with_action, default_domain, ) prediction_without_action = trained_policy.predict_action_probabilities( tracker_without_action, default_domain, ) # Memoization shouldn't be affected with the # presence of action_unlikely_intent. assert (prediction_with_action.probabilities == prediction_without_action.probabilities)
async def test_finetune_after_load(self, trained_policy: MemoizationPolicy, default_domain: Domain, tmp_path: Path): trained_policy.persist(tmp_path) loaded_policy = MemoizationPolicy.load(tmp_path, should_finetune=True) assert loaded_policy.finetune_mode new_story = TrackerWithCachedStates.from_events( "channel", domain=default_domain, slots=default_domain.slots, evts=[ ActionExecuted(ACTION_LISTEN_NAME), UserUttered(intent={"name": "why"}), ActionExecuted("utter_channel"), ActionExecuted(ACTION_LISTEN_NAME), ], ) original_train_data = await train_trackers(default_domain, augmentation_factor=20) loaded_policy.train(original_train_data + [new_story], default_domain, RegexInterpreter()) # Get the hash of the tracker state of new story new_story_states, _ = loaded_policy.featurizer.training_states_and_actions( [new_story], default_domain) # Feature keys for each new state should be present in the lookup for states in new_story_states: state_key = loaded_policy._create_feature_key(states) assert state_key in loaded_policy.lookup
def test_memorise_with_nlu(self, trained_policy: MemoizationPolicy, default_domain: Domain): tracker = tracker_from_dialogue(TEST_DEFAULT_DIALOGUE, default_domain) states = trained_policy._prediction_states(tracker, default_domain) recalled = trained_policy.recall(states, tracker, default_domain, None) assert recalled is not None
def test_memorise_with_nlu(self, trained_policy: MemoizationPolicy, default_domain: Domain): filename = "data/test_dialogues/default.json" dialogue = read_dialogue_file(filename) tracker = DialogueStateTracker(dialogue.name, default_domain.slots) tracker.recreate_from_dialogue(dialogue) states = trained_policy._prediction_states(tracker, default_domain) recalled = trained_policy.recall(states, tracker, default_domain) assert recalled is not None
def test_predict_next_action_raises_limit_reached_exception(domain: Domain): interpreter = RegexInterpreter() ensemble = SimplePolicyEnsemble( policies=[RulePolicy(), MemoizationPolicy()]) tracker_store = InMemoryTrackerStore(domain) lock_store = InMemoryLockStore() processor = MessageProcessor( interpreter, ensemble, domain, tracker_store, lock_store, TemplatedNaturalLanguageGenerator(domain.responses), max_number_of_predictions=1, ) tracker = DialogueStateTracker.from_events( "test", evts=[ ActionExecuted(ACTION_LISTEN_NAME), UserUttered("Hi!"), ActionExecuted("test_action"), ], ) tracker.set_latest_action({"action_name": "test_action"}) with pytest.raises(ActionLimitReached): processor.predict_next_action(tracker)
def create_policy( self, featurizer: Optional[TrackerFeaturizer], priority: int ) -> Policy: max_history = None if isinstance(featurizer, MaxHistoryTrackerFeaturizer): max_history = featurizer.max_history return MemoizationPolicy(priority=priority, max_history=max_history)
def train_core( domain_file: Text = "domain.yml", model_directory: Text = "models", model_name: Text = "current", training_data_file: Text = "data/stories.md", ): agent = Agent( domain_file, policies=[ MemoizationPolicy(max_history=3), MappingPolicy(), RestaurantPolicy(batch_size=100, epochs=100, validation_split=0.2), ], ) # augmentation_factor 扩展系数 10 training_data = agent.load_data(training_data_file, augmentation_factor=10) agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. 未打包的的模型文件 model_path = os.path.join(model_directory, model_name, "core") agent.persist(model_path) logger.info("Model trained. Stored in '{}'.".format(model_path)) return model_path
def train_dialogue(domain_file = 'customer_domain.yml', model_path = './models/dialogue', training_data_file = 'stories.md'): ''' This function uses the intent clasifier to build responses to sent messages input : domain_file : containing the actions, templates , entities necessary for underdtanding the dialogue.The file is located in the dialogue system model_path : directory for saving trained dialogue system training_data_file : contains the story file. It is training file needed to train rasa core dialogue system ''' featurizer = MaxHistoryTrackerFeaturizer(BinarySingleStateFeaturizer(), max_history=5) # define featurizer for learning # define a bot agent agent = Agent(domain_file, policies = [MemoizationPolicy(), KerasPolicy(featurizer, epochs=300, batch_size=10)]) # asyncio is needed for concurrent wait during training loop = asyncio.get_event_loop() # define asyncio data = loop.run_until_complete( agent.load_data(training_data_file )) # embedded data in asyncio # let agent train agent.train( data, validation=0.2) # persistent saving of model agent.persist(model_path) # return agent return agent
async def train_core(domain_file="domain.yml", model_directory="models", model_name="current", training_data_file="data/stories.md"): agent = Agent( domain_file, policies=[ MemoizationPolicy(max_history=3), MappingPolicy(), RestaurantPolicy(batch_size=100, epochs=100, validation_split=0.2), ], ) training_data_file = "data/tiny_stories.md" training_data = await agent.load_data(training_data_file, augmentation_factor=10) # show_training_data(training_data) # print(type(training_data)) # print(training_data) for data in training_data: print(type(data)) # viz_domain(data.domain) # viz_TrackerWithCachedStates(data, view_domain=False) # exit() agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. model_path = os.path.join(model_directory, model_name, "core") agent.persist(model_path) logger.info(f"Model trained. Stored in '{model_path}'.") return model_path
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) test_stories = await _generate_trackers( E2E_STORY_FILE_TRIPS_CIRCUIT_BREAKER, agent, use_e2e=True) story_evaluation, num_stories = 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 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_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 default_agent(default_domain): agent = Agent(default_domain, policies=[MemoizationPolicy()], interpreter=RegexInterpreter(), tracker_store=InMemoryTrackerStore(default_domain)) training_data = await agent.load_data(DEFAULT_STORIES_FILE) agent.train(training_data) return agent
async def train_core(domain_file, training_data_file, model_directory): agent = Agent(domain_file, policies=[MemoizationPolicy(max_history=3), MappingPolicy(), KerasPolicy(epochs=500)]) training_data = await agent.load_data(training_data_file, augmentation_factor=10) agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. model_path = os.path.join(model_directory, "core") agent.persist(model_path) logger.info(f"Model trained. Stored in '{model_path}'.") return model_path
async def test_train_memorise(): default_domain = Domain.load(DEFAULT_DOMAIN_PATH_WITH_SLOTS) trackers = await training.load_data( DEFAULT_STORIES_FILE, default_domain, augmentation_factor=50, debug_plots=False ) policy = MemoizationPolicy(priority=1, max_history=None) policy.train(trackers, default_domain) policy.persist("{}/models/memorise".format(prj_dir))
def run_weather_online(interpreter, domain_file="weather_domain.yml", training_data_file='data/stories.md'): agent = Agent(domain_file, policies=[MemoizationPolicy(), KerasPolicy()], interpreter=interpreter) agent.train(training_data_file, max_history=2, batch_size=50, epochs=200, max_training_samples=300) return agent
def train_dialogue(domain_file='domain.yml', model_path='./models/dialogue', training_data_file='./data/stories.md'): agent = Agent(domain_file, policies=[MemoizationPolicy(), KerasPolicy()]) agent.train(training_data_file, max_history=3, epochs=300, batch_size=50, validation_split=0.2, augmentation_factor=50) agent.persist(model_path) return agent
def train_dialogue(domain_file='./data/criminal_domain.yml', model_path='./models/dialogue', training_data_file='./data/stories.md'): agent = Agent(domain_file, policies=[ MemoizationPolicy(), KerasPolicy(max_history=3, epochs=200, batch_size=50) ]) data = agent.load_data(training_data_file) agent.train(data) agent.persist(model_path) return agent
async def train_dialogue(domain_file="domain.yml", model_path="models/dialogue", training_data_file="data/stories.md"): agent = Agent(domain_file, policies=[MemoizationPolicy(max_history=3), MappingPolicy(), RestaurantPolicy(batch_size=100, epochs=400, validation_split=0.2)]) training_data = await agent.load_data(training_data_file) agent.train( training_data ) agent.persist(model_path) return agent
def test_finetune_after_load( self, trained_policy: MemoizationPolicy, resource: Resource, model_storage: ModelStorage, execution_context: ExecutionContext, default_domain: Domain, stories_path: Text, ): execution_context = dataclasses.replace(execution_context, is_finetuning=True) loaded_policy = MemoizationPolicy.load(trained_policy.config, model_storage, resource, execution_context) assert loaded_policy.finetune_mode new_story = TrackerWithCachedStates.from_events( "channel", domain=default_domain, slots=default_domain.slots, evts=[ ActionExecuted(ACTION_LISTEN_NAME), UserUttered(intent={"name": "why"}), ActionExecuted("utter_channel"), ActionExecuted(ACTION_LISTEN_NAME), ], ) original_train_data = train_trackers(default_domain, stories_path, augmentation_factor=20) loaded_policy.train( original_train_data + [new_story], default_domain, ) # Get the hash of the tracker state of new story new_story_states, _ = loaded_policy.featurizer.training_states_and_labels( [new_story], default_domain) # Feature keys for each new state should be present in the lookup for states in new_story_states: state_key = loaded_policy._create_feature_key(states) assert state_key in loaded_policy.lookup
async def test_infer(): default_domain = Domain.load(DEFAULT_DOMAIN_PATH_WITH_SLOTS) trackers = await training.load_data( DEFAULT_STORIES_FILE, default_domain, augmentation_factor=0, debug_plots=True ) policy = MemoizationPolicy.load("{}/models/memorise".format(prj_dir)) for tracker in trackers: tracker_as_states = policy.featurizer.prediction_states([tracker], default_domain) states = tracker_as_states[0] print(states) states = [None, {}, {'prev_action_listen': 1.0, 'intent_greet': 1.0}, {'intent_greet': 1.0, 'prev_utter_greet': 1.0}, {'prev_action_listen': 1.0, 'intent_default': 1.0}] states = [{'prev_action_listen': 1.0, 'intent_greet': 1.0}, {'intent_greet': 1.0, 'prev_utter_greet': 1.0}, {'prev_action_listen': 1.0, 'intent_default': 1.0}, {'prev_utter_default': 1.0, 'intent_default': 1.0}, {'prev_action_listen': 1.0, 'intent_goodbye': 1.0}] recalled = policy.recall(states, tracker, default_domain) print(default_domain.action_names[recalled])
def train_dialogue(domain_file='domain.yml', model_path='./models/dialogue', training_data_file='./data/stories.md'): fallback = FallbackPolicy(fallback_action_name="utter_unclear", core_threshold=0.3, nlu_threshold=0.75) agent = Agent(domain_file, policies=[ MemoizationPolicy(max_history=7), KerasPolicy(current_epoch=100, max_history=7), fallback ]) data = asyncio.run(agent.load_data(training_data_file)) agent.train(data) # agent.train( # data, # epochs=500, # batch_size=50, # validation_split=0.2) agent.persist(model_path) return agent
async def train_core(domain_file: Text = "domain.yml", model_directory: Text = "models", model_name: Text = "core", training_data_file: Text = "data/stories.md",): logging.basicConfig(filename=logfile, level=logging.DEBUG) agent = Agent( domain_file, policies=[ MemoizationPolicy(max_history=3), KerasPolicy(batch_size=100, epochs=400, validation_split=0.2) ] ) training_data = await agent.load_data(training_data_file) agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. model_path = os.path.join(model_directory, model_name) agent.persist(model_path)
def test_memorise( self, trained_policy: MemoizationPolicy, default_domain: Domain, stories_path: Text, ): trackers = train_trackers(default_domain, stories_path, augmentation_factor=20) trained_policy.train(trackers, default_domain) lookup_with_augmentation = trained_policy.lookup trackers = [ t for t in trackers if not hasattr(t, "is_augmented") or not t.is_augmented ] ( all_states, all_actions, ) = trained_policy.featurizer.training_states_and_labels( trackers, default_domain) for tracker, states, actions in zip(trackers, all_states, all_actions): recalled = trained_policy.recall(states, tracker, default_domain, None) assert recalled == actions[0] nums = np.random.randn(default_domain.num_states) random_states = [{ f: num for f, num in zip(default_domain.input_states, nums) }] assert trained_policy._recall_states(random_states) is None # compare augmentation for augmentation_factor of 0 and 20: trackers_no_augmentation = train_trackers(default_domain, stories_path, augmentation_factor=0) trained_policy.train(trackers_no_augmentation, default_domain) lookup_no_augmentation = trained_policy.lookup assert lookup_no_augmentation == lookup_with_augmentation
async def train_core( domain_file: Text = "domain.yml", model_path: Text = "models/core", training_data_file: Text = "data/stories.md", ): agent = Agent( domain_file, policies=[ MemoizationPolicy(max_history=3), MappingPolicy(), RestaurantPolicy(batch_size=100, epochs=400, validation_split=0.2), ], ) training_data = await agent.load_data(training_data_file) agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. agent.persist(model_path) logger.info("Model trained. Stored in '{}'.".format(model_path)) return model_path
def create_policy(self, featurizer, priority): max_history = None if isinstance(featurizer, MaxHistoryTrackerFeaturizer): max_history = featurizer.max_history p = MemoizationPolicy(priority=priority, max_history=max_history) return p
from __future__ import absolute_import from __future__ import division from __future__ import unicode_literals import logging from rasa.core.agent import Agent from rasa.core.policies.keras_policy import KerasPolicy from rasa.core.policies.memoization import MemoizationPolicy if __name__ == 'main': logging.basicConfig(level='info') training_data_file = './data/stories.md' model_path = './models/' agent = Agent('domain.yml', policies=[MemoizationPolicy(), KerasPolicy()]) agent.train(training_data_file, augmentation_factor=50, max_history=5, epochs=100, batch_size=10, validation_split=0.2) agent.persist(model_path)
from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from rasa.core import utils from rasa.core.agent import Agent from rasa.core.policies.memoization import MemoizationPolicy from rasa.core.policies.sklearn_policy import SklearnPolicy if __name__ == '__main__': #utils.io.configure_colored_logging(loglevel="DEBUG") training_data_file = './data/stories.md' model_path = './models/dialogue' agent = Agent("spotybot_domain.yml", policies=[MemoizationPolicy(max_history=2), SklearnPolicy()]) training_data = agent.load_data(training_data_file) agent.train(training_data, augmentation_factor=50, epochs=500, batch_size=10, validation_split=0.2) agent.persist(model_path)
def test_predict_next_action_with_hidden_rules(): rule_intent = "rule_intent" rule_action = "rule_action" story_intent = "story_intent" story_action = "story_action" rule_slot = "rule_slot" story_slot = "story_slot" domain = Domain.from_yaml(f""" version: "2.0" intents: - {rule_intent} - {story_intent} actions: - {rule_action} - {story_action} slots: {rule_slot}: type: text {story_slot}: type: text """) rule = TrackerWithCachedStates.from_events( "rule", domain=domain, slots=domain.slots, evts=[ ActionExecuted(RULE_SNIPPET_ACTION_NAME), ActionExecuted(ACTION_LISTEN_NAME), UserUttered(intent={"name": rule_intent}), ActionExecuted(rule_action), SlotSet(rule_slot, rule_slot), ActionExecuted(ACTION_LISTEN_NAME), ], is_rule_tracker=True, ) story = TrackerWithCachedStates.from_events( "story", domain=domain, slots=domain.slots, evts=[ ActionExecuted(ACTION_LISTEN_NAME), UserUttered(intent={"name": story_intent}), ActionExecuted(story_action), SlotSet(story_slot, story_slot), ActionExecuted(ACTION_LISTEN_NAME), ], ) interpreter = RegexInterpreter() ensemble = SimplePolicyEnsemble( policies=[RulePolicy(), MemoizationPolicy()]) ensemble.train([rule, story], domain, interpreter) tracker_store = InMemoryTrackerStore(domain) lock_store = InMemoryLockStore() processor = MessageProcessor( interpreter, ensemble, domain, tracker_store, lock_store, TemplatedNaturalLanguageGenerator(domain.responses), ) tracker = DialogueStateTracker.from_events( "casd", evts=[ ActionExecuted(ACTION_LISTEN_NAME), UserUttered(intent={"name": rule_intent}), ], slots=domain.slots, ) action, prediction = processor.predict_next_action(tracker) assert action._name == rule_action assert prediction.hide_rule_turn processor._log_action_on_tracker(tracker, action, [SlotSet(rule_slot, rule_slot)], prediction) action, prediction = processor.predict_next_action(tracker) assert isinstance(action, ActionListen) assert prediction.hide_rule_turn processor._log_action_on_tracker(tracker, action, None, prediction) tracker.events.append(UserUttered(intent={"name": story_intent})) # rules are hidden correctly if memo policy predicts next actions correctly action, prediction = processor.predict_next_action(tracker) assert action._name == story_action assert not prediction.hide_rule_turn processor._log_action_on_tracker(tracker, action, [SlotSet(story_slot, story_slot)], prediction) action, prediction = processor.predict_next_action(tracker) assert isinstance(action, ActionListen) assert not prediction.hide_rule_turn
# !/usr/bin/python # -*- coding:utf-8 -*- from rasa.core.agent import Agent from rasa.core.policies.keras_policy import KerasPolicy from rasa.core.policies.memoization import MemoizationPolicy if __name__ == '__main__': agent = Agent("/home/user/xiaoqq/chat_robot/rasa_learn/configs/domain.yml", policies=[MemoizationPolicy(), KerasPolicy()]) agent.visualize( "/home/user/xiaoqq/chat_robot/rasa_learn/data/stories/stories.md", output_file="graph.html", max_history=2)