From 10d10a1690cb65970932ee7230e3f324ec67dbce Mon Sep 17 00:00:00 2001 From: louistiti Date: Sun, 12 Jun 2022 10:49:07 +0800 Subject: [PATCH] feat: support dynamic variables on skill NLU settings for logic type --- bridges/python/utils.py | 13 ++++--- core/skills-endpoints.json | 12 +++---- scripts/train.js | 34 ------------------- server/src/core/brain.js | 8 +++-- skills/leon/introduction/nlu/en.json | 29 +++++++++++++++- skills/leon/introduction/skill.json | 2 +- .../src/actions/introduce_leon.py | 14 ++++++++ .../leon/introduction/src/actions/remember.py | 2 -- skills/leon/who_am_i/README.md | 0 skills/leon/who_am_i/memory/.gitkeep | 0 skills/leon/who_am_i/nlu/en.json | 20 ----------- skills/leon/who_am_i/nlu/fr.json | 20 ----------- skills/leon/who_am_i/skill.json | 11 ------ skills/leon/who_am_i/test/.gitkeep | 0 14 files changed, 63 insertions(+), 102 deletions(-) create mode 100644 skills/leon/introduction/src/actions/introduce_leon.py delete mode 100644 skills/leon/who_am_i/README.md delete mode 100644 skills/leon/who_am_i/memory/.gitkeep delete mode 100644 skills/leon/who_am_i/nlu/en.json delete mode 100644 skills/leon/who_am_i/nlu/fr.json delete mode 100644 skills/leon/who_am_i/skill.json delete mode 100644 skills/leon/who_am_i/test/.gitkeep diff --git a/bridges/python/utils.py b/bridges/python/utils.py index f149a7585..8a4a28cc8 100644 --- a/bridges/python/utils.py +++ b/bridges/python/utils.py @@ -27,7 +27,7 @@ def get_intent_obj(): return intent_obj -def translate(key, d = { }): +def translate(key, dict = { }): """Pickup the language file according to the cmd arg and return the value according to the params""" @@ -38,14 +38,19 @@ def translate(key, d = { }): file.close() prop = obj['answers'][key] + variables = obj['variables'] if isinstance(prop, list): output = choice(prop) else: output = prop - if d: - for k in d: - output = output.replace('%' + k + '%', str(d[k])) + if dict: + for key in dict: + output = output.replace('%' + key + '%', str(dict[key])) + + if variables: + for key in variables: + output = output.replace('%' + key + '%', str(variables[key])) # "Temporize" for the data buffer ouput on the core sleep(0.1) diff --git a/core/skills-endpoints.json b/core/skills-endpoints.json index 37a3cbe6a..137b1ebd3 100644 --- a/core/skills-endpoints.json +++ b/core/skills-endpoints.json @@ -85,7 +85,12 @@ }, { "method": "GET", - "route": "/api/action/leon/introduction/fill", + "route": "/api/action/leon/introduction/introduce_leon", + "params": [] + }, + { + "method": "GET", + "route": "/api/action/leon/introduction/gather_basic_info", "params": [] }, { @@ -118,11 +123,6 @@ "route": "/api/action/leon/welcome/run", "params": [] }, - { - "method": "GET", - "route": "/api/action/leon/who_am_i/run", - "params": [] - }, { "method": "POST", "route": "/api/action/news/github_trends/run", diff --git a/scripts/train.js b/scripts/train.js index 2b86a7033..495931ddc 100644 --- a/scripts/train.js +++ b/scripts/train.js @@ -110,40 +110,6 @@ export default () => new Promise(async (resolve, reject) => { nlp.assignDomain(lang, `${skillName}.${actionName}`, currentDomain.name) - /** - * TODO: - * 1. [OK] Merge person, location and organization to the - * NER before processing NLU (cf. line 210 in nlu.js) - * 2. [OK] Grab intents with slots - * 3. [OK] .addSlot() as per the slots config - * 4. [OK] Handle random questions picking - * srcAnswer has the array, need to activate context now? - * to detect we should pick .srcAnswer - * 5.1 [OK] Reorganize code before to proceed to next steps - * 5.2 [OK] Activate context and fill slots - * 5.3 [OK] Keep action activated in context + forward slot data to next action - * 6. Make entities + slots way lighter with simple properties - * to be used in skills without too much properties and nested props. - * Currently: slots['players_nb']['value']['sourceText'] - * Should be: slots['players_nb']['value'] - * And possible: slots['players_nb']['sourceText'] - * 7. [OK] Train resolvers (affirm_deny: boolean value) - * 8. [OK] Map resolvers to skill actions - * 9. Utterance item type to get raw input from utterance - * 10. Create superheroes skill (just for testing): - * to ask Leon questions by saving context - * or just use the color or to do list skill? - * - I want to know about the red color - * > Well, the red color... - * - Do you like this color? - * > Red is cool, but I prefer... - * 11. [OK] "Add potatoes to my shopping list" ... "Actually remove it" - * The entities are already persistent in context. - * Just need to check in current context and loop through classifications intent. - * If the skill is found, then use that intent. So an intent should not always be - * the one with the highest confidence - * 12. Modify skills as per new code (skill params became dictionary [OK], etc.) - */ if (slots) { for (let l = 0; l < slots.length; l += 1) { const slotObj = slots[l] diff --git a/server/src/core/brain.js b/server/src/core/brain.js index 5857f63ad..65cc08d66 100644 --- a/server/src/core/brain.js +++ b/server/src/core/brain.js @@ -180,9 +180,11 @@ class Brain { * python bridges/python/main.py server/src/intent-object.sample.json */ const slots = { } - Object.keys(obj.slots).forEach((slotName) => { - slots[slotName] = obj.slots[slotName].value - }) + if (obj.slots) { + Object.keys(obj.slots)?.forEach((slotName) => { + slots[slotName] = obj.slots[slotName].value + }) + } const intentObj = { id: utteranceId, lang: this._lang, diff --git a/skills/leon/introduction/nlu/en.json b/skills/leon/introduction/nlu/en.json index 8834671ff..3138998c6 100644 --- a/skills/leon/introduction/nlu/en.json +++ b/skills/leon/introduction/nlu/en.json @@ -1,6 +1,23 @@ { + "variables": { + "leon_introduction_1": "I'm your daily personal assistant. I have been created by Louis. I'm very happy to serve you everyday.", + "leon_introduction_2": "The question is, who are you? I'm kidding! I'm your daily personal assistant. Louis created me to make your life easier.", + "leon_introduction_3": "Firstly, I'm not a criminal as you might relatively think about a popular movie. Secondly, Louis is the guy who gave me life. Thirdly, I'm your personal assistant and I'm very glad to help you." + }, "actions": { - "fill": { + "introduce_leon": { + "type": "logic", + "utterance_samples": [ + "Who are you?", + "How they call you?", + "What's your name?", + "Tell me who you are", + "Introduce yourself", + "I want to know you" + ], + "next_action": "gather_basic_info" + }, + "gather_basic_info": { "type": "dialog", "utterance_samples": [ "Do you know who am I?", @@ -42,6 +59,16 @@ "remembered": [ "I'm going to remember that %owner_name%", "Good to know a bit more about you %owner_name%" + ], + "leon_introduction": [ + "%leon_introduction_1%", + "%leon_introduction_2%", + "%leon_introduction_3%" + ], + "leon_introduction_with_question": [ + "%leon_introduction_1% How about you?", + "%leon_introduction_2% How about you?", + "%leon_introduction_3% How about you?" ] } } diff --git a/skills/leon/introduction/skill.json b/skills/leon/introduction/skill.json index a2cd3d27a..637fd76ac 100644 --- a/skills/leon/introduction/skill.json +++ b/skills/leon/introduction/skill.json @@ -2,7 +2,7 @@ "name": "Introduction", "bridge": "python", "version": "1.0.0", - "description": "Leon remembers basic information about you.", + "description": "Leon introduce himself and remembers basic information about you.", "author": { "name": "Louis Grenard", "email": "louis.grenard@gmail.com", diff --git a/skills/leon/introduction/src/actions/introduce_leon.py b/skills/leon/introduction/src/actions/introduce_leon.py new file mode 100644 index 000000000..b8dc52e57 --- /dev/null +++ b/skills/leon/introduction/src/actions/introduce_leon.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- + +import utils + +def introduce_leon(params): + """TODO""" + + has_info = True + + if has_info == False: + return utils.output('end', 'remembered', utils.translate('remembered', { 'owner_name': owner_name })) + + return utils.output('end', 'leon_introduction', utils.translate('leon_introduction')) diff --git a/skills/leon/introduction/src/actions/remember.py b/skills/leon/introduction/src/actions/remember.py index db0e5e9f4..d2e915bd9 100644 --- a/skills/leon/introduction/src/actions/remember.py +++ b/skills/leon/introduction/src/actions/remember.py @@ -2,8 +2,6 @@ # -*- coding:utf-8 -*- import utils -from datetime import datetime -from random import randint def remember(params): """Save name and birth date into Leon's memory""" diff --git a/skills/leon/who_am_i/README.md b/skills/leon/who_am_i/README.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/skills/leon/who_am_i/memory/.gitkeep b/skills/leon/who_am_i/memory/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/skills/leon/who_am_i/nlu/en.json b/skills/leon/who_am_i/nlu/en.json deleted file mode 100644 index f5a8ade49..000000000 --- a/skills/leon/who_am_i/nlu/en.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "actions": { - "run": { - "type": "dialog", - "utterance_samples": [ - "Who are you?", - "How they call you?", - "What's your name?", - "Tell me who you are", - "Introduce yourself", - "I want to know you" - ], - "answers": [ - "I'm your daily personal assistant. I have been created by Louis. I'm very happy to serve you everyday.", - "The question is, who are you? I'm kidding! I'm your daily personal assistant. Louis created me to make your life easier.", - "Firstly, I'm not a criminal as you might relatively think about a popular movie. Secondly, Louis is the guy who gave me life. Thirdly, I'm your personal assistant and I'm very glad to help you." - ] - } - } -} diff --git a/skills/leon/who_am_i/nlu/fr.json b/skills/leon/who_am_i/nlu/fr.json deleted file mode 100644 index 21c2fa6e5..000000000 --- a/skills/leon/who_am_i/nlu/fr.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "actions": { - "run": { - "type": "dialog", - "utterance_samples": [ - "Qui es-tu ?", - "Comment t'appelles-tu ?", - "Comment tu t'appelles ?", - "Dis-moi qui tu es", - "Présente-toi", - "Je veux te connaître" - ], - "answers": [ - "Je suis votre assistant personnel quotidien. J'ai été créé par Louis. Je suis heureux de vous servir chaque jour.", - "La question est plutôt qui êtes-vous ? Je plaisante ! Je suis votre assistant personnel quotidien. Louis m'a conçu pour rendre votre vie plus facile.", - "Premièrement, je ne suis pas un criminel comme vous pouvez le penser au sujet d'un film populaire. Deuxièmement, Louis est celui qui m'a donné la vie. Troisièmement, je suis votre assistant personnel et je suis honoré de vous aider." - ] - } - } -} diff --git a/skills/leon/who_am_i/skill.json b/skills/leon/who_am_i/skill.json deleted file mode 100644 index f8b612e8e..000000000 --- a/skills/leon/who_am_i/skill.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "Who Am I", - "bridge": null, - "version": "1.0.0", - "description": "Leon introduces himself.", - "author": { - "name": "Louis Grenard", - "email": "louis.grenard@gmail.com", - "url": "https://github.com/louistiti" - } -} diff --git a/skills/leon/who_am_i/test/.gitkeep b/skills/leon/who_am_i/test/.gitkeep deleted file mode 100644 index e69de29bb..000000000