Skip to content

Commit

Permalink
Added graph output, usefull for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
YuukanOO committed Dec 13, 2018
1 parent 3a9be57 commit 7591027
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
20 changes: 18 additions & 2 deletions pytlas/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Agent:
"""

def __init__(self, interpreter, model=None, handlers=None, **meta):
def __init__(self, interpreter, model=None, handlers=None, transitions_graph_path=None, **meta):
"""Initialize an agent.
When skills ask or answer something to the client, they can send markdown formatted text
Expand All @@ -53,12 +53,14 @@ def __init__(self, interpreter, model=None, handlers=None, **meta):
interpreter (Interpreter): Interpreter used to convert human language to intents
model (object): Model which will receive events raised by this agent instance
handlers (dict): Dictionary of intent: handler to use. If no one is provided, all handlers registered will be used instead.
transitions_graph_path (str): If pygraphviz is installed, where to output the transitions graph
meta (dict): Every other properties will be made available through the self.meta property
"""

self._logger = logging.getLogger(self.__class__.__name__.lower())
self._interpreter = interpreter
self._transitions_graph_path = transitions_graph_path

self._on_ask = None
self._on_answer = None
Expand Down Expand Up @@ -94,7 +96,18 @@ def build(self):
intents = [i for i in self._interpreter.intents if not is_builtin(i)]
states = [STATE_ASLEEP, STATE_ASK, STATE_FALLBACK, STATE_CANCEL] + intents

self._machine = Machine(
MachineKlass = Machine

if self._transitions_graph_path:
try:
import pygraphviz
from transitions.extensions import GraphMachine

MachineKlass = GraphMachine
except:
self._logger.error('Could not use a GraphMachine, is pygraphviz installed?')

self._machine = MachineKlass(
model=self,
states=states,
send_event=True,
Expand Down Expand Up @@ -132,6 +145,9 @@ def build(self):
intent,
after=self._on_intent)

if self._transitions_graph_path and getattr(self._machine, 'get_graph', None):
self._machine.get_graph().draw(self._transitions_graph_path, prog='dot')

@property
def model(self):
"""Retrieve the model associated with this agent instance.
Expand Down
5 changes: 3 additions & 2 deletions pytlas/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pytlas.cli.utils import install_logs
from pytlas.cli.config import get, getboolean, write_config, log_configuration, \
CONFIG_DEFAULT_FILENAME, OPT_VERBOSE, OPT_DEBUG, OPT_LANG, OPT_SKILLS, OPT_CACHE, \
OPT_WATCH, OPT_TRAINING_FILE, OPT_PARSE, OPT_WATCH, OPT_DRY
OPT_WATCH, OPT_TRAINING_FILE, OPT_PARSE, OPT_WATCH, OPT_DRY, OPT_GRAPH
from pytlas.importers import restrict_load_languages, import_skills
from pytlas.pam import get_loaded_skills, install_skills, uninstall_skills, update_skills
import click, logging, os
Expand All @@ -16,6 +16,7 @@
@click.option('-l', '--lang', help='Lang of the interpreter to use')
@click.option('-s', '--skills', type=click.Path(), help='Specifies the directory containing pytlas skills')
@click.option('-c', '--cache', type=click.Path(), help='Path to the directory where engine cache will be outputted')
@click.option('-g', '--graph', type=click.Path(), help='Output the transitions graph to the given path')
@write_config
def main():
"""An open-source 馃 assistant library built for people and made to be super easy to setup and understand.
Expand Down Expand Up @@ -51,7 +52,7 @@ def repl():
interpreter.fit_from_skill_data()

if not getboolean(OPT_DRY):
Prompt(Agent(interpreter, **os.environ), get(OPT_PARSE)).cmdloop()
Prompt(Agent(interpreter, transitions_graph_path=get(OPT_GRAPH), **os.environ), get(OPT_PARSE)).cmdloop()
except ImportError:
logging.critical('Could not import the "snips" interpreter, is "snips-nlu" installed?')

Expand Down
1 change: 1 addition & 0 deletions pytlas/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
OPT_PARSE = 'parse'
OPT_WATCH = 'watch'
OPT_TRAINING_FILE = 'training_file'
OPT_GRAPH = 'graph'

CONFIG_SECTION = 'pytlas'
CONFIG_DEFAULT_FILENAME = 'pytlas.conf'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
include_package_data=True,
install_requires=[
'click==7.0',
'transitions==0.6.4',
'transitions==0.6.9',
'fuzzywuzzy==0.16.0',
'colorlog==3.1.4',
'pychatl==1.2.3',
Expand Down

0 comments on commit 7591027

Please sign in to comment.