Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

Commit

Permalink
domain yaml endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyFaulkner committed Jun 29, 2018
1 parent 1667307 commit 2f650be
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion rasa_core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def instantiate_actions(factory_name, action_classes, action_names,
def _slot_definitions(self):
return {slot.name: slot.persistence_info() for slot in self.slots}

def persist(self, filename):
def _make_domain_data(self):
additional_config = {
"store_entities_as_slots": self.store_entities_as_slots}
action_names = self.action_names[len(Domain.DEFAULT_ACTIONS):]
Expand All @@ -511,9 +511,16 @@ def persist(self, filename):
"action_names": action_names, # names in stories
"action_factory": self._factory_name
}
return domain_data

def persist(self, filename):
domain_data = self._make_domain_data()
utils.dump_obj_as_yaml_to_file(filename, domain_data)

def to_yaml(self):
domain_data = self._make_domain_data()
return utils.dump_obj_as_yaml_to_string(domain_data)

@utils.lazyproperty
def templates(self):
return self._templates
Expand Down
10 changes: 10 additions & 0 deletions rasa_core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@ def update_tracker(sender_id):
agent().tracker_store.save(tracker)
return jsonify(tracker.current_state(should_include_events=True))

@app.route("/domain",
methods=['GET'])
@cross_origin(origins=cors_origins)
@requires_auth(auth_token)
@ensure_loaded_agent(agent)
def get_domain_yaml():
"""Use a list of events to set a conversations tracker to a state."""
domain_yaml = agent().domain.to_yaml()
return jsonify(domain_yaml)

@app.route("/conversations/<sender_id>/parse",
methods=['GET', 'POST', 'OPTIONS'])
@cross_origin(origins=cors_origins)
Expand Down
22 changes: 22 additions & 0 deletions rasa_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from builtins import input, range, str
from numpy import all, array
from typing import Text, Any, List, Optional, Tuple, Dict, Set
from cStringIO import StringIO

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -341,6 +342,27 @@ def dump_obj_as_yaml_to_file(filename, obj):
with io.open(filename, 'w', encoding="utf-8") as yaml_file:
yaml_writer.dump(obj, yaml_file)

def dump_obj_as_yaml_to_string(obj):
"""Writes data (python dict) to a yaml string."""

if six.PY2:
import yaml
str_io = StringIO()

yaml.safe_dump(obj, str_io,
default_flow_style=False,
allow_unicode=True)
return str_io.get_value()
else:
import ruamel.yaml

yaml_writer = ruamel.yaml.YAML(pure=True, typ="safe")
yaml_writer.unicode_supplementary = True
yaml_writer.default_flow_style = False
yaml_writer.allow_unicode = True
str_io = StringIO()
yaml_writer.dump(obj, str_io)
return str_io.get_value()

def read_file(filename, encoding="utf-8"):
"""Read text from a file."""
Expand Down

0 comments on commit 2f650be

Please sign in to comment.