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
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
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
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
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
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
async def test_missing_template_breaks_training(tmpdir, domain_missing_template): training_data_file = "examples/moodbot/data/stories.md" domain_path = utilities.write_text_to_file(tmpdir, "domain.yml", domain_missing_template) agent = Agent(domain_path, policies=[AugmentedMemoizationPolicy()]) training_data = await agent.load_data(training_data_file) with pytest.raises(InvalidDomain): agent.train(training_data)
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
async def default_processor(default_domain, default_nlg): agent = Agent(default_domain, SimplePolicyEnsemble([AugmentedMemoizationPolicy()]), interpreter=RegexInterpreter()) training_data = await agent.load_data(DEFAULT_STORIES_FILE) agent.train(training_data) tracker_store = InMemoryTrackerStore(default_domain) return MessageProcessor(agent.interpreter, agent.policy_ensemble, default_domain, tracker_store, default_nlg)
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 _trained_default_agent(tmpdir_factory: TempdirFactory) -> Tuple[Agent, str]: model_path = tmpdir_factory.mktemp("model").strpath agent = Agent( "data/test_domains/default_with_slots.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
async def _trained_default_agent() -> Tuple[Agent, str]: model_path = "{}/models/".format(prj_dir) agent = Agent( "{}/data/default_with_slots.yml".format(prj_dir), policies=[AugmentedMemoizationPolicy(max_history=3)], ) training_data = await agent.load_data( "{}/data/stories_defaultdomain.md".format(prj_dir)) agent.train(training_data) agent.persist(model_path) 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
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)
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
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
async def train( domain_file: Union[Domain, Text], stories_file: Text, output_path: Text, interpreter: Optional["NaturalLanguageInterpreter"] = None, endpoints: "AvailableEndpoints" = None, dump_stories: bool = False, policy_config: Text = None, exclusion_percentage: int = None, kwargs: Optional[Dict] = None, ): from rasa.core.agent import Agent from rasa.core import config, utils from rasa.core.utils import AvailableEndpoints if not endpoints: endpoints = AvailableEndpoints() if not kwargs: kwargs = {} policies = config.load(policy_config) agent = Agent( domain_file, generator=endpoints.nlg, action_endpoint=endpoints.action, interpreter=interpreter, policies=policies, ) data_load_args, kwargs = utils.extract_args( kwargs, { "use_story_concatenation", "unique_last_num_states", "augmentation_factor", "remove_duplicates", "debug_plots", }, ) training_data = await agent.load_data( stories_file, exclusion_percentage=exclusion_percentage, **data_load_args) agent.train(training_data, **kwargs) agent.persist(output_path, dump_stories) 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
async 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()], ) training_data = await agent.load_data(stories_path) agent.train(training_data) agent.persist(model_path) return agent
async def train( domain_file: Union[Domain, Text], training_resource: Union[Text, "TrainingDataImporter"], output_path: Text, interpreters: Optional[Dict[Text, "NaturalLanguageInterpreter"]] = None, endpoints: "AvailableEndpoints" = None, dump_stories: bool = False, policy_config: Optional[Union[Text, Dict]] = None, exclusion_percentage: int = None, additional_arguments: Optional[Dict] = None, ): from rasa.core.agent import Agent from rasa.core import config, utils from rasa.core.utils import AvailableEndpoints 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, interpreters=interpreters or {}, # fix to avoid model not ready error 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) agent.train(training_data, **additional_arguments) agent.persist(output_path, dump_stories) 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 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 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)
async def test_agent_train(tmpdir, default_domain): training_data_file = "examples/moodbot/data/stories.md" agent = Agent("examples/moodbot/domain.yml", policies=[AugmentedMemoizationPolicy()]) training_data = await agent.load_data(training_data_file) agent.train(training_data) agent.persist(tmpdir.strpath) loaded = Agent.load(tmpdir.strpath) # test domain assert loaded.domain.action_names == agent.domain.action_names assert loaded.domain.intents == agent.domain.intents assert loaded.domain.entities == agent.domain.entities assert loaded.domain.templates == agent.domain.templates assert [s.name for s in loaded.domain.slots ] == [s.name for s in agent.domain.slots] # test policies assert isinstance(loaded.policy_ensemble, type(agent.policy_ensemble)) assert [type(p) for p in loaded.policy_ensemble.policies ] == [type(p) for p in agent.policy_ensemble.policies]
async 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=[CustomPolicy()], ) 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, model_name, "core") agent.persist(model_path) logger.info("Model trained. Stored in '{}'.".format(model_path)) return 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)