def train_dialogue(): #domain_file,model_path,training_data_file): utils.configure_colored_logging(loglevel='INFO') agent = Agent(getData()["domain"], policies=[MemoizationPolicy(max_history=2), KerasPolicy()]) training_data = agent.load_data(getData()["stories"]) agent.train(training_data, epochs=400, batch_size=100, validation_split=0.2) agent.persist(getData()["dialogue"]) return agent
def main() -> None: # Running as standalone python application parse_last_positional_argument_as_model_path() arg_parser = create_argument_parser() cmdline_arguments = arg_parser.parse_args() if hasattr(cmdline_arguments, "func"): configure_colored_logging(cmdline_arguments.loglevel) cmdline_arguments.func(cmdline_arguments) elif hasattr(cmdline_arguments, "version"): print_version() else: # user has not provided a subcommand, let's print the help logger.error("No command specified.") arg_parser.print_help() exit(1)
def train_interactive(): import rasa_core.train as tr import rasa_core.utils as utils arg_parser = tr.create_argument_parser() tr.set_default_subparser(arg_parser, 'default') args = [ 'interactive', '-o', 'models/dialog', '-d', 'domain.yml', '-s', 'data/stories/stories.md', '--nlu', 'models/nlu/current', '--endpoints', 'endpoints.yml' ] cmdline_arguments = arg_parser.parse_args(args) additional_args = tr._additional_arguments(cmdline_arguments) utils.configure_colored_logging(cmdline_arguments.loglevel) training_stories = cmdline_arguments.stories tr.do_interactive_learning(cmdline_arguments, training_stories, additional_args)
def trainingBot(to_bot_queue, to_human_queue, base_model, output_model, nlu_model, training_data): utils.configure_colored_logging(loglevel="INFO") max_history = None interactive_learning_on = True channel = TrainingInputChannel(to_bot_queue, to_human_queue) preloaded_model = True if preloaded_model: agent = CustomAgent.load(base_model, NaturalLanguageInterpreter.create(nlu_model)) training_data = agent.load_data(training_data) agent.train_online_preloaded_model(training_data, input_channel=channel, model_path=output_model) else: agent = CustomAgent( "domain.yml", policies=[ MemoizationPolicy(max_history=max_history), KerasPolicy( MaxHistoryTrackerFeaturizer(BinarySingleStateFeaturizer(), max_history=max_history)), FallbackPolicy(fallback_action_name="utter_fallback", nlu_threshold=0.3) ]) training_data = agent.load_data(training_data) agent.interpreter = NaturalLanguageInterpreter.create(nlu_model) agent.train_online(training_data, input_channel=channel, model_path=output_model, augmentation_factor=50, epochs=250, batch_size=10, validation_split=0.2) agent.persist(output_model)
def train_dialogue(): utils.configure_colored_logging(loglevel='DEBUG') training_data_file = './horoscopeBot/data/stories.md' model_path = './horoscopeBot/models/dialogue' fallback = FallbackPolicy(fallback_action_name="action_default_fallback", core_threshold=0.3,nlu_threshold=0.3) agent = Agent('./horoscopeBot/horoscope_domain.yml', policies = [MemoizationPolicy(),KerasPolicy(),SklearnPolicy(),fallback]) training_data = agent.load_data(training_data_file) agent.train( training_data, augmentation_factor = 50, epoch = 500, batch_size =10, validation_split=0.2 ) agent.persist(model_path) return agent
def train_core(): from rasa_core.policies.fallback import FallbackPolicy from rasa_core.policies.keras_policy import KerasPolicy from rasa_core.policies.memoization import MemoizationPolicy from rasa_core.interpreter import RasaNLUInterpreter from rasa_core.agent import Agent from rasa_core import utils, server from rasa_core.channels.channel import UserMessage from rasa_core_sdk.executor import ActionExecutor utils.configure_colored_logging("DEBUG") utils.configure_file_logging("DEBUG", "rasa_core_logs.txt") agent = Agent(os.path.join(current_dir, "sample/domain.yml"), policies=[ MemoizationPolicy(), KerasPolicy(), FallbackPolicy(fallback_action_name="action_default_fallback", core_threshold=0.3, nlu_threshold=0.3)]) data = agent.load_data(os.path.join(current_dir, "sample/stories")) agent.train(data) agent.persist(os.path.join(current_dir, "sample/models/current/dialogue"))
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.keras_policy import KerasPolicy from rasa_core.policies.memoization import MemoizationPolicy from rasa_core.policies.sklearn_policy import SklearnPolicy if __name__ == '__main__': utils.configure_colored_logging(loglevel="DEBUG") training_data_file = './data/stories.md' model_path = './models/dialogue' agent = Agent("horoscope_domain.yml", policies=[MemoizationPolicy(max_history=2), KerasPolicy()]) 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 server(serve_forever=True): # path to your NLU model interpreter = RasaNLUInterpreter("models/nlu/default/current") # path to your dialogues models agent = Agent.load("models/stories", interpreter=interpreter) # http api endpoint for responses input_channel = SimpleWebBot() if serve_forever: agent.handle_channel(HttpInputChannel(7454, "/bot", input_channel)) return agent if __name__ == '__main__': utils.configure_colored_logging(loglevel="INFO") parser = argparse.ArgumentParser(description='starts the bot') parser.add_argument( 'task', choices=["train-nlu", "train-stories", "server", "console"], help="what the bot should do - e.g. run or train?") parser.add_argument("-r", "--remote", dest="remote", default=False, help="write report to FILE", metavar="FILE")
def main(model_directory, nlu_model=None, channel=None, port=None, credentials_file=None): """Run the agent.""" log = logging.getLogger('werkzeug') log.setLevel(logging.WARN) logger.info("Rasa process starting") agent = Agent.load(model_directory, nlu_model) logger.info("Finished loading agent, starting input channel & server.") if channel: input_channel = create_input_channel(channel, port, credentials_file) agent.handle_channel(input_channel) return agent if __name__ == '__main__': # Running as standalone python application arg_parser = create_argument_parser() cmdline_args = arg_parser.parse_args() utils.configure_colored_logging(cmdline_args.loglevel) main(cmdline_args.core, cmdline_args.nlu, cmdline_args.connector, cmdline_args.port, cmdline_args.credentials)
nlu_data_path = load_data(nlu_data_path) else: nlu_data_path = None logger.info("Starting to visualize stories...") agent.visualize(stories_path, output_path, max_history, nlu_training_data=nlu_data_path) full_output_path = "file://{}".format(os.path.abspath(output_path)) logger.info( "Finished graph creation. Saved into {}".format(full_output_path)) import webbrowser webbrowser.open(full_output_path) if __name__ == '__main__': parser = argparse.ArgumentParser( description='Visualize the stories in a dialogue training file') arg_parser = add_arguments(parser) args = arg_parser.parse_args() utils.configure_colored_logging(args.loglevel) stories = rasa_core.cli.train.stories_from_cli_args(args) visualize(args.config[0], args.domain, stories, args.nlu_data, args.output, args.max_history)
import logging import os from rasa_core.utils import configure_colored_logging, AvailableEndpoints from rasa_core.run import start_server, load_agent from rasa_core.interpreter import NaturalLanguageInterpreter from connector import RocketChatInput from tracker_store import ElasticTrackerStore logger = logging.getLogger(__name__) configure_colored_logging(loglevel='DEBUG') def run(core_dir, nlu_dir): configs = { 'user': os.getenv('ROCKETCHAT_BOT_USERNAME'), 'password': os.getenv('ROCKETCHAT_BOT_PASSWORD'), 'server_url': os.getenv('ROCKETCHAT_URL'), } input_channel = RocketChatInput(user=configs['user'], password=configs['password'], server_url=configs['server_url']) _endpoints = AvailableEndpoints.read_endpoints(None) _interpreter = NaturalLanguageInterpreter.create(nlu_dir) elastic_user = os.getenv('ELASTICSEARCH_USER') if elastic_user is None: _tracker_store = ElasticTrackerStore(
return jsonify(probability_dict) return app if __name__ == '__main__': # Running as standalone python application from rasa_core import run arg_parser = run.create_argument_parser() cmdline_args = arg_parser.parse_args() logging.getLogger('werkzeug').setLevel(logging.WARN) logging.getLogger('matplotlib').setLevel(logging.WARN) utils.configure_colored_logging(cmdline_args.loglevel) utils.configure_file_logging(cmdline_args.loglevel, cmdline_args.log_file) logger.warning("USING `rasa_core.server` is deprecated and will be " "removed in the future. Use `rasa_core.run --enable_api` " "instead.") logger.info("Rasa process starting") _endpoints = run.read_endpoints(cmdline_args.endpoints) _interpreter = NaturalLanguageInterpreter.create(cmdline_args.nlu, _endpoints.nlu) _agent = run.load_agent(cmdline_args.core, interpreter=_interpreter, endpoints=_endpoints)
#!/usr/bin/env python import os from rasa_core import utils from rasa_core.channels.console import ConsoleInputChannel from chatbot.nlp_models import dialog, train def run(): classificator = dialog.load_classificator() return train.train_dialog_online(classificator, ConsoleInputChannel()) if __name__ == '__main__': utils.configure_colored_logging(loglevel=os.getenv('LOGLEVEL', 'ERROR')) run()
help="max history to consider when merging " "paths in the output graph") parser.add_argument('-nlu', '--nlu_data', default=None, type=str, help="path of the Rasa NLU training data, " "used to insert example messages into the graph") utils.add_logging_option_arguments(parser) return parser if __name__ == '__main__': parser = create_argument_parser() args = parser.parse_args() utils.configure_colored_logging(args.loglevel) agent = Agent(args.domain, policies=[MemoizationPolicy(), KerasPolicy()]) # this is optional, only needed if the `_greet` type of # messages in the stories should be replaced with actual # messages (e.g. `hello`) if args.nlu_data is not None: from rasa_nlu.training_data import load_data nlu_data = load_data(args.nlu_data) else: nlu_data = None logger.info("Starting to visualize stories...") agent.visualize(args.stories, args.output, args.max_history,
from rasa_core.policies.memoization import MemoizationPolicy from rasa_core.interpreter import RasaNLUInterpreter from config import getData def run_weather_online(input_channel, interpreter, domain_file, training_data_file): agent = Agent(domain_file, policies=[MemoizationPolicy(max_history=3), KerasPolicy()], interpreter=interpreter) training_data = agent.load_data(training_data_file) agent.train_online(training_data, input_channel=input_channel, epochs=400, batch_size=100, validation_split=0.2) if __name__ == '__main__': utils.configure_colored_logging(loglevel='INFO') nlu_interpretter = RasaNLUInterpreter(getData()["model_directory"] + '/default/' + getData()["model_name"]) run_weather_online(ConsoleInputChannel(), nlu_interpretter, getData()["domain"], getData()["stories"])
fixed_model_name="current") return model_directory def run(serve_forever=True): interpreter = RasaNLUInterpreter("models/nlu/default/current") agent = Agent.load("models/dialogue", interpreter=interpreter) if serve_forever: agent.handle_channel(ConsoleInputChannel()) return agent if __name__ == '__main__': utils.configure_colored_logging(loglevel="debug") parser = argparse.ArgumentParser(description='starts the bot') parser.add_argument('task', choices=["train-nlu", "train-dialogue", "run"], help="what the bot should do - e.g. run or train?") task = parser.parse_args().task # decide what to do based on first parameter of the script if task == "train-nlu": train_nlu() elif task == "train-dialogue": train_dialogue() elif task == "run": run()
model_directory = trainer.persist('models/nlu', fixed_model_name="current") return model_directory def run(serve_forever=True): interpreter = RasaNLUInterpreter("models/nlu/default/current") agent = Agent.load("models/current/dialogue", interpreter=interpreter) if serve_forever: agent.handle_channel(ConsoleInputChannel()) return agent if __name__ == '__main__': utils.configure_colored_logging(loglevel="INFO") parser = argparse.ArgumentParser( description='starts the bot') parser.add_argument( 'task', choices=["train-nlu", "train-dialogue", "run"], help="what the bot should do - e.g. run or train?") task = parser.parse_args().task # decide what to do based on first parameter of the script if task == "train-nlu": train_nlu() elif task == "train-dialogue": train_dialogue()
model_directory = trainer.persist('models/nlu/', fixed_model_name="current") return model_directory def run(serve_forever=True): interpreter = RasaNLUInterpreter("models/nlu/default/current") agent = Agent.load("models/dialogue", interpreter=interpreter) if serve_forever: agent.handle_channel(ConsoleInputChannel()) return agent if __name__ == '__main__': utils.configure_colored_logging(verbose=True) parser = argparse.ArgumentParser( description='starts the bot') parser.add_argument( 'task', choices=["train-nlu", "train-dialogue", "run"], help="what the bot should do - e.g. run or train?") task = parser.parse_args().task # decide what to do based on first parameter of the script if task == "train-nlu": train_nlu() elif task == "train-dialogue": train_dialogue()
import os from os import environ as env from gevent.pywsgi import WSGIServer from server import create_app from rasa_core import utils from rasa_core.interpreter import RasaNLUHttpInterpreter utils.configure_colored_logging("DEBUG") user_input_dir = "/app/nlu/" + os.environ["RASA_NLU_PROJECT_NAME"] + "/user_input" if not os.path.exists(user_input_dir): os.makedirs(user_input_dir) nlu_interpreter = RasaNLUHttpInterpreter( model_name = env["RASA_NLU_MODEL_NAME"], token = env["RASA_NLU_SERVER_TOKEN"], server = env["RASA_NLU_SERVER_ADDRESS"], project_name = env["RASA_NLU_PROJECT_NAME"]) app = create_app( model_directory = env["RASA_CORE_MODEL_PATH"], cors_origins="*", loglevel = "DEBUG", logfile = "./logs/rasa_core.log", interpreter = nlu_interpreter) http_server = WSGIServer(('0.0.0.0', 5005), app) http_server.serve_forever()
def enable_logging(): utils.configure_colored_logging(logging.DEBUG) utils.configure_file_logging(logging.DEBUG, './rasa_core.log') log = logging.getLogger('werkzeug') log.setLevel(logging.WARN)