def test_training_data_is_reproducible(): training_data_file = "data/test_moodbot/data/stories.yml" agent = Agent("data/test_moodbot/domain.yml") training_data = agent.load_data(training_data_file) # make another copy of training data same_training_data = 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())
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
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_rule_based_agent( moodbot_domain: Domain, train_file_name: Path, monkeypatch: MonkeyPatch, ignore_action_unlikely_intent: bool, ) -> Agent: # We need `RulePolicy` to predict the correct actions # in a particular conversation context as seen during training. # Since it can get affected by `action_unlikely_intent` being triggered in # some cases. We monkey-patch the method which creates # prediction states to ignore `action_unlikely_intent`s if needed. monkeypatch.setattr( RulePolicy, "_prediction_states", _custom_prediction_states_for_rules(ignore_action_unlikely_intent), ) deterministic_policy = RulePolicy(restrict_rules=False) agent = Agent(moodbot_domain, SimplePolicyEnsemble([deterministic_policy])) training_data = agent.load_data(str(train_file_name)) # Make the trackers compatible with rules # so that they are picked up by the policy. for tracker in training_data: tracker.is_rule_tracker = True agent.train(training_data) return agent
def train_dialogue(domain_file='./config/domain.yml', model_path='./models/dialogue', training_data_file='./data/stories.md'): policies = config.load("./config/config.yml") agent = Agent(domain_file, policies=policies) loop = asyncio.get_event_loop() data = loop.run_until_complete(agent.load_data(training_data_file)) agent.train(data) agent.persist(model_path) return agent
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
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 train(): domain_file = "data/sct11/rasa_models/sct11_domain.yml" caller_model_path = "data/sct11/rasa_models/caller" callee_model_path = "data/sct11/rasa_models/callee" caller_training = "data/sct11/rasa_models/sct11_story_caller.md" callee_training = "data/sct11/rasa_models/sct11_story_callee.md" caller_agent = Agent(domain_file, policies=[RandomChoicePolicy()]) callee_agent = Agent(domain_file, policies=[RandomChoicePolicy()]) caller_train_data = caller_agent.load_data(caller_training) caller_agent.train(caller_train_data, epochs=100, batch_size=100, validation_split=0.2) caller_agent.persist(caller_model_path) callee_train_data = callee_agent.load_data(callee_training) callee_agent.train(callee_train_data, epochs=100, batch_size=100, validation_split=0.2) callee_agent.persist(callee_model_path)
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
def _trained_default_agent(tmpdir_factory: TempdirFactory, stories_path: Text) -> Agent: model_path = tmpdir_factory.mktemp("model").strpath agent = Agent( "data/test_domains/default_with_slots.yml", policies=[AugmentedMemoizationPolicy(max_history=3), RulePolicy()], model_directory=model_path, ) training_data = agent.load_data(stories_path) agent.train(training_data) agent.persist(model_path) 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(max_history=3), KerasPolicy()]) #training_data = agent.load_data(training_data_file) #agent.train(training_data,epochs=400,batch_size=100,validation_split=0.2) #agent.persist(model_path) policies = policy_config.load("./policies.yml") agent = Agent(domain_file, policies=policies) loop = asyncio.get_event_loop() dataa = loop.run_until_complete(agent.load_data(training_data_file)) agent.train(dataa) agent.persist(model_path) return agent
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 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 = agent.load_data(e2e_story_file_trips_circuit_breaker_path) agent.train(training_data) 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
# !/usr/bin/python # -*- coding:utf-8 -*- from rasa.core.agent import Agent from rasa.core.interpreter import RasaNLUInterpreter from rasa.core.policies.keras_policy import KerasPolicy # agent=Agent(domain=None, policies=None, interpreter=None, generator=None, # tracker_store=None, lock_store=None, action_endpoint=None, # fingerprint=None, model_directory=None, model_server=None, remote_storage=None, path_to_model_archive=None) if __name__ == '__main__': domain_file = '/home/user/xiaoqq/chat_robot/rasa_learn/configs/domain_slot.yml' agent = Agent(domain_file, policies=[KerasPolicy(validation_split=0.0, epochs=400)]) training_data_file = '/home/user/xiaoqq/chat_robot/rasa_learn/data/nlu/nlu_slot.md' training_data = agent.load_data(training_data_file) model_path = '/home/user/xiaoqq/chat_robot/rasa_learn/data/models' agent.train(training_data ) agent.persist(model_path)
model_dir = os.path.abspath(os.path.join(root_dir, 'models')) policy_fixed_model_name = "dialogue" # ------------ # In[23]: policies = config.load(policy_config) # In[24]: agent = Agent(domain_config, policies=policies) # In[25]: loop = asyncio.get_event_loop() training_data = loop.run_until_complete(agent.load_data(training_data_file)) # In[26]: agent.train(training_data) # In[27]: agent_model_path = os.path.join(model_dir, policy_fixed_model_name) # In[28]: agent.persist(agent_model_path) # In[29]: