Skip to content
This repository has been archived by the owner on Feb 25, 2020. It is now read-only.

Commit

Permalink
Move chatbot from bin
Browse files Browse the repository at this point in the history
* Add chatbot models

Add chatbot built models to source control

Signed-off-by: PGobz <p.gobin@gmail.com>

* Address Rasa NLU issue

See explosion/spaCy#2995

Bump base Core image to 12.3, rebuild training data, and
address dependency issue.

Signed-off-by: PGobz <p.gobin@gmail.com>

* Move chatbot from bin

- Move server start to entrypoint
- Rename build -> models
- Move train to module inside top-level chatbot dir

Signed-off-by: PGobz <p.gobin@gmail.com>
  • Loading branch information
pgobin committed Dec 7, 2018
1 parent e9764f2 commit cb7ff9b
Show file tree
Hide file tree
Showing 27 changed files with 2,872 additions and 85 deletions.
77 changes: 5 additions & 72 deletions bin/train
Original file line number Diff line number Diff line change
Expand Up @@ -15,81 +15,14 @@
# limitations under the License.
# -----------------------------------------------------------------------------

import argparse
import os
import sys
import logging
from os.path import dirname as DIRNAME

from rasa_nlu.training_data import load_data
from rasa_nlu.model import Trainer
from rasa_nlu import config

from rasa_core.agent import Agent
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.fallback import FallbackPolicy
TOP_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.insert(0, TOP_DIR)

from chatbot.train.main import main

LOGGER = logging.getLogger(__name__)
LOGGER.level = logging.INFO
LOGGER.addHandler(logging.StreamHandler(sys.stdout))


TOP_DIR = DIRNAME(DIRNAME(__file__))
PROJECT_DIR = os.path.join(TOP_DIR, "chatbot")

NLU_DATA = os.path.join(PROJECT_DIR, "data/nlu.md")
NLU_CONFIG = os.path.join(PROJECT_DIR, "nlu_config.yml")
NLU_MODEL = os.path.join(PROJECT_DIR, "build/")

CORE_DATA = os.path.join(PROJECT_DIR, "data/stories.md")
CORE_DOMAIN = os.path.join(PROJECT_DIR, "domain.yml")
CORE_MODEL = os.path.join(PROJECT_DIR, "build/current/core")


def parse_args(args):
parser = argparse.ArgumentParser()
parser.add_argument(
"--nlu", help="Train the language processing model", action="store_true"
)
parser.add_argument(
"--core", help="Train the dialog engine model", action="store_true"
)
return parser.parse_args(args)


def train_nlu():
LOGGER.info("[NLU] Loading training data from %s", NLU_DATA)
training_data = load_data(NLU_DATA)

LOGGER.info("[NLU] Loading config from %s", NLU_CONFIG)
trainer = Trainer(config.load(NLU_CONFIG))

LOGGER.info("[NLU] Training NLU...")
trainer.train(training_data)
trainer.persist(NLU_MODEL, fixed_model_name="nlu", project_name="current")


def train_core():
keras = KerasPolicy(max_history=5)
memoization = MemoizationPolicy(max_history=5)
fallback = FallbackPolicy(nlu_threshold=0.28)

LOGGER.info("[CORE] Creating agent from YAML at %s", CORE_DOMAIN)
agent = Agent(CORE_DOMAIN, policies=[memoization, keras, fallback])

LOGGER.info("[CORE] Loading training data from %s", CORE_DATA)
data = agent.load_data(CORE_DATA)

LOGGER.info("[CORE] Training Core...")
agent.train(data)
agent.persist(CORE_MODEL)


if __name__ == "__main__":
opts = parse_args(sys.argv[1:])
if opts.nlu:
train_nlu()
if opts.core:
train_core()
if __name__ == '__main__':
main()
14 changes: 14 additions & 0 deletions chatbot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2018 Contributors to Hyperledger Sawtooth
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------------
File renamed without changes.
10 changes: 10 additions & 0 deletions chatbot/domain.yml → chatbot/config/domain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.
# -----------------------------------------------------------------------------

%YAML 1.1
---
intents:
- recommend
- affirm
Expand All @@ -31,6 +33,14 @@ intents:
- whoami
- name

config:
store_entities_as_slots: true

entities:
- reason

forms: []

slots:
token:
type: unfeaturized
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions bin/chatbot → chatbot/entrypoint
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/bash

# Copyright 2018 Contributors to Hyperledger Sawtooth
#
Expand All @@ -15,11 +15,11 @@
# limitations under the License.
# -----------------------------------------------------------------------------

TOP_DIR=$(cd $(dirname $(dirname $0)) && pwd)
MODELS_DIR=chatbot/models/current
CONFIG_DIR=chatbot/config

