def run_concertbot_online(input_channel, interpreter):
    training_data_file = 'examples/concerts/data/stories.md'

    agent = Agent("examples/concerts/concert_domain.yml",
                  policies=[MemoizationPolicy(),
                            ConcertPolicy()],
                  interpreter=interpreter)

    agent.train_online(training_data_file,
                       input_channel=input_channel,
                       max_history=2,
                       batch_size=50,
                       epochs=200,
                       max_training_samples=300)

    return agent
Ejemplo n.º 2
0
def run_concerts(serve_forever=True):
    agent = Agent.load("examples/concerts/models/policy/init",
                       interpreter=RegexInterpreter())

    if serve_forever:
        agent.handle_channel(ConsoleInputChannel())
    return agent
Ejemplo n.º 3
0
def test_tracker_state_regression(default_domain):
    from conversationinsights.channels import UserMessage

    class HelloInterpreter(NaturalLanguageInterpreter):
        def parse(self, text):
            intent = "greet" if 'hello' in text else "default"
            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('default')
        n_actions.append(len(tracker.latest_action_ids))
    # Ensures that the tracker has changed between the utterances (and wasn't reset in between them)
    assert n_actions[0] != n_actions[1]
    def run_online_training(self,
                            ensemble,
                            domain,
                            interpreter=None,
                            input_channel=None):
        from conversationinsights.agent import Agent
        if interpreter is None:
            interpreter = RegexInterpreter()

        bot = Agent(domain,
                    ensemble,
                    featurizer=self.featurizer,
                    interpreter=interpreter)
        bot.toggle_memoization(False)

        bot.handle_channel(
            input_channel if input_channel else ConsoleInputChannel())
Ejemplo n.º 5
0
def train_dialogue_model(domain_file, stories_file, output_path, kwargs):
    agent = Agent(domain_file, policies=[MemoizationPolicy(), KerasPolicy()])

    agent.train(stories_file, validation_split=0.1, **kwargs)

    agent.persist(output_path)
def _test_stories(story_file,
                  policy_model_path,
                  nlu_model_path,
                  max_stories=None):
    """Test the stories from a file, running them through the stored model."""
    def actions_since_last_utterance(tracker):
        actions = []
        for e in reversed(tracker.events):
            if isinstance(e, UserUttered):
                break
            elif isinstance(e, ActionExecuted):
                actions.append(e.action_name)
        actions.reverse()
        return actions

    if nlu_model_path is not None:
        interpreter = RasaNLUInterpreter(model_directory=nlu_model_path)
    else:
        interpreter = RegexInterpreter()

    agent = Agent.load(policy_model_path, interpreter=interpreter)
    stories = _get_stories(story_file, agent.domain, max_stories=max_stories)
    preds = []
    actual = []

    logger.info("Evaluating {} stories\nProgress:".format(len(stories)))

    for s in tqdm(stories):
        sender = "default-" + uuid.uuid4().hex

        dialogue = s.as_dialogue(sender, agent.domain)
        actions_between_utterances = []
        last_prediction = []

        for i, event in enumerate(dialogue.events[1:]):
            if isinstance(event, UserUttered):
                p, a = _min_list_distance(last_prediction,
                                          actions_between_utterances)
                preds.extend(p)
                actual.extend(a)

                actions_between_utterances = []
                agent.handle_message(event.text, sender=sender)
                tracker = agent.tracker_store.retrieve(sender)
                last_prediction = actions_since_last_utterance(tracker)

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

        if last_prediction:
            preds.extend(last_prediction)
            preds_padding = len(actions_between_utterances) - \
                            len(last_prediction)
            preds.extend(["None"] * preds_padding)

            actual.extend(actions_between_utterances)
            actual_padding = len(last_prediction) - \
                             len(actions_between_utterances)
            actual.extend(["None"] * actual_padding)

    return actual, preds
Ejemplo n.º 7
0
 def _create_agent(model_directory, nlu_model):
     return Agent.load(model_directory, nlu_model)
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 examples.concerts.policy import ConcertPolicy
from conversationinsights.agent import Agent
from conversationinsights.policies.memoization import MemoizationPolicy

if __name__ == '__main__':
    logging.basicConfig(level="INFO")

    training_data_file = 'examples/concerts/data/stories.md'
    model_path = 'examples/concerts/models/policy/init'

    agent = Agent("examples/concerts/concert_domain.yml",
                  policies=[MemoizationPolicy(),
                            ConcertPolicy()])

    agent.train(training_data_file,
                augmentation_factor=50,
                max_history=2,
                epochs=500,
                batch_size=10,
                validation_split=0.2)

    agent.persist(model_path)