1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-27 02:35:50 +03:00

feat(server): trigger next action once all slots have been filled

This commit is contained in:
louistiti 2022-03-29 00:11:08 +08:00
parent 9124687eb0
commit 9b870010dd
No known key found for this signature in database
GPG Key ID: 0A1C3B043E70C77D
6 changed files with 44 additions and 25 deletions

View File

@ -160,7 +160,10 @@ class Brain {
executionTime executionTime
}) })
} else { } else {
const { actionType } = obj const { nluDataFilePath, classification: { action: actionName } } = obj
const { actions } = JSON.parse(fs.readFileSync(nluDataFilePath, 'utf8'))
const { type: actionType } = actions[actionName]
if (!actionType || !supportedActionTypes.includes(actionType)) { if (!actionType || !supportedActionTypes.includes(actionType)) {
log.error(`This action type isn't supported: ${actionType}`) log.error(`This action type isn't supported: ${actionType}`)
} }

View File

@ -7,6 +7,7 @@ const defaultActiveContext = {
domain: null, domain: null,
intent: null, intent: null,
slots: { }, slots: { },
nextAction: null,
activatedAt: 0 activatedAt: 0
} }
@ -57,9 +58,12 @@ class Conversation {
// If slots are required to trigger next actions, then go through the context activation // If slots are required to trigger next actions, then go through the context activation
if (slotKeys.length > 0) { if (slotKeys.length > 0) {
// Grab output context from the NLU data file
const { actions } = JSON.parse(fs.readFileSync(nluDataFilePath, 'utf8')) const { actions } = JSON.parse(fs.readFileSync(nluDataFilePath, 'utf8'))
const actionsKeys = Object.keys(actions)
// Grab output context from the NLU data file
const { output_context: outputContext } = actions[actionName] const { output_context: outputContext } = actions[actionName]
// Define next action
const [nextAction] = actionsKeys.filter((key) => actions[key].input_context === outputContext)
/** /**
* If there is an active context and a new one is triggered * If there is an active context and a new one is triggered
@ -74,6 +78,7 @@ class Conversation {
domain, domain,
intent, intent,
slots: { }, slots: { },
nextAction,
activatedAt: Date.now() activatedAt: Date.now()
} }
} }

View File

@ -120,9 +120,11 @@ class Nlu {
const { domain, intent } = this.conv.activeContext const { domain, intent } = this.conv.activeContext
const [skillName, actionName] = intent.split('.') const [skillName, actionName] = intent.split('.')
const nluDataFilePath = join(process.cwd(), 'skills', domain, skillName, `nlu/${this.brain.lang}.json`) const nluDataFilePath = join(process.cwd(), 'skills', domain, skillName, `nlu/${this.brain.lang}.json`)
const nluResultObj = { // TODO: create specific method to build this important object
let nluResultObj = {
utterance, utterance,
entities: [], entities: [],
nluDataFilePath,
classification: { classification: {
domain, domain,
skill: skillName, skill: skillName,
@ -165,16 +167,29 @@ class Nlu {
* 2. [OK] If none of them match any slot in the active context, then continue * 2. [OK] If none of them match any slot in the active context, then continue
* 3. [OK] If an entity match slot in active context, then fill it * 3. [OK] If an entity match slot in active context, then fill it
* 4. [OK] Move skill type to action type * 4. [OK] Move skill type to action type
* 5.1 In Conversation, need to chain output/input contexts to each other * 5.1 [OK] In Conversation, need to chain output/input contexts to each other
* to understand what action should be next * to understand what action should be next
* 5.2 Execute next action (based on input_context?) * 5.2 [OK] Execute next action (based on input_context?)
* 5.3 Need to handle the case if a context is filled in one shot * 5.3 Need to handle the case if a context is filled in one shot
* e.g. I wanna play with 2 players and louis.grenard@gmail.com * e.g. I wanna play with 2 players and louis.grenard@gmail.com
* 6. Split this process() method into several ones * 6. What's next once the next action has been executed?
* 7. Add logs in terminal about context switching, active context, etc. * 7. Handle a "loop" feature from action (guess the number)
* 8. Split this process() method into several ones + clean nlu.js and brain.js
* 9. Add logs in terminal about context switching, active context, etc.
*/ */
// TODO: recreate nluResultObj for the NEXT action (actionType, etc.) nluResultObj = {
utterance: '',
entities: [],
// TODO: the brain needs to forward slots to the skill execution
slots: this.conv.activeContext.slots,
nluDataFilePath,
classification: {
domain,
skill: skillName,
action: this.conv.activeContext.nextAction
}
}
const data = await this.brain.execute(nluResultObj, { mute: opts.mute }) const data = await this.brain.execute(nluResultObj, { mute: opts.mute })
return resolve({ return resolve({
@ -280,10 +295,7 @@ class Nlu {
log.success(`Intent found: ${nluResultObj.classification.skill}.${nluResultObj.classification.action} (domain: ${nluResultObj.classification.domain})`) log.success(`Intent found: ${nluResultObj.classification.skill}.${nluResultObj.classification.action} (domain: ${nluResultObj.classification.domain})`)
const nluDataFilePath = join(process.cwd(), 'skills', nluResultObj.classification.domain, nluResultObj.classification.skill, `nlu/${this.brain.lang}.json`) const nluDataFilePath = join(process.cwd(), 'skills', nluResultObj.classification.domain, nluResultObj.classification.skill, `nlu/${this.brain.lang}.json`)
const { actions } = JSON.parse(fs.readFileSync(nluDataFilePath, 'utf8')) nluResultObj.nluDataFilePath = nluDataFilePath
const { type: actionType } = actions[actionName]
nluResultObj.actionType = actionType
try { try {
nluResultObj.entities = await this.ner.extractEntities( nluResultObj.entities = await this.ner.extractEntities(

View File

@ -1,7 +1,7 @@
{ {
"lang": "en", "lang": "en",
"domain": "checker", "domain": "utilities",
"skill": "isitdown", "skill": "is_it_down",
"action": "run", "action": "run",
"utterance": "Check if github.com, mozilla.org and twitter.com are up", "utterance": "Check if github.com, mozilla.org and twitter.com are up",
"entities": [ "entities": [

View File

@ -0,0 +1,9 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import utils
def pick_up(string, entities):
"""This is a test"""
return utils.output('end', 'ready', utils.translate('ready'))

View File

@ -1,10 +0,0 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import utils
from random import randint
def run(string, entities):
"""TODO..."""
return utils.output('end', 'ready', utils.translate('ready')