python -m rasa_core.run --enable_api \
-d ./chatbot/build/current/core \
-u ./chatbot/build/current/nlu \
--credentials ./chatbot/credentials.yml \
--endpoints ./chatbot/endpoints.yml \
--port 5005
python -m rasa_core.run --enable_api --port 5005 \
--credentials $CONFIG_DIR/credentials.yml \
--endpoints $CONFIG_DIR/endpoints.yml \
-d $MODELS_DIR/core \
-u $MODELS_DIR/nlu
45 changes: 45 additions & 0 deletions chatbot/models/current/core/domain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"states": [
"intent_affirm",
"intent_awe",
"intent_bye",
"intent_cancel",
"intent_deny",
"intent_feeling",
"intent_greet",
"intent_help",
"intent_insult",
"intent_laugh",
"intent_name",
"intent_reason",
"intent_recommend",
"intent_send",
"intent_thank",
"intent_whoami",
"entity_reason",
"prev_action_listen",
"prev_action_restart",
"prev_action_default_fallback",
"prev_action_deactivate_form",
"prev_utter_default",
"prev_utter_standby",
"prev_utter_recommendation",
"prev_utter_ask_request_access",
"prev_utter_greet",
"prev_utter_draft",
"prev_utter_exclame",
"prev_utter_fanfare_sent",
"prev_utter_request_bye",
"prev_utter_thank",
"prev_utter_feeling",
"prev_utter_bye",
"prev_utter_laugh",
"prev_utter_awe",
"prev_utter_insult",
"prev_utter_help",
"prev_utter_whoami",
"prev_utter_name",
"prev_utter_passive",
"prev_action_request_access"
]
}
165 changes: 165 additions & 0 deletions chatbot/models/current/core/domain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
%YAML 1.1
---
actions:
- utter_default
- utter_standby
- utter_recommendation
- utter_ask_request_access
- utter_greet
- utter_draft
- utter_exclame
- utter_fanfare_sent
- utter_request_bye
- utter_thank
- utter_feeling
- utter_bye
- utter_laugh
- utter_awe
- utter_insult
- utter_help
- utter_whoami
- utter_name
- utter_passive
- action_request_access
config:
store_entities_as_slots: true
entities:
- reason
forms: []
intents:
- recommend:
use_entities: true
- affirm:
use_entities: true
- deny:
use_entities: true
- greet:
use_entities: true
- send:
use_entities: true
- cancel:
use_entities: true
- bye:
use_entities: true
- thank:
use_entities: true
- feeling:
use_entities: true
- reason:
use_entities: true
- laugh:
use_entities: true
- awe:
use_entities: true
- insult:
use_entities: true
- help:
use_entities: true
- whoami:
use_entities: true
- name:
use_entities: true
slots:
approver_name:
auto_fill: true
initial_value: null
type: rasa_core.slots.UnfeaturizedSlot
reason:
auto_fill: true
initial_value: null
type: rasa_core.slots.UnfeaturizedSlot
resource_id:
auto_fill: true
initial_value: null
type: rasa_core.slots.UnfeaturizedSlot
resource_name:
auto_fill: true
initial_value: null
type: rasa_core.slots.UnfeaturizedSlot
token:
auto_fill: true
initial_value: null
type: rasa_core.slots.UnfeaturizedSlot
templates:
utter_ask_request_access:
- buttons:
- payload: Yes, please.
title: Yes, please
- payload: No, thanks.
title: No, thanks
text: Do you want me to request access for you?
utter_awe:
- text: Pretty great, right?
- text: Yep.
utter_bye:
- text: Talk to you later.
- text: Later gater.
- text: Until next time.
- text: Bye. I will always be here for you.
utter_default:
- text: Sorry, I didn't get that. Can you clarify?
- text: I don't understand what you mean.
utter_draft:
- buttons:
- payload: '/send{"reason": "I need access."}'
title: Send request
- payload: /cancel
title: Nevermind
text: So you'd like to request access for {resource_name}? Great. All you need
to do is draft a note for {approver_name} and I'll take care of the rest.
utter_exclame:
- text: Awesome!
utter_fanfare_sent:
- text: Ta-da! Request sent
utter_feeling:
- text: I'm okay.
- text: I'm just hanging around.
- text: Eh. Could be better I guess.
- text: Do you actually care?
utter_greet:
- text: Bonjour! How can I help you?
- text: Hola!
- text: Sup?
- text: Howdy partner.
- text: Oh hi there.
- text: What's up?
- text: How may I serve you?
utter_help:
- text: I am here to help you get access to resources you need.
utter_insult:
- text: I am trying my best to be helpful. Please let me know more clearly how I
can help you.
- text: All robots make mistakes at some point. This may be one of those moments.
Please tell me what it is you'd like me to do.
utter_laugh:
- text: I know right?
- text: You're hilarious.
- text: Pretty funny.
utter_name:
- text: It's a pleasure to be getting to know you.
- text: That's great.
- text: Cool stuff.
- text: Uh huh.
utter_passive:
- text: Fine.
- text: Have it your way.
utter_recommendation:
- text: Hi! Based on your role, I bet having access to {resource_name} would make
it easier for you to function.
utter_request_bye:
- text: Feel free to ping me anytime. I will notify you once I hear from {approver_name}.
Au revoir!
utter_standby:
- text: Feel free to ping me anytime.
- text: I'll be here for you.
utter_thank:
- text: You're most welcome.
- text: No problem.
- text: My pleasure.
- text: You got it.
- text: Anytime.
- text: Sure thing.
utter_whoami:
- text: I am Dez, a robot designed specifically to help you because you are special
and deserve it.
- text: Who are you?
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"py/object": "rasa_core.featurizers.MaxHistoryTrackerFeaturizer", "max_history": 5, "remove_duplicates": true, "state_featurizer": {"py/object": "rasa_core.featurizers.SingleStateFeaturizer", "slot_feature_len": null, "user_feature_len": null}, "use_intent_probabilities": false}
Loading

0 comments on commit cb7ff9b

Please sign in to comment.