Ejemplo n.º 1
0
def run_fake_user(input_channel, max_training_samples=10, serve_forever=True):
    customer = Customer()
    training_data = 'examples/babi/data/babi_task5_fu_rasa_fewer_actions.md'

    logger.info("Starting to train policy")

    agent = Agent("examples/restaurant_domain.yml",
                  policies=[MemoizationPolicy(),
                            KerasPolicy()],
                  interpreter=RegexInterpreter())

    agent.train_online(training_data,
                       input_channel=input_channel,
                       epochs=1,
                       max_training_samples=max_training_samples)

    while serve_forever:
        tracker = agent.tracker_store.retrieve('default')
        back = customer.respond_to_action(tracker)
        if back == 'reset':
            agent.handle_message("_greet",
                                 output_channel=ConsoleOutputChannel())
        else:
            agent.handle_message(back, output_channel=ConsoleOutputChannel())

    return agent
def run_fake_user(input_channel, max_training_samples=10, serve_forever=True):
    logger.info("Starting to train policy")
    agent = Agent(RASA_CORE_DOMAIN_PATH,
                  policies=[MemoizationPolicy(), KerasPolicy()],
                  interpreter=RegexInterpreter())

    agent.train_online(RASA_CORE_TRAINING_DATA_PATH,
                       input_channel=input_channel,
                       epochs=RASA_CORE_EPOCHS,
                       max_training_samples=max_training_samples)

    while serve_forever:
        agent.handle_message(UserMessage(back, ConsoleOutputChannel()))

    return agent
Ejemplo n.º 3
0
def test_tracker_state_regression(default_domain):
    class HelloInterpreter(NaturalLanguageInterpreter):
        def parse(self, text):
            intent = "greet" if 'hello' in text else "nlu"
            return {"text": text, "intent": {"name": intent}, "entities": []}

    agent = Agent(domain, [SimplePolicy()],
                  BinaryFeaturizer(),
                  interpreter=HelloInterpreter())

    n_actions = []
    for i in range(0, 2):
        agent.handle_message("hello")
    tracker = agent.tracker_store.get_or_create_tracker('nlu')

    # Ensures that the tracker has changed between the utterances
    # (and wasn't reset in between them)
    expected = ("action_listen;"
                "_greet;utter_greet;action_listen;"
                "_greet;utter_greet;action_listen")
    assert ";".join([e.as_story_string() for e in tracker.events]) == expected
Ejemplo n.º 4
0
def replay_events(tracker: DialogueStateTracker, agent: Agent) -> None:
    """Take a tracker and replay the logged user utterances against an agent.

    During replaying of the user utterances, the executed actions and events
    created by the agent are compared to the logged ones of the tracker that
    is getting replayed. If they differ, a warning is logged.

    At the end, the tracker stored in the agent's tracker store for the
    same sender id will have quite the same state as the one
    that got replayed."""

    actions_between_utterances = []
    last_prediction = [ACTION_LISTEN_NAME]

    for i, event in enumerate(tracker.events_after_latest_restart()):
        if isinstance(event, UserUttered):
            _check_prediction_aligns_with_story(last_prediction,
                                                actions_between_utterances)

            actions_between_utterances = []
            print(utils.wrap_with_color(event.text, utils.bcolors.OKGREEN))
            out = CollectingOutputChannel()
            agent.handle_message(event.text,
                                 sender_id=tracker.sender_id,
                                 output_channel=out)
            for m in out.messages:
                console.print_bot_output(m)

            tracker = agent.tracker_store.retrieve(tracker.sender_id)
            last_prediction = actions_since_last_utterance(tracker)

        elif isinstance(event, ActionExecuted):
            actions_between_utterances.append(event.action_name)

    _check_prediction_aligns_with_story(last_prediction,
                                        actions_between_utterances)
