1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-25 09:44:22 +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
})
} 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)) {
log.error(`This action type isn't supported: ${actionType}`)
}

View File

@ -7,6 +7,7 @@ const defaultActiveContext = {
domain: null,
intent: null,
slots: { },
nextAction: null,
activatedAt: 0
}
@ -57,9 +58,12 @@ class Conversation {
// If slots are required to trigger next actions, then go through the context activation
if (slotKeys.length > 0) {
// Grab output context from the NLU data file
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]
// 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
@ -74,6 +78,7 @@ class Conversation {
domain,
intent,
slots: { },
nextAction,
activatedAt: Date.now()
}
}

View File

@ -120,9 +120,11 @@ class Nlu {
const { domain, intent } = this.conv.activeContext
const [skillName, actionName] = intent.split('.')
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,
entities: [],
nluDataFilePath,
classification: {
domain,
skill: skillName,
@ -165,17 +167,30 @@ class Nlu {
* 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
* 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
* 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
* e.g. I wanna play with 2 players and louis.grenard@gmail.com
* 6. Split this process() method into several ones
* 7. Add logs in terminal about context switching, active context, etc.
* 6. What's next once the next action has been executed?
* 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 })
return resolve({
...data
@ -280,10 +295,7 @@ class Nlu {
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 { actions } = JSON.parse(fs.readFileSync(nluDataFilePath, 'utf8'))
const { type: actionType } = actions[actionName]
nluResultObj.actionType = actionType
nluResultObj.nluDataFilePath = nluDataFilePath
try {
nluResultObj.entities = await this.ner.extractEntities(

View File

@ -1,7 +1,7 @@
{
"lang": "en",
"domain": "checker",
"skill": "isitdown",
"domain": "utilities",
"skill": "is_it_down",
"action": "run",
"utterance": "Check if github.com, mozilla.org and twitter.com are up",
"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')