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 _trained_default_agent(tmpdir_factory: TempdirFactory) -> 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(DEFAULT_STORIES_FILE) agent.train(training_data) agent.persist(model_path) return agent
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 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 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 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 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
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 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 _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='./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 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
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 test_agent_with_model_server_in_thread( model_server: TestClient, moodbot_domain: Domain, moodbot_metadata: Any ): model_endpoint_config = EndpointConfig.from_dict( {"url": model_server.make_url("/model"), "wait_time_between_pulls": 2} ) agent = Agent() agent = await rasa.core.agent.load_from_server( agent, model_server=model_endpoint_config ) await asyncio.sleep(5) assert agent.fingerprint == "somehash" assert hash(agent.domain) == hash(moodbot_domain) agent_policies = { utils.module_path_from_instance(p) for p in agent.policy_ensemble.policies } moodbot_policies = set(moodbot_metadata["policy_names"]) assert agent_policies == moodbot_policies assert model_server.app.number_of_model_requests == 1 jobs.kill_scheduler()