Ejemplo n.º 5
0
class Query_answer_unit(object):
    """ This object contains tools to answer in natural language any message Fib related

	Attributes:
		nlu(:class:`Fibot.NLP.nlu.NLU_unit`): Object that interprets queries
		training_data_file(:obj:`str`): String indicating the path to the stories markdown file
		model_path(:obj:`str`): String indicating where the dialog model is
		agent_ca(:class:`rasa_core.agent.Agent`): Agent capable of handling any incoming messages in catalan
		agent_es(:class:`rasa_core.agent.Agent`): Agent capable of handling any incoming messages in spanish
		agent_en(:class:`rasa_core.agent.Agent`): Agent capable of handling any incoming messages in english
	"""
    def __init__(self):
        self.nlu = NLU_unit()
        self.training_data_file = './Fibot/NLP/core/stories.md'
        self.domain_path = './Fibot/NLP/core/domain.yml'
        self.model_path = './models/dialogue'
        self.agent_ca = Agent(self.domain_path,
                              policies=[MemoizationPolicy(),
                                        KerasPolicy()])
        self.agent_es = Agent(self.domain_path,
                              policies=[MemoizationPolicy(),
                                        KerasPolicy()])
        self.agent_en = Agent(self.domain_path,
                              policies=[MemoizationPolicy(),
                                        KerasPolicy()])
        self.agent_ca.toggle_memoization(activate=True)
        self.agent_es.toggle_memoization(activate=True)
        self.agent_en.toggle_memoization(activate=True)

    def log(self, text):
        print(colored("LOG: {}".format(text), 'cyan'))

    """
		Parameters:
			train (:obj:`bool`): Specifies if the agents have to be trained
		This function loads the model into the agents, and trains them if necessary
	"""

    def load(self, trainNLG=False, trainNLU=False, train_list=None):
        self.log("Cargando word vectors")
        self.nlu.load(trainNLU, train_list=train_list)
        self.log("Modelos NLU cargados")
        if trainNLG: self.train()
        self.agent_ca = Agent.load(self.model_path,
                                   interpreter=self.nlu.interpreter_ca)
        self.agent_es = Agent.load(self.model_path,
                                   interpreter=self.nlu.interpreter_es)
        self.agent_en = Agent.load(self.model_path,
                                   interpreter=self.nlu.interpreter_en)
        self.log("Agentes de diálogo cargados")

    """
		Parameters:
			augmentation_factor (:obj:`int`): augmentation factor for the training
			max_history (:obj:`int`): max_history factor for the training
			epochs (:obj:`int`): epochs (steps) for the training
			batch_size (:obj:`int`): batch_size for the training
			validation_split (:obj:`int`): validation_split factor for the error calculation

		This function trains the agents and saves the models in the dialog's model path
	"""

    def train(self,
              augmentation_factor=200,
              max_history=7,
              epochs=300,
              batch_size=256,
              validation_split=0.3):
        self.agent_es.train(
            self.training_data_file,
            #augmentation_factor=augmentation_factor,
            #max_history=max_history,
            epochs=epochs,
            batch_size=batch_size,
            validation_split=validation_split)
        self.agent_es.persist(self.model_path)

    """
		Parameters:
			augmentation_factor (:obj:`int`): augmentation factor for the training
			max_history (:obj:`int`): max_history factor for the training
			epochs (:obj:`int`): epochs (steps) for the training
			batch_size (:obj:`int`): batch_size for the training
			validation_split (:obj:`int`): validation_split factor for the error calculation

		This function makes it possible to generate new stories manually.
	"""

    def train_manual(self,
                     augmentation_factor=50,
                     max_history=2,
                     epochs=500,
                     batch_size=50,
                     validation_split=0.2):
        self.agent_es.train_online(self.training_data_file,
                                   input_channel=ConsoleInputChannel(),
                                   augmentation_factor=augmentation_factor,
                                   max_history=max_history,
                                   epochs=epochs,
                                   batch_size=batch_size,
                                   validation_split=validation_split)

    """
		Parameters:
			message (:obj:`str`): the incoming message from some user
			sender_id(:obj:`str`): The id (chat_id) of the sender of the messages
			language(:obj:`str`): The language of the sender ('ca', 'es' or 'en')
			debug(:obj:`bool`): Boolean value indicating wether it has to output model's response

		This function returns the response from the agent using the actions
		defined in Fibot/NLP/core/actions.py
	"""

    def get_response(self,
                     message,
                     sender_id=UserMessage.DEFAULT_SENDER_ID,
                     language='es',
                     debug=True):
        confidence = self.nlu.get_intent(message, language)['confidence']
        if debug:
            print("\n\n\n#######  UN USUARIO HA DICHO: {} #######".format(
                colored(message, 'magenta')))
            print("\n\nINFORMACIÓN DE MENSAJE:")
            print("__________________________________________")
            print("El intérprete ha predecido la siguiente intención:")
            intent = self.nlu.get_intent(message, language)
            entities = self.nlu.get_entities(message, language)
            print('Intención: ' +
                  colored(intent['name'], 'cyan', attrs=['bold']))
            print('Confianza: ' +
                  colored(str(intent['confidence'])[:8], 'cyan'))
            if entities: print("\nY las siguientes entidades:")
            else: print("\nNo se han encontrado entidades en el mensaje")
            i = 0
            for entity in entities:
                print(colored('[' + str(i) + ']', 'red'))
                print('Tipo: ' +
                      colored(entity['entity'], 'cyan', attrs=['bold']))
                print('Valor: ' +
                      colored(entity['value'], 'cyan', attrs=['bold']))
                print('Confianza: ' +
                      colored(str(entity['confidence'])[:8], 'cyan'))
                i += 1
        if confidence < 0.5:
            with open('./Data/error_responses.json', 'rb') as fp:
                messages = json.load(fp)['not_understand']
            return [{
                'recipient_id':
                sender_id,
                'text':
                messages[language][randint(0,
                                           len(messages[language]) - 1)]
            }]
        if language == 'ca':
            return self.agent_ca.handle_message(message, sender_id=sender_id)
        elif language == 'es':
            return self.agent_es.handle_message(message, sender_id=sender_id)
        else:
            return self.agent_en.handle_message(message, sender_id=sender_id)
Ejemplo n.º 6
0
class Query_answer_unit(object):

	""" This object contains tools to answer in natural language any message Fib related

	Attributes:
		nlu(:class:`Fibot.NLP.nlu.NLU_unit`): Object that interprets queries
		training_data_file(:obj:`str`): String indicating the path to the stories markdown file
		model_path(:obj:`str`): String indicating where the dialog model is
		domain_path(:obj:`str`): String indicating where the domain yml file is
		agent(:class:`rasa_core.agent.Agent`): Agent capable of handling any incoming messages
	"""
	def __init__(self):
		self.nlu = NLU_unit()
		self.training_data_file = './Fibot/NLP/core/stories.md'
		self.domain_path = './Fibot/NLP/core/domain.yml'
		self.model_path = './models/dialogue'
		self.agent =  Agent(self.domain_path,
			                  policies=[MemoizationPolicy(), KerasPolicy()])

	"""
		Parameters:
			train(:obj:`bool`): Specifies if the agent has to be trained
		This function loads the model into the agent, and trains if necessary
	"""
	def load(self, train=False):
		self.nlu.load(train)
		if train: self.train()
		self.agent = Agent.load(self.model_path,
				interpreter = self.nlu.interpreter)

	"""
		Parameters:
			augmentation_factor(:obj:`int`): augmentation factor for the training
			max_history(:obj:`int`): max_history factor for the training
			epochs(:obj:`int`): epochs (steps) for the training
			batch_size(:obj:`int`): batch_size for the training
			validation_split(:obj:`int`): validation_split factor for the error calculation

		This function trains the agent and saves the model in the dialog's model path
	"""
	def train(self, augmentation_factor=50, max_history=2, epochs=500, batch_size=50, validation_split=0.2):
		self.agent.train(self.training_data_file,
			augmentation_factor=augmentation_factor,
			max_history=max_history,
			epochs=epochs,
		 	batch_size=batch_size,
			validation_split=validation_split
		)
		self.agent.persist(self.model_path)

	"""

	"""
	def train_manual(self, augmentation_factor=50, max_history=2, epochs=500, batch_size=50, validation_split=0.2):
		self.agent.train_online(self.training_data_file,
			input_channel = ConsoleInputChannel(),
			augmentation_factor=augmentation_factor,
			max_history=max_history,
			epochs=epochs,
		 	batch_size=batch_size,
			validation_split=validation_split
		)

	"""
		Parameters:
			message(:obj:`str`): the incoming message from some user

		This function returns the response from the agent using the actions
		defined in Fibot/NLP/core/actions.py
	"""
	def get_response(self, message, sender_id=UserMessage.DEFAULT_SENDER_ID, debug=True):
		if debug:
			print("Interpreter understood the following intent:")
			pprint(self.nlu.get_intent(message))
			print("And the following entities:")
			pprint(self.nlu.get_entities(message))
		return self.agent.handle_message(message, sender_id=sender_id)
Ejemplo n.º 7
0
interpreter = trainer.train(training_data)
# storeing it for future
model_directory = trainer.persist("./models/nlu", fixed_model_name="current")
print("Done")

print("STEP 2: Training the CORE model")
fallback = FallbackPolicy(fallback_action_name="utter_default",
                          core_threshold=0.2,
                          nlu_threshold=0.1)

agent = Agent(domain='restaurant_domain.yml',
              policies=[
                  MemoizationPolicy(),
                  KerasPolicy(validation_split=0.0, epochs=200), fallback
              ])
training_data = agent.load_data('Core_Stories.md')
agent.train(training_data)
agent.persist('models/dialogue')
print("Done")
print("STEP 3: Starting the Bot")
from rasa_core.agent import Agent
agent = Agent.load('models/dialogue', interpreter=model_directory)

print("Your bot is ready to talk! Type your messages here or send 'stop'")
while True:
    a = input("You:")
    if a == 'stop':
        break
    responses = agent.handle_message(a)
    for response in responses:
        print("Bot: " + response["text"])
Ejemplo n.º 8
0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
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/examples/rasa/stories.md'
    model_path = 'models/dialogue'
    agent = Agent("domain.yml", policies=[MemoizationPolicy(), KerasPolicy()])
    agent.train(training_data_file,
                augmentation_factor=50,
                max_history=2,
                epochs=500,
                batch_size=10,
                validation_split=0.2)
    agent.persist(model_path)
    print(agent.handle_message("hi"))
    print(agent.handle_message("My name is eduardo"))
    print(agent.handle_message("my email is [email protected]